Passed
Pull Request — master (#1929)
by
unknown
02:32
created

gammapy/background/tests/test_phase.py (1 issue)

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
6
from ...utils.testing import requires_data
7
from ...data import DataStore, EventList
8
from .. import BackgroundEstimate, PhaseBackgroundEstimator
9
10
11
@pytest.fixture
12
def on_region():
13
    """Example on_region for testing."""
14
    pos = SkyCoord("08h35m20.65525s", "-45d10m35.1545s", frame="icrs")
15
    radius = Angle(0.2, "deg")
16
    return CircleSkyRegion(pos, radius)
17
18
19
@pytest.fixture
20
def observations():
21
    """Example observation list for testing."""
22
    datastore = DataStore.from_dir("$GAMMAPY_EXTRA/datasets/cta-1dc/index/gps")
23
    return datastore.get_observations([111630])
24
25
26
@pytest.fixture(scope="session")
27
def phase_bkg_estimator():
28
    """Example background estimator for testing."""
29
    return PhaseBackgroundEstimator(
30
        observations=observations(),
31
        on_region=on_region(),
32
        on_phase=(0.5, 0.6),
33
        off_phase=(0.7, 1),
34
    )
35
36
37
@requires_data("gammapy-extra")
38
def test_basic(phase_bkg_estimator):
39
    assert "PhaseBackgroundEstimator" in str(phase_bkg_estimator)
40
41
42
@requires_data("gammapy-extra")
43
def test_run(phase_bkg_estimator):
44
    phase_bkg_estimator.run()
45
    assert len(phase_bkg_estimator.result) == 1
46
47
48
@requires_data("gammapy-extra")
49
def test_filter_events(observations, on_region):
50
    all_events = observations[0].events.select_circular_region(on_region)
51
    ev1 = PhaseBackgroundEstimator.filter_events(all_events, (0, 0.3))
52
    assert isinstance(ev1, EventList)
53
    ev2 = PhaseBackgroundEstimator.filter_events(all_events, (0.3, 1))
54
    assert len(all_events.table) == len(ev1.table) + len(ev2.table)
55
56
57
@pytest.mark.parametrize(
58
    "pars",
59
    [
60
        {"p_in": [[0.2, 0.3]], "p_out": [[0.2, 0.3]]},
61
        {"p_in": [[0.9, 0.1]], "p_out": [[0.9, 1], [0, 0.1]]},
62
    ],
63
)
64
def test_check_phase_intervals(pars):
65
    assert PhaseBackgroundEstimator._check_intervals(pars["p_in"]) == pars["p_out"]
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _check_intervals was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
66
67
68
@requires_data("gammapy-extra")
69
def test_process(phase_bkg_estimator, observations):
70
    assert isinstance(phase_bkg_estimator.process(observations[0]), BackgroundEstimate)
71