|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
def test_models_from_config(): |
|
5
|
|
|
"""Test reading models from asgardpy config.""" |
|
6
|
|
|
|
|
7
|
|
|
from asgardpy.config import AsgardpyConfig, get_model_template |
|
8
|
|
|
from asgardpy.data.target import read_models_from_asgardpy_config, set_models |
|
9
|
|
|
|
|
10
|
|
|
config_eclp = AsgardpyConfig.read(get_model_template("eclp")) |
|
11
|
|
|
config_bpl2 = AsgardpyConfig.read(get_model_template("bpl2")) |
|
12
|
|
|
config_lp = AsgardpyConfig.read(get_model_template("lp")) |
|
13
|
|
|
config_fov = AsgardpyConfig.read(get_model_template("fov")) |
|
14
|
|
|
|
|
15
|
|
|
config_eclp.target.components[0].spectral.ebl_abs.reference = "" |
|
16
|
|
|
config_bpl2.target.components[0].spectral.ebl_abs.reference = "" |
|
17
|
|
|
config_lp.target.components[0].spectral.ebl_abs.reference = "" |
|
18
|
|
|
config_fov.target.components[0].spectral.ebl_abs.reference = "" |
|
19
|
|
|
|
|
20
|
|
|
model_eclp = read_models_from_asgardpy_config(config_eclp.target) |
|
21
|
|
|
model_bpl2 = read_models_from_asgardpy_config(config_bpl2.target) |
|
22
|
|
|
model_lp = read_models_from_asgardpy_config(config_lp.target) |
|
23
|
|
|
model_fov = read_models_from_asgardpy_config(config_fov.target) |
|
24
|
|
|
|
|
25
|
|
|
assert model_eclp[0].spectral_model.tag[0] == "ExpCutoffLogParabolaSpectralModel" |
|
26
|
|
|
assert model_bpl2[0].spectral_model.tag[0] == "BrokenPowerLaw2SpectralModel" |
|
27
|
|
|
assert model_lp[0].spectral_model.tag[0] == "LogParabolaSpectralModel" |
|
28
|
|
|
assert model_fov[0].spectral_model.tag[0] == "PowerLawNormSpectralModel" |
|
29
|
|
|
assert model_fov[0].spatial_model.tag[0] == "ConstantSpatialModel" |
|
30
|
|
|
|
|
31
|
|
|
# Exception for empty models information in config. |
|
32
|
|
|
with pytest.raises(TypeError): |
|
33
|
|
|
_, _ = set_models() |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def test_set_models(base_config, gammapy_data_path): |
|
37
|
|
|
"""Test non-standard components of Target module.""" |
|
38
|
|
|
|
|
39
|
|
|
from asgardpy.analysis import AsgardpyAnalysis |
|
40
|
|
|
from asgardpy.data.target import set_models |
|
41
|
|
|
|
|
42
|
|
|
ebl_file_name = "ebl_franceschini_2017.fits.gz" |
|
43
|
|
|
ebl_file = f"{gammapy_data_path}ebl/{ebl_file_name}" |
|
44
|
|
|
model_file_0 = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_models.yaml" |
|
45
|
|
|
model_file_1 = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_datasets.yaml" |
|
46
|
|
|
|
|
47
|
|
|
base_config.target.components[0].spectral.ebl_abs.filename = ebl_file |
|
48
|
|
|
|
|
49
|
|
|
analysis_0 = AsgardpyAnalysis(base_config) |
|
50
|
|
|
analysis_1 = AsgardpyAnalysis(base_config) |
|
51
|
|
|
analysis_2 = AsgardpyAnalysis(base_config) |
|
52
|
|
|
|
|
53
|
|
|
# Check when using create_source_skymodel function |
|
54
|
|
|
analysis_0.config.target.from_3d = True |
|
55
|
|
|
analysis_2.config.target.from_3d = True |
|
56
|
|
|
|
|
57
|
|
|
analysis_0.run(["datasets-3d"]) |
|
58
|
|
|
analysis_2.run(["datasets-3d"]) |
|
59
|
|
|
# Check when using read_models_from_asgardpy_config |
|
60
|
|
|
analysis_1.run(["datasets-1d"]) |
|
61
|
|
|
|
|
62
|
|
|
analysis_1.config.target.source_name = "4FGL J0534.5+2201i" |
|
63
|
|
|
analysis_0.config.target.source_name = "Crab Nebula" |
|
64
|
|
|
|
|
65
|
|
|
data_0, model_0 = set_models( |
|
66
|
|
|
analysis_0.config.target, |
|
67
|
|
|
analysis_0.datasets, |
|
68
|
|
|
datasets_name_list=None, |
|
69
|
|
|
models=model_file_0, |
|
70
|
|
|
) |
|
71
|
|
|
|
|
72
|
|
|
data_1, model_1 = set_models( |
|
73
|
|
|
analysis_0.config.target, |
|
74
|
|
|
analysis_0.datasets, |
|
75
|
|
|
datasets_name_list=None, |
|
76
|
|
|
) |
|
77
|
|
|
data_2, model_2 = set_models( |
|
78
|
|
|
analysis_1.config.target, |
|
79
|
|
|
analysis_1.datasets, |
|
80
|
|
|
datasets_name_list=None, |
|
81
|
|
|
) |
|
82
|
|
|
|
|
83
|
|
|
# Check when not providing target source name for creating the center of ROI in the exclusion mask |
|
84
|
|
|
analysis_2.config.target.source_name = "" |
|
85
|
|
|
data_3, model_3 = set_models( |
|
86
|
|
|
analysis_2.config.target, |
|
87
|
|
|
analysis_2.datasets, |
|
88
|
|
|
datasets_name_list=None, |
|
89
|
|
|
) |
|
90
|
|
|
|
|
91
|
|
|
with pytest.raises(KeyError): |
|
92
|
|
|
_, _ = set_models( |
|
93
|
|
|
analysis_0.config.target, |
|
94
|
|
|
analysis_0.datasets, |
|
95
|
|
|
datasets_name_list=None, |
|
96
|
|
|
models=model_file_1, |
|
97
|
|
|
) |
|
98
|
|
|
with pytest.raises(TypeError): |
|
99
|
|
|
_, _ = set_models( |
|
100
|
|
|
analysis_0.config.target, |
|
101
|
|
|
analysis_0.datasets, |
|
102
|
|
|
datasets_name_list=None, |
|
103
|
|
|
models=1, |
|
104
|
|
|
) |
|
105
|
|
|
assert model_0[0].datasets_names == ["Fermi-LAT_00", "Fermi-LAT_01"] |
|
106
|
|
|
assert model_1[0].spectral_model.model2.filename.name == ebl_file_name |
|
107
|
|
|
assert model_2[0].spectral_model.model2.filename.name == ebl_file_name |
|
108
|
|
|
assert analysis_0.final_model[0].spectral_model.model2.filename.name == ebl_file_name |
|
109
|
|
|
assert model_3[0].name == "" |
|
110
|
|
|
|