-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDataset_load.py
190 lines (162 loc) · 7.05 KB
/
Dataset_load.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
from gammapy.datasets import MapDataset
from gammapy.modeling.models import (
FoVBackgroundModel,
Models,
PowerLawNormSpectralModel,
SkyModel,
LogParabolaSpectralModel,
SmoothBrokenPowerLawSpectralModel,
ExpCutoffPowerLawSpectralModel,
create_crab_spectral_model,
PiecewiseNormSpectralModel
)
from gammapy.modeling import Parameter, Parameters
import astropy.units as u
from gammapy.modeling.models.IRF import ( # ,IRFModel
EffAreaIRFModel,
ERecoIRFModel,
IRFModels,
)
def load_config():
import yaml
import os
from pathlib import Path
path = os.getcwd()
substring = "nuisance_summary"
path = path[: path.find(substring)] + substring + "/"
config = yaml.safe_load(Path(path + "config.yaml").read_text())
colors = config['colors']
# asimov without
awo = [tuple(colors[0][0]), tuple(colors[0][1])]
# asimov with
aw = [tuple(colors[1][0]), tuple(colors[1][1])]
# example without
ewo = [tuple(colors[2][0]), tuple(colors[2][1])]
# example with
ew = [tuple(colors[3][0]), tuple(colors[3][1])]
config['_colors'] = [awo, aw, ewo, ew]
config['folder'] = config['sys']+"_"+ config['source']+"_" +config['model']
return config
config = load_config()
case = config["case"]
path = config[case]["path"]
figformat = config["figformat"]
path_pksflare = config[case]["path_pksflare"]
def get_path(source):
return path_pksflare
def create_asimov(model, source, parameters=None, livetime=None, spatial_model = None):
path = get_path(source)
models = set_model(path, model)
if livetime is not None:
model = livetime
if source == "MSH":
dataset = MapDataset.read(f"{path}/HESS_public/dataset-msh-simulated-{model}.fits.gz")
print("loaded dataset:")
print(f"{path}/HESS_public/dataset-MSH-simulated-{model}.fits.gz")
if source == "PKSflare":
dataset = MapDataset.read(f"{path}/HESS_public/dataset-simulated-{model}.fits.gz")
print("loaded dataset:")
print(f"{path}/HESS_public/dataset-simulated-{model}.fits.gz")
if parameters is not None:
for p in parameters:
models.parameters[p.name].value = p.value
models.parameters[p.name].error = p.error
if spatial_model is not None:
models[0].spatial_model = spatial_model
bkg_model = FoVBackgroundModel(dataset_name=dataset.name)
bkg_model.parameters["tilt"].frozen = False
models.append(bkg_model)
dataset.models = models
dataset.counts = dataset.npred()
return dataset
def set_model(path, model):
if model == 'crab':
model_crab =create_crab_spectral_model(reference="hess_ecpl")
skymodelpl = Models.read(f"{path}/HESS_public/model-pl.yaml").copy()[0]
skymodel = Models([SkyModel(spatial_model = skymodelpl.spatial_model,
spectral_model = model_crab,
name = "Crab")])
elif "crab_break" in model:
model_crab = SmoothBrokenPowerLawSpectralModel(amplitude = 3.35e-10 *u.Unit(" 1 / (cm2 s TeV)"),
index1 = 1.61,
index2 = 2.95,
ebreak = 0.33*u.TeV,
beta = 1.73
)
skymodelpl = Models.read(f"{path}/HESS_public/model-pl.yaml").copy()[0]
skymodel = Models([SkyModel(spatial_model = skymodelpl.spatial_model,
spectral_model = model_crab,
name = "Crabbreak")])
if model == "crab_break_1f":
skymodel.parameters['index1'].frozen = True
if model == "crab_break_ef":
skymodel.parameters['index1'].frozen = True
skymodel.parameters['ebreak'].frozen = True
skymodel.parameters['beta'].frozen = False
elif model == "crab_log":
model_crab = LogParabolaSpectralModel(amplitude = 3.85e-11*u.Unit(" 1 / (cm2 s TeV)"),
alpha = 2.51,
beta = 0.24,
reference = 1*u.TeV)
skymodelpl = Models.read(f"{path}/HESS_public/model-pl.yaml").copy()[0]
skymodel = Models([SkyModel(spatial_model = skymodelpl.spatial_model,
spectral_model = model_crab,
name = "Crablog")])
elif model == "crab_cutoff":
model_crab = ExpCutoffPowerLawSpectralModel(amplitude = 3.85e-11*u.Unit(" 1 / (cm2 s TeV)"),
index = 2.3,
cutoff = 1/10 /u.TeV)
skymodelpl = Models.read(f"{path}/HESS_public/model-pl.yaml").copy()[0]
skymodel = Models([SkyModel(spatial_model = skymodelpl.spatial_model,
spectral_model = model_crab,
name = "Crablog")])
else:
skymodel = Models.read(f"{path}/HESS_public/model-{model}.yaml")#.copy()
return skymodel
def load_dataset_N(dataset_empty, path, bkg_sys=False, energy = None):
models_load = Models.read(path).copy()
Source = models_load.names[0]
models = Models(models_load[Source].copy())
dataset_read = dataset_empty.copy()
if bkg_sys:
import operator
l = len(energy)
norms = Parameters([Parameter ("norm"+str(i), value = 0, frozen = False) for i in range(l)])
piece = PiecewiseNormSpectralModel(energy = energy,
norms = norms,
interp="lin")
bkg = FoVBackgroundModel(spectral_model = piece,
dataset_name=dataset_read.name)
else:
bkg = FoVBackgroundModel(dataset_name=dataset_read.name)
models.append(bkg)
for p in bkg.parameters:
p.value = models_load.parameters[p.name].value
p.error = models_load.parameters[p.name].error
for m in models_load:
if m.type == "irf":
irf = IRFModels(
e_reco_model=m.e_reco_model,
eff_area_model=m.eff_area_model,
datasets_names=dataset_read.name,
)
#for p in irf.parameters:
# p.frozen = False
# p.value = models_load.parameters[p.name].value
# p.error = models_load.parameters[p.name].error
# p.frozen = models_load.parameters[p.name].frozen
models.append(irf)
dataset_read.models = models
return dataset_read
def load_dataset(dataset_empty, path):
models_load = Models.read(path).copy()
Source = models_load.names[0]
models = Models(models_load[Source].copy())
dataset_read = dataset_empty.copy()
bkg = FoVBackgroundModel(dataset_name=dataset_read.name)
for p in bkg.parameters:
p.value = models_load.parameters[p.name].value
p.error = models_load.parameters[p.name].error
models.append(bkg)
dataset_read.models = models
return dataset_read