Skip to content

Commit

Permalink
Added test, docs and moved the file from spectrum to background
Browse files Browse the repository at this point in the history
  • Loading branch information
Marion Spir-Jacob authored and dcfidalgo committed Jul 11, 2018
1 parent c09922a commit 035b2ac
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions gammapy/background/__init__.py
Expand Up @@ -8,3 +8,4 @@
from .models import *
from .off_data_background_maker import *
from .reflected import *
from .phase import *
17 changes: 13 additions & 4 deletions gammapy/spectrum/phase.py → gammapy/background/phase.py
@@ -1,15 +1,24 @@
import numpy as np
from gammapy.background.background_estimate import BackgroundEstimate
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
from ..background.background_estimate import BackgroundEstimate

__all__ = [
'PhaseBackgroundEstimator',
]

class PhaseBackgroundEstimator(object):
"""Background estimation with on and off phases.
This class is responsible for creating a
`~gammapy.background.BackgroundEstimate` by counting events in the on-phase-zone and off-phase-zone in an ON-region,
`~gammapy.background.BackgroundEstimate` by counting events
in the on-phase-zone and off-phase-zone in an ON-region,
given an observation, an on_region, an on-phase-zone, an off-phase-zone.
For a usage example see future notebook
For a usage example see future notebook.
TODO : The phase interval is assumed to be between 0 and 1.
TODO : It supports only one ON-phase zone and one OFF-phase zone.
TODO : In case one phase zone is between 0.9 and 0.1 (for example), it won't understand it.
Parameters
----------
Expand Down
56 changes: 56 additions & 0 deletions gammapy/background/tests/test_phase.py
@@ -0,0 +1,56 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, unicode_literals
import pytest
from astropy.coordinates import SkyCoord, Angle
from regions import CircleSkyRegion
from ...data import DataStore, EventList
from .. import BackgroundEstimate, PhaseBackgroundEstimator

@pytest.fixture
def on_region():
"""Example on_region for testing."""
pos = SkyCoord('08h35m20.65525s', '-45d10m35.1545s', frame='icrs')
radius = Angle(0.2, 'deg')
region = CircleSkyRegion(pos, radius)
return region


@pytest.fixture
def obs_list():
"""Example observation list for testing."""
DATA_DIR = '$GAMMAPY_EXTRA/datasets/cta-1dc/index/gps'
datastore = DataStore.from_dir(DATA_DIR)
obs_ids = [111630]
return datastore.obs_list(obs_ids)


@pytest.fixture(scope='session')
def phase_bkg_estimator():
"""Example background estimator for testing."""
estimator = PhaseBackgroundEstimator(obs_list=obs_list(),
on_region=on_region(),
on_phase=(0.5, 0.6),
off_phase=(0.7, 1))
return estimator


def test_basic(phase_bkg_estimator):
assert 'PhaseBackgroundEstimator' in str(phase_bkg_estimator)


def test_run(phase_bkg_estimator):
phase_bkg_estimator.run()
assert len(phase_bkg_estimator.result) == 1


def test_filter_events(obs_list, on_region):
all_events = obs_list[0].events.select_circular_region(on_region)
ev1 = PhaseBackgroundEstimator.filter_events(all_events, (0, 0.3))
assert isinstance(ev1, EventList)
ev2 = PhaseBackgroundEstimator.filter_events(all_events, (0.3, 1))
assert len(all_events.table) == len(ev1.table) + len(ev2.table)


def test_process(phase_bkg_estimator, obs_list):
assert isinstance(phase_bkg_estimator.process(obs_list[0]), BackgroundEstimate)

0 comments on commit 035b2ac

Please sign in to comment.