Mercurial > repos > muon-spectroscopy-computational-project > muspinsim_combine
comparison combine.py @ 0:06aabd70b869 draft default tip
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
author | muon-spectroscopy-computational-project |
---|---|
date | Tue, 18 Jul 2023 13:26:03 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:06aabd70b869 |
---|---|
1 import json | |
2 import sys | |
3 | |
4 import numpy as np | |
5 | |
6 import scipy.stats as stats | |
7 | |
8 | |
9 def main(): | |
10 input_json_path = sys.argv[1] | |
11 params = json.load(open(input_json_path, "r")) | |
12 | |
13 x_equal = True | |
14 data = np.loadtxt(params["data_in"][0], usecols=(0, 1)) | |
15 x_values = [data[:, 0]] | |
16 y_values = [data[:, 1]] | |
17 bins = len(data) | |
18 | |
19 for path in params["data_in"][1:]: | |
20 data = np.loadtxt(path, usecols=(0, 1)) | |
21 x_values.append(data[:, 0]) | |
22 y_values.append(data[:, 1]) | |
23 length_equal = bins == len(data) | |
24 bins = min(bins, len(data)) | |
25 x_equal = ( | |
26 x_equal and length_equal and np.allclose(x_values, x_values[-1]) | |
27 ) | |
28 | |
29 if x_equal: | |
30 print( | |
31 "All x ranges were identical, performing direct average over " | |
32 f"{len(x_values)} files" | |
33 ) | |
34 means = np.mean(y_values, axis=0) | |
35 np.savetxt("data_out.dat", np.column_stack((x_values[0], means))) | |
36 return | |
37 | |
38 if params["bins"] is not None: | |
39 bins = params["bins"] | |
40 | |
41 x_flat = np.concatenate(x_values) | |
42 y_flat = np.concatenate(y_values) | |
43 print(f"Averaging {len(x_flat)} data points into {bins} bins") | |
44 means, edges, _ = stats.binned_statistic(x_flat, y_flat, bins=bins) | |
45 data_out = np.column_stack(((edges[1:] + edges[:-1]) / 2, means)) | |
46 np.savetxt("data_out.dat", data_out) | |
47 | |
48 | |
49 if __name__ == "__main__": | |
50 main() |