Issues (144)

gammapy/catalog/tests/test_gammacat.py (7 issues)

Check for undefined variables.

Best Practice Comprehensibility Minor
1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2
import pytest
3
from numpy.testing import assert_allclose
4
from astropy import units as u
5
from astropy.utils.data import get_pkg_data_filename
6
from gammapy.catalog import SourceCatalogGammaCat
7
from gammapy.utils.gauss import Gauss2DPDF
8
from gammapy.utils.testing import assert_quantity_allclose, requires_data
9
10
SOURCES = [
11
    {
12
        "name": "Vela X",
13
        "str_ref_file": "data/gammacat_vela_x.txt",
14
        "spec_type": "ecpl",
15
        "dnde_1TeV": 1.36e-11 * u.Unit("cm-2 s-1 TeV-1"),
16
        "dnde_1TeV_err": 7.531e-13 * u.Unit("cm-2 s-1 TeV-1"),
17
        "flux_1TeV": 2.104e-11 * u.Unit("cm-2 s-1"),
18
        "eflux_1_10TeV": 9.265778680255336e-11 * u.Unit("erg cm-2 s-1"),
19
        "n_flux_points": 24,
20
        "spatial_model": "GaussianSpatialModel",
21
        "ra": 128.287003,
22
        "dec": -45.189999,
23
    },
24
    {
25
        "name": "HESS J1848-018",
26
        "str_ref_file": "data/gammacat_hess_j1848-018.txt",
27
        "spec_type": "pl",
28
        "dnde_1TeV": 3.7e-12 * u.Unit("cm-2 s-1 TeV-1"),
29
        "dnde_1TeV_err": 4e-13 * u.Unit("cm-2 s-1 TeV-1"),
30
        "flux_1TeV": 2.056e-12 * u.Unit("cm-2 s-1"),
31
        "eflux_1_10TeV": 6.235650344765057e-12 * u.Unit("erg cm-2 s-1"),
32
        "n_flux_points": 11,
33
        "spatial_model": "GaussianSpatialModel",
34
        "ra": 282.119995,
35
        "dec": -1.792,
36
    },
37
    {
38
        "name": "HESS J1813-178",
39
        "str_ref_file": "data/gammacat_hess_j1813-178.txt",
40
        "spec_type": "pl2",
41
        "dnde_1TeV": 2.678e-12 * u.Unit("cm-2 s-1 TeV-1"),
42
        "dnde_1TeV_err": 2.55e-13 * u.Unit("cm-2 s-1 TeV-1"),
43
        "flux_1TeV": 2.457e-12 * u.Unit("cm-2 s-1"),
44
        "eflux_1_10TeV": 8.923614018939419e-12 * u.Unit("erg cm-2 s-1"),
45
        "n_flux_points": 13,
46
        "spatial_model": "GaussianSpatialModel",
47
        "ra": 273.362915,
48
        "dec": -17.84889,
49
    },
50
]
51
52
53
@pytest.fixture(scope="session")
54
def gammacat():
55
    filename = "$GAMMAPY_DATA/catalogs/gammacat/gammacat.fits.gz"
56
    return SourceCatalogGammaCat(filename=filename)
57
58
59
@requires_data()
60
class TestSourceCatalogGammaCat:
61
    def test_source_table(self, gammacat):
62
        assert gammacat.tag == "gamma-cat"
63
        assert len(gammacat.table) == 162
64
65
    def test_positions(self, gammacat):
66
        assert len(gammacat.positions) == 162
67
68
    def test_w28_alias_names(self, gammacat):
69
        for name in [
70
            "W28",
71
            "HESS J1801-233",
72
            "W 28",
73
            "SNR G6.4-0.1",
74
            "SNR G006.4-00.1",
75
            "GRO J1801-2320",
76
        ]:
77
            assert gammacat[name].row_index == 112
