Skip to content

Commit

Permalink
Better errors in case of no events in train tools
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnoe committed Mar 28, 2023
1 parent 38d173c commit 70c1094
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ctapipe/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CTAPipeException(Exception):
pass


class TooFewEvents(CTAPipeException):
"""Raised if something that needs a minimum number of event gets fewer"""
5 changes: 5 additions & 0 deletions ctapipe/tools/train_disp_reconstructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ctapipe.core import Tool
from ctapipe.core.traits import Bool, Int, IntTelescopeParameter, Path
from ctapipe.exceptions import TooFewEvents
from ctapipe.io import TableLoader
from ctapipe.reco import CrossValidator, DispReconstructor
from ctapipe.reco.preprocessing import check_valid_rows, horizontal_to_telescope
Expand Down Expand Up @@ -113,10 +114,14 @@ def start(self):
def _read_table(self, telescope_type):
table = self.loader.read_telescope_events([telescope_type])
self.log.info("Events read from input: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"Input file does not contain any events for telescope type {telescope_type}")

mask = self.models.quality_query.get_table_mask(table)
table = table[mask]
self.log.info("Events after applying quality query: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"No events after quality query for telescope type {telescope_type}")

table = self.models.feature_generator(table)

Expand Down
7 changes: 6 additions & 1 deletion ctapipe/tools/train_energy_regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ctapipe.core import Tool
from ctapipe.core.traits import Int, IntTelescopeParameter, Path
from ctapipe.exceptions import TooFewEvents
from ctapipe.io import TableLoader
from ctapipe.reco import CrossValidator, EnergyRegressor
from ctapipe.reco.preprocessing import check_valid_rows
Expand Down Expand Up @@ -113,11 +114,15 @@ def start(self):

def _read_table(self, telescope_type):
table = self.loader.read_telescope_events([telescope_type])

self.log.info("Events read from input: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"Input file does not contain any events for telescope type {telescope_type}")

mask = self.regressor.quality_query.get_table_mask(table)
table = table[mask]
self.log.info("Events after applying quality query: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"No events after quality query for telescope type {telescope_type}")

table = self.regressor.feature_generator(table)

Expand Down
7 changes: 6 additions & 1 deletion ctapipe/tools/train_particle_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ctapipe.core.tool import Tool
from ctapipe.core.traits import Int, IntTelescopeParameter, Path
from ctapipe.exceptions import TooFewEvents
from ctapipe.io import TableLoader
from ctapipe.reco import CrossValidator, ParticleClassifier
from ctapipe.reco.preprocessing import check_valid_rows
Expand Down Expand Up @@ -164,11 +165,15 @@ def start(self):

def _read_table(self, telescope_type, loader, n_events=None):
table = loader.read_telescope_events([telescope_type])

self.log.info("Events read from input: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"Input file does not contain any events for telescope type {telescope_type}")

mask = self.classifier.quality_query.get_table_mask(table)
table = table[mask]
self.log.info("Events after applying quality query: %d", len(table))
if len(table) == 0:
raise TooFewEvents(f"No events after quality query for telescope type {telescope_type}")

table = self.classifier.feature_generator(table)

Expand Down

0 comments on commit 70c1094

Please sign in to comment.