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

gammapy/background/background_estimate.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
from regions import CircleSkyRegion
0 ignored issues
show
Unable to import 'regions'
Loading history...
4
from .ring import ring_area_factor
5
6
__all__ = ["BackgroundEstimate", "ring_background_estimate"]
7
8
9
class BackgroundEstimate(object):
10
    """Container class for background estimate.
11
12
    This container holds the result from a region based background estimation
13
    for one observation. Currently, it is filled by the functions
14
    :func:`~gammapy.background.ring_background_estimate` and
15
    the `~gammapy.background.ReflectedRegionsBackgroundEstimator`.
16
17
    Parameters
18
    ----------
19
    on_region : `~regions.SkyRegion`
20
        Signal extraction region
21
    on_events : `~gammapy.data.EventList`
22
        Signal events
23
    off_region : `~regions.SkyRegion`
24
        Background extraction region
25
    off_events : `~gammapy.data.EventList`
26
        Background events
27
    a_on : float
28
        Relative background exposure of the on region
29
    a_off : float
30
        Relative background exposure of the off region
31
    method : str
32
        Background estimation method
33
    """
34
35
    def __init__(
0 ignored issues
show
Too many arguments (8/5)
Loading history...
36
        self,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
37
        on_region,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
38
        on_events,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
39
        off_region,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
40
        off_events,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
41
        a_on,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
42
        a_off,
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
43
        method="default",
0 ignored issues
show
Wrong hanging indentation before block (add 4 spaces).
Loading history...
44
    ):
45
        self.on_region = on_region
46
        self.on_events = on_events
47
        self.off_region = off_region
48
        self.off_events = off_events
49
        self.a_on = a_on
50
        self.a_off = a_off
51
        self.method = method
52
53
    def __str__(self):
54
        ss = self.__class__.__name__
55
        ss += "\n Method: {}".format(self.method)
56
        ss += "\n on region"
57
        ss += "\n {}".format(self.on_region)
58
        ss += "\n {}".format(self.on_events)
59
        ss += "\n off region"
60
        ss += "\n {}".format(self.off_region)
61
        ss += "\n {}".format(self.off_events)
62
        return ss
63
64
65
def ring_background_estimate(pos, on_radius, inner_radius, outer_radius, events):
66
    """Simple ring background estimate.
67
68
    No acceptance correction is applied
69
70
    TODO : Replace with AnnulusSkyRegion
71
72
    Parameters
73
    ----------
74
    pos : `~astropy.coordinates.SkyCoord`
75
        On region radius
76
    on_radius : `~astropy.coordinates.Angle`
77
        On region radius
78
    inner_radius, outer_radius : `~astropy.coordinates.Angle`
79
        Inner and outer ring radius
80
    events : `~gammapy.data.EventList`
81
        Event list
82
83
    Returns
84
    -------
85
    bkg : `~gammapy.data.BackgroundEstimate`
86
        Background estimate
87
    """
88
    on_region = CircleSkyRegion(center=pos, radius=on_radius)
89
    on_events = events.select_circular_region(on_region)
90
91
    off_region = dict(inner=inner_radius, outer=outer_radius)
92
    off_events = events.select_sky_ring(pos, inner_radius, outer_radius)
93
94
    # TODO: change to region areas here (e.g. in steratian?)
0 ignored issues
show
TODO and FIXME comments should generally be avoided.
Loading history...
95
    a_on = 1
96
    a_off = ring_area_factor(on_radius, inner_radius, outer_radius).value
97
98
    return BackgroundEstimate(
99
        on_region=on_region,
100
        on_events=on_events,
101
        off_region=off_region,
102
        off_events=off_events,
103
        a_on=a_on,
104
        a_off=a_off,
105
        method="ring",
106
    )
107