Skip to content

Commit

Permalink
use TableLoader for input handling
Browse files Browse the repository at this point in the history
  • Loading branch information
TjarkMiener committed Nov 14, 2024
1 parent 1e73fe2 commit 606fec5
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions src/ctapipe/tools/calculate_pixel_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,10 @@ class StatisticsCalculatorTool(Tool):
examples = """
To calculate statistics of pixel-wise image data files:
> ctapipe-calculate-pixel-statistics --input_url input.dl1.h5 --output_path /path/monitoring.h5 --overwrite
> ctapipe-calculate-pixel-statistics --TableLoader.input_url input.dl1.h5 --output_path /path/monitoring.h5 --overwrite
"""

input_url = Path(
help="Input CTA HDF5 files including pixel-wise image data",
allow_none=True,
exists=True,
directory_ok=False,
file_ok=True,
).tag(config=True)

allowed_tels = Set(
trait=CInt(),
default_value=None,
Expand Down Expand Up @@ -75,7 +67,7 @@ class StatisticsCalculatorTool(Tool):
overwrite = Bool(help="Overwrite output file if it exists").tag(config=True)

aliases = {
("i", "input_url"): "StatisticsCalculatorTool.input_url",
("i", "input_url"): "TableLoader.input_url",
("o", "output_path"): "StatisticsCalculatorTool.output_path",
}

Expand All @@ -86,34 +78,39 @@ class StatisticsCalculatorTool(Tool):
),
}

classes = classes_with_traits(PixelStatisticsCalculator)
classes = [
TableLoader,
] + classes_with_traits(PixelStatisticsCalculator)

def setup(self):
# Read the input data with the 'TableLoader'
self.input_data = TableLoader(
parent=self,
)
# Check that the input and output files are not the same
if self.input_url == self.output_path:
if self.input_data.input_url == self.output_path:
raise ToolConfigurationError(
"Input and output files are same. Fix your configuration / cli arguments."
)

# Load the subarray description from the input file
subarray = SubarrayDescription.from_hdf(self.input_url)
# Initialization of the statistics calculator
self.stats_calculator = PixelStatisticsCalculator(
parent=self, subarray=subarray
)
# Read the input data with the 'TableLoader'
self.input_data = TableLoader(input_url=self.input_url)
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
)
# Initialization of the statistics calculator
self.stats_calculator = PixelStatisticsCalculator(
parent=self, subarray=subarray
)

def start(self):
# Iterate over the telescope ids and calculate the statistics
for tel_id in self.tel_ids:
# Read the whole dl1 images for one particular telescope
dl1_table = self.input_data.read_telescope_events_by_id(
telescopes=tel_id,
telescopes=[
tel_id,
],
dl1_images=True,
dl1_parameters=False,
dl1_muons=False,
Expand Down

0 comments on commit 606fec5

Please sign in to comment.