cait.models

class cait.models.CNNModule(input_size, nmbr_out, label_keys, feature_keys, lr, device_name='cpu', down=1, down_keys=None, norm_vals=None, offset_keys=None, weight_decay=1e-05, norm_type='minmax', lr_scheduler=True, kernelsize=8)[source]

Bases: object

Lightning module for the training of a CNN model for classification.

Parameters
  • input_size (int) – The number of features that get passed to the CNN as one sample.

  • nmbr_out (int) – The number of output nodes the last linear layer has.

  • device_name (string, either 'cpu' or 'cude') – The device on that the NN is trained.

  • label_keys (list of strings) – The keys of the dataset that are used as labels.

  • feature_keys (list of strings) – The keys of the dataset that are used as nn inputs.

  • lr (float between 0 and 1) – The learning rate for the neural network training.

  • down (int) – The downsample factor of the training data set, if one is applied.

  • down_keys (list of string) – The keys of the data that is to downsample (usually the event time series).

  • norm_vals (dictionary, every enty is a list of 2 ints (mean, std)) – The keys of this dictionary get scaled in the sample with (x - mu)/sigma.

  • offset_keys (list of strings) – The keys in the sample from that we want to subtract the baseline offset level.

  • weight_decay (float) – The weight decay parameter for the optimizer.

  • norm_type (string) – Either ‘z’ (mu=0, sigma=1) or ‘minmax’ (min=0, max=1). The type of normalization.

  • lr_scheduler (bool) – If true, a learning rate scheduler is used.

  • kernelsize (int) – The size of the kernels used for the CNN.

configure_optimizers(lr=None, weight_decay=None)[source]
forward(x)[source]
get_prob(sample)[source]

Get the outputs for all classes, before the decision rule is applied.

loss_function(logits, labels)[source]
predict(sample)[source]

Predict the class for a sample.

test_step(batch, batch_idx)[source]
training_step(batch, batch_idx)[source]
validation_step(val_batch, batch_idx)[source]
class cait.models.LSTMModule(input_size, hidden_size, num_layers, seq_steps, nmbr_out, label_keys, feature_keys, lr, device_name='cpu', is_classifier=True, down=1, down_keys=None, norm_vals=None, offset_keys=None, weight_decay=1e-05, bidirectional=False, norm_type='minmax', lr_scheduler=True, indiv_norm=False, attention=False)[source]

Bases: object

Lightning module for the training of an LSTM model for classification or regression. For classification, the classes need to get one hot encoded, best with the corresponding transform.

Parameters
  • input_size (int) – The number of features that get passed to the LSTM in one time step.

  • hidden_size (int) – The number of nodes in the hidden layer of the lstm.

  • num_layers (int) – The number of LSTM layers.

  • seq_steps (int) – The number of time steps.

  • device_name (string, either 'cpu' or 'cude') – The device on that the NN is trained.

  • nmbr_out (int) – The number of output nodes the last linear layer after the lstm has.

  • label_keys (list of strings) – The keys of the dataset that are used as labels.

  • feature_keys (list of strings) – The keys of the dataset that are used as nn inputs.

  • lr (float between 0 and 1) – The learning rate for the neural network training.

  • is_classifier (bool) – If true, the output of the nn gets an additional softmax activation.

  • down (int) – The downsample factor of the training data set, if one is applied.

  • down_keys (list of string) – The keys of the data that is to downsample (usually the event time series).

  • norm_vals (dictionary, every enty is a list of 2 ints (mean, std)) – The keys of this dictionary get scaled in the sample with (x - mu)/sigma.

  • offset_keys (list of strings) – The keys in the sample from that we want to subtract the baseline offset level.

  • weight_decay (float) – The weight decay parameter for the optimizer.

  • bidirectional (bool) – If true, a bidirectional LSTM is used.

  • norm_type (string) – Either ‘z’ (mu=0, sigma=1) or ‘minmax’ (min=0, max=1). The type of normalization.

  • lr_scheduler (bool) – If true, a learning rate scheduler is used.

  • indiv_norm (bool) – If true, every event is divide by its maximal value before passing into the network.

  • attention (bool) – If activated, an attention layer is added before passing into the model.

configure_optimizers(lr=None, weight_decay=None)[source]
forward(x)[source]

The forward pass in the neural network.

Parameters

x (torch tensor of size (batchsize, nmbr_features)) – The input features.

Returns

The ouput of the neural network.

Return type

torch tensor of size (batchsize, nmbr_outputs)

loss_function(logits, labels)[source]
predict(sample)[source]

Give a prediction for incoming data array or batch of arrays, does all essential transforms.

Parameters

sample (1D numpy array or batch of arrays, i.e. then 2D array) – The features for one (1D case) or more (2D case) samples.

Returns

The prediction.

Return type

torch tensor of size (batchsize - 1 if no batch, nn_output_size)

