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

gammapy/background/tests/test_ring.py (11 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
0 ignored issues
show
Unable to import 'pytest'
Loading history...
4
from numpy.testing import assert_allclose
5
import numpy as np
6
from astropy import units as u
7
from ...maps import WcsNDMap
8
from ...background import RingBackgroundEstimator, AdaptiveRingBackgroundEstimator
9
10
11
@pytest.fixture
12
def images():
13
    fov = 2.5 * u.deg
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
14
15
    m_ref = WcsNDMap.create(binsz=0.05, npix=201, dtype=float)
16
    m_ref.data += 1.0
17
    coords = m_ref.geom.get_coord().skycoord
18
    center = m_ref.geom.center_skydir
19
    mask = coords.separation(center) < fov
20
21
    images = dict()
0 ignored issues
show
Comprehensibility Bug introduced by
images 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...
22
    images["counts"] = m_ref.copy(data=np.zeros_like(m_ref.data) + 2.0)
23
    images["counts"].data *= mask
24
25
    images["exposure_on"] = m_ref.copy(data=np.zeros_like(m_ref.data) + 1.0)
26
    images["exposure_on"].data *= mask
27
28
    exclusion = m_ref.copy(data=np.zeros_like(m_ref.data) + 1.0)
29
    exclusion.data[90:110, 90:110] = 0
30
    images["exclusion"] = exclusion
31
    return images
32
33
34
def test_ring_background_estimator(images):
0 ignored issues
show
Comprehensibility Bug introduced by
images 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...
35
    ring = RingBackgroundEstimator(0.35 * u.deg, 0.3 * u.deg)
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
36
37
    result = ring.run(images)
38
39
    in_fov = images["exposure_on"].data > 0
40
41
    assert_allclose(result["background"].data[in_fov], 2.0)
42
    assert_allclose(result["alpha"].data[in_fov].mean(), 0.003488538457592745)
43
    assert_allclose(result["exposure_off"].data[in_fov].mean(), 305.1268970794541)
44
    assert_allclose(result["off"].data[in_fov].mean(), 610.2537941589082)
45
46
    assert_allclose(result["off"].data[~in_fov], 0.0)
47
    assert_allclose(result["exposure_off"].data[~in_fov], 0.0)
48
    assert_allclose(result["alpha"].data[~in_fov], 0.0)
49
50
51
class TestAdaptiveRingBackgroundEstimator:
0 ignored issues
show
The variable __class__ seems to be unused.
Loading history...
52
    def setup(self):
53
        self.images = {}
0 ignored issues
show
The attribute images was defined outside __init__.

It is generally a good practice to initialize all attributes to default values in the __init__ method:

class Foo:
    def __init__(self, x=None):
        self.x = x
Loading history...
54
        self.images["counts"] = WcsNDMap.create(binsz=0.02, npix=101, dtype=float)
55
        self.images["counts"].data += 1.0
56
        self.images["exposure_on"] = WcsNDMap.create(binsz=0.02, npix=101, dtype=float)
57
        self.images["exposure_on"].data += 1e10
58
        exclusion = WcsNDMap.create(binsz=0.02, npix=101, dtype=float)
59
        exclusion.data += 1
60
        exclusion.data[40:60, 40:60] = 0
61
        self.images["exclusion"] = exclusion
62
63 View Code Duplication
    def test_run_const_width(self):
64
        ring = AdaptiveRingBackgroundEstimator(
65
            r_in=0.22 * u.deg, r_out_max=0.8 * u.deg, width=0.1 * u.deg
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
66
        )
67
        result = ring.run(self.images)
68
69
        assert_allclose(result["background"].data[50, 50], 1)
70
        assert_allclose(result["alpha"].data[50, 50], 0.002638522427440632)
71
        assert_allclose(result["exposure_off"].data[50, 50], 379 * 1e10)
72
        assert_allclose(result["off"].data[50, 50], 379)
73
74
        assert_allclose(result["background"].data[0, 0], 1)
75
        assert_allclose(result["alpha"].data[0, 0], 0.008928571428571418)
76
        assert_allclose(result["exposure_off"].data[0, 0], 112 * 1e10)
77
        assert_allclose(result["off"].data[0, 0], 112)
78
79 View Code Duplication
    def test_run_const_r_in(self):
80
        ring = AdaptiveRingBackgroundEstimator(
81
            r_in=0.22 * u.deg,
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
82
            r_out_max=0.8 * u.deg,
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
83
            width=0.1 * u.deg,
0 ignored issues
show
Module 'astropy.units' has no 'deg' member; maybe 'dex'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
84
            method="fixed_r_in",
85
        )
86
        result = ring.run(self.images)
87
88
        assert_allclose(result["background"].data[50, 50], 1)
89
        assert_allclose(result["alpha"].data[50, 50], 0.002638522427440632)
90
        assert_allclose(result["exposure_off"].data[50, 50], 379 * 1e10)
91
        assert_allclose(result["off"].data[50, 50], 379)
92
93
        assert_allclose(result["background"].data[0, 0], 1)
94
        assert_allclose(result["alpha"].data[0, 0], 0.008928571428571418)
95
        assert_allclose(result["exposure_off"].data[0, 0], 112 * 1e10)
96
        assert_allclose(result["off"].data[0, 0], 112)
97