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

Add integration test #96

Merged
merged 19 commits into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -40,7 +40,6 @@ install:

script:
- pytest --cov=pyirf --cov-report=xml
- python examples/calculate_eventdisplay_irfs.py
- travis-sphinx -v --outdir=docbuild build --source=docs/

after_script:
Expand Down
7 changes: 3 additions & 4 deletions pyirf/io/gadf.py
Expand Up @@ -17,6 +17,7 @@

DEFAULT_HEADER = Header()
DEFAULT_HEADER["CREATOR"] = f"pyirf v{__version__}"
# fmt: off
DEFAULT_HEADER["HDUDOC"] = "https://github.com/open-gamma-ray-astro/gamma-astro-data-formats"
DEFAULT_HEADER["HDUVERS"] = "0.2"
DEFAULT_HEADER["HDUCLASS"] = "GADF"
Expand Down Expand Up @@ -212,13 +213,11 @@ def create_energy_dispersion_hdu(
#:
#: see https://github.com/open-gamma-ray-astro/gamma-astro-data-formats/issues/153
#: for a discussion on why this is MeV not TeV as everywhere else
GADF_BACKGROUND_UNIT = u.Unit('MeV-1 s-1 sr-1')
GADF_BACKGROUND_UNIT = u.Unit("MeV-1 s-1 sr-1")


@u.quantity_input(
background=GADF_BACKGROUND_UNIT,
reco_energy_bins=u.TeV,
fov_offset_bins=u.deg,
background=GADF_BACKGROUND_UNIT, reco_energy_bins=u.TeV, fov_offset_bins=u.deg,
)
def create_background_2d_hdu(
background_2d,
Expand Down
4 changes: 0 additions & 4 deletions pyirf/io/tests/test_gadf.py
Expand Up @@ -87,7 +87,6 @@ def rad_max_hdu():

def test_effective_area2d_gammapy(aeff2d_hdus):
'''Test our effective area is readable by gammapy'''
pytest.importorskip('gammapy')
from gammapy.irf import EffectiveAreaTable2D

area, hdus = aeff2d_hdus
Expand All @@ -112,7 +111,6 @@ def test_effective_area2d_schema(aeff2d_hdus):

def test_energy_dispersion_gammapy(edisp_hdus):
'''Test our energy dispersion is readable by gammapy'''
pytest.importorskip('gammapy')
from gammapy.irf import EnergyDispersion2D

edisp, hdus = edisp_hdus
Expand All @@ -137,7 +135,6 @@ def test_energy_dispersion_schema(edisp_hdus):

def test_psf_table_gammapy(psf_hdu):
'''Test our psf is readable by gammapy'''
pytest.importorskip('gammapy')
from gammapy.irf import PSF3D

psf, hdu = psf_hdu
Expand Down Expand Up @@ -167,7 +164,6 @@ def test_psf_schema(psf_hdu):
@pytest.mark.xfail
def test_background_2d_gammapy(bg_hdu):
'''Test our background hdu is readable by gammapy'''
pytest.importorskip('gammapy')
from gammapy.irf import Background2D

background, hdu = bg_hdu
Expand Down
63 changes: 63 additions & 0 deletions pyirf/tests/test_eventdisplay_comparison.py
@@ -0,0 +1,63 @@
import numpy as np
import subprocess
import logging
import os
import uproot
import sys
from astropy.io import fits
from ogadf_schema.irfs import AEFF_2D, EDISP_2D, PSF_TABLE, BKG_2D, RAD_MAX
from pathlib import Path
import pytest
import astropy.units as u
from astropy.table import QTable


IRF_FILE = "DESY.d20180113.V3.ID0NIM2LST4MST4SST4SCMST4.prod3b-paranal20degs05b-NN.S.3HB9-FD.180000s.root"


@pytest.mark.integration
def test_eventdisplay_example(caplog):
"""Runs the EventDisplay example script and check its output."""

ROOT_DIR = Path(__file__).parent.parent.parent

# run script and check that it doesn't crash
script_path = os.path.join(ROOT_DIR, "examples/calculate_eventdisplay_irfs.py")
subprocess.check_output([sys.executable, script_path])

# check that the output file exists and it's not empty
outpath = ROOT_DIR / "pyirf_eventdisplay.fits.gz"
assert outpath.is_file() and outpath.stat().st_size > 0

# open FITS file
output_hdul = fits.open(outpath)

# known errors
caplog.set_level(logging.WARNING, logger="fits_schema")

# check that each HDU respects the OGADF schema
AEFF_2D.validate_hdu(output_hdul["EFFECTIVE_AREA"], onerror="log")
EDISP_2D.validate_hdu(output_hdul["ENERGY_DISPERSION"], onerror="log")
PSF_TABLE.validate_hdu(output_hdul["PSF"], onerror="log")
BKG_2D.validate_hdu(output_hdul["BACKGROUND"], onerror="log")
RAD_MAX.validate_hdu(output_hdul["RAD_MAX"], onerror="log")

errors_to_ignore = {
# error due to astropy bug, which should be fixed in 4.0.2.
# TODO: remove when we require astropy >= 4.0.2
"Dimensionality of rows is 0, should be 1",
}

assert all(rec.message in errors_to_ignore for rec in caplog.records)

# compare to reference

f = uproot.open(ROOT_DIR / "data" / IRF_FILE)
sensitivity_ed = f['DiffSens'] * u.Unit('erg s-1 cm-2')
table = QTable.read(outpath, hdu='SENSITIVITY')
sensitivity_pyirf = table['reco_energy_center']**2 * table['flux_sensitivity']

ratio = (sensitivity_pyirf[1:-1] / sensitivity_ed[1:-1]).to_value(u.one)

# TODO shrink margin when we get closer to prevent a regression
assert np.all((ratio < 2) & (ratio > 0.45))
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -18,10 +18,11 @@
"pytest-cov",
"gammapy~=0.17",
"ogadf-schema~=0.2.3",
"uproot~=3.0",
],
}

extras_require["all"] = extras_require["tests"] + extras_require["docs"]
extras_require["all"] = list(set(extras_require["tests"] + extras_require["docs"]))

setup(
version=__version__,
Expand Down