utils package
Submodules
utils.adapt_variables module
- utils.adapt_variables.adapt_variables(config, x, masks, labels)
Adapts the variables to fit the dimension of the model selected
- Parameters:
config (configuration dictionary.)
x (input data of dims)
masks (masks locating where we have values or not.)
labels (ground truth data.)
form (Inputs of the) –
1D: (batch, variables, time)
- 2D: (batch, variables, time, x)
can be converted to: -> 1D: (batch * x, variables, time)
- 3D: (batch, variables, time, lat, lon)
can be converted to: -> 1D: (batch * (lat * lon), variables, time) -> 2D: (batch * time, variables, lat, lon)
- Returns:
x (adapted input.)
masks (adapted masks.)
labels (adapted labels.)
utils.determine_threshold module
- utils.determine_threshold.determine_threshold(y_pred, y_true, metric_name='f1_score', metric_params={}, num=100, lower_is_better=False)
Find optimal threshold based on defined metric.
- Parameters:
y_pred (Array) – Prediction/output variables by your model.
y_true (Array) – Target/ground-truth labels.
metric_name (Str) – Torchmetrics for threshold optimization, defaults to “f1_score”.
metric_params (Dict) – Parameters for Torchmetrics, defaults to {}.
num (Int) – Number of candidates for threshold optimization, defaults to 100.
lower_is_better (Float) – Indicate whether or not lower metric value is better, defaults to False.
- Returns:
Dictionary with metrics from torchmetrics to be evaluated.
- Return type:
Dict
utils.loss module
- class utils.loss.Loss(package, configuration)
Bases:
ModuleThis class is used for wrapping for Torch loss functions.
- forward(outputs, labels)
Forward function of the loss, playing the same role as __call__ does for a regular python class.
- Parameters:
outputs (Torch.Tensor) – Prediction/output variables by your model.
labels (Torch.Tensor) – Target/ground-truth labels.
- Returns:
The loss with respect to all Tensors.
- Return type:
Torch.Tensor
- utils.loss.set_loss(parameters)
Check if loss is user defined.
utils.metrics_pyod module
- utils.metrics_pyod.evaluate_print(clf_name, y, scores, config, threshold=0.5)
Utility function for evaluating and printing the results obtained given a PyOD model. Default metrics include ROC, Accuracy, Precision, Recall and Average Precision.
- Parameters:
clf_name (str) – Name for the experiment.
y (np.array) – Target/ground-truth labels.
scores (np.array) – Output scores provided by the model.
threshold (float, optional) – Threshold to compute binary scores for evaluation purposes, defaults to 0.5
utils.metrics_pytorch module
- utils.metrics_pytorch.init_metrics(config)
Initialization of metric objects for evaluation purposes.
- Parameters:
config (dict) – A dictionary containing all the configuration variables for the experiment.
- Returns:
Dictionary with metrics from torchmetrics to be evaluated.
- Return type:
dict
utils.misc module
- utils.misc.plot_confusion_matrix(cm, outputs, labels, path)
Compute and plot confusion matrix for evaluation
- Parameters:
cm (class: 'torchmetrics.ConfusionMatrix') – Confusion matrix object
outputs (tensor) – Categorized model output
labels (tensor) – Samples’ ground truth
path (str) – Path to save the generated figures
- utils.misc.plot_spatial_aggregation(path, output, label, mask, coordinates, num_classes)
Plot extreme event detection saliency map for a given time step
- Parameters:
path (str) – Path to save the generated figures
output – Categorized model output
label – Samples’ ground truth
mask – Quality mask
coordinates (tuple) – Bounding box of the region of interest (lon_min, lon_max, lat_min, lat_max)
num_classes (int) – Number of classess in the classification
- utils.misc.plot_spatial_signal(path, output, label, mask, coordinates, contourf_params, title)
Plot extreme event detection categorical map for a given time step
- Parameters:
path (str) – Path to save the generated figures
output – Decision scores at the output of the model
label – Samples’ ground truth
mask – Quality mask
coordinates (tuple) – Bounding box of the region of interest (lon_min, lon_max, lat_min, lat_max)
contourf_params (dict) – Dictionary containing customizable parameters for the contourf function
title (str) – Title of the generated visualization
- utils.misc.plot_temporal_results(path, xticks_labels, labels, outputs_when_event, output_without_event=None)
- Plot extreme event detection signals through time:
Ground truth extreme event signal
Extreme signal (Aggregated mean over region affected by the extreme event)
Non-extreme signal (Aggregated mean over region non-affected by the extreme event)
- Parameters:
path (str) – Path to save the generated figures
xticks_labels (list) – X axis’ labels
labels (list) – Samples’s ground truth
outputs_when_event (list) – Aggregated mean over region affected by the extreme event
output_without_event (list, optional) – Aggregated mean over region non-affected by the extreme event, defaults to None
utils.setup_config module
- utils.setup_config.setup(filename)
Opens the configuration file as a dictionary
- Parameters:
filename (str) – path to .yaml configuration file
- Returns:
configuration file
- Return type:
dict
- utils.setup_config.setup_config()
Gets the configuration for the current experiment and creates the folder structure. If provided, some hyperparameters can be added
- Returns:
experiment configuration
- Return type:
dict
utils.validate_config module
- utils.validate_config.validate_config(config)
Checks for the consistency between the data dimension, the dimension of the model requested and the output dimension
- Parameters:
config (dict) – configuration file