PredictCTLearnModel#

class ctlearn.tools.predict_model.PredictCTLearnModel(*args: t.Any, **kwargs: t.Any)[source]#

Bases: Tool

Base (abstract) tool to predict the gammaness, energy and arrival direction from R1/DL1 data using CTLearn models.

This class handles the prediction of the gammaness, energy and arrival direction from pixel-wise image or waveform data. It also supports the extraction of the feature vectors from the backbone submodel to store them in the output file. The input data is loaded from the input url using the ~dl1_data_handler.reader.DLDataReader and ~ctlearn.core.keras.sequence.KerasSequence or ~ctlearn.core.pytorch.dataset.PyTorchDataset. The prediction is performed using the CTLearn models. The data is stored in the output file following the ctapipe DL2 data format. By default, waveforms and images are not copied to the DL2 output file unless explicitly enabled in the configuration. The start method is implemented in the subclasses to handle the prediction for mono and stereo mode.

Attributes#

input_urlpathlib.Path

Input ctapipe HDF5 files including pixel-wise image or waveform data.

dl1_featuresbool

Set whether to include the dl1 feature vectors in the output file.

dl2_telescopebool

Set whether to include dl2 telescope-event-wise data in the output file.

dl2_subarraybool

Set whether to include dl2 subarray-event-wise data in the output file.

dl1dh_readerdl1_data_handler.reader.DLDataReader

DLDataReader object to read the data.

dl1dh_reader_typestr

Type of the DLDataReader to use for the prediction.

stack_telescope_imagesbool

Set whether to stack the telescope images in the data loader. Requires stereo.

sort_by_intensitybool

Set whether to sort the telescope images by intensity in the data loader. Requires stereo.

prefixstr

Name of the reconstruction algorithm used to generate the dl2 data.

load_type_model_frompathlib.Path

Path to a Keras or PyTorch model file for the classification of the primary particle type.

load_energy_model_frompathlib.Path

Path to a Keras or PyTorch model file for the regression of the primary particle energy.

load_cameradirection_model_frompathlib.Path

Path to a Keras or PyTorch model file for the regression of the primary particle arrival direction based on camera coordinate offsets.

load_skydirection_model_frompathlib.Path

Path to a Keras or PyTorch model file for the regression of the primary particle arrival direction based on spherical coordinate offsets.

output_pathpathlib.Path

Output path to save the dl2 prediction results.

keras_verboseint

Verbosity mode of Keras during the prediction.

strategytf.distribute.Strategy

MirroredStrategy to distribute the prediction.

data_loaderctlearn.core.keras.sequence.KerasSequence

KerasSequence object to load the data.

indiceslist of int

List of indices for the data loaders.

batch_sizeint

Size of the batch to perform inference of the neural network.

last_batch_sizeint

Size of the last batch in the data loaders.

Methods#

setup()

Set up the tool.

finish()

Finish the tool.

_predict_with_model(model_path)

Load and predict with a CTLearn model.

_predict_particletype(example_identifiers)

Predict the classification of the primary particle type.

_predict_energy(example_identifiers)

Predict the energy of the primary particle.

_predict_cameradirection(example_identifiers)

Predict the arrival direction of the primary particle based on camera coordinate offsets.

_predict_skydirection(example_identifiers)

Predict the arrival direction of the primary particle based on spherical coordinate offsets.

_transform_cam_coord_offsets_to_sky(table)

Transform to camera coordinate offsets w.r.t. the telescope pointing to Alt/Az coordinates.

_transform_spher_coord_offsets_to_sky(table)

Transform to spherical coordinate offsets w.r.t. the telescope pointing to Alt/Az coordinates.

_create_nan_table(nonexample_identifiers, columns, shapes, reco_task)

Create a table with NaNs for missing predictions.

_store_pointing(all_identifiers)

Store the telescope pointing table from to the output file.

_create_feature_vectors_table(example_identifiers, nonexample_identifiers, particletype_feature_vectors, energy_feature_vectors, direction_feature_vectors)

Create the table for the DL1 feature vectors.

Attributes Summary

aliases

batch_size

Size of the batch to perform inference of the neural network.

classes

dl1_features

Set whether to include the dl1 feature vectors in the output file.

dl1dh_reader_type

dl2_subarray

Set whether to include dl2 subarray-event-wise data in the output file.

dl2_telescope

Set whether to include dl2 telescope-event-wise data in the output file.

flags

input_url

Input ctapipe HDF5 files including pixel-wise image or waveform data

load_cameradirection_model_from

Path to a Keras or PyTorch model file for the reconstruction of the primary particle arrival direction based on camera coordinate offsets.

load_energy_model_from

Path to a Keras or PyTorch model file for the regression of the primary particle energy.

load_skydirection_model_from

Path to a Keras or PyTorch model file for the reconstruction of the primary particle arrival direction based on spherical coordinate offsets.

load_type_model_from

Path to a Keras or PyTorch model file for the classification of the primary particle type.

output_path

Output path to save the dl2 prediction results

prefixes

Name of the reconstruction algorithm used to generate the dl2 data for each task.

sort_by_intensity

Set whether to sort the telescope images by intensity in the data loader.

stack_telescope_images

Set whether to stack the telescope images in the data loader.

Methods Summary

deduplicate_first_valid(table[, keys, valid_col])

Return a deduplicated Astropy Table.

finish()

Perform final cleanup and save metadata after the core logic completes.

setup()

Initialize the tool configuration, validate parameters, and setup data handlers.

Attributes Documentation

