Skip to content

Commit

Permalink
Add test for using incompatible cuts for irf computation
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasBeiske committed Nov 21, 2024
1 parent 1406781 commit e13bbd5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/ctapipe/tools/compute_irf.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,17 @@ def start(self):
"""
reduced_events = dict()
for sel in self.particles:
if sel.epp.gammaness_classifier != self.opt_result.clf_prefix:
raise RuntimeError(
"G/H cuts are only valid for gammaness scores predicted by "
"the same classifier model. Requested model: %s. "
"Model used for g/h cuts: %s."
% (
sel.epp.gammaness_classifier,
self.opt_result.clf_prefix,
)
)

if sel.epp.quality_criteria != self.opt_result.precuts.quality_criteria:
self.log.warning(
"Precuts are different from precuts used for calculating "
Expand All @@ -485,17 +496,6 @@ def start(self):
quality_criteria=self.opt_result.precuts.quality_criteria,
)

if sel.epp.gammaness_classifier != self.opt_result.clf_prefix:
raise RuntimeError(
"G/H cuts are only valid for gammaness scores predicted by "
"the same classifier model. Requested model: %s. "
"Model used for g/h cuts: %s."
% (
sel.epp.gammaness_classifier,
self.opt_result.clf_prefix,
)
)

self.log.debug("%s Precuts: %s" % (sel.kind, sel.epp.quality_criteria))
evs, cnt, meta = sel.load_preselected_events(self.chunk_size, self.obs_time)
# Only calculate event weights if background or sensitivity should be calculated.
Expand Down
71 changes: 71 additions & 0 deletions src/ctapipe/tools/tests/test_compute_irf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import os

Expand Down Expand Up @@ -212,6 +213,7 @@ def test_point_like_irf_no_theta_cut(
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"--cuts={gh_cuts_path}",
f"--output={output_path}",
Expand All @@ -221,3 +223,72 @@ def test_point_like_irf_no_theta_cut(
],
raises=True,
)


def test_irf_tool_wrong_cuts(
gamma_diffuse_full_reco_file, proton_full_reco_file, dummy_cuts_file, tmp_path
):
from ctapipe.tools.compute_irf import IrfTool

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

with pytest.raises(RuntimeError):
run_tool(
IrfTool(),
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"--cuts={dummy_cuts_file}",
f"--output={output_path}",
f"--benchmark-output={output_benchmarks_path}",
],
raises=True,
)

config_path = tmp_path / "config.json"
with config_path.open("w") as f:
json.dump(
{
"EventPreProcessor": {
"energy_reconstructor": "ExtraTreesRegressor",
"geometry_reconstructor": "HillasReconstructor",
"gammaness_classifier": "ExtraTreesClassifier",
"quality_criteria": [
# No criteria for minimum event multiplicity
("valid classifier", "ExtraTreesClassifier_is_valid"),
("valid geom reco", "HillasReconstructor_is_valid"),
("valid energy reco", "ExtraTreesRegressor_is_valid"),
],
}
},
f,
)

logpath = tmp_path / "test_irf_tool_wrong_cuts.log"
logger = logging.getLogger("ctapipe.tools.compute_irf")
logger.addHandler(logging.FileHandler(logpath))

ret = run_tool(
IrfTool(),
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"--cuts={dummy_cuts_file}",
f"--output={output_path}",
f"--benchmark-output={output_benchmarks_path}",
f"--config={config_path}",
f"--log-file={logpath}",
],
)
assert ret == 0
assert output_path.exists()
assert output_benchmarks_path.exists()
assert (
"Precuts are different from precuts used for calculating g/h / theta cuts."
in logpath.read_text()
)

0 comments on commit e13bbd5

Please sign in to comment.