From b3dbbc928de80cc80fef1eb8abf41f77973c4a14 Mon Sep 17 00:00:00 2001 From: Maximilian Linhoff Date: Fri, 27 Oct 2023 13:49:51 +0200 Subject: [PATCH] Change definition of leakage_pixel_{1,2} --- ctapipe/image/leakage.py | 14 +++-- ctapipe/image/tests/test_leakage.py | 90 ++++++++++++++++++++++++++++- docs/changes/2432.api.rst | 3 + 3 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 docs/changes/2432.api.rst diff --git a/ctapipe/image/leakage.py b/ctapipe/image/leakage.py index 7405b8c901a..2f0d1fde0a1 100644 --- a/ctapipe/image/leakage.py +++ b/ctapipe/image/leakage.py @@ -3,8 +3,8 @@ """ import numpy as np -from ..containers import LeakageContainer +from ..containers import LeakageContainer __all__ = ["leakage_parameters"] @@ -40,11 +40,13 @@ def leakage_parameters(geom, image, cleaning_mask): leakage_intensity1 = np.sum(image[mask1]) leakage_intensity2 = np.sum(image[mask2]) - size = np.sum(image[cleaning_mask]) + clean = image[cleaning_mask] + n_pixels_cleaning = len(clean) + n_pe_cleaning = np.sum(clean) return LeakageContainer( - pixels_width_1=leakage_pixel1 / geom.n_pixels, - pixels_width_2=leakage_pixel2 / geom.n_pixels, - intensity_width_1=leakage_intensity1 / size, - intensity_width_2=leakage_intensity2 / size, + pixels_width_1=leakage_pixel1 / n_pixels_cleaning, + pixels_width_2=leakage_pixel2 / n_pixels_cleaning, + intensity_width_1=leakage_intensity1 / n_pe_cleaning, + intensity_width_2=leakage_intensity2 / n_pe_cleaning, ) diff --git a/ctapipe/image/tests/test_leakage.py b/ctapipe/image/tests/test_leakage.py index 28c2913dc13..44502fa4e2b 100644 --- a/ctapipe/image/tests/test_leakage.py +++ b/ctapipe/image/tests/test_leakage.py @@ -1,7 +1,95 @@ import numpy as np +import pytest +from ctapipe.containers import LeakageContainer +from ctapipe.instrument import CameraGeometry -def test_leakage(prod5_lst): +# simple dummy test cases +geometry = CameraGeometry.make_rectangular(5, 5) +image_no_leakage = np.array( + [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 1, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + ] +).ravel() +container_no_leakage = LeakageContainer( + pixels_width_1=0.0, + pixels_width_2=0.0, + intensity_width_1=0.0, + intensity_width_2=0.0, +) + +image_leakage_2_1 = np.array( + [ + [0, 0, 0, 0, 0], + [0, 1, 1, 1, 0], + [0, 1, 8, 1, 0], + [0, 1, 1, 1, 0], + [0, 0, 0, 0, 0], + ] +).ravel() +container_leakage_2_1 = LeakageContainer( + pixels_width_1=0.0, + pixels_width_2=8 / 9, + intensity_width_1=0.0, + intensity_width_2=0.5, +) + +image_leakage_2_2 = np.array( + [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 9, 1, 0], + [0, 0, 0, 2, 0], + [0, 0, 0, 0, 0], + ] +).ravel() +container_leakage_2_2 = LeakageContainer( + pixels_width_1=0.0, + pixels_width_2=2 / 3, + intensity_width_1=0.0, + intensity_width_2=0.25, +) + +image_leakage_1 = np.array( + [ + [0, 0, 0, 0, 0], + [0, 4, 1, 0, 0], + [0, 1, 9, 2, 0], + [0, 0, 2, 3, 1], + [0, 0, 0, 1, 2], + ] +).ravel() +container_leakage_1 = LeakageContainer( + pixels_width_1=3 / 10, + pixels_width_2=9 / 10, + intensity_width_1=4 / 26, + intensity_width_2=17 / 26, +) + +images = (image_no_leakage, image_leakage_2_1, image_leakage_2_2, image_leakage_1) +containers = ( + container_no_leakage, + container_leakage_2_1, + container_leakage_2_2, + container_leakage_1, +) + + +@pytest.mark.parametrize("image,expected", zip(images, containers)) +def test_leakage_toy(image, expected): + from ctapipe.image.leakage import leakage_parameters + + leakage = leakage_parameters(geometry, image, image > 0) + + for key, val in expected.items(): + assert leakage[key] == val, f"{key} does not match" + + +def test_leakage_lst(prod5_lst): from ctapipe.image.leakage import leakage_parameters geom = prod5_lst.camera.geometry diff --git a/docs/changes/2432.api.rst b/docs/changes/2432.api.rst new file mode 100644 index 00000000000..d41bacacd6f --- /dev/null +++ b/docs/changes/2432.api.rst @@ -0,0 +1,3 @@ +Change the definition of the ``leakage_pixels_width_{1,2}`` image features +to give the ratio of pixels at the border to the pixels after cleaning +instead of to the total number of pixels of the camera.