Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change EventList from Table subclass to attribute #754

Merged
merged 8 commits into from Nov 18, 2016
Merged
13 changes: 0 additions & 13 deletions examples/test_event_list_check.py

This file was deleted.

4 changes: 2 additions & 2 deletions gammapy/background/fov_cube.py
Expand Up @@ -866,8 +866,8 @@ def _fill_one_event_list(self, events):
Event list objects.
"""
energy = events.energy.to('TeV').value
detx = np.array(events['DETX'])
dety = np.array(events['DETY'])
detx = np.array(events.table['DETX'])
dety = np.array(events.table['DETY'])
sample = np.vstack([energy, detx, dety]).T

bins = [self.energy_edges.value, self.coordy_edges.value, self.coordx_edges.value]
Expand Down
9 changes: 5 additions & 4 deletions gammapy/background/models.py
Expand Up @@ -8,6 +8,7 @@
from astropy.modeling.models import Gaussian1D
from astropy.table import Table
from astropy.units import Quantity
from ..data import EventList
from ..utils.energy import EnergyBounds
from .energy_offset_array import EnergyOffsetArray
from .fov_cube import _make_bin_edges_array, FOVCube
Expand Down Expand Up @@ -72,7 +73,7 @@ def _compute_pie_fraction(sources, pointing_position, fov_radius):


def _select_events_outside_pie(sources, events, pointing_position, fov_radius):
"""The index table of the events outside the pie.
"""Table row indices of events outside the pie.

Parameters
----------
Expand All @@ -89,14 +90,14 @@ def _select_events_outside_pie(sources, events, pointing_position, fov_radius):
Returns
-------
idx : `~numpy.array`
coord of the events that are outside the pie
Table row indices of the events that are outside the pie
"""
sources = _add_column_and_sort_table(sources, pointing_position)
radius = Angle(sources["Radius"])[0]
phi = Angle(sources["phi"])[0]
separation = Angle(sources["separation"])[0]
if separation > fov_radius:
return np.arange(len(events))
return np.arange(len(events.table))
else:
phi_min = phi - np.arctan(radius / separation)
phi_max = phi + np.arctan(radius / separation)
Expand Down Expand Up @@ -668,7 +669,7 @@ def fill_obs(self, obs_ids, data_store, excluded_sources=None, fov_radius=Angle(
if excluded_sources:
pie_fraction = _compute_pie_fraction(excluded_sources, events.pointing_radec, fov_radius)
idx = _select_events_outside_pie(excluded_sources, events, events.pointing_radec, fov_radius)
events = events[idx]
events = EventList(events.table[idx])
else:
pie_fraction = 0

Expand Down
12 changes: 4 additions & 8 deletions gammapy/background/tests/test_energy_offset_array.py
Expand Up @@ -21,15 +21,13 @@ def make_test_array(dummy_data=False):
# Define an EventList with three events
table = Table()
table['RA'] = [0.6, 0, 2]
table['DEC'] = [0, 1.5, 0]
table['ENERGY'] = [0.12, 22, 55]
table['RA'].unit = 'deg'
table['DEC'] = [0, 1.5, 0] * u.deg
table['ENERGY'] = [0.12, 22, 55] * u.TeV
table.meta['RA_PNT'] = 0
table.meta['DEC_PNT'] = 0
table.meta['EUNIT'] = 'TeV'
events = EventList(table)
ev_list = [events]
# Fill the array with these three events
array.fill_events(ev_list)
array.fill_events([events])
return array, events.offset, events.energy
else:
return array
Expand All @@ -52,10 +50,8 @@ def test_energy_offset_array_fill():
dir = '$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2'
data_store = DataStore.from_dir(dir)
ev_list = data_store.load_all('events')

array = make_test_array()
array.fill_events(ev_list)

# TODO: add some assert, e.g. counts in some bin with non-zero entries.


Expand Down
1 change: 1 addition & 0 deletions gammapy/background/tests/test_fov_cube.py
Expand Up @@ -110,6 +110,7 @@ def event_lists():
dir = '$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2'
data_store = DataStore.from_dir(dir)
event_lists = data_store.load_all('events')

return event_lists


Expand Down
12 changes: 7 additions & 5 deletions gammapy/background/tests/test_models.py
Expand Up @@ -198,13 +198,14 @@ def test_select_events_outside_pie():
pointing_position = SkyCoord(0.5, 0.5, unit='deg')

# Create fake EventList with the radec of all the pixel in the empty image
events = EventList()
events["RA"] = [0.25, 0.02, 359.3, 1.04, 1.23, 359.56, 359.48]
events["DEC"] = [0.72, 0.96, 1.71, 1.05, 0.19, 2.01, 0.24]
table = Table()
table['RA'] = [0.25, 0.02, 359.3, 1.04, 1.23, 359.56, 359.48] * u.deg
table['DEC'] = [0.72, 0.96, 1.71, 1.05, 0.19, 2.01, 0.24] * u.deg
events = EventList(table=table)

# Test that if the sources are out of the fov, it gives the index for all the events since no event will be removed
idx = _select_events_outside_pie(excluded_sources, events, pointing_position, Angle(0.3, "deg"))
assert_allclose(np.arange(len(events)), idx)
assert_allclose(np.arange(len(events.table)), idx)

# Test if after calling the select_events_outside_pie, the image is 0 inside the pie and 1 outside the pie
idx = _select_events_outside_pie(excluded_sources, events, pointing_position, Angle(5, "deg"))
Expand Down Expand Up @@ -253,5 +254,6 @@ def test_fillobs_pie(self):
offmax = multi_array1.counts.offset.max()

# This is important since in the counts array the events > offsetmax will not be in the histogram.
nevents_sup_offmax = len(np.where(events[idx].offset > offmax)[0])
events2 = events.select_row_subset(idx)
nevents_sup_offmax = len(np.where(events2.offset > offmax)[0])
assert_allclose(np.sum(multi_array1.counts.data), len(idx) - nevents_sup_offmax)
10 changes: 6 additions & 4 deletions gammapy/cube/tests/test_core.py
Expand Up @@ -213,10 +213,12 @@ def test_bin_events_in_cube():
filename = ('$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2/run023400-023599'
'/run023523/hess_events_023523.fits.gz')
events = EventList.read(filename)
counts = SkyCube.empty(emin=0.5, emax=80, enumbins=8, eunit='TeV',
nxpix=200, nypix=200, xref=events.meta['RA_OBJ'],
yref=events.meta['DEC_OBJ'], dtype='int',
coordsys='CEL')
meta = events.table.meta
counts = SkyCube.empty(
emin=0.5, emax=80, enumbins=8, eunit='TeV',
dtype='int', nxpix=200, nypix=200,
xref=meta['RA_OBJ'], yref=meta['DEC_OBJ'], coordsys='CEL',
)

counts.fill_events(events)

Expand Down
11 changes: 11 additions & 0 deletions gammapy/data/data_store.py
Expand Up @@ -43,6 +43,17 @@ class DataStore(object):
Observation index table
name : str
Data store name

Examples
--------

Here's an example how to create a `DataStore` to access H.E.S.S. data:

>>> from gammapy.data import DataStore
>>> dir = '$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2'
>>> data_store = DataStore.from_dir(dir)
>>> data_store.info()

"""
DEFAULT_HDU_TABLE = 'hdu-index.fits.gz'
"""Default HDU table filename."""
Expand Down