From 4192ac79a941432d524063a3f089fff9b1cd4962 Mon Sep 17 00:00:00 2001 From: vmarandon Date: Wed, 31 Jul 2024 17:10:50 +0200 Subject: [PATCH] Split on event type (#123) * Add a Tool that can split data based on delimiter trigger * Improve documentation * A bit of cleaning * Refactored split_run, some coding style --------- Co-authored-by: Jean-Philippe Lenain --- src/nectarchain/makers/core.py | 48 +++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/nectarchain/makers/core.py b/src/nectarchain/makers/core.py index ad423411..5fdf8de8 100644 --- a/src/nectarchain/makers/core.py +++ b/src/nectarchain/makers/core.py @@ -5,7 +5,7 @@ from datetime import datetime import numpy as np -from ctapipe.containers import Container +from ctapipe.containers import Container, EventType from ctapipe.core import Component, Tool from ctapipe.core.container import FieldValidationError from ctapipe.core.traits import ( @@ -32,7 +32,10 @@ log = logging.getLogger(__name__) log.handlers = logging.getLogger("__main__").handlers -__all__ = ["EventsLoopNectarCAMCalibrationTool"] +__all__ = [ + "EventsLoopNectarCAMCalibrationTool", + "DelimiterLoopNectarCAMCalibrationTool", +] """The code snippet is a part of a class hierarchy for data processing. It includes the `BaseMaker` abstract class, the `EventsLoopMaker` and `ArrayDataMaker` @@ -258,12 +261,12 @@ def _init_writer(self, sliced: bool = False, slice_index: int = 0, group_name=No ) group_name = f"data_{slice_index}" self.log.info( - f"initilization of writer in sliced mode (output written " + f"initialization of writer in sliced mode (output written " f"to {group_name})" ) mode = "a" else: - self.log.info("initilization of writter in full mode") + self.log.info("initialization of writer in full mode") if self.overwrite: try: log.info( @@ -383,7 +386,9 @@ def start( # self._init_trigger_type(_trigger_type) if restart_from_begining: - self.log.debug("restart from begining : creation of the EventSource reader") + self.log.debug( + "restart from beginning : creation of the EventSource " "reader" + ) self._load_eventsource() n_events_in_slice = 0 @@ -407,10 +412,8 @@ def start( n_events_in_slice += 1 if self._n_traited_events >= n_events: break - if ( - not (self.events_per_slice is None) - and n_events_in_slice >= self.events_per_slice - ): + + if self.split_run(n_events_in_slice, event): self.log.info(f"slice number {slice_index} is full, pulling buffer") self._finish_components(*args, **kwargs) self.writer.close() @@ -419,6 +422,14 @@ def start( self._setup_components() n_events_in_slice = 0 + def split_run(self, n_events_in_slice, event): + """Method to decide if criteria to end a run slice are met""" + condition = ( + self.events_per_slice is not None + and n_events_in_slice >= self.events_per_slice + ) + return condition + def finish(self, return_output_component=False, *args, **kwargs): self.log.info("finishing Tool") @@ -431,7 +442,7 @@ def finish(self, return_output_component=False, *args, **kwargs): return output def _finish_components(self, *args, **kwargs): - self.log.info("finishing components and writting to output file") + self.log.info("finishing components and writing to output file") output = [] for component in self.components: output.append(component.finish(*args, **kwargs)) @@ -517,6 +528,23 @@ def pixels_id(self): return copy.deepcopy(self.__pixels_id) +class DelimiterLoopNectarCAMCalibrationTool(EventsLoopNectarCAMCalibrationTool): + """ + Class that will split data based on the EventType UNKNOWN. + Each time this particular type is seen, it will trigger the change of slice. + Note that the UNKONWN event will be seen by the component, so it must be filtered + there. + """ + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def split_run(self, n_events_in_slice, event): + """Method to decide if criteria to end a run slice is met""" + condition = event.trigger.event_type == EventType.UNKNOWN + return condition + + def main(): """run the tool""" tool = EventsLoopNectarCAMCalibrationTool()