Mercurial > repos > muon-spectroscopy-computational-project > mudirac
annotate spectrum_plot.py @ 0:eaf0f01cc0f6 draft default tip
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
author | muon-spectroscopy-computational-project |
---|---|
date | Thu, 18 Jul 2024 09:07:00 +0000 |
parents | |
children |
rev | line source |
---|---|
0
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
1 import argparse |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
2 import csv |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
3 from typing import List, Tuple |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
4 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
5 import matplotlib.pyplot as plt |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
6 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
7 X_AXIS_LABEL = "Energy (ev)" |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
8 Y_AXIS_LABEL = "Intensity" |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
9 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
10 column_names = ["Column 1", "Column 2"] |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
11 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
12 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
13 def create_tabular_file(x: List[float], y: List[float]) -> None: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
14 with open("plots/spectrum.csv", "w", newline="") as csvfile: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
15 writer = csv.writer(csvfile, delimiter="\t") |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
16 writer.writerow(column_names) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
17 for value1, value2 in zip(x, y): |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
18 writer.writerow([value2, value1]) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
19 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
20 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
21 def create_plot_image(name: str, x: List[float], y: List[float]) -> None: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
22 plot_path = f"plots/{name}.png" |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
23 plt.plot(x, y) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
24 plt.xlabel(X_AXIS_LABEL) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
25 plt.ylabel(Y_AXIS_LABEL) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
26 plt.savefig(plot_path) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
27 plt.close() |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
28 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
29 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
30 def read_file(data_file: str) -> Tuple[List[float], List[float]]: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
31 x: List[float] = [] |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
32 y: List[float] = [] |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
33 # read the data file |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
34 with open(data_file, "r") as file: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
35 reader = csv.reader(file, delimiter="\t") |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
36 for row in reader: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
37 x.append(float(row[0])) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
38 y.append(float(row[1])) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
39 return x, y |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
40 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
41 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
42 def main() -> None: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
43 parser = argparse.ArgumentParser() |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
44 parser.add_argument("file_name") |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
45 parser.add_argument("-f", action="store_true") |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
46 args = parser.parse_args() |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
47 x, y = read_file(args.file_name) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
48 create_plot_image(args.file_name, x, y) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
49 if args.f: |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
50 create_tabular_file(x, y) |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
51 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
52 |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
53 if __name__ == "__main__": |
eaf0f01cc0f6
planemo upload for repository https://github.com/muon-spectroscopy-computational-project/muon-galaxy-tools/tree/main/mudirac commit 389608650050596b28bc0c055de9f98c513a6f65
muon-spectroscopy-computational-project
parents:
diff
changeset
|
54 main() |