annotate light_curve.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 "comma", "tab", "space", "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 = "T" # 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 use_quantile_values = False # http://odahub.io/ontology#Boolean
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
25 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
26 xlabel = "time, s" # 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 ylabel = "Ncounts" # 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
28 plot_mode = "flux" # http://odahub.io/ontology#String ; oda:allowed_value "counts", "flux"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
29
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
30 _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
31
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
32 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
33 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
34 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
35 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
36 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
46 use_quantile_values = bool(inp_pdic["use_quantile_values"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
47 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
48 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
49 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
50 plot_mode = str(inp_pdic["plot_mode"])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
51
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
52 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
53 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
54 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
55
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
56 if plot_mode != "counts" and ylabel == "Ncounts":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
57 ylabel = plot_mode # replace default value
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
58
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
59 assert minval >= 0 or not use_quantile_values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
60 assert maxval >= 0 or not use_quantile_values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
61 assert minval <= 1 or not use_quantile_values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
62 assert maxval <= 1 or not use_quantile_values
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
63 assert minval < maxval or minval == 0 or maxval == 0
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
64
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
65 separators = {
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
66 "tab": "\t",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
67 "comma": ",",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
68 "semicolon": ";",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
69 "whitespace": "\s+",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
70 "space": " ",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
71 }
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
72
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
73 df = None
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
74
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
75 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
76 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
77 try:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
78 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
79 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
80 sep = s
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
81 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
82 break
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
83 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
84 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
85 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
86
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
87 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
88 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
89
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
90 df.columns
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
91
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
92 def weighted_quantile(
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
93 values, quantiles, sample_weight=None, values_sorted=False, old_style=False
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
94 ):
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
95 """Very close to numpy.percentile, but supports weights.
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
96 NOTE: quantiles should be in [0, 1]!
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
97 :param values: numpy.array with data
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
98 :param quantiles: array-like with many quantiles needed
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
99 :param sample_weight: array-like of the same length as `array`
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
100 :param values_sorted: bool, if True, then will avoid sorting of initial array
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
101 :param old_style: if True, will correct output to be consistent with numpy.percentile.
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
102 :return: numpy.array with computed quantiles.
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
103 """
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
104 values = np.array(values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
105 quantiles = np.array(quantiles)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
106 if sample_weight is None:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
107 sample_weight = np.ones(len(values))
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
108 sample_weight = np.array(sample_weight)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
109 assert np.all(quantiles >= 0) and np.all(
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
110 quantiles <= 1
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
111 ), "quantiles should be in [0, 1]"
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
112
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
113 if not values_sorted:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
114 sorter = np.argsort(values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
115 values = values[sorter]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
116 sample_weight = sample_weight[sorter]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
117
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
118 weighted_quantiles = np.cumsum(sample_weight) - 0.5 * sample_weight
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
119 if old_style:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
120 # To be convenient with np.percentile
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
121 weighted_quantiles -= weighted_quantiles[0]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
122 weighted_quantiles /= weighted_quantiles[-1]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
123 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
124 weighted_quantiles /= np.sum(sample_weight)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
125 return np.interp(quantiles, weighted_quantiles, values)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
126
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
127 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
128 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
129 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
130 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
131 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
132 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
133 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
134 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
135
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
136 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
137 return None
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 delays = 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
140 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
141 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
142 weights = np.ones_like(delays)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
143
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
144 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
145 min_positive_val = np.min(delays[delays > 0])
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
146 delays[delays <= 0] = (
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
147 min_positive_val # replace zero delays with minimal positive value
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
148 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
149
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
150 if use_quantile_values:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
151 minval, maxval = weighted_quantile(
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
152 delays, [minval, maxval], sample_weight=weights
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
153 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
154 if minval == maxval:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
155 print("ignoreing minval and maxval (empty range)")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
156 minval = np.min(delays)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
157 maxval = np.max(delays)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
158 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
159 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
160 minval = np.min(delays)
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
161 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
162 maxval = np.max(delays)
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 if minval == maxval:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
165 print("correcting minval and maxval (empty range)")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
166 maxval = minval * 1.1 if minval > 0 else 1e-100
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
167
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
168 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
169
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
170 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
171 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
172 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
173 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
174 bins
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
175
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
176 if plot_mode == "flux" and binning == "logarithmic":
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
177 weights = weights / delays
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
178
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
179 plt.figure()
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
180 h = plt.hist(delays, 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
181
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
182 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
183 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
184 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
185 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
186 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
187 plt.savefig("Histogram.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
188 hist_counts = h[0]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
189 hist_bins = h[1]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
190 hist_mins = hist_bins[:-1]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
191 hist_maxs = hist_bins[1:]
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
192
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
193 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
194 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
195
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
196 names = ("bins_min", "bins_max", "counts")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
197 res = ODAAstropyTable(Table([hist_mins, hist_maxs, hist_counts], names=names))
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
198
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
199 plot = PictureProduct.from_file("Histogram.png")
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
200
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
201 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
202 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
203
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
204 # output gathering
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
205 _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
206 _oda_outs = []
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
207 _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
208 (
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
209 "out_light_curve_histogram_data",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
210 "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
211 histogram_data,
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
212 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
213 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
214 _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
215 (
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
216 "out_light_curve_histogram_picture",
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
217 "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
218 histogram_picture,
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
219 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
220 )
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
221
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
222 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
223 _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
224 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
225 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
226 _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
227 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
228 _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
229 _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
230 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
231 _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
232 _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
233 else:
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
234 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
235 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
236 _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
237
2b1759ccaa8b planemo upload for repository https://github.com/esg-epfl-apc/tools-astro/tree/main/tools commit f28a8cb73a7f3053eac92166867a48b3d4af28fd
astroteam
parents:
diff changeset
238 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
239 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
240 print("*** Job finished successfully ***")