|
1
|
|
|
from regions import PointSkyRegion |
|
2
|
|
|
|
|
3
|
|
|
from asgardpy.analysis import AsgardpyAnalysis |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
def test_gpy_mwl(gpy_mwl_config, gammapy_data_path): |
|
7
|
|
|
""" |
|
8
|
|
|
Test for running the 3D+1D joint analysis tutorial example from Gammapy. |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
|
|
from gammapy.datasets import FluxPointsDataset |
|
12
|
|
|
from gammapy.estimators import FluxPoints |
|
13
|
|
|
from gammapy.modeling.models import create_crab_spectral_model |
|
14
|
|
|
|
|
15
|
|
|
from asgardpy.data.target import set_models |
|
16
|
|
|
|
|
17
|
|
|
analysis = AsgardpyAnalysis(gpy_mwl_config) |
|
18
|
|
|
|
|
19
|
|
|
# Update model parameters |
|
20
|
|
|
# LP-amplitude |
|
21
|
|
|
analysis.config.target.components[0].spectral.parameters[0].value /= 1e4 |
|
22
|
|
|
analysis.config.target.components[0].spectral.parameters[0].min = 1.0e-13 |
|
23
|
|
|
analysis.config.target.components[0].spectral.parameters[0].max = 0.01 |
|
24
|
|
|
analysis.config.target.components[0].spectral.parameters[0].frozen = False |
|
25
|
|
|
|
|
26
|
|
|
# LP-reference |
|
27
|
|
|
analysis.config.target.components[0].spectral.parameters[1].value *= 1e3 |
|
28
|
|
|
analysis.config.target.components[0].spectral.parameters[1].min = 0.001 |
|
29
|
|
|
analysis.config.target.components[0].spectral.parameters[1].max = 100 |
|
30
|
|
|
|
|
31
|
|
|
# LP-alpha |
|
32
|
|
|
analysis.config.target.components[0].spectral.parameters[2].min = 0.5 |
|
33
|
|
|
analysis.config.target.components[0].spectral.parameters[2].max = 5.0 |
|
34
|
|
|
analysis.config.target.components[0].spectral.parameters[2].frozen = False |
|
35
|
|
|
|
|
36
|
|
|
# LP-beta |
|
37
|
|
|
analysis.config.target.components[0].spectral.parameters[3].min = 0.001 |
|
38
|
|
|
analysis.config.target.components[0].spectral.parameters[3].max = 1.0 |
|
39
|
|
|
analysis.config.target.components[0].spectral.parameters[3].frozen = False |
|
40
|
|
|
|
|
41
|
|
|
# Spatial-lon |
|
42
|
|
|
analysis.config.target.components[0].spatial.parameters[0].error = 1.0e-6 |
|
43
|
|
|
analysis.config.target.components[0].spatial.parameters[0].min = 83.0 |
|
44
|
|
|
analysis.config.target.components[0].spatial.parameters[0].max = 84.0 |
|
45
|
|
|
|
|
46
|
|
|
# Spatial-lat |
|
47
|
|
|
analysis.config.target.components[0].spatial.parameters[1].error = 1.0e-6 |
|
48
|
|
|
analysis.config.target.components[0].spatial.parameters[1].min = -90 |
|
49
|
|
|
analysis.config.target.components[0].spatial.parameters[1].max = +90 |
|
50
|
|
|
|
|
51
|
|
|
# FoV-bkg-Norm - Not being read exactly |
|
52
|
|
|
analysis.config.target.components[1].spectral.parameters[0].min = 0.0 |
|
53
|
|
|
analysis.config.target.components[1].spectral.parameters[0].max = 10.0 |
|
54
|
|
|
analysis.config.target.components[1].spectral.parameters[0].frozen = False |
|
55
|
|
|
|
|
56
|
|
|
analysis.run(["datasets-3d"]) |
|
57
|
|
|
analysis.run(["datasets-1d"]) |
|
58
|
|
|
|
|
59
|
|
|
# Include HAWC Flux Points |
|
60
|
|
|
# Read to Gammapy objects |
|
61
|
|
|
filename = f"{gammapy_data_path}hawc_crab/HAWC19_flux_points.fits" |
|
62
|
|
|
fp_hawc = FluxPoints.read(filename, reference_model=create_crab_spectral_model("meyer")) |
|
63
|
|
|
fpd_hawc = FluxPointsDataset(data=fp_hawc, name="HAWC") |
|
64
|
|
|
|
|
65
|
|
|
analysis.datasets.append(fpd_hawc) |
|
66
|
|
|
|
|
67
|
|
|
# Update other dataset info |
|
68
|
|
|
analysis.dataset_name_list.append("HAWC") |
|
69
|
|
|
|
|
70
|
|
|
""" |
|
71
|
|
|
# FPE to only run for Fermi and HESS datasets, as HAWC is already estimated. |
|
72
|
|
|
analysis.instrument_spectral_info["name"].append("HAWC") |
|
73
|
|
|
|
|
74
|
|
|
hawc_en = np.array([1, 1.78, 3.16, 5.62, 10.0, 17.8, 31.6, 56.2, 100, 177, 316]) * u.TeV |
|
75
|
|
|
analysis.instrument_spectral_info["spectral_energy_ranges"].append(hawc_en) |
|
76
|
|
|
analysis.instrument_spectral_info["en_bins"] += 10 |
|
77
|
|
|
analysis.instrument_spectral_info["DoF"] += 10 |
|
78
|
|
|
""" |
|
79
|
|
|
|
|
80
|
|
|
# Reset models to the updated dataset |
|
81
|
|
|
analysis.datasets, analysis.final_model = set_models( |
|
82
|
|
|
analysis.config.target, |
|
83
|
|
|
analysis.datasets, |
|
84
|
|
|
analysis.dataset_name_list, |
|
85
|
|
|
models=analysis.final_model, |
|
86
|
|
|
) |
|
87
|
|
|
|
|
88
|
|
|
# Update Fit energy range |
|
89
|
|
|
analysis.config.fit_params.fit_range.max = "300 TeV" |
|
90
|
|
|
|
|
91
|
|
|
analysis.run(["fit"]) |
|
92
|
|
|
analysis.get_flux_points() |
|
93
|
|
|
|
|
94
|
|
|
assert analysis.fit_result.success is True |
|
95
|
|
|
assert len(analysis.datasets) == 3 |
|
96
|
|
|
assert len(analysis.flux_points) == 2 |
|
97
|
|
|
assert analysis.datasets[1].counts.geom.region is None |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
def test_3d_hess_1d_magic(gpy_hess_magic): |
|
101
|
|
|
"""Test for running HESS (3D) + MAGIC (1D) joint analysis.""" |
|
102
|
|
|
|
|
103
|
|
|
analysis = AsgardpyAnalysis(gpy_hess_magic) |
|
104
|
|
|
analysis.config.dataset3d.instruments[0].dataset_info.background.method = "ring" |
|
105
|
|
|
analysis.config.dataset3d.instruments[0].dataset_info.background.parameters = { |
|
106
|
|
|
"r_in": "1 deg", |
|
107
|
|
|
"width": "3 deg", |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
analysis.run(["datasets-3d", "datasets-1d", "fit"]) |
|
111
|
|
|
|
|
112
|
|
|
assert int(analysis.datasets[0].gti.time_sum.value) == 5056 |
|
113
|
|
|
assert isinstance(analysis.datasets[1].counts.geom.region, PointSkyRegion) |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
def test_gpy_mwl_failsafe(gpy_hess_magic): |
|
117
|
|
|
"""Test for checking some failsafe options.""" |
|
118
|
|
|
import pytest |
|
119
|
|
|
|
|
120
|
|
|
analysis_1 = AsgardpyAnalysis(gpy_hess_magic) |
|
121
|
|
|
with pytest.raises(ValueError): |
|
122
|
|
|
analysis_1.config.dataset1d.instruments[0].dataset_info.background.exclusion.regions[0].type = "a" |
|
123
|
|
|
analysis_1.run(["datasets-1d"]) |
|
124
|
|
|
|
|
125
|
|
|
analysis_2 = AsgardpyAnalysis(gpy_hess_magic) |
|
126
|
|
|
with pytest.raises(ValueError): |
|
127
|
|
|
analysis_2.config.target.components[0].name = "" |
|
128
|
|
|
analysis_2.config.dataset1d.instruments[0].dataset_info.background.exclusion.regions[ |
|
129
|
|
|
0 |
|
130
|
|
|
].type = "CircleSkyRegion" |
|
131
|
|
|
analysis_2.run(["datasets-1d"]) |
|
132
|
|
|
|
|
133
|
|
|
analysis_3 = AsgardpyAnalysis(gpy_hess_magic) |
|
134
|
|
|
analysis_3.config.target.source_name = "" |
|
135
|
|
|
analysis_3.config.target.components[0].name = "Crab Nebula" |
|
136
|
|
|
analysis_3.run(["datasets-3d", "datasets-1d"]) |
|
137
|
|
|
|
|
138
|
|
|
assert analysis_3.final_model.names[0] == "" |
|
139
|
|
|
|