From 8dbe199002299be9886a8c6657f1d8a11fd37ef7 Mon Sep 17 00:00:00 2001 From: tjark miener Date: Wed, 12 Feb 2025 08:09:56 +0000 Subject: [PATCH 1/7] store subarray description in stats tool --- src/ctapipe/tools/calculate_pixel_stats.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/ctapipe/tools/calculate_pixel_stats.py b/src/ctapipe/tools/calculate_pixel_stats.py index 18aa07632eb..ab7e390a7b8 100644 --- a/src/ctapipe/tools/calculate_pixel_stats.py +++ b/src/ctapipe/tools/calculate_pixel_stats.py @@ -99,14 +99,14 @@ def setup(self): ) self.input_data.dl1_images = True # Load the subarray description from the input file - subarray = SubarrayDescription.from_hdf(self.input_data.input_url) + self.subarray = SubarrayDescription.from_hdf(self.input_data.input_url) # Get the telescope ids from the input data or use the allowed_tels configuration self.tel_ids = ( - subarray.tel_ids if self.allowed_tels is None else self.allowed_tels + self.subarray.tel_ids if self.allowed_tels is None else self.allowed_tels ) # Initialization of the statistics calculator self.stats_calculator = PixelStatisticsCalculator( - parent=self, subarray=subarray + parent=self, subarray=self.subarray ) def start(self): @@ -171,13 +171,21 @@ def start(self): f"/dl1/monitoring/telescope/{self.output_table_name}/tel_{tel_id:03d}", overwrite=self.overwrite, ) - - def finish(self): self.log.info( "DL1 monitoring data was stored in '%s' under '%s'", self.output_path, f"/dl1/monitoring/telescope/{self.output_table_name}", ) + + def finish(self): + # Store the subarray description in the output file + self.subarray.to_hdf(self.output_path, overwrite=self.overwrite) + self.log.info( + "Subarray description was stored in '%s'", + self.output_path, + ) + # Close the file in the TableLoader + self.input_data.close() self.log.info("Tool is shutting down") From 2a9eb88477f71eb7a7d9b867b3675291705c7afd Mon Sep 17 00:00:00 2001 From: tjark miener Date: Wed, 12 Feb 2025 08:58:12 +0000 Subject: [PATCH 2/7] add changelog --- docs/changes/2696.feature.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 docs/changes/2696.feature.rst diff --git a/docs/changes/2696.feature.rst b/docs/changes/2696.feature.rst new file mode 100644 index 00000000000..1707ab2c7a5 --- /dev/null +++ b/docs/changes/2696.feature.rst @@ -0,0 +1 @@ +Store also the SubarrayDescription in the camera monitoring data produced by the stats tool From bbf802d7f1283ca7c34ca4c5ebcc5c62dd1ae411 Mon Sep 17 00:00:00 2001 From: tjark miener Date: Wed, 12 Feb 2025 09:08:26 +0000 Subject: [PATCH 3/7] fix whitespace --- docs/changes/2696.feature.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes/2696.feature.rst b/docs/changes/2696.feature.rst index 1707ab2c7a5..e4856a4d7ef 100644 --- a/docs/changes/2696.feature.rst +++ b/docs/changes/2696.feature.rst @@ -1 +1 @@ -Store also the SubarrayDescription in the camera monitoring data produced by the stats tool +Store also the SubarrayDescription in the camera monitoring data produced by the stats tool From cc634ef0104a081760608a4cb1b48943cc7d4181 Mon Sep 17 00:00:00 2001 From: tjark miener Date: Wed, 12 Feb 2025 09:53:06 +0000 Subject: [PATCH 4/7] use .select_subarray() for allowed_tels config --- src/ctapipe/tools/calculate_pixel_stats.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/ctapipe/tools/calculate_pixel_stats.py b/src/ctapipe/tools/calculate_pixel_stats.py index ab7e390a7b8..4470394be84 100644 --- a/src/ctapipe/tools/calculate_pixel_stats.py +++ b/src/ctapipe/tools/calculate_pixel_stats.py @@ -99,10 +99,12 @@ def setup(self): ) self.input_data.dl1_images = True # Load the subarray description from the input file - self.subarray = SubarrayDescription.from_hdf(self.input_data.input_url) - # Get the telescope ids from the input data or use the allowed_tels configuration - self.tel_ids = ( - self.subarray.tel_ids if self.allowed_tels is None else self.allowed_tels + subarray = SubarrayDescription.from_hdf(self.input_data.input_url) + # Select a new subarray if the allowed_tels configuration is used + self.subarray = ( + subarray + if self.allowed_tels is None + else subarray.select_subarray(self.allowed_tels, "Subarray") ) # Initialization of the statistics calculator self.stats_calculator = PixelStatisticsCalculator( @@ -111,7 +113,7 @@ def setup(self): def start(self): # Iterate over the telescope ids and calculate the statistics - for tel_id in self.tel_ids: + for tel_id in self.subarray.tel_ids: # Read the whole dl1 images for one particular telescope dl1_table = self.input_data.read_telescope_events_by_id( telescopes=[ From 878b405d016ca2108c92cdc7eda2963f5ee5e7d0 Mon Sep 17 00:00:00 2001 From: tjark miener Date: Fri, 14 Feb 2025 09:24:19 +0000 Subject: [PATCH 5/7] add TableLoader in contexts use default naming for selected subarray --- src/ctapipe/tools/calculate_pixel_stats.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ctapipe/tools/calculate_pixel_stats.py b/src/ctapipe/tools/calculate_pixel_stats.py index 4470394be84..3e672d06332 100644 --- a/src/ctapipe/tools/calculate_pixel_stats.py +++ b/src/ctapipe/tools/calculate_pixel_stats.py @@ -84,8 +84,10 @@ class PixelStatisticsCalculatorTool(Tool): def setup(self): # Read the input data with the 'TableLoader' - self.input_data = TableLoader( - parent=self, + self.input_data = self.enter_context( + TableLoader( + parent=self, + ) ) # Check that the input and output files are not the same if self.input_data.input_url == self.output_path: @@ -104,7 +106,7 @@ def setup(self): self.subarray = ( subarray if self.allowed_tels is None - else subarray.select_subarray(self.allowed_tels, "Subarray") + else subarray.select_subarray(self.allowed_tels) ) # Initialization of the statistics calculator self.stats_calculator = PixelStatisticsCalculator( @@ -186,8 +188,6 @@ def finish(self): "Subarray description was stored in '%s'", self.output_path, ) - # Close the file in the TableLoader - self.input_data.close() self.log.info("Tool is shutting down") From b1113b10a04b5d172ef24a0ff39d658911e98efe Mon Sep 17 00:00:00 2001 From: tjark miener Date: Fri, 14 Feb 2025 14:04:58 +0000 Subject: [PATCH 6/7] add check for correctness of Subarray writing for cam monitoring file --- src/ctapipe/tools/tests/test_calculate_pixel_stats.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py index 1e0330fa40f..f70f87c14ef 100644 --- a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py +++ b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py @@ -8,6 +8,7 @@ from ctapipe.core import run_tool from ctapipe.core.tool import ToolConfigurationError +from ctapipe.instrument import SubarrayDescription from ctapipe.io import read_table from ctapipe.tools.calculate_pixel_stats import PixelStatisticsCalculatorTool @@ -57,6 +58,10 @@ def test_calculate_pixel_stats_tool(tmp_path, dl1_image_file): )["mean"] is not None ) + # Read subarray description frm the created monitoring file + subarray = SubarrayDescription.from_hdf(monitoring_file) + # Check for the selected telescope + assert subarray.tel_ids[0] == tel_id def test_tool_config_error(tmp_path, dl1_image_file): From be649f5ce91d098a4df471cc82ecdcae69a155a4 Mon Sep 17 00:00:00 2001 From: tjark miener Date: Fri, 14 Feb 2025 14:07:48 +0000 Subject: [PATCH 7/7] fix typo in comment --- src/ctapipe/tools/tests/test_calculate_pixel_stats.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py index f70f87c14ef..9e2e9674664 100644 --- a/src/ctapipe/tools/tests/test_calculate_pixel_stats.py +++ b/src/ctapipe/tools/tests/test_calculate_pixel_stats.py @@ -58,7 +58,7 @@ def test_calculate_pixel_stats_tool(tmp_path, dl1_image_file): )["mean"] is not None ) - # Read subarray description frm the created monitoring file + # Read subarray description from the created monitoring file subarray = SubarrayDescription.from_hdf(monitoring_file) # Check for the selected telescope assert subarray.tel_ids[0] == tel_id