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

Improve tests for HESS data #473

Merged
merged 8 commits into from Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 0 additions & 18 deletions gammapy/data/tests/test_data_store.py
Expand Up @@ -14,14 +14,7 @@ def test_datastore_hd_hap():
"""Test HESS HAP-HD data access."""
data_store = DataStore.from_dir('$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2')

# Check DataStore functionality
assert data_store.obs_table['OBS_ID'][0] == 23523

# Check DataStoreObservation functionality
obs = data_store.obs(obs_id=23523)
obs.info()
filename = str(obs.location(hdu_type='events').path(abs_path=False))
assert filename == 'run023400-023599/run023523/hess_events_023523.fits.gz'

assert str(type((obs.events))) == "<class 'gammapy.data.event_list.EventList'>"
assert str(type(obs.gti)) == "<class 'gammapy.data.gti.GTI'>"
Expand All @@ -31,25 +24,14 @@ def test_datastore_hd_hap():
# TODO: no background model available yet
# assert str(type(obs.bkg)) == ""

assert_quantity_allclose(obs.observation_time_duration, Quantity(1687, 's'))
assert_allclose(obs.observation_dead_time_fraction, 0.06239670515060425)


@requires_dependency('scipy')
@requires_data('gammapy-extra')
def test_datastore_pa():
"""Test HESS ParisAnalysis data access."""
data_store = DataStore.from_dir('$GAMMAPY_EXTRA/datasets/hess-crab4-pa')

# Check DataStore functionality
assert data_store.obs_table['OBS_ID'][0] == 23523

# Check DataStoreObservation functionality
# TODO: no simulated events available yet!
obs = data_store.obs(obs_id=23523)
# obs.info()
filename = str(obs.location(hdu_type='events').path(abs_path=False))
assert filename == 'run23400-23599/run23523/events_23523.fits.gz'
filename = str(obs.location(hdu_type='bkg').path(abs_path=False))
assert filename == 'background/bgmodel_alt7_az0.fits.gz'

Expand Down
10 changes: 9 additions & 1 deletion gammapy/irf/energy_dispersion.py
Expand Up @@ -810,11 +810,19 @@ def info(self):
"""Print some basic info.
"""
ss = "\nSummary EnergyDispersion2D info\n"
ss += "----------------\n"
ss += "--------------------------------\n"
# Summarise data members
ss += array_stats_str(self.energy, 'energy')
ss += array_stats_str(self.offset, 'offset')
ss += array_stats_str(self.migra, 'migra')
ss += array_stats_str(self.dispersion, 'dispersion')

energy = Energy('1 TeV')
e_reco = EnergyBounds([0.8, 1.2], 'TeV')
offset = Angle('0.5 deg')
p = self.get_response(offset, energy, e_reco)[0]

ss += 'Probability to reconstruct a {} photon in the range {} at {}' \
' offset: {:.2f}'.format(energy, e_reco, offset, p)

return ss
45 changes: 40 additions & 5 deletions gammapy/irf/tests/test_hess_chains.py
@@ -1,6 +1,7 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import, division, print_function, \
unicode_literals

from astropy.tests.helper import pytest

from ...datasets.core import GammapyExtraNotFoundError
Expand Down Expand Up @@ -33,9 +34,43 @@ def get_list_of_chains():
@requires_dependency('scipy')
@requires_dependency('yaml')
@requires_data('gammapy-extra')
def test_EffectiveArea(data_manager, chain):
ref_file = make_path(chain['aeff2D_reference_file'])
ref_aeff = open(str(ref_file), 'r').read()
def test_hess_chains(data_manager, chain):

ref_file_aeff = make_path(chain['aeff2D_reference_file'])
ref_aeff = open(str(ref_file_aeff), 'r').read()
ref_file_edisp = make_path(chain['edisp2D_reference_file'])
ref_edisp = open(str(ref_file_edisp), 'r').read()
ref_file_psf = make_path(chain['psf_reference_file'])
ref_psf = open(str(ref_file_psf), 'r').read()
ref_file_obs = make_path(chain['obs_reference_file'])
ref_obs = open(str(ref_file_obs), 'r').read()
ref_file_loc = make_path(chain['location_reference_file'])
ref_loc = open(str(ref_file_loc), 'r').read()

obs_nr = chain['obs']
obs_id = chain['obs_id']

store = data_manager[chain['store']]
aeff = store.obs(obs_id=chain['obs']).aeff
assert aeff.info() == ref_aeff
obs = store.obs(obs_id=obs_id)

assert str(obs.location(hdu_type='events').path(abs_path=False)) == ref_loc

# Todo : Find a better way to capture stdout
# see http://stackoverflow.com/questions/5136611/capture-stdout-from-a-script-in-python/10743550#10743550
import sys
from StringIO import StringIO
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work in Python 3:

$ python3 -c 'import StringIO'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named 'StringIO'

Also: this should really be done in a with statement, which guarantees that sys.stdout is restored when it exists.

Bottom line: you have to put this in gammapy.utils and add a test that captures something that's printed to show that it works on Python 2 and 3.
Let me know if you want me to add this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I had the feeling I wouldn't get away with that ... I will try to add a context manager


backup = sys.stdout
sys.stdout = StringIO()
obs.info()
obs_info = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = backup

assert store.obs_table['OBS_ID'][obs_nr] == obs_id
assert obs_info == ref_obs

assert obs.aeff.info() == ref_aeff
assert obs.edisp.info() == ref_edisp
assert obs.psf.info() == ref_psf