aliases = {('d', 'cameradirection_model'): 'PredictCTLearnModel.load_cameradirection_model_from', ('e', 'energy_model'): 'PredictCTLearnModel.load_energy_model_from', ('i', 'input_url'): 'PredictCTLearnModel.input_url', ('o', 'output'): 'PredictCTLearnModel.output_path', ('s', 'skydirection_model'): 'PredictCTLearnModel.load_skydirection_model_from', ('t', 'type_model'): 'PredictCTLearnModel.load_type_model_from'}#
batch_size#

Size of the batch to perform inference of the neural network.

classes = [<class 'dl1_data_handler.reader.DLDataReader'>, <class 'dl1_data_handler.reader.DLImageReader'>, <class 'dl1_data_handler.reader.DLWaveformReader'>, <class 'dl1_data_handler.reader.DLFeatureVectorReader'>]#
dl1_features#

Set whether to include the dl1 feature vectors in the output file.

dl1dh_reader_type#

. Possible values: []

dl2_subarray#

Set whether to include dl2 subarray-event-wise data in the output file.

dl2_telescope#

Set whether to include dl2 telescope-event-wise data in the output file.

flags = {'dl1-features': ({'PredictCTLearnModel': {'dl1_features': True}}, 'Include dl1 features'), 'dl1-images': ({'HDF5Merger': {'dl1_images': True}}, 'Include dl1 images'), 'dl1-parameters': ({'HDF5Merger': {'dl1_parameters': True}}, 'Include dl1 parameters'), 'dl2-subarray': ({'PredictCTLearnModel': {'dl2_subarray': True}}, 'Include dl2 subarray-event-wise data in the output file'), 'dl2-telescope': ({'PredictCTLearnModel': {'dl2_telescope': True}}, 'Include dl2 telescope-event-wise data in the output file'), 'no-dl1-features': ({'PredictCTLearnModel': {'dl1_features': False}}, 'Exclude dl1 features'), 'no-dl1-images': ({'HDF5Merger': {'dl1_images': False}}, 'Exclude dl1 images'), 'no-dl1-parameters': ({'HDF5Merger': {'dl1_parameters': False}}, 'Exclude dl1 parameters'), 'no-dl2-subarray': ({'PredictCTLearnModel': {'dl2_subarray': False}}, 'Exclude dl2 subarray-event-wise data in the output file'), 'no-dl2-telescope': ({'PredictCTLearnModel': {'dl2_telescope': False}}, 'Exclude dl2 telescope-event-wise data in the output file'), 'no-overwrite': ({'HDF5Merger': {'overwrite': False}}, 'Do not overwrite the output file if it exists'), 'no-r0-waveforms': ({'HDF5Merger': {'r0_waveforms': False}}, 'Exclude r0 waveforms'), 'no-r1-waveforms': ({'HDF5Merger': {'r1_waveforms': False}}, 'Exclude r1 waveforms'), 'no-true-images': ({'HDF5Merger': {'true_images': False}}, 'Exclude true images'), 'no-true-parameters': ({'HDF5Merger': {'true_parameters': False}}, 'Exclude true parameters'), 'overwrite': ({'HDF5Merger': {'overwrite': True}}, 'Overwrite the output file if it exists'), 'r0-waveforms': ({'HDF5Merger': {'r0_waveforms': True}}, 'Include r0 waveforms'), 'r1-waveforms': ({'HDF5Merger': {'r1_waveforms': True}}, 'Include r1 waveforms'), 'true-images': ({'HDF5Merger': {'true_images': True}}, 'Include true images'), 'true-parameters': ({'HDF5Merger': {'true_parameters': True}}, 'Include true parameters')}#
input_url#

Input ctapipe HDF5 files including pixel-wise image or waveform data

load_cameradirection_model_from#

Path to a Keras or PyTorch model file for the reconstruction of the primary particle arrival direction based on camera coordinate offsets. Requires Keras/PyTorch consistency with other model paths.

load_energy_model_from#

Path to a Keras or PyTorch model file for the regression of the primary particle energy. Requires Keras/PyTorch consistency with other model paths.

load_skydirection_model_from#

Path to a Keras or PyTorch model file for the reconstruction of the primary particle arrival direction based on spherical coordinate offsets. Requires Keras/PyTorch consistency with other model paths.

load_type_model_from#

Path to a Keras or PyTorch model file for the classification of the primary particle type. Requires Keras/PyTorch consistency with other model paths.

output_path#

Output path to save the dl2 prediction results

prefixes#

Name of the reconstruction algorithm used to generate the dl2 data for each task.

sort_by_intensity#

Set whether to sort the telescope images by intensity in the data loader. Requires DLDataReader mode to be stereo.

stack_telescope_images#

Set whether to stack the telescope images in the data loader. Requires DLDataReader mode to be stereo.

Methods Documentation

deduplicate_first_valid(table: Table, keys=('obs_id', 'event_id'), valid_col='CTLearn_is_valid')[source]#

Return a deduplicated Astropy Table.

For each group defined by keys, keep the first row where valid_col is True. If none are valid, keep the first row.

Parameters#

tableastropy.table.Table

The table to deduplicate.

keystuple or list of str, optional

Column names defining groups for deduplication. Default is (“obs_id”, “event_id”).

valid_colstr, optional

Column name indicating row validity. Default is “CTLearn_is_valid”.

Returns#

deduplicated_tableastropy.table.Table

Deduplicated table containing first valid entry per group.

finish()[source]#

Perform final cleanup and save metadata after the core logic completes.

This method writes provenance data (such as configuration and execution context) to the output file, finalizes tables, and ensures all file handlers are safely closed.

setup()[source]#

Initialize the tool configuration, validate parameters, and setup data handlers.

This method is the first part of the tool execution lifecycle. It validates input arguments, sets up the configuration dictionary, configures the output file, and prepares the DataReader to ensure everything is ready before the core processing begins.