ctlearn_manager.tri_model


CTLearnTriModelManager class for handling three CTLearn models: direction, energy, and type.

class ctlearn_manager.tri_model.CTLearnTriModelManager(direction_model, energy_model, type_model, project_directories, cluster_configuration=<ctlearn_manager.utils.utils.ClusterConfiguration object>)[source]

Bases: object

A manager class for handling three CTLearn models: direction, energy, and type.

direction_model(CTLearnModelManager)
Type:

The direction model manager.

energy_model(CTLearnModelManager)
Type:

The energy model manager.

type_model(CTLearnModelManager)
Type:

The type model manager.

__init__(direction_model, energy_model, type_model):

Initializes the CTLearnTriModelManager with the given models.

launch_testing():

Placeholder method for launching testing.

produce_irfs():

Placeholder method for producing IRFs.

plot_irfs():

Uses gammapy to plot the IRFs. (Not yet implemented)

plot_loss():

Plots the training and validation loss for each model using matplotlib.

Initialize the CTLearnTriModelManager.

Parameters:
  • direction_model (CTLearnModelManager) – The model manager for direction reconstruction. Must be a direction model.

  • energy_model (CTLearnModelManager) – The model manager for energy reconstruction. Must be an energy model.

  • type_model (CTLearnModelManager) – The model manager for type reconstruction. Must be a type model.

  • cluster_configuration (ClusterConfiguration, optional) – The cluster configuration for the model manager. Defaults to a new instance of ClusterConfiguration.

  • project_directories (CTLMDirectories)

Raises:
  • ValueError – If direction_model is not a direction model.

  • ValueError – If energy_model is not an energy model.

  • ValueError – If type_model is not a type model.

  • ValueError – If the channels of all models are not the same.

  • ValueError – If the stereo values of all models are not the same.

  • ValueError – If the telescope IDs of all models are not the same.

  • ValueError – If the minimum number of telescopes of all models are not the same.

direction_model

The direction model manager.

Type:

CTLearnModelManager

energy_model

The energy model manager.

Type:

CTLearnModelManager

type_model

The type model manager.

Type:

CTLearnModelManager

channels

The channels used by the models.

Type:

list

stereo

Indicates whether the models use stereo reconstruction.

Type:

bool

min_telescopes

The minimum number of telescopes required by the models.

Type:

int

telescope_ids

The IDs of the telescopes used by the models.

Type:

list

telescope_names

The names of the telescopes used by the models.

Type:

list

cluster_configuration

The cluster configuration for the model manager.

Type:

ClusterConfiguration

reconstruction_method

The reconstruction method used, set to “CTLearn”.

Type:

str

reco_field_suffix

The suffix for the reconstruction field, determined by the stereo value.

Type:

str

set_keys()[source]

Set the keys for various data fields used in the model.

This method initializes attributes for accessing specific data fields such as reconstructed and true values for energy, altitude, azimuth, intensity, and time. The keys are determined based on the configuration of the model (e.g., whether stereo mode is enabled).

gammaness_key

Key for the predicted gammaness value.

Type:

str

reco_energy_key

Key for the reconstructed energy value.

Type:

str

intensity_key

Key for the Hillas intensity value.

Type:

str

reco_alt_key

Key for the reconstructed altitude value.

Type:

str

reco_az_key

Key for the reconstructed azimuth value.

Type:

str

true_alt_key

Key for the true altitude value.

Type:

str

true_az_key

Key for the true azimuth value.

Type:

str

true_energy_key

Key for the true energy value.

Type:

str

pointing_alt_key

Key for the pointing altitude value, determined by stereo mode.

Type:

str

pointing_az_key

Key for the pointing azimuth value, determined by stereo mode.

Type:

str

time_key

Key for the time value.

Type:

str

set_testing_data(testing_samples)[source]

Set the testing data for the models.

Parameters:

testing_samples (list[DataSample]) – A list of DataSample objects to be used as testing data.

Notes

This method updates the testing data for each of the models (direction_model, energy_model, and type_model) using the provided testing samples. After updating, it retrieves the available testing directions.

delete_table_from_index(path)[source]

Delete a table from the HDF5 file at the specified path.

Parameters:

path (str) – The path of the table to delete within the HDF5 file.