78
79
80
@requires_data()
81
class TestSourceCatalogObjectGammaCat:
82
    def test_data(self, gammacat):
83
        source = gammacat[0]
84
85
        assert isinstance(source.data, dict)
86
        assert source.data["common_name"] == "CTA 1"
87
        assert_quantity_allclose(source.data["dec"], 72.782997 * u.deg)
88
89
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
90
    def test_str(self, gammacat, ref):
91
        actual = str(gammacat[ref["name"]])
92
        expected = open(get_pkg_data_filename(ref["str_ref_file"])).read()
93
        assert actual == expected
94
95
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
96
    def test_spectral_model(self, gammacat, ref):
97
        source = gammacat[ref["name"]]
98
        spectral_model = source.spectral_model()
99
100
        assert source.data["spec_type"] == ref["spec_type"]
101
102
        e_min, e_max, e_inf = [1, 10, 1e10] * u.TeV
103
104
        dne = spectral_model(e_min)
105
        flux = spectral_model.integral(energy_min=e_min, energy_max=e_inf)
106
        eflux = spectral_model.energy_flux(energy_min=e_min, energy_max=e_max).to(
107
            "erg cm-2 s-1"
108
        )
109
110
        print(spectral_model)
111
        assert_quantity_allclose(dne, ref["dnde_1TeV"], rtol=1e-3)
112
        assert_quantity_allclose(flux, ref["flux_1TeV"], rtol=1e-3)
113
        assert_quantity_allclose(eflux, ref["eflux_1_10TeV"], rtol=1e-3)
114
115
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
116
    def test_spectral_model_err(self, gammacat, ref):
117
        source = gammacat[ref["name"]]
118
        spectral_model = source.spectral_model()
119
120
        e_min, e_max, e_inf = [1, 10, 1e10] * u.TeV
121
122
        dnde, dnde_err = spectral_model.evaluate_error(e_min)
123
124
        assert_quantity_allclose(dnde, ref["dnde_1TeV"], rtol=1e-3)
125
        assert_quantity_allclose(dnde_err, ref["dnde_1TeV_err"], rtol=1e-3)
126
127
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
128
    def test_flux_points(self, gammacat, ref):
129
        source = gammacat[ref["name"]]
130
131
        flux_points = source.flux_points
132
133
        assert flux_points.energy_axis.nbin == ref["n_flux_points"]
134
135
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
136
    def test_position(self, gammacat, ref):
137
        source = gammacat[ref["name"]]
138
139
        position = source.position
140
141
        assert_allclose(position.ra.deg, ref["ra"], atol=1e-3)
142
        assert_allclose(position.dec.deg, ref["dec"], atol=1e-3)
143
144
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
145
    def test_spatial_model(self, gammacat, ref):
146
        source = gammacat[ref["name"]]
147
148
        spatial_model = source.spatial_model()
149
        assert spatial_model.frame == "galactic"
150
151
        # TODO: put better asserts on model properties
152
        # TODO: add a point and shell source -> separate list of sources for
153
        # morphology test parametrization?
154
        assert spatial_model.__class__.__name__ == ref["spatial_model"]
155
156
        model = gammacat["HESS J1634-472"].spatial_model()
157
        pos_err = model.position_error
158
        scale_r95 = Gauss2DPDF().containment_radius(0.95)
159
        assert_allclose(pos_err.height.value, 2 * 0.044721 * scale_r95, rtol=1e-4)
160
        assert_allclose(pos_err.width.value, 2 * 0.044721 * scale_r95, rtol=1e-4)
161
        assert_allclose(model.position.l.value, pos_err.center.l.value)
162
        assert_allclose(model.position.b.value, pos_err.center.b.value)
163
164
    @pytest.mark.parametrize("ref", SOURCES, ids=lambda _: _["name"])
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable _ does not seem to be defined.
Loading history...
165
    def test_sky_model(self, gammacat, ref):
166
        gammacat[ref["name"]].sky_model()
167