Skip to content

Commit

Permalink
Add test for irf tool
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBeiske committed Oct 11, 2024
1 parent e192d8c commit 2936eb4
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 21 deletions.
15 changes: 10 additions & 5 deletions src/ctapipe/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,18 +695,23 @@ def dl1_mon_pointing_file(dl1_file, dl1_tmp_path):
return path


@pytest.fixture(scope="session")
def irf_tmp_path(tmp_path_factory):
return tmp_path_factory.mktemp("irf")


@pytest.fixture(scope="session")
def gamma_diffuse_full_reco_file(
gamma_train_clf,
particle_classifier_path,
model_tmp_path,
irf_tmp_path,
):
"""
Energy reconstruction and geometric origin reconstruction have already been done.
"""
from ctapipe.tools.apply_models import ApplyModels

output_path = model_tmp_path / "gamma_diffuse_full_reco.dl2.h5"
output_path = irf_tmp_path / "gamma_diffuse_full_reco.dl2.h5"
run_tool(
ApplyModels(),
argv=[
Expand All @@ -725,14 +730,14 @@ def gamma_diffuse_full_reco_file(
def proton_full_reco_file(
proton_train_clf,
particle_classifier_path,
model_tmp_path,
irf_tmp_path,
):
"""
Energy reconstruction and geometric origin reconstruction have already been done.
"""
from ctapipe.tools.apply_models import ApplyModels

output_path = model_tmp_path / "proton_full_reco.dl2.h5"
output_path = irf_tmp_path / "proton_full_reco.dl2.h5"
run_tool(
ApplyModels(),
argv=[
Expand All @@ -748,7 +753,7 @@ def proton_full_reco_file(


@pytest.fixture(scope="session")
def irf_events_loader_test_config():
def irf_event_loader_test_config():
from traitlets.config import Config

return Config(
Expand Down
6 changes: 3 additions & 3 deletions src/ctapipe/irf/tests/test_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ def test_make_2d_ang_res(irf_events_table):

@pytest.mark.skipif(sys.version_info.minor > 11, reason="Pyirf+numpy 2.0 errors out")
def test_make_2d_sensitivity(
gamma_diffuse_full_reco_file, proton_full_reco_file, irf_events_loader_test_config
gamma_diffuse_full_reco_file, proton_full_reco_file, irf_event_loader_test_config
):
from ctapipe.irf import EventLoader, Sensitivity2dMaker, Spectra

gamma_loader = EventLoader(
config=irf_events_loader_test_config,
config=irf_event_loader_test_config,
kind="gammas",
file=gamma_diffuse_full_reco_file,
target_spectrum=Spectra.CRAB_HEGRA,
Expand All @@ -87,7 +87,7 @@ def test_make_2d_sensitivity(
obs_time=u.Quantity(50, u.h),
)
proton_loader = EventLoader(
config=irf_events_loader_test_config,
config=irf_event_loader_test_config,
kind="protons",
file=proton_full_reco_file,
target_spectrum=Spectra.IRFDOC_PROTON_SPECTRUM,
Expand Down
10 changes: 5 additions & 5 deletions src/ctapipe/irf/tests/test_optimize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from ctapipe.irf.optimize import CutOptimizerBase


def test_optimization_result_store(tmp_path, irf_events_loader_test_config):
def test_optimization_result_store(tmp_path, irf_event_loader_test_config):
from ctapipe.irf import (
EventPreProcessor,
OptimizationResult,
Expand All @@ -19,7 +19,7 @@ def test_optimization_result_store(tmp_path, irf_events_loader_test_config):
)

result_path = tmp_path / "result.h5"
epp = EventPreProcessor(irf_events_loader_test_config)
epp = EventPreProcessor(irf_event_loader_test_config)
store = OptimizationResultStore(epp)

with pytest.raises(
Expand Down Expand Up @@ -94,12 +94,12 @@ def test_cut_optimizer(
Optimizer,
gamma_diffuse_full_reco_file,
proton_full_reco_file,
irf_events_loader_test_config,
irf_event_loader_test_config,
):
from ctapipe.irf import OptimizationResultStore

gamma_loader = EventLoader(
config=irf_events_loader_test_config,
config=irf_event_loader_test_config,
kind="gammas",
file=gamma_diffuse_full_reco_file,
target_spectrum=Spectra.CRAB_HEGRA,
Expand All @@ -109,7 +109,7 @@ def test_cut_optimizer(
obs_time=u.Quantity(50, u.h),
)
proton_loader = EventLoader(
config=irf_events_loader_test_config,
config=irf_event_loader_test_config,
kind="protons",
file=proton_full_reco_file,
target_spectrum=Spectra.IRFDOC_PROTON_SPECTRUM,
Expand Down
4 changes: 2 additions & 2 deletions src/ctapipe/irf/tests/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ def test_normalise_column_names(dummy_table):
_ = epp.normalise_column_names(dummy_table)


def test_events_loader(gamma_diffuse_full_reco_file, irf_events_loader_test_config):
def test_event_loader(gamma_diffuse_full_reco_file, irf_event_loader_test_config):
from ctapipe.irf import EventLoader, Spectra

loader = EventLoader(
config=irf_events_loader_test_config,
config=irf_event_loader_test_config,
kind="gammas",
file=gamma_diffuse_full_reco_file,
target_spectrum=Spectra.CRAB_HEGRA,
Expand Down
109 changes: 109 additions & 0 deletions src/ctapipe/tools/tests/test_make_irf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import json
import os

import pytest
from astropy.io import fits

from ctapipe.core import run_tool


@pytest.fixture(scope="module")
def event_loader_config_path(irf_event_loader_test_config, irf_tmp_path):
config_path = irf_tmp_path / "event_loader_config.json"
with config_path.open("w") as f:
json.dump(irf_event_loader_test_config, f)

return config_path


@pytest.fixture(scope="module")
def dummy_cuts_file(
gamma_diffuse_full_reco_file,
proton_full_reco_file,
event_loader_config_path,
irf_tmp_path,
):
from ctapipe.tools.optimize_event_selection import IrfEventSelector

# Do "point-like" cuts to have both g/h and theta cuts in the file
output_path = irf_tmp_path / "dummy_cuts.fits"
run_tool(
IrfEventSelector(),
argv=[
f"--gamma-file={gamma_diffuse_full_reco_file}",
f"--proton-file={proton_full_reco_file}",
# Use diffuse gammas weighted to electron spectrum as stand-in
f"--electron-file={gamma_diffuse_full_reco_file}",
f"--output={output_path}",
f"--config={event_loader_config_path}",
],
)
return output_path


@pytest.mark.parametrize("include_bkg", (False, True))
@pytest.mark.parametrize("point_like", (True, False))
def test_irf_tool(
gamma_diffuse_full_reco_file,
proton_full_reco_file,
event_loader_config_path,
dummy_cuts_file,
tmp_path,
include_bkg,
point_like,
):
from ctapipe.tools.make_irf import IrfTool

output_path = tmp_path / "irf.fits.gz"
output_benchmarks_path = tmp_path / "benchmarks.fits.gz"

argv = [
f"--gamma-file={gamma_diffuse_full_reco_file}",
f"--cuts={dummy_cuts_file}",
f"--output={output_path}",
f"--config={event_loader_config_path}",
]
if not point_like:
argv.append("--full-enclosure")

if include_bkg:
argv.append(f"--proton-file={proton_full_reco_file}")
# Use diffuse gammas weighted to electron spectrum as stand-in
argv.append(f"--electron-file={gamma_diffuse_full_reco_file}")
else:
argv.append("--no-do-background")

ret = run_tool(IrfTool(), argv=argv)
assert ret == 0

assert output_path.exists()
assert not output_benchmarks_path.exists()
# Readability by gammapy is tested by pyirf tests, so not repeated here
with fits.open(output_path) as hdul:
assert isinstance(hdul["ENERGY DISPERSION"], fits.BinTableHDU)
assert isinstance(hdul["EFFECTIVE AREA"], fits.BinTableHDU)
if point_like:
assert isinstance(hdul["RAD_MAX"], fits.BinTableHDU)
else:
assert isinstance(hdul["PSF"], fits.BinTableHDU)

if include_bkg:
assert isinstance(hdul["BACKGROUND"], fits.BinTableHDU)

os.remove(output_path) # Delete output file

# Include benchmarks
argv.append(f"--benchmark-output={output_benchmarks_path}")
ret = run_tool(IrfTool(), argv=argv)
assert ret == 0

assert output_path.exists()
assert output_benchmarks_path.exists()
with fits.open(output_benchmarks_path) as hdul:
assert isinstance(hdul["ENERGY BIAS RESOLUTION"], fits.BinTableHDU)
assert isinstance(hdul["ANGULAR RESOLUTION"], fits.BinTableHDU)
if include_bkg:
assert isinstance(hdul["SENSITIVITY"], fits.BinTableHDU)


# TODO: Add test using point-like gammas
9 changes: 3 additions & 6 deletions src/ctapipe/tools/tests/test_optimize_event_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
def test_cuts_optimization(
gamma_diffuse_full_reco_file,
proton_full_reco_file,
irf_events_loader_test_config,
irf_event_loader_test_config,
tmp_path,
point_like,
):
Expand All @@ -25,7 +25,7 @@ def test_cuts_optimization(
output_path = tmp_path / "cuts.fits"
config_path = tmp_path / "config.json"
with config_path.open("w") as f:
json.dump(irf_events_loader_test_config, f)
json.dump(irf_event_loader_test_config, f)

argv = [
f"--gamma-file={gamma_diffuse_full_reco_file}",
Expand All @@ -38,10 +38,7 @@ def test_cuts_optimization(
if not point_like:
argv.append("--full-enclosure")

ret = run_tool(
IrfEventSelector(),
argv=argv,
)
ret = run_tool(IrfEventSelector(), argv=argv)
assert ret == 0

result = OptimizationResultStore().read(output_path)
Expand Down

0 comments on commit 2936eb4

Please sign in to comment.