test_step(batch, batch_idx)[source]
training_step(batch, batch_idx)[source]
validation_step(val_batch, batch_idx)[source]
class cait.models.RNNModule(input_size, hidden_size, num_layers, seq_steps, nmbr_out, label_keys, feature_keys, lr, device_name='cpu', is_classifier=True, down=1, down_keys=None, norm_vals=None, offset_keys=None, weight_decay=1e-05)[source]

Bases: object

Lightning module for the training of an RNN model for classification or regression.

For classification, the classes need to get one hot encoded, best with the corresponding transform.

Parameters
  • input_size (int) – The number of features that get passed to the RNN in one time step.

  • hidden_size (int) – The number of nodes in the hidden layer of the RNN.

  • num_layers (int) – The number of RNN layers.

  • seq_steps (int) – The number of time steps.

  • device_name (string, either 'cpu' or 'cude') – The device on that the NN is trained.

  • nmbr_out (int) – The number of output nodes the last linear layer after the RNN has.

  • label_keys (list of strings) – The keys of the dataset that are used as labels.

  • feature_keys (list of strings) – The keys of the dataset that are used as nn inputs.

  • lr (float between 0 and 1) – The learning rate for the neural network training.

  • is_classifier (bool) – If true, the output of the nn gets an additional softmax activation.

  • down (int) – The downsample factor of the training data set, if one is applied.

  • down_keys (list of string) – The keys of the data that is to downsample (usually the event time series).

  • norm_vals (dictionary, every enty is a list of 2 ints (mean, std)) – The keys of this dictionary get scaled in the sample with (x - mu)/sigma.

  • offset_keys (list of strings) – The keys in the sample from that we want to subtract the baseline offset level.

  • weight_decay (float) – The weight decay parameter for the optimizer.

configure_optimizers(lr=None, weight_decay=None)[source]
forward(x)[source]

The forward pass in the neural network.

Parameters

x (torch tensor of size (batchsize, nmbr_features)) – The input features.

Returns

The ouput of the neural network.

Return type

torch tensor of size (batchsize, nmbr_outputs)

loss_function(logits, labels)[source]
predict(sample)[source]

Give a prediction for incoming data array or batch of arrays, does all essential transforms

Parameters

sample (1D numpy array or batch of arrays, i.e. then 2D array) – The features for one (1D case) or more (2D case) samples.

Returns

The prediction.

Return type

torch tensor of size (batchsize - 1 if no batch, nn_output_size)

test_step(batch, batch_idx)[source]
training_step(batch, batch_idx)[source]
validation_step(val_batch, batch_idx)[source]
class cait.models.SeparationLSTM(nmbr_pileup, label_keys, input_size, hidden_size, num_layers, seq_steps, feature_keys, lr, device_name='cpu', down=1, down_keys=None, norm_vals=None, offset_keys=None, weight_decay=1e-05, norm_type='minmax', lr_scheduler=True)[source]

Bases: object

Lightning module for the training of an LSTM model for separation of PileUp events.

Parameters
  • nmbr_pileup (int) – The number of pile up events that are assumed to be on each trace.

  • label_keys (list of strings) – The keys of the dataset that are used as labels.

  • input_size (int) – The number of features that get passed to the LSTM in one time step.

  • hidden_size (int) – The number of nodes in the hidden layer of the lstm.

  • num_layers (int) – The number of LSTM layers.

  • seq_steps (int) – The number of time steps.

  • feature_keys (list of strings) – The keys of the dataset that are used as nn inputs.

  • lr (float between 0 and 1) – The learning rate for the neural network training.

  • device_name (string, either 'cpu' or 'cude') – The device on that the NN is trained.

  • down (int) – The downsample factor of the training data set, if one is applied.

  • down_keys (list of string) – The keys of the data that is to downsample (usually the event time series).

  • norm_vals (dictionary, every enty is a list of 2 ints (mean, std)) – The keys of this dictionary get scaled in the sample with (x - mu)/sigma.

  • offset_keys (list of strings) – The keys in the sample from that we want to subtract the baseline offset level.

  • weight_decay (float) – The weight decay parameter for the optimizer.

  • norm_type (string) – Either ‘z’ (mu=0, sigma=1) or ‘minmax’ (min=0, max=1). The type of normalization.

  • lr_scheduler (bool) – If true, a learning rate scheduler is used.

configure_optimizers(lr=None, weight_decay=None)[source]
forward(x)[source]
loss_function(y_hat, x)[source]
predict(sample)[source]
test_step(batch, batch_idx)[source]
training_step(batch, batch_idx)[source]
validation_step(val_batch, batch_idx)[source]
class cait.models.TransformerModule(input_size, d_model, number_heads, dim_feedforward, num_layers, nmbr_out, seq_steps, device_name, label_keys, feature_keys, lr, is_classifier, down, down_keys, offset_keys, norm_vals, weight_decay=1e-05, dropout=0.5, norm_type='minmax', pos_enc=True, lr_scheduler=True)[source]

Bases: object

Lightning module for the training of an Transformer Encoder model for classification or regression. For classification, the classes need to get one hot encoded, best with the corresponding transform.

