asgardpy.conftest   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 87
dl 0
loc 180
rs 10
c 0
b 0
f 0
wmc 16

13 Functions

Rating   Name   Duplication   Size   Complexity  
A pytest_configure() 0 5 3
A base_config_path() 0 5 1
A ebl_deabs_path() 0 5 1
A gpy_mwl_config() 0 22 1
A base_config() 0 18 1
A mwl_config_path() 0 5 1
A ebl_hess_pks() 0 12 1
A hawc_dl3_config() 0 12 1
A hess_magic_config_path() 0 5 1
A gpy_hess_magic() 0 20 1
A gammapy_data_path() 0 14 2
A hawc_config_path() 0 5 1
A base_config_1d() 0 15 1
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.fixture  # (scope="session")
21
def base_config_path():
22
    """Get the base config path for basic tests."""
23
24
    return "src/asgardpy/tests/config_test_base.yaml"
25
26
27
@pytest.fixture  # (scope="session")
28
def mwl_config_path():
29
    """Get the Gammapy MWL tutorial config path."""
30
31
    return "src/asgardpy/tests/config_gpy_mwl.yaml"
32
33
34
@pytest.fixture  # (scope="session")
35
def hess_magic_config_path():
36
    """Get the config path for HESS (3D) + MAGIC (1D)."""
37
38
    return "src/asgardpy/tests/config_test_gadf.yaml"
39
40
41
@pytest.fixture  # (scope="session")
42
def hawc_config_path():
43
    """Get the config path for HAWC (3D)."""
44
45
    return "src/asgardpy/tests/config_hawc.yaml"
46
47
48
@pytest.fixture  # (scope="session")
49
def ebl_deabs_path():
50
    """Get the base config path for basic tests."""
51
52
    return "src/asgardpy/tests/config_test_ebl.yaml"
53
54
55
@pytest.fixture  # (scope="session")
56
def gammapy_data_path():
57
    """Save a copy of path of gammapy-data for easy and general use."""
58
59
    # Check first for the path used in CI test
60
    if os.path.exists("./gammapy-datasets/1.3/"):
61
        GAMMAPY_DATA = "./gammapy-datasets/1.3/"
62
        # Update the environ for builtin EBL models
63
        os.environ["GAMMAPY_DATA"] = GAMMAPY_DATA
64
    else:
65
        # Using the saved path in the environ for users
66
        GAMMAPY_DATA = os.environ.get("GAMMAPY_DATA", "not set")
67
68
    return GAMMAPY_DATA
69
70
71
@pytest.fixture  # (scope="session")
72
def base_config(base_config_path, gammapy_data_path):
73
    """Define the base config for basic tests."""
74
75
    from asgardpy.config import AsgardpyConfig
76
77
    config = AsgardpyConfig().read(base_config_path)
78
79
    # Update DL3 file paths
80
    config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}fermipy-crab/"
81
82
    config.dataset3d.instruments[0].input_dl3[1].input_dir = f"{gammapy_data_path}fermipy-crab/"
83
84
    config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/"
85
86
    config.dataset1d.instruments[1].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/"
87
88
    return config
89
90
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.fixture  # (scope="session")
109
def gpy_mwl_config(mwl_config_path, gammapy_data_path):
110
    """Define the Gammapy MWL Tutorial config."""
111
112
    from asgardpy.config import AsgardpyConfig, gammapy_model_to_asgardpy_model_config
113
114
    config = AsgardpyConfig().read(mwl_config_path)
115
116
    # Update DL4 file paths and models file path
117
    config.target.models_file = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_models.yaml"
118
    config.dataset3d.instruments[
119
        0
120
    ].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}fermi-3fhl-crab/Fermi-LAT-3FHL_datasets.yaml"
121
    config.dataset1d.instruments[
122
        0
123
    ].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}joint-crab/spectra/hess/"
124
125
    other_config = gammapy_model_to_asgardpy_model_config(config.target.models_file, recursive_merge=False)
126
127
    config = config.update(other_config)
128
129
    return config
130
131
132
@pytest.fixture  # (scope="session")
133
def gpy_hess_magic(hess_magic_config_path, gammapy_data_path):
134
    """Define the config for HESS (3D) + MAGIC (1D)."""
135
136
    from asgardpy.config import AsgardpyConfig
137
138
    config = AsgardpyConfig().read(hess_magic_config_path)
139
140
    # Update DL3 file paths
141
    config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hess-dl3-dr1/"
142
143
    config.dataset3d.instruments[
144
        0
145
    ].dataset_info.background.exclusion.exclusion_file = (
146
        f"{gammapy_data_path}joint-crab/exclusion/exclusion_mask_crab.fits.gz"
147
    )
148
149
    config.dataset1d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}magic/rad_max/data/"
150
151
    return config
152
153
154
@pytest.fixture  # (scope="session")
155
def ebl_hess_pks(ebl_deabs_path, gammapy_data_path):
156
    """Define the config for HESS PKS 2155-304 data."""
157
158
    from asgardpy.config import AsgardpyConfig
159
160
    config = AsgardpyConfig().read(ebl_deabs_path)
161
162
    # Update DL4 file path
163
    config.dataset1d.instruments[0].dl4_dataset_info.dl4_dataset.input_dir = f"{gammapy_data_path}PKS2155-steady/"
164
165
    return config
166
167
168
@pytest.fixture  # (scope="session")
169
def hawc_dl3_config(hawc_config_path, gammapy_data_path):
170
    """Define the config for HAWC (3D)."""
171
172
    from asgardpy.config import AsgardpyConfig
173
174
    config = AsgardpyConfig().read(hawc_config_path)
175
176
    # Update DL3 file path
177
    config.dataset3d.instruments[0].input_dl3[0].input_dir = f"{gammapy_data_path}hawc/crab_events_pass4/"
178
179
    return config
180