annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
1 import json
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
2 import sys
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
3
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
4 import numpy as np
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
5
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
6 import scipy.stats as stats
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
7
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
8
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
9 def main():
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
10 input_json_path = sys.argv[1]
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
11 params = json.load(open(input_json_path, "r"))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
12
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
13 x_equal = True
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
14 data = np.loadtxt(params["data_in"][0], usecols=(0, 1))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
15 x_values = [data[:, 0]]
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
16 y_values = [data[:, 1]]
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
17 bins = len(data)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
18
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
19 for path in params["data_in"][1:]:
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
20 data = np.loadtxt(path, usecols=(0, 1))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
21 x_values.append(data[:, 0])
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
22 y_values.append(data[:, 1])
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
23 length_equal = bins == len(data)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
24 bins = min(bins, len(data))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
25 x_equal = (
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
26 x_equal and length_equal and np.allclose(x_values, x_values[-1])
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
27 )
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
28
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
29 if x_equal:
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
30 print(
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
31 "All x ranges were identical, performing direct average over "
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
32 f"{len(x_values)} files"
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
33 )
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
34 means = np.mean(y_values, axis=0)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
35 np.savetxt("data_out.dat", np.column_stack((x_values[0], means)))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
36 return
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
37
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
38 if params["bins"] is not None:
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
39 bins = params["bins"]
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
40
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
41 x_flat = np.concatenate(x_values)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
42 y_flat = np.concatenate(y_values)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
43 print(f"Averaging {len(x_flat)} data points into {bins} bins")
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
44 means, edges, _ = stats.binned_statistic(x_flat, y_flat, bins=bins)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
45 data_out = np.column_stack(((edges[1:] + edges[:-1]) / 2, means))
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
46 np.savetxt("data_out.dat", data_out)
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
47
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
48
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
49 if __name__ == "__main__":
06aabd70b869 planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/main/muspinsim_combine commit 70a4d37ecdf5d586703cfc509922311e95d3205c
muon-spectroscopy-computational-project
parents:
diff changeset
50 main()