From 035b2ac6d9fcc8072c35bfe94f871413ccb7735a Mon Sep 17 00:00:00 2001 From: Marion Spir-Jacob Date: Wed, 11 Jul 2018 12:23:13 +0200 Subject: [PATCH] Added test, docs and moved the file from spectrum to background --- gammapy/background/__init__.py | 1 + gammapy/{spectrum => background}/phase.py | 17 +++++-- gammapy/background/tests/test_phase.py | 56 +++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) rename gammapy/{spectrum => background}/phase.py (77%) create mode 100644 gammapy/background/tests/test_phase.py diff --git a/gammapy/background/__init__.py b/gammapy/background/__init__.py index 57a283fc36..6abbbd45c8 100644 --- a/gammapy/background/__init__.py +++ b/gammapy/background/__init__.py @@ -8,3 +8,4 @@ from .models import * from .off_data_background_maker import * from .reflected import * +from .phase import * diff --git a/gammapy/spectrum/phase.py b/gammapy/background/phase.py similarity index 77% rename from gammapy/spectrum/phase.py rename to gammapy/background/phase.py index c52ac02e7b..171f62d6f9 100644 --- a/gammapy/spectrum/phase.py +++ b/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 ---------- diff --git a/gammapy/background/tests/test_phase.py b/gammapy/background/tests/test_phase.py new file mode 100644 index 0000000000..32a7827707 --- /dev/null +++ b/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) +