Skip to content

Commit c7b252e

Browse files
authored
Merge pull request #4 from fact-project/more_output
Add option to store more features from the unfolding to json
2 parents 5a883b2 + d33be76 commit c7b252e

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

fact_funfolding/io.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
22
import astropy.units as u
3+
import numpy as np
34

45
from fact.analysis.statistics import POINT_SOURCE_FLUX_UNIT
56

67

7-
def save_spectrum(outputfile, bins, flux, flux_err, counts, counts_err, g=None, bg=None, **metadata):
8+
def save_spectrum(outputfile, bins, flux, flux_err, counts, counts_err, g=None, bg=None, add_features=None, **metadata):
89
bins = bins.to(u.GeV)
910
bin_centers = 0.5 * (bins[:-1] + bins[1:])
1011

@@ -39,16 +40,34 @@ def save_spectrum(outputfile, bins, flux, flux_err, counts, counts_err, g=None,
3940
**metadata
4041
}
4142

42-
if g is not None:
43-
data['g'] = g.tolist()
43+
add_if_not_none(data, g, 'g')
44+
add_if_not_none(data, bg, 'bg')
4445

45-
if bg is not None:
46-
data['bg'] = bg.tolist()
46+
if add_features is not None:
47+
for key in add_features:
48+
add_if_not_none(data, add_features[key], key)
49+
50+
if metadata is not None:
51+
for key in metadata:
52+
add_if_not_none(data, metadata[key], key)
4753

4854
with open(outputfile, 'w') as f:
4955
json.dump(data, f, indent=4)
5056

5157

58+
def add_if_not_none(result_dict, value, key):
59+
if value is not None:
60+
if isinstance(value, u.quantity.Quantity):
61+
result_dict[key] = {
62+
'value': value.value if value.isscalar else value.value.tolist(),
63+
'unit': str(value.unit)
64+
}
65+
elif isinstance(value, np.ndarray):
66+
result_dict[key] = value.tolist() if len(value) > 1 else value
67+
else:
68+
result_dict[key] = value
69+
70+
5271
def read_spectrum(inputfile):
5372
with open(inputfile, 'r') as f:
5473
data = json.load(f)

fact_funfolding/scripts/unfold_observations.py

+6
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ def main(
167167
vec_f_est = vec_f_est[1:]
168168
sigma_vec_f = sigma_vec_f[:, 1:]
169169

170+
additional_features_to_save = dict()
171+
additional_features_to_save['a_eff'] = a_eff
172+
additional_features_to_save['a_eff_low'] = a_eff_low
173+
additional_features_to_save['a_eff_high'] = a_eff_high
174+
170175
save_spectrum(
171176
output_file,
172177
bins_true,
@@ -178,6 +183,7 @@ def main(
178183
bg=vec_g_bg,
179184
tau=config.tau,
180185
label=config.label,
186+
add_features=additional_features_to_save,
181187

182188
)
183189

fact_funfolding/scripts/unfold_simulations.py

+6
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,11 @@ def main(
162162
vec_f_est = vec_f_est[1:]
163163
sigma_vec_f = sigma_vec_f[:, 1:]
164164

165+
additional_features_to_save = dict()
166+
additional_features_to_save['a_eff'] = a_eff
167+
additional_features_to_save['a_eff_low'] = a_eff_low
168+
additional_features_to_save['a_eff_high'] = a_eff_high
169+
165170
save_spectrum(
166171
output_file,
167172
bins_true,
@@ -171,6 +176,7 @@ def main(
171176
counts_err=sigma_vec_f,
172177
tau=config.tau,
173178
label=config.label,
179+
add_features=additional_features_to_save,
174180
)
175181

176182

0 commit comments

Comments
 (0)