Raises:

KeyError – If the specified path does not exist in the HDF5 file.

Notes

This method modifies the HDF5 file in place by removing the specified table. Ensure that the path exists in the file before calling this method to avoid errors.

get_available_testing_directions()[source]

Retrieve and display available testing directions for each particle type.

This method reads testing data from an HDF5 file and extracts zenith and azimuth angles for each particle type. It identifies unique combinations of zenith and azimuth angles and prints them along with the particle types available for each combination.

Parameters:

None

Return type:

None

Notes

  • The method uses the astropy.io.misc.hdf5.read_table_hdf5 function to read data from the HDF5 file.

  • If an exception occurs while reading data for a particle type, it assumes no data is available for that type.

  • The zenith and azimuth angles are sorted by zenith angle before being displayed.

  • The output is printed to the console.

get_available_MC_directions(verbose=True)[source]

Retrieve and display available Monte Carlo (MC) directions.

This method reads MC direction data from an HDF5 file for each particle type defined in the ParticleType enumeration. It extracts zenith and azimuth angles, combines them into unique coordinate pairs, and optionally prints the available directions along with the corresponding particle types.

Parameters:

verbose (bool, optional) – If True, print the available MC directions and their associated particle types. Default is True.

Returns:

A sorted list of unique (zenith, azimuth) coordinate pairs available in the MC data.

Return type:

list of tuple

Notes

  • The method handles missing or unavailable data gracefully by skipping particle types that do not have corresponding MC data.

  • The printed output, if verbose is True, includes zenith and azimuth angles in degrees and lists the particle types available for each coordinate pair.

launch_testing(zenith, azimuth, launch_particle_types=[ParticleType.GAMMA_POINT], batch_size=64, dl2_subarray=True, force_dl1_lookup=False, overwrite=False, config=None)[source]

Launch the testing process for CTLearn models.

Parameters:
  • zenith (float) – Zenith angle for the testing data.

  • azimuth (float) – Azimuth angle for the testing data.

  • output_dirs (list of str) – List of output directories corresponding to each particle type.

  • config_dir (str or None, optional) – Directory for configuration files. Defaults to None.

  • launch_particle_types (list of ParticleType, optional) – List of particle types to launch testing for. Defaults to [ParticleType.GAMMA_POINT].

  • batch_size (int, optional) – Batch size for prediction. Defaults to 64.

  • dl2_subarray (bool, optional) – Whether to include DL2 subarray predictions. Defaults to True.

  • force_dl1_lookup (bool, optional) – Whether to force DL1 lookup during prediction. Defaults to False.

  • overwrite (bool, optional) – Whether to overwrite existing output files. Defaults to False.

  • config (dict or None, optional) – Additional configuration parameters. Defaults to None.

Raises:
  • ValueError – If the number of output directories does not match the number of particle types.

  • ValueError – If the cluster configuration has more than one node.

  • ValueError – If the testing directories for the models do not match.

  • ValueError – If no matching directory is found for the given zenith and azimuth.

  • ValueError – If the testing directories are empty.

Notes

This function handles the testing process for CTLearn models, including directory validation, file matching, and command execution for both stereo and mono models. It supports cluster-based execution using SLURM or local execution.

predict_lstchain_data(input_file, output_file, run=None, subrun=None, config_dir=None, overwrite=False, pointing_table='/dl1/event/telescope/parameters/LST_LSTCam', batch_size=64)[source]

Predict DL2 data from DL1 input using CTLearn models for LST-1 data.

Parameters:
  • input_file (str) – Path to the input DL1 file.

  • output_file (str) – Path to the output DL2 file.

  • run (int, optional) – Run number to override observation ID. Default is None.

  • subrun (int, optional) – Subrun number to override observation ID. Default is None.

  • config_dir (str, optional) – Directory to save the configuration file. Default is None.

  • overwrite (bool, optional) – Whether to overwrite the output file if it already exists. Default is False.

  • pointing_table (str, optional) – Path to the pointing table in the input file. Default is “/dl1/event/telescope/parameters/LST_LSTCam”.

  • batch_size (int, optional) – Batch size for prediction. Default is 64.

Raises:

ValueError – If the cluster configuration specifies more than one node, as the CTLearn prediction tool can only run on a single GPU.

Notes

  • This method generates a configuration file for the CTLearn prediction tool and executes the prediction command.

  • If a cluster configuration is enabled, the prediction command is submitted as a SLURM job using an SBATCH script.

  • The method assumes the presence of pre-trained CTLearn models for event type, energy, and direction predictions.

predict_data(input_file, output_file, config_dir=None, overwrite=False, pointing_table='dl0/monitoring/subarray/pointing')[source]

Predict data using CTLearn models and save the results.

Parameters:
  • input_file (str) – Path to the input file containing the data to be processed.

  • output_file (str) – Path to the output file where the prediction results will be saved.

  • config_dir (str, optional) – Directory where the configuration file will be saved. Defaults to None.

  • overwrite (bool, optional) – Whether to overwrite existing output files. Defaults to False.

  • pointing_table (str, optional) – Path to the pointing table within the input file. Defaults to “dl0/monitoring/subarray/pointing”.

Raises:

ValueError – If the cluster configuration specifies more than one node, as CTLearn prediction can only run on a single GPU.

Notes

This method generates a configuration file for CTLearn, determines the appropriate model directories, and constructs a command to run the CTLearn prediction tool. If a cluster is configured, the command is submitted as a job using SLURM; otherwise, it is executed locally.

merge_DL2_files(zenith, azimuth, particle_type, overwrite=False)[source]

Merge DL2 files for a specific zenith, azimuth, and particle type.

This method retrieves DL2 Monte Carlo (MC) files for the specified zenith, azimuth, and particle type. If multiple files are found, they are merged into a single output file using the ctapipe-merge command. The merged file is then registered with the direction, energy, and type models. If only one file exists, no merging is performed.

Parameters:
  • zenith (str) – The zenith angle of the observation.

  • azimuth (str) – The azimuth angle of the observation.

  • output_file (str) – The path to the output file where merged data will be saved.

  • particle_type (ParticleType) – The type of particle (e.g., gamma, proton) for which DL2 files are being merged.

  • overwrite (bool, optional) – Whether to overwrite the output file if it already exists (default is False).

Raises:

RuntimeError – If the merging process fails.

Notes

  • The ctapipe-merge command is used for merging files.

  • Original files are not deleted after merging.

  • If only one file exists for the given parameters, merging is skipped.

plot_DL2_classification(zenith, azimuth, particle_types=[ParticleType.GAMMA_POINT, ParticleType.PROTON])[source]

Plot the DL2 classification results for given zenith and azimuth angles.

Parameters:
  • zenith (float) – The zenith angle of the simulated events.

  • azimuth (float) – The azimuth angle of the simulated events.

  • particle_types (list of ParticleType, optional) – A list of particle types to include in the plot. Defaults to [ParticleType.GAMMA_POINT, ParticleType.PROTON].

Notes

This method retrieves DL2 Monte Carlo (MC) files for the specified zenith and azimuth angles and particle types. It then loads the DL2 data, extracts the gammaness values, and plots their distribution as histograms. The histograms are normalized to represent densities. The gammaness key is used to assess the classification performance of the model, where higher values typically indicate a higher likelihood of the event being a gamma-ray. The plot is displayed using matplotlib.pyplot.show().

See also

direction_model.get_DL2_MC_files

Retrieves the DL2 MC files for the specified parameters.

load_DL2_data_MC

Loads DL2 data from a given file.

plot_DL2_energy(zenith, azimuth, particle_types=[ParticleType.GAMMA_POINT, ParticleType.PROTON])[source]

Plot the distribution of reconstructed DL2 energy for given zenith, azimuth, and particle types.

Parameters:
  • zenith (float) – Zenith angle of the simulated events in degrees.

  • azimuth (float) – Azimuth angle of the simulated events in degrees.

  • particle_types (list of ParticleType, optional) – List of particle types to include in the plot. Defaults to [ParticleType.GAMMA_POINT, ParticleType.PROTON].

Notes

This method retrieves the DL2 Monte Carlo (MC) files for the specified zenith, azimuth, and particle types, loads the data, and plots the reconstructed energy distribution as a histogram. The energy is displayed on a logarithmic scale for both axes. The method uses the self.reco_energy_key attribute to access the reconstructed energy values in the data. If self.stereo is True, all telescopes are used; otherwise, only the telescope specified by self.telescope_ids[0] is used.

