annotate spectrum.py @ 0:2b1759ccaa8b draft default tip

planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
author astroteam
date Fri, 25 Apr 2025 21:48:27 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
1 #!/usr/bin/env python
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
2 # coding: utf-8
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
3
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
4 #!/usr/bin/env python
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
5
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
6 # This script is generated with nb2galaxy
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
7
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
8 # flake8: noqa
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
9
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
10 import json
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
11 import os
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
12 import shutil
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
13
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
14 from oda_api.json import CustomJSONEncoder
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
15
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
16 fn = "data.tsv" # oda:POSIXPath
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
17 skiprows = 0 # http://odahub.io/ontology#Integer
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
18 sep = "whitespace" # http://odahub.io/ontology#String ; oda:allowed_value "auto", "comma", "tab", "whitespace", "semicolon"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
19 column = "c1" # http://odahub.io/ontology#String
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
20 weight_col = "" # http://odahub.io/ontology#String
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
21 binning = "logarithmic" # http://odahub.io/ontology#String ; oda:allowed_value "linear","logarithmic"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
22 minval = 0 # http://odahub.io/ontology#Float
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
23 maxval = 0 # http://odahub.io/ontology#Float
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
24 nbins = 15 # http://odahub.io/ontology#Integer
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
25 xlabel = "Energy, [eV]" # http://odahub.io/ontology#String
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
26 ylabel = "Flux E^2, [eV]" # http://odahub.io/ontology#String
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
27 spec_power = 2.0 # http://odahub.io/ontology#Float
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
28
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
29 _galaxy_wd = os.getcwd()
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
30
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
31 with open("inputs.json", "r") as fd:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
32 inp_dic = json.load(fd)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
33 if "C_data_product_" in inp_dic.keys():
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
34 inp_pdic = inp_dic["C_data_product_"]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
35 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
36 inp_pdic = inp_dic
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
37 fn = str(inp_pdic["fn"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
38 skiprows = int(inp_pdic["skiprows"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
39 sep = str(inp_pdic["sep"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
40 column = str(inp_pdic["column"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
41 weight_col = str(inp_pdic["weight_col"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
42 binning = str(inp_pdic["binning"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
43 minval = float(inp_pdic["minval"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
44 maxval = float(inp_pdic["maxval"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
45 nbins = int(inp_pdic["nbins"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
46 xlabel = str(inp_pdic["xlabel"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
47 ylabel = str(inp_pdic["ylabel"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
48 spec_power = float(inp_pdic["spec_power"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
49
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
50 import matplotlib.pyplot as plt
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
51 import numpy as np
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
52 import pandas as pd
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
53
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
54 separators = {
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
55 "tab": "\t",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
56 "comma": ",",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
57 "semicolon": ";",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
58 "whitespace": "\s+",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
59 "space": " ",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
60 }
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
61
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
62 df = None
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
63
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
64 if sep == "auto":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
65 for name, s in separators.items():
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
66 try:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
67 df = pd.read_csv(fn, sep=s, index_col=False, skiprows=skiprows)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
68 if len(df.columns) > 2:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
69 sep = s
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
70 print("Detected separator: ", name)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
71 break
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
72 except Exception as e:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
73 print("Separator ", s, " failed", e)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
74 assert sep != "auto", "Failed to find valid separator"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
75
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
76 if df is None:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
77 df = pd.read_csv(fn, sep=separators[sep], index_col=False)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
78
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
79 df.columns
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
80
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
81 def read_data(df, colname, optional=False):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
82 for i, c in enumerate(df.columns):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
83 if colname == f"c{i+1}":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
84 print(colname, c)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
85 return df[c].values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
86 elif colname == c:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
87 print(colname, c)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
88 return df[c].values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
89
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
90 assert optional, colname + " column not found"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
91 return None
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
92
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
93 values = read_data(df, column)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
94 weights = read_data(df, weight_col, optional=True)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
95 if weights is None:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
96 weights = np.ones_like(values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
97
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
98 values, weights
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
99
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
100 from numpy import log10
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
101
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
102 if minval == 0:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
103 minval = np.min(values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
104
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
105 if maxval == 0:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
106 maxval = np.max(values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
107
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
108 if binning == "linear":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
109 bins = np.linspace(minval, maxval, nbins + 1)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
110 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
111 bins = np.logspace(log10(minval), log10(maxval), nbins + 1)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
112 bins
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
113
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
114 bin_val, _ = np.histogram(values, weights=weights, bins=bins)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
115 len(bin_val), len(bins)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
116 bin_width = bins[1:] - bins[:-1]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
117 flux = bin_val / bin_width
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
118 if binning == "linear":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
119 spec_point = 0.5 * (bins[1:] + bins[:-1])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
120 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
121 spec_point = np.sqrt(bins[1:] * bins[:-1])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
122
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
123 plt.figure()
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
124 h = plt.plot(spec_point, flux * spec_point**spec_power)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
125
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
126 if binning == "logarithmic":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
127 plt.xscale("log")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
128 plt.yscale("log")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
129
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
130 plt.xlabel(xlabel)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
131 plt.ylabel(ylabel)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
132 plt.savefig("spectrum.png", format="png", dpi=150)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
133
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
134 from astropy.table import Table
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
135 from oda_api.data_products import ODAAstropyTable, PictureProduct
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
136
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
137 names = ("bins_min", "bins_max", "flux")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
138
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
139 res = ODAAstropyTable(Table([bins[:-1], bins[1:], flux], names=names))
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
140
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
141 plot = PictureProduct.from_file("spectrum.png")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
142
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
143 histogram_data = res # http://odahub.io/ontology#ODAAstropyTable
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
144 histogram_picture = plot # http://odahub.io/ontology#ODAPictureProduct
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
145
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
146 # output gathering
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
147 _galaxy_meta_data = {}
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
148 _oda_outs = []
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
149 _oda_outs.append(
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
150 (
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
151 "out_spectrum_histogram_data",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
152 "histogram_data_galaxy.output",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
153 histogram_data,
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
154 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
155 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
156 _oda_outs.append(
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
157 (
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
158 "out_spectrum_histogram_picture",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
159 "histogram_picture_galaxy.output",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
160 histogram_picture,
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
161 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
162 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
163
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
164 for _outn, _outfn, _outv in _oda_outs:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
165 _galaxy_outfile_name = os.path.join(_galaxy_wd, _outfn)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
166 if isinstance(_outv, str) and os.path.isfile(_outv):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
167 shutil.move(_outv, _galaxy_outfile_name)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
168 _galaxy_meta_data[_outn] = {"ext": "_sniff_"}
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
169 elif getattr(_outv, "write_fits_file", None):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
170 _outv.write_fits_file(_galaxy_outfile_name)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
171 _galaxy_meta_data[_outn] = {"ext": "fits"}
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
172 elif getattr(_outv, "write_file", None):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
173 _outv.write_file(_galaxy_outfile_name)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
174 _galaxy_meta_data[_outn] = {"ext": "_sniff_"}
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
175 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
176 with open(_galaxy_outfile_name, "w") as fd:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
177 json.dump(_outv, fd, cls=CustomJSONEncoder)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
178 _galaxy_meta_data[_outn] = {"ext": "json"}
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
179
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
180 with open(os.path.join(_galaxy_wd, "galaxy.json"), "w") as fd:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
181 json.dump(_galaxy_meta_data, fd)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
182 print("*** Job finished successfully ***")