contraction_net.utils

Functions

simulate_training_data(folder[, input_len, n, ...])

Simulate training data for ContractionNet with sinusoidal patterns, random drift, and noise.

plot_selection_training_data(dataset, n_sample)

Plot a random selection of training data sequences.

find_txt_files(root_dir)

Find all .txt files in a directory and its subdirectories.

get_device([print_device])

Determines the most suitable device (CUDA, MPS, or CPU) for PyTorch operations.

distance_transform(input, target)

Compute a normalized distance transform for each labeled region in the target.

process_contractions(contractions[, signal, ...])

Process contraction time series to filter based on specified criteria.

Module Contents

contraction_net.utils.simulate_training_data(folder, input_len=512, n=100, freq_range=(0.04, 0.25), prob_zeros=0.5, clip_thrs=(-0.75, 0.75), random_drift_amp_range=(0.005, 0.02), random_drift_freq_range=(0, 0.05), noise_amp_range=(0, 0.25), plot=False)[source]

Simulate training data for ContractionNet with sinusoidal patterns, random drift, and noise.

Parameters:
  • folder (str) – Path to the folder where the simulated data will be saved.

  • input_len (int, optional) – Length of each simulated sequence, by default 512.

  • n (int, optional) – Number of sequences to simulate, by default 100.

  • freq_range (tuple of float, optional) – Range of frequencies for the sinusoidal patterns, by default (0.04, 0.25).

  • prob_zeros (float, optional) – Probability of zero frequency (flat line), by default 0.5.

  • clip_thrs (tuple of float, optional) – Clipping thresholds for the sinusoidal values, by default (-0.75, 0.75).

  • random_drift_amp_range (tuple of float, optional) – Amplitude range for the random drift, by default (0.005, 0.02).

  • random_drift_freq_range (tuple of float, optional) – Frequency range for the random drift, by default (0, 0.05).

  • noise_amp_range (tuple of float, optional) – Amplitude range for the added noise, by default (0, 0.25).

  • plot (bool, optional) – Whether to plot the simulated data, by default False.

contraction_net.utils.plot_selection_training_data(dataset, n_sample)[source]

Plot a random selection of training data sequences.

Parameters:
  • dataset (object) – Dataset object containing the training data.

  • n_sample (int) – Number of samples to plot.

contraction_net.utils.find_txt_files(root_dir)[source]

Find all .txt files in a directory and its subdirectories.

Parameters:

root_dir (str) – Root directory to search for text files.

Returns:

List of paths to the found text files.

Return type:

list of str

contraction_net.utils.get_device(print_device=False)[source]

Determines the most suitable device (CUDA, MPS, or CPU) for PyTorch operations.

Returns: - A torch.device object representing the selected device.

contraction_net.utils.distance_transform(input, target)[source]

Compute a normalized distance transform for each labeled region in the target.

The function calculates the Euclidean distance transform for each unique label in the target array. Each distance transform is then normalized by its maximum value to ensure distances are scaled between 0 and 1. These normalized distance transforms are summed to produce a composite distance map.

Parameters:
  • input (ndarray) – The input image array. This parameter is currently not used in the function, but included for future extensions or modifications.

  • target (ndarray) – The target image array containing labeled regions. The regions should be labeled as distinct integers, with background typically labeled as 0.

Returns:

distances – An array of the same shape as target, containing the normalized distance transform values for each labeled region in target.

Return type:

ndarray

Notes

This implementation assumes that the target contains non-overlapping labeled regions. Overlapping regions will result in undefined behavior.

contraction_net.utils.process_contractions(contractions, signal=None, threshold=0.05, area_min=3, dilate_surrounding=2, len_min=4, merge_max=3)[source]

Process contraction time series to filter based on specified criteria.

Parameters:
  • contractions (ndarray) – Array indicating intervals of potential contractions (output of ContractionNet).

  • signal (ndarray) – Array of the original signal values corresponding to contractions. If None, no signal will be processed.

  • threshold (float, optional) – Threshold value to binarize contractions, by default 0.05.

  • area_min (int, optional) – Minimum area under the signal curve for contraction interval to consider, by default 3 frames.

  • dilate_surrounding (int, optional) – Number of frames to dilate around each contraction for offset calculation, by default 2 frames.

  • len_min (int, optional) – Minimum length of a contraction to consider, by default 4 frames.

  • merge_max (int, optional) – Maximum gap to merge subsequent filtered contractions, by default 3 frames.

Returns:

Binary array with filtered contractions based on the specified criteria.

Return type:

ndarray