Raises:

KeyError – If the particle type is not found in the DL2 MC files.

Parameters:
  • zenith (float)

  • azimuth (float)

  • particle_types (list[ParticleType])

plot_DL2_AltAz(zenith, azimuth, particle_types=[ParticleType.GAMMA_POINT], cuts=<ctlearn_manager.utils.utils.Cuts object>)[source]

Plot the reconstructed Altitude-Azimuth distribution for DL2 data.

Parameters:
  • zenith (float) – Zenith angle of the simulated observation in degrees.

  • azimuth (float) – Azimuth angle of the simulated observation in degrees.

  • particle_types (list of ParticleType, optional) – List of particle types to include in the plot. Defaults to [ParticleType.GAMMA_POINT].

  • cuts (Cuts, optional) – Cuts to apply to the data. Must be of type CutType.GLOBAL. Defaults to DefaultCuts.NO_CUTS.value.

Raises:

ValueError – If the provided cuts are not of type CutType.GLOBAL.

Notes

This method visualizes the reconstructed altitude and azimuth of events for the specified particle types. It uses a 2D histogram to represent the density of reconstructed events and overlays the array pointing direction as a scatter point. The method assumes that the direction_model attribute provides access to the DL2 Monte Carlo files and that the load_DL2_data_MC function is available for loading the data. The plot is displayed using Matplotlib and includes a color bar to indicate the event counts in the 2D histogram.

plot_migration_matrix(zenith, azimuth, particle_types=[ParticleType.GAMMA_POINT], cuts=<ctlearn_manager.utils.utils.Cuts object>)[source]

Plot the migration matrix for reconstructed vs true energy.

Parameters:
  • zenith (float) – Zenith angle of the observation in degrees.

  • azimuth (float) – Azimuth angle of the observation in degrees.

  • particle_types (list[ParticleType], optional) – List of particle types to include in the plot. Default is [ParticleType.GAMMA_POINT].

  • cuts (Cuts, optional) – Cuts to apply to the data. Must be of type CutType.GLOBAL. Default is DefaultCuts.NO_CUTS.value.

Raises:

ValueError – If the provided cuts are not of type CutType.GLOBAL.

Notes

This method generates a 2D histogram (migration matrix) comparing reconstructed energy to true energy for the specified particle types. The plot is displayed using logarithmic scales for both axes. The method also overlays a diagonal line representing perfect reconstruction and applies the specified cuts to the data. The method uses Matplotlib for plotting and Astropy for data manipulation.

produce_irfs(zenith, azimuth, config, pointlike=True, electrons=False, protons=True, overwrite=False)[source]

Produce Instrument Response Functions (IRFs) for given observational parameters.

This method generates IRFs, cuts files, and benchmark files for a specified zenith and azimuth angle using the provided configuration. It supports processing gamma, electron, and proton particle types.

Parameters:
  • zenith (float) – Zenith angle of the observation in degrees.

  • azimuth (float) – Azimuth angle of the observation in degrees.

  • config (str) – Path to the configuration file for IRF generation.

  • pointlike (bool, optional) – If True, use point-like gamma files. If False, use diffuse gamma files. Default is True.

  • electrons (bool, optional) – If True, include electron files in the IRF generation. Default is False.

  • protons (bool, optional) – If True, include proton files in the IRF generation. Default is True.

  • overwrite (bool, optional) – If True, overwrite existing output files. Default is False.

Raises:
  • ValueError – If multiple files are found for a particle type or if required parameters are missing.

  • RuntimeError – If the system commands for generating cuts or IRFs fail.

Notes

  • Ensure that the configuration file and input files are not moved or deleted as they are extensively used in the code for plotting and analysis.

  • Use CTLearnTriModelManager.merge_DL2_files() to merge multiple files before calling this method if necessary.

produce_irfs_with_uncertainties(zenith, azimuth, config, pointlike=True, electrons=False, unblinded=True, resume_file_index=None, resume_cut_index=None, overwrite=False)[source]

Produce Instrument Response Functions (IRFs) for given observational parameters.

