Mercurial > repos > muon-spectroscopy-computational-project > mudirac
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:eaf0f01cc0f6 |
---|---|
1 import argparse | |
2 import csv | |
3 from typing import List, Tuple | |
4 | |
5 import matplotlib.pyplot as plt | |
6 | |
7 X_AXIS_LABEL = "Energy (ev)" | |
8 Y_AXIS_LABEL = "Intensity" | |
9 | |
10 column_names = ["Column 1", "Column 2"] | |
11 | |
12 | |
13 def create_tabular_file(x: List[float], y: List[float]) -> None: | |
14 with open("plots/spectrum.csv", "w", newline="") as csvfile: | |
15 writer = csv.writer(csvfile, delimiter="\t") | |
16 writer.writerow(column_names) | |
17 for value1, value2 in zip(x, y): | |
18 writer.writerow([value2, value1]) | |
19 | |
20 | |
21 def create_plot_image(name: str, x: List[float], y: List[float]) -> None: | |
22 plot_path = f"plots/{name}.png" | |
23 plt.plot(x, y) | |
24 plt.xlabel(X_AXIS_LABEL) | |
25 plt.ylabel(Y_AXIS_LABEL) | |
26 plt.savefig(plot_path) | |
27 plt.close() | |
28 | |
29 | |
30 def read_file(data_file: str) -> Tuple[List[float], List[float]]: | |
31 x: List[float] = [] | |
32 y: List[float] = [] | |
33 # read the data file | |
34 with open(data_file, "r") as file: | |
35 reader = csv.reader(file, delimiter="\t") | |
36 for row in reader: | |
37 x.append(float(row[0])) | |
38 y.append(float(row[1])) | |
39 return x, y | |
40 | |
41 | |
42 def main() -> None: | |
43 parser = argparse.ArgumentParser() | |
44 parser.add_argument("file_name") | |
45 parser.add_argument("-f", action="store_true") | |
46 args = parser.parse_args() | |
47 x, y = read_file(args.file_name) | |
48 create_plot_image(args.file_name, x, y) | |
49 if args.f: | |
50 create_tabular_file(x, y) | |
51 | |
52 | |
53 if __name__ == "__main__": | |
54 main() |