1 | # Licensed under a 3-clause BSD style license - see LICENSE.rst |
||
2 | from __future__ import absolute_import, division, print_function, unicode_literals |
||
3 | import pytest |
||
4 | from astropy.coordinates import SkyCoord, Angle |
||
5 | from regions import CircleSkyRegion |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
6 | from ...utils.testing import ( |
||
7 | requires_data, |
||
8 | requires_dependency, |
||
9 | assert_quantity_allclose, |
||
10 | mpl_plot_check, |
||
11 | ) |
||
12 | from ...maps import WcsNDMap, WcsGeom |
||
13 | from ...data import DataStore |
||
14 | from ..reflected import ReflectedRegionsFinder, ReflectedRegionsBackgroundEstimator |
||
15 | |||
16 | |||
17 | @pytest.fixture |
||
18 | def mask(): |
||
19 | """Example mask for testing.""" |
||
20 | pos = SkyCoord(83.63, 22.01, unit="deg", frame="icrs") |
||
21 | exclusion_region = CircleSkyRegion(pos, Angle(0.3, "deg")) |
||
22 | geom = WcsGeom.create(skydir=pos, binsz=0.02, width=10.0) |
||
23 | mask = geom.region_mask([exclusion_region], inside=False) |
||
24 | return WcsNDMap(geom, data=mask) |
||
25 | |||
26 | |||
27 | @pytest.fixture |
||
28 | def on_region(): |
||
29 | """Example on_region for testing.""" |
||
30 | pos = SkyCoord(83.63, 22.01, unit="deg", frame="icrs") |
||
31 | radius = Angle(0.11, "deg") |
||
32 | region = CircleSkyRegion(pos, radius) |
||
33 | return region |
||
34 | |||
35 | |||
36 | @pytest.fixture |
||
37 | def observations(): |
||
38 | """Example observation list for testing.""" |
||
39 | datastore = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/hess-dl3-dr1") |
||
40 | obs_ids = [23523, 23526] |
||
41 | return datastore.get_observations(obs_ids) |
||
42 | |||
43 | |||
44 | @pytest.fixture |
||
45 | def bkg_estimator(): |
||
46 | """Example background estimator for testing.""" |
||
47 | return ReflectedRegionsBackgroundEstimator( |
||
48 | observations=observations(), on_region=on_region(), exclusion_mask=mask() |
||
49 | ) |
||
50 | |||
51 | |||
52 | @requires_data("gammapy-extra") |
||
53 | def test_find_reflected_regions(mask, on_region): |
||
54 | pointing = SkyCoord(83.2, 22.5, unit="deg") |
||
55 | fregions = ReflectedRegionsFinder( |
||
56 | center=pointing, |
||
57 | region=on_region, |
||
58 | exclusion_mask=mask, |
||
59 | min_distance_input=Angle("0 deg"), |
||
60 | ) |
||
61 | fregions.run() |
||
62 | regions = fregions.reflected_regions |
||
63 | assert len(regions) == 15 |
||
64 | assert_quantity_allclose(regions[3].center.icrs.ra, Angle("83.674 deg"), rtol=1e-2) |
||
65 | |||
66 | # Test without exclusion |
||
67 | fregions.exclusion_mask = None |
||
68 | fregions.run() |
||
69 | regions = fregions.reflected_regions |
||
70 | assert len(regions) == 16 |
||
71 | |||
72 | # Test with too small exclusion |
||
73 | small_mask = mask.cutout(pointing, Angle("0.2 deg")) |
||
74 | fregions.exclusion_mask = small_mask |
||
75 | fregions.run() |
||
76 | regions = fregions.reflected_regions |
||
77 | assert len(regions) == 16 |
||
78 | assert_quantity_allclose(regions[3].center.icrs.ra, Angle("83.674 deg"), rtol=1e-2) |
||
79 | |||
80 | # Test with maximum number of regions |
||
81 | fregions.max_region_number = 5 |
||
82 | fregions.run() |
||
83 | regions = fregions.reflected_regions |
||
84 | assert len(regions) == 5 |
||
85 | |||
86 | |||
87 | @requires_data("gammapy-extra") |
||
0 ignored issues
–
show
|
|||
88 | class TestReflectedRegionBackgroundEstimator: |
||
89 | def setup(self): |
||
90 | self.bg_maker = bkg_estimator() |
||
0 ignored issues
–
show
|
|||
91 | |||
92 | def test_basic(self): |
||
93 | assert "ReflectedRegionsBackgroundEstimator" in str(self.bg_maker) |
||
94 | |||
95 | def test_run(self): |
||
96 | self.bg_maker.finder.min_distance = Angle("0.2 deg") |
||
97 | self.bg_maker.run() |
||
98 | assert len(self.bg_maker.result[1].off_region) == 11 |
||
99 | assert "Reflected" in str(self.bg_maker.result[1]) |
||
100 | |||
101 | @requires_dependency("matplotlib") |
||
102 | def test_plot(self): |
||
103 | # The following line can be removed once we drop support for regions 0.2 |
||
104 | # See https://github.com/gammapy/gammapy/issues/1758 |
||
105 | pytest.importorskip("regions", minversion="0.3") |
||
106 | self.bg_maker.run() |
||
107 | with mpl_plot_check(): |
||
108 | self.bg_maker.plot() |
||
109 | self.bg_maker.plot(idx=1) |
||
110 | self.bg_maker.plot(idx=[0, 1]) |
||
111 | self.bg_maker.plot(add_legend=True) |
||
112 |