This method generates IRFs, cuts files, and benchmark files for a specified zenith and azimuth angle using the provided configuration. It supports processing gamma, electron, and proton particle types.

Parameters:
  • zenith (float) – Zenith angle of the observation in degrees.

  • azimuth (float) – Azimuth angle of the observation in degrees.

  • config (str) – Path to the configuration file for IRF generation.

  • pointlike (bool, optional) – If True, use point-like gamma files. If False, use diffuse gamma files. Default is True.

  • electrons (bool, optional) – If True, include electron files in the IRF generation. Default is False.

  • overwrite (bool, optional) – If True, overwrite existing output files. Default is False.

Raises:
  • ValueError – If multiple files are found for a particle type or if required parameters are missing.

  • RuntimeError – If the system commands for generating cuts or IRFs fail.

Notes

  • Ensure that the configuration file and input files are not moved or deleted as they are extensively used in the code for plotting and analysis.

  • Use CTLearnTriModelManager.merge_DL2_files() to merge multiple files before calling this method if necessary.

plot_sensitivity_benchmark(zenith, azimuth, cuts=[<ctlearn_manager.utils.utils.Cuts object>], title=None, ax=None, label=None)[source]
Parameters:
  • zenith (float)

  • azimuth (float)

  • cuts (list[Cuts])

  • title (str)

plot_angular_resolution_benchmark(zenith, azimuth, cuts=[<ctlearn_manager.utils.utils.Cuts object>], containments=[68, 95], title=None, ax=None, label=None)[source]
Parameters:
  • zenith (float)

  • azimuth (float)

  • cuts (list[Cuts])

  • containments (list[int])

  • title (str)

plot_energy_resolution_benchmark(zenith, azimuth, cuts=[<ctlearn_manager.utils.utils.Cuts object>], title=None, ax=None, label=None)[source]
Parameters:
  • zenith (float)

  • azimuth (float)

  • cuts (list[Cuts])

  • title (str)

plot_energy_bias_benchmark(zenith, azimuth, cuts=[<ctlearn_manager.utils.utils.Cuts object>], title=None, ax=None, label=None)[source]
Parameters:
  • zenith (float)

  • azimuth (float)

  • cuts (list[Cuts])

  • title (str)

plot_cuts(zeniths=None, azimuths=None, cuts=[<ctlearn_manager.utils.utils.Cuts object>], axs=None, label=None, export_to_h5=None, import_from_h5=None, import_label=None)[source]

Plot the cuts for given zenith and azimuth directions.

Parameters:
  • zeniths (list of float, optional) – List of zenith angles in degrees. If None, use all available Monte Carlo directions.

  • azimuths (list of float, optional) – List of azimuth angles in degrees. Must match the length of zeniths.

  • cuts (list of Cuts, optional) – List of cut configurations to plot. Defaults to a single cut with 70% efficiency.

  • axs (matplotlib.axes.Axes, optional) – Axes to plot on. If None, create new subplots.

  • label (str, optional) – Label for the plot legend. If None, use the cut’s label if multiple cuts are provided.

  • export_to_h5 (str)

  • import_from_h5 (str)

  • import_label (str)

Notes

This method retrieves the IRF data for the specified directions and plots the gammaness and theta cuts as a function of energy. If multiple cuts are provided, they are plotted on the same axes with a legend.

plot_irfs(zenith, azimuth, cuts=<ctlearn_manager.utils.utils.Cuts object>)[source]

Plot Instrument Response Functions (IRFs) for a given zenith and azimuth angle.

Parameters:
  • zenith (float) – Zenith angle in degrees for which the IRFs are to be plotted.

  • azimuth (float) – Azimuth angle in degrees for which the IRFs are to be plotted.

  • cuts (Cuts, optional) – Selection cuts to apply when retrieving the IRF data. Defaults to DefaultCuts.EFF_70.value.

Notes

This method reads the IRF data from the direction model using the specified zenith and azimuth angles and plots the effective area, background, and energy dispersion using the peek method of the respective IRF classes from gammapy.irf.

plot_loss()[source]

Plot the training and validation loss for multiple models.

