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

gammapy/background/tests/test_phase.py (6 issues)

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):
0 ignored issues
show
Comprehensibility Bug introduced by
phase_bkg_estimator is re-defining a name which is already available in the outer-scope (previously defined on line 27).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
39
    assert "PhaseBackgroundEstimator" in str(phase_bkg_estimator)
40
41
42
@requires_data("gammapy-extra")
43
def test_run(phase_bkg_estimator):
0 ignored issues
show
Comprehensibility Bug introduced by
phase_bkg_estimator is re-defining a name which is already available in the outer-scope (previously defined on line 27).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
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):
0 ignored issues
show
Comprehensibility Bug introduced by
observations is re-defining a name which is already available in the outer-scope (previously defined on line 20).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Comprehensibility Bug introduced by
on_region is re-defining a name which is already available in the outer-scope (previously defined on line 12).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
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"]
66
67
68
@requires_data("gammapy-extra")
69
def test_process(phase_bkg_estimator, observations):
0 ignored issues
show
Comprehensibility Bug introduced by
phase_bkg_estimator is re-defining a name which is already available in the outer-scope (previously defined on line 27).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
Comprehensibility Bug introduced by
observations is re-defining a name which is already available in the outer-scope (previously defined on line 20).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
70
    assert isinstance(phase_bkg_estimator.process(observations[0]), BackgroundEstimate)
71