Parameters
  • input_size (int) – The number of features that get passed to the Model in one time step.

  • d_model (int) – The dimensions of the model.

  • number_heads (int) – The number of heads for the attention layer.

  • dim_feedforward (int) – The dimensions in the feed forward net.

  • hidden_size (int) – The number of nodes in the hidden layer of the lstm.

  • num_layers (int) – The number of LSTM layers.

  • seq_steps (int) – The number of time steps.

  • device_name (string) – The device on that the NN is trained. Either ‘cpu’ or ‘cuda’.

  • nmbr_out (int) – The number of output nodes the last linear layer after the lstm has.

  • label_keys (list of strings) – The keys of the dataset that are used as labels.

  • feature_keys (list of strings) – The keys of the dataset that are used as nn inputs.

  • lr (float between 0 and 1) – The learning rate for the neural network training.

  • is_classifier (bool) – If true, the output of the nn gets an additional softmax activation.

  • down (int) – The downsample factor of the training data set, if one is applied.

  • down_keys (list of string) – The keys of the data that is to downsample (usually the event time series).

  • norm_vals (dictionary, every enty is a list of 2 ints (mean, std)) – The keys of this dictionary get scaled in the sample with (x - mu)/sigma.

  • offset_keys (list of strings) – The keys in the sample from that we want to subtract the baseline offset level.

  • weight_decay (float) – The weight decay parameter for the optimizer.

  • dropout (float) – The share of weights that is set to zero in the dropout layer.

  • norm_type (string) – Either ‘z’ (mu=0, sigma=1) or ‘minmax’ (min=0, max=1). The type of normalization.

  • pos_enc (bool) – If true, we include a positional encoding layer.

  • lr_scheduler (bool) – If true, a learning rate scheduler is used.

configure_optimizers(lr=None, weight_decay=None)[source]
forward(src, src_mask=None)[source]

The forward pass in the neural network

Parameters

x (torch tensor of size (batchsize, nmbr_features)) – the input features

Returns

the ouput of the neural network

Return type

torch tensor of size (batchsize, nmbr_outputs)

generate_square_subsequent_mask(sz)[source]
loss_function(logits, labels)[source]

Calculates the loss value, for classfiers the negative log likelihood, for regressors the MSE.

Parameters
  • logits (float) – The output values of the neural network.

  • labels (float) – The labels, e.g. the objective values or classes.

Returns

The loss value

Return type

float

predict(sample)[source]

Give a prediction for incoming data array or batch of arrays, does all essential transforms

Parameters

sample (1D numpy array or batch of arrays, i.e. then 2D array) – the features for one (1D case) or more (2D case) samples

Returns

the prediction

Return type

torch tensor of size (batchsize - 1 if no batch, nn_output_size)

test_step(batch, batch_idx)[source]
training_step(batch, batch_idx)[source]
validation_step(val_batch, batch_idx)[source]
cait.models.mh_predict(h5_path: str, feature_channel: int, group_name: str = 'events', prediction_name: str = 'prediction', model_handler: Optional[object] = None, mh_path: Optional[str] = None, which_data: str = 'mainpar')[source]

Add predictions from a Scikit-Learn model to the HDF5 set.

Parameters
  • h5_path (string) – The path to the HDF5 file.

  • feature_channel (int) – The channel of the detector module on that we make the predictions.

  • group_name (string) – The name of the group within the HDF5 file.

  • prediction_name (string) – The name of the prediction that is saved to the HDF5 set.

  • model_handler (object) – A model handler with that we want to make predictions.

  • mh_path (string) – A path to load a model handler from.

  • which_data (string) – Used for the evaluation tools instance, to tell which data is used for the prediction.

cait.models.nn_predict(h5_path: str, feature_channel: int, model: Optional[object] = None, ptl_module: Optional[object] = None, ptl_ckp_path: Optional[str] = None, group_name: str = 'events', prediction_name: str = 'prediction', keys: list = ['event'], chunk_size: int = 50, no_channel_idx_in_pred: bool = False, use_prob=False)[source]

Add predictions from a PyTorch model to the HDF5 set.

Parameters
  • h5_path (string) – The path to the HDF5 file.

  • feature_channel (int) – The channel of the detector module on that we make the predictions.

  • model (object) – A trained PyTorch Lightning module or Pytorch model.

  • ptl_module (object) – A Pytorch lightning model class.

  • ptl_ckp_path (string) – A path to the checkpoint from where we load the PyTorch lightning model parameters.

  • group_name (string) – The name of the group within the HDF5 file.

  • prediction_name (string) – The name of the prediction that is saved to the HDF5 set.

  • keys (list) – The keys from the HDF5 set that are included as features into every sample handed to the neural network.

  • chunk_size (int) – The size of the chunks to predict at once.

  • no_channel_idx_in_pred (bool) – If True, then we assume that there is no channel index in the data set from the HDF5 file.

  • use_prob (bool) – Include the probabilities corresponding to all classes, instead of the prediction for one class.