This method visualizes the training and validation loss curves for three models: direction, energy, and type models. It reads the training logs from CSV files, aggregates the loss values, and plots them against the epochs.

Parameters:

None

Returns:

Displays the loss plots for the models.

Return type:

None

Notes

  • The method searches for training log files in directories specified by the model’s parameters.

  • If no training logs are found in the first search pattern, an alternative pattern is used.

  • The method uses Matplotlib for plotting and Pandas for reading the CSV files.

plot_angular_resolution_DL2(zeniths=None, azimuths=None, cuts=[<ctlearn_manager.utils.utils.Cuts object>], ylim=None, particle_type=ParticleType.GAMMA_POINT, figsize=None, ax=None, label=None, export_to_h5=None, import_from_h5=None, import_label=None)[source]

Plot the angular resolution as a function of true energy for DL2 data.

Parameters:
  • zeniths (list[float], optional) – List of zenith angles in degrees. If None, use available Monte Carlo directions.

  • azimuths (list[float], optional) – List of azimuth angles in degrees. Must have the same length as zeniths.

  • cuts (list[Cuts], optional) – List of cuts to apply. Defaults to [DefaultCuts.NO_CUTS.value].

  • ylim (tuple, optional) – Tuple specifying the y-axis limits as (min, max). If None, use default limits.

  • particle_type (ParticleType, optional) – Type of particle to analyze. Defaults to ParticleType.GAMMA_POINT.

  • figsize (tuple, optional) – Figure size as (width, height). If None, use default size.

  • ax (matplotlib.axes.Axes, optional) – Matplotlib Axes object to plot on. If None, create a new figure and axes.

  • label (str, optional) – Label for the plot legend. If None, generate a default label.

  • export_to_h5 (str)

  • import_from_h5 (str)

  • import_label (str)

Raises:

AssertionError – If zeniths and azimuths are provided but have different lengths. If both zeniths/azimuths and cuts have lengths greater than 1.

Notes

  • The function calculates the angular resolution for the given zenith and azimuth angles or cuts and plots it as a function of true energy.

  • If multiple zenith/azimuth pairs are provided, the closest pair to the training data is highlighted in the plot.

  • The function supports plotting on an existing Axes object or creating a new plot.

get_DL2_tables(zenith, azimuth, cuts=<ctlearn_manager.utils.utils.Cuts object>, particle_type=ParticleType.GAMMA_POINT, apply_theta_cut=True)[source]

Retrieve DL2 tables for a given zenith, azimuth, and particle type, applying specified cuts.

Parameters:
  • zenith (float) – Zenith angle of the simulated events in degrees.

  • azimuth (float) – Azimuth angle of the simulated events in degrees.

  • cuts (Cuts, optional) – Cuts to apply to the data. Defaults to DefaultCuts.NO_CUTS.value.

  • particle_type (ParticleType, optional) – Type of particle to filter (e.g., gamma, proton). Defaults to ParticleType.GAMMA_POINT.

  • apply_theta_cut (bool, optional) – Whether to apply a theta cut during energy-dependent cuts. Defaults to True.

Returns:

A tuple containing: - true_energy (astropy.units.Quantity): True energies of the events. - reco_energy (astropy.units.Quantity): Reconstructed energies of the events. - true_alt (astropy.units.Quantity): True altitudes of the events. - reco_alt (astropy.units.Quantity): Reconstructed altitudes of the events. - true_az (astropy.units.Quantity): True azimuths of the events. - reco_az (astropy.units.Quantity): Reconstructed azimuths of the events. - log_bins (astropy.units.Quantity): Logarithmic energy bins.

Return type:

tuple

Raises:

ValueError – If an unknown cut type is provided.

get_angular_resolution_DL2(zenith=None, azimuth=None, cuts=<ctlearn_manager.utils.utils.Cuts object>, particle_type=ParticleType.GAMMA_POINT)[source]

Compute the angular resolution as a function of energy for DL2 data.

Parameters:
  • zenith (float, optional) – Zenith angle of the observation in degrees. If None, use all available data.

  • azimuth (float, optional) – Azimuth angle of the observation in degrees. If None, use all available data.

  • cuts (Cuts, optional) – Selection cuts to apply to the data. Defaults to DefaultCuts.NO_CUTS.value.

  • particle_type (ParticleType, optional) – Type of particle to consider. Defaults to ParticleType.GAMMA_POINT.

Returns:

A tuple containing: - e : numpy.ndarray

Energy bins used for the angular resolution calculation.

  • ang_resnumpy.ndarray

    Angular resolution values corresponding to the energy bins.

Return type:

tuple

Notes

This method uses the ctaplot library to calculate the angular resolution based on true and reconstructed event parameters. The get_DL2_tables method is used to retrieve the necessary data.

plot_energy_resolution_DL2(zeniths=None, azimuths=None, cuts=[<ctlearn_manager.utils.utils.Cuts object>], ylim=None, particle_type=ParticleType.GAMMA_POINT, figsize=None, ax=None, label=None, export_to_h5=None, import_from_h5=None, import_label=None)[source]

Plot the energy resolution as a function of true energy for DL2 data.

Parameters:
  • zeniths (list of float, optional) – List of zenith angles in degrees. If None, use all available MC directions.

  • azimuths (list of float, optional) – List of azimuth angles in degrees. Must have the same length as zeniths.

  • cuts (list of Cuts, optional) – List of cuts to apply. Default is [DefaultCuts.NO_CUTS.value].

  • ylim (tuple of float, optional) – Limits for the y-axis as (ymin, ymax). If None, use default limits.

  • particle_type (ParticleType, optional) – Type of particle to consider. Default is ParticleType.GAMMA_POINT.

  • figsize (tuple of float, optional) – Size of the figure as (width, height). If None, use default size.

  • ax (matplotlib.axes.Axes, optional) – Axes object to plot on. If None, create a new figure and axes.

  • label (str, optional) – Label for the plot legend. If None, generate labels automatically.

  • export_to_h5 (str)

  • import_from_h5 (str)

  • import_label (str)

Raises:

AssertionError – If zeniths and azimuths have different lengths. If both zeniths/azimuths and cuts have lengths greater than 1.

Notes

  • If multiple zenith/azimuth pairs are provided, the closest pair to the training data is highlighted.

  • If multiple cuts are provided, each cut is plotted separately.

See also

get_energy_resolution_DL2

Compute energy resolution for given parameters.

get_energy_resolution_DL2(zenith=None, azimuth=None, cuts=<ctlearn_manager.utils.utils.Cuts object>, particle_type=ParticleType.GAMMA_POINT)[source]

Compute the energy resolution for DL2 data.

Parameters:
  • zenith (float, optional) – The zenith angle of the observations. If None, use all available data.

  • azimuth (float, optional) – The azimuth angle of the observations. If None, use all available data.

  • cuts (Cuts, optional) – The selection cuts to apply to the data. Defaults to DefaultCuts.NO_CUTS.value.

  • particle_type (ParticleType, optional) – The type of particle to consider. Defaults to ParticleType.GAMMA_POINT.

Returns:

  • e (numpy.ndarray) – The energy bin centers.

  • e_res (numpy.ndarray) – The energy resolution for each energy bin.

Notes

This method uses ctaplot.energy_resolution_per_energy to compute the energy resolution based on true and reconstructed energies.

plot_ROC_curve_DL2(zenith, azimuth, nbins=3)[source]

Plot the ROC curve for DL2 data based on gammaness and true energy.

Parameters:
  • zenith (float) – Zenith angle of the simulated data in degrees.

  • azimuth (float) – Azimuth angle of the simulated data in degrees.

  • nbins (int, optional) – Number of energy bins for the ROC curve, by default 3.

Notes

This function uses DL2 data for gamma and proton events to compute the Receiver Operating Characteristic (ROC) curve. It requires the ctaplot library for plotting and assumes the presence of specific keys for gammaness and true energy in the DL2 data. The function retrieves the DL2 data for gamma and proton events, combines them, and calculates the ROC curve for different energy bins. The resulting plot is displayed using matplotlib.

Raises:
  • KeyError – If the required keys (gammaness_key or true_energy_key) are not present in the DL2 data.

  • ValueError – If no DL2 files are found for the given zenith and azimuth angles.

Parameters:
  • zenith (float)

  • azimuth (float)

  • nbins (int)

get_IRF_data(zenith=None, azimuth=None, cuts=None)[source]

Retrieve Instrument Response Function (IRF) data based on specified parameters.

Parameters:
  • zenith (float, optional) – The zenith angle for which to retrieve IRF data. If None, the average zenith from the validity range is used.

  • azimuth (float, optional) – The azimuth angle for which to retrieve IRF data. If None, the average azimuth from the validity range is used.

  • cuts (Cuts) – The cuts object specifying the IRF type and efficiency parameters.

Returns:

A tuple containing: - config (str): The configuration string for the matched IRF data. - cuts_file (str): The file path to the cuts file. - irf_file (str): The file path to the IRF file. - benchmark_file (str): The file path to the benchmark file.

Return type:

tuple

Raises:

IndexError – If no IRF data is found for the specified cuts or direction, or if multiple matches are found for the given parameters.

Notes

If both zenith and azimuth are None, the method calculates the average zenith and azimuth from the validity range and retrieves the closest IRF data.

compare_irfs_to_RF(zenith, azimuth=None)[source]

Compare Instrument Response Functions (IRFs) to Random Forest (RF) benchmarks.

This method visualizes and compares the performance metrics of IRFs generated by CTLearn with RF benchmark data for a given zenith angle and optional azimuth angle. It generates plots for flux sensitivity, angular resolution, and energy resolution.

Parameters:
  • zenith (float) – The zenith angle in degrees for which the comparison is performed.

  • azimuth (float, optional) – The azimuth angle in degrees for which the comparison is performed. If not provided, defaults to None.

Raises:
  • ImportError – If required modules cannot be imported.

  • FileNotFoundError – If the required IRF or RF benchmark files are not found.

  • KeyError – If expected data keys are missing in the IRF or RF benchmark files.

Notes

  • The method uses HDF5 and FITS files to load performance data.

  • Plots are displayed using Matplotlib for visual comparison.

  • The RF benchmark data is dynamically imported based on the provided zenith angle and telescope configuration.

See also

self.direction_model.get_IRF_data

Method to retrieve IRF data for a given zenith and azimuth angle.

plot_everything_dl2(output_directory, dl2_files, gammaness_cut=0.9, edep_cuts=False)[source]

Generate DL2 plots using the trained tri-model and submit the job to the cluster.

Parameters:
  • output_directory (str) – The directory where the output files and plots will be saved.

  • dl2_files (list[str]) – A list of paths to the DL2 data files to be processed.

  • gammaness_cut (float, optional) – The gammaness cut value to filter events, by default 0.9.

  • edep_cuts (bool, optional) – Whether to apply energy-dependent cuts, by default False.

Notes

This method serializes the current instance of the tri-model to a pickle file and generates a command to create DL2 plots. It temporarily disables the cluster configuration to process any unprocessed DL2 files in the same job. The plotting job is submitted to the cluster using an SBATCH script.

plot_zenith_azimuth_ranges()[source]

Plot the zenith and azimuth ranges for the direction model.

This method delegates the plotting of zenith and azimuth ranges to the plot_zenith_azimuth_ranges method of the direction_model attribute.

Notes

Ensure that the direction_model attribute is properly initialized and has a plot_zenith_azimuth_ranges method before calling this method to avoid runtime errors.

apply_energy_dependent_cuts_MC(data, cuts_file, theta_cut=True)[source]

Apply energy-dependent cuts to Monte Carlo (MC) data.

Parameters:
  • data (pandas.DataFrame or astropy.table.Table) – The input data containing reconstructed and true event information.

  • cuts_file (str) – Path to the FITS file containing the cut values for gammaness and theta.

  • theta_cut (bool, optional) – Whether to apply the theta cut based on angular separation, by default True.

Returns:

  • dl2 (pandas.DataFrame or astropy.table.Table) – The data after applying the energy-dependent cuts.

  • E_bins (astropy.units.Quantity) – The energy bins used for the cuts, in units of TeV.

Notes

  • The function reads cut values for gammaness and theta from the provided FITS file.

  • It ensures that the energy ranges for gammaness and theta cuts match.

  • If theta_cut is enabled, the angular separation between true and reconstructed coordinates is calculated and used for filtering.

  • The function creates masks for each energy bin and combines them to filter the data.