1
|
|
|
import os |
2
|
|
|
|
3
|
|
|
import pytest |
4
|
|
|
from gammapy.utils.check import check_tutorials_setup |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
# add a marker for the tests that need private data and don't run them |
8
|
|
|
# by default |
9
|
|
|
def pytest_configure(config): |
10
|
|
|
if "test_data" not in config.option.markexpr: # pragma: no cover |
11
|
|
|
if config.option.markexpr: |
12
|
|
|
config.option.markexpr += " and " |
13
|
|
|
config.option.markexpr += "not test_data" |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
# Check if gammapy-data is downloaded and if not, then download it. |
17
|
|
|
check_tutorials_setup(download_datasets_path="./gammapy-data") |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
@pytest.mark.test_data |
21
|
|
|
@pytest.fixture # (scope="session") |
22
|
|
|
def base_config_path(): |
23
|
|
|
"""Get the base config path for basic tests.""" |
24
|
|
|
|
25
|
|
|
return "src/asgardpy/tests/config_test_base.yaml" |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
@pytest.mark.test_data |
29
|
|
|
@pytest.fixture # (scope="session") |
30
|
|
|
def mwl_config_path(): |
31
|
|
|
"""Get the Gammapy MWL tutorial config path.""" |
32
|
|
|
|
33
|
|
|
return "src/asgardpy/tests/config_gpy_mwl.yaml" |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
@pytest.mark.test_data |
37
|
|
|
@pytest.fixture # (scope="session") |
38
|
|
|
def hess_magic_config_path(): |
39
|
|
|
"""Get the config path for HESS (3D) + MAGIC (1D).""" |
40
|
|
|
|
41
|
|
|
return "src/asgardpy/tests/config_test_gadf.yaml" |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
@pytest.mark.test_data |
45
|
|
|
@pytest.fixture # (scope="session") |
46
|
|
|
def ebl_deabs_path(): |
47
|
|
|
"""Get the base config path for basic tests.""" |
48
|
|
|
|
49
|
|
|
return "src/asgardpy/tests/config_test_ebl.yaml" |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
@pytest.mark.test_data |
53
|
|
|
@pytest.fixture # (scope="session") |
54
|
|
|
def gammapy_data_path(): |
55
|
|
|
"""Save a copy of path of gammapy-data for easy and general use.""" |
56
|
|
|
|
57
|
|
|
# Check first for the path used in CI test |
58
|
|
|
if os.path.exists("./gammapy-datasets/1.2/"): |
59
|
|
|
GAMMAPY_DATA = "./gammapy-datasets/1.2/" |
60
|
|
|
# Update the environ for builtin EBL models |
61
|
|
|
os.environ["GAMMAPY_DATA"] = GAMMAPY_DATA |
62
|
|
|
else: |
63
|
|
|
# Using the saved path in the environ for users |
64
|
|
|
GAMMAPY_DATA = os.environ.get("GAMMAPY_DATA", "not set") |
65
|
|
|
|
66
|
|
|
return GAMMAPY_DATA |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
@pytest.mark.test_data |
70
|
|
|
@pytest.fixture # (scope="session") |
71
|
|
|
def base_config(base_config_path, gammapy_data_path): |
72
|
|
|
"""Define the base config for basic tests.""" |
73
|
|
|
|
74
|
|
|
from asgardpy.config import AsgardpyConfig |
75
|
|
|
|
76
|
|
|
config = AsgardpyConfig().read(base_config_path) |
77
|
|
|
|
78
|
|
|
# Update DL3 file paths |
79
|
|
|
config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}fermipy-crab/" |
80
|
|
|
|
81
|
|
|
config.dataset3d.instruments[0].input_dl3[1].input_dir = f"{gammapy_data_path}fermipy-crab/" |
82
|
|
|
|
83
|
|
|
config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/" |
84
|
|
|
|
85
|
|
|
config.dataset1d.instruments[1].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/" |
86
|
|
|
|
87
|
|
|
return config |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
@pytest.mark.test_data |
91
|
|
|
@pytest.fixture # (scope="session") |
92
|
|
|
def base_config_1d(base_config): |
93
|
|
|
"""Define base config for only 1D analysis.""" |
94
|
|
|
|
95
|
|
|
base_config_1d = base_config |
96
|
|
|
base_config_1d.target.source_name = "Crab Nebula" |
97
|
|
|
|
98
|
|
|
# Update model parameters |
99
|
|
|
base_config_1d.target.components[0].spectral.parameters[0].value = 1.0e-9 |
100
|
|
|
base_config_1d.target.components[0].spectral.parameters[1].value = 0.4 |
101
|
|
|
base_config_1d.target.components[0].spectral.parameters[2].value = 2.0 |
102
|
|
|
|
103
|
|
|
base_config_1d.fit_params.fit_range.min = "100 GeV" |
104
|
|
|
|
105
|
|
|
return base_config_1d |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
@pytest.mark.test_data |
109
|
|
|
@pytest.fixture # (scope="session") |
110
|
|
|
def gpy_mwl_config(mwl_config_path, gammapy_data_path): |
111
|
|
|
"""Define the Gammapy MWL Tutorial config.""" |
112
|
|
|
|
113
|
|
|
from asgardpy.config import AsgardpyConfig, gammapy_to_asgardpy_model_config |
114
|
|
|
|
115
|
|
|
config = AsgardpyConfig().read(mwl_config_path) |
116
|
|
|
|
117
|
|
|
# Update DL4 file paths and models file path |
118
|
|
|
config.target.models_file = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_models.yaml" |
119
|
|
|
config.dataset3d.instruments[ |
120
|
|
|
0 |
121
|
|
|
].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_datasets.yaml" |
122
|
|
|
config.dataset1d.instruments[ |
123
|
|
|
0 |
124
|
|
|
].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}joint-crab/spectra/hess/" |
125
|
|
|
|
126
|
|
|
other_config = gammapy_to_asgardpy_model_config(config.target.models_file, recursive_merge=False) |
127
|
|
|
|
128
|
|
|
config = config.update(other_config) |
129
|
|
|
|
130
|
|
|
return config |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
@pytest.mark.test_data |
134
|
|
|
@pytest.fixture # (scope="session") |
135
|
|
|
def gpy_hess_magic(hess_magic_config_path, gammapy_data_path): |
136
|
|
|
"""Define the config for HESS (3D) + MAGIC (1D).""" |
137
|
|
|
|
138
|
|
|
from asgardpy.config import AsgardpyConfig |
139
|
|
|
|
140
|
|
|
config = AsgardpyConfig().read(hess_magic_config_path) |
141
|
|
|
|
142
|
|
|
# Update DL3 file paths |
143
|
|
|
config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/" |
144
|
|
|
|
145
|
|
|
config.dataset3d.instruments[ |
146
|
|
|
0 |
147
|
|
|
].dataset_info.background.exclusion.exclusion_file = ( |
148
|
|
|
f"{gammapy_data_path}joint-crab/exclusion/exclusion_mask_crab.fits.gz" |
149
|
|
|
) |
150
|
|
|
|
151
|
|
|
config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/" |
152
|
|
|
|
153
|
|
|
return config |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
@pytest.mark.test_data |
157
|
|
|
@pytest.fixture # (scope="session") |
158
|
|
|
def ebl_hess_pks(ebl_deabs_path, gammapy_data_path): |
159
|
|
|
"""Define the config for HESS PKS 2155-304 data.""" |
160
|
|
|
|
161
|
|
|
from asgardpy.config import AsgardpyConfig |
162
|
|
|
|
163
|
|
|
config = AsgardpyConfig().read(ebl_deabs_path) |
164
|
|
|
|
165
|
|
|
# Update DL4 file path |
166
|
|
|
config.dataset1d.instruments[0].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}PKS2155-steady/" |
167
|
|
|
|
168
|
|
|
return config |
169
|
|
|
|