API Documentation

meegnet.dataloaders module

class meegnet.dataloaders.ContinuousDataset(window: int = 2, overlap: float = 0, offset: int = 10, sfreq: int = 500, n_subjects: int = None, scaling: str = 'minmax', n_samples: int = None, split_sizes: tuple = (0.8, 0.1, 0.1), sensortype: str = None, lso: bool = False, random_state: int = 0)

Creates a dataset from continuous data by loading continuous data and splitting it in segments according to set parameters.

Parameters:
  • window (float, optional) – Window size in seconds. Defaults to 2.

  • overlap (float, optional) – Overlap between windows (0-1). Defaults to 0.

  • offset (int, optional) – Offset in seconds. Defaults to 10.

  • sfreq (float, optional) – Sampling frequency. Defaults to 500.

  • n_subjects (int, optional) – Number of subjects. Defaults to None (all subjects).

  • scaling (str, optional) – The scaling method. available options are “zscore” and “minmax”. By default, minmax.

  • n_samples (int, optional) – Number of samples per subject. Defaults to None.

  • split_sizes(tuple or int), optional – A tuple of (train_size, valid_size, test_size) for splits or a float <= 1, in which case the valid sizes and test sizes are deduced to be as half or the remaining.

  • sensortype (str, optional) – Sensor type. Defaults to None.

  • lso (bool, optional) – Leave subjects out. Defaults to False.

  • random_state (int, optional) – Random state for reproducibility. Defaults to 0.

window

Window size in seconds.

Type:

int

overlap

Overlap between windows.

Type:

float

offset

Offset in seconds.

Type:

int

sfreq

Sampling frequency.

Type:

int

n_subjects

Number of subjects.

Type:

int

n_samples

Number of samples per subject.

Type:

int

data

Data.

Type:

torch.Tensor

targets

targets.

Type:

torch.Tensor

groups

Groups.

Type:

list

subject_list

Subject list.

Type:

list

_load_sub(filepath)

Loads a subject’s data with windowing and overlap.

meegnet.dataloaders.Dataset

alias of EpochedDataset

class meegnet.dataloaders.EpochedDataset(sfreq: float = 500, n_subjects: int = None, scaling: str = 'minmax', n_samples: int = None, split_sizes: tuple = (0.8, 0.1, 0.1), sensortype: str = None, lso: bool = False, random_state: int = 0, target_labels: list = None)

Creates a dataset for epoch-based M/EEG data.

Parameters:
  • sfreq (int, optional) – The sampling frequency, by default 500.

  • n_subjects (int, optional) – The number of subjects. Default value is None, which means all subjects are processed.

  • scaling (str, optional) – The scaling method. available options are “zscore” and “minmax”. By default, minmax.

  • n_samples (int, optional) – The number of samples to include, by default None.

  • sensortype (str, optional) – The type of sensor to use, by default None.

  • lso (bool, optional) – Leave subjects out. If False, within-subject splitting is used, by default False.

  • random_state (int, optional) – The random state for reproducibility, by default 0.

sfreq

The sampling frequency.

Type:

int

n_subjects

The number of subjects.

Type:

int

n_samples

The number of samples for each subject.

Type:

int

data

The data.

Type:

torch.Tensor

targets

The targets.

Type:

torch.Tensor

groups

The groups.

Type:

list

subject_list

The subject list.

Type:

list

data_path

The path to the data.

Type:

str

load(*args, **kwargs)

Wrapper for the load_from_path method.

Parameters:
  • *args (tuple) – Positional arguments to pass to load_from_path.

  • **kwargs (dict) – Keyword arguments to pass to load_from_path.

Return type:

None

load_from_path(data_path: str = None, csv_path: str = None, one_sub: str = None, verbose: int = 2, target_col: str = 'label', subject_col: str = 'sub') None

Loads data from the data_path.

Parameters:
  • data_path (str, optional) – Path to the data. Defaults to self.data_path if None.

  • csv_path (str, optional) – Path to the CSV file. Defaults to “participants_info.csv” in data_path.

  • one_sub (str, optional) – Subject ID or “random” to select a random subject.

  • verbose (int, optional) – Logging verbosity level (0-2).

  • target_col (str, optional) – Column name for targets in the CSV file.

preload(data_path: str, csv_path: str = None, subject_col: str = 'sub') DataFrame

Loads the subject list from a CSV file.

Parameters:
  • data_path (str) – Path to the folder containing the dataset.

  • csv_path (str, optional) – Path to the CSV file. Defaults to “participants_info.csv” in the data_path.

Returns:

DataFrame containing participant information.

Return type:

pd.DataFrame

set_data(data, targets, groups=None, target_labels=None)

Sets the data, targets, and groups for the dataset, with optional random subject selection.

Parameters:
  • data (torch.Tensor or np.ndarray) – The dataset containing all subjects’ data.

  • targets (torch.Tensor or np.ndarray) – The target labels corresponding to the data.

  • groups (torch.Tensor or np.ndarray) – The group labels indicating the subject for each sample.

  • target_labels (list, optional) – The unique target labels. If None, they will be inferred from the targets.

split_data(train_size: float = None, valid_size: float = None, test_size: float = None)

Splits data into training, validation, and test sets.

Parameters:
  • train_size (float) – Train set size (%).

  • valid_size (float) – Validation set size (%).

  • test_size (float, optional) – Test set size (%). Defaults to None.

Returns:

Indices for the splits.

Return type:

tuple

torchDataset(index)

Returns a Torch dataset instance of the torch Dataset class for the given index.

class meegnet.dataloaders.InfiniteDataLoader(dataset, batch_size, num_workers=0, pin_memory=False, weights=None)

Creates an infinite data loader.

Parameters:
  • dataset (torch.utils.data.Dataset) – The dataset to be loaded.

  • batch_size (int) – The size of the batches to be loaded.

  • num_workers (int, optional) – The number of workers to use for loading the data, by default 0.

  • pin_memory (bool, optional) – If True, the data loader will copy tensors into CUDA pinned memory before returning them. This can make data transfer faster, but requires more memory.

  • weights (torch.Tensor, optional) – A 1D tensor assigning a weight to each sample in the dataset. If not provided, all samples are assumed to have the same weight.

Returns:

An infinite data loader.

Return type:

InfiniteDataLoader

meegnet.dataloaders.RestDataset

alias of ContinuousDataset

meegnet.network module

class meegnet.network.AutoEncoder(input_size)

Bases: CustomNet

forward(x)

Defines the forward pass through the network.

Args: x (torch.Tensor): Input tensor.

Returns: torch.Tensor: Output tensor.

class meegnet.network.CustomNet(input_size: tuple, n_outputs: int)

Bases: Module

Base class for custom neural networks.

Parameters:
  • input_size (tuple) – Input shape of the network.

  • n_outputs (int) – Number of output neurons.

input_size

Input shape of the network.

Type:

tuple

n_outputs

Number of output neurons.

Type:

int

forward(x)

Defines the forward pass through the network.

get_output_shape(layers)

Computes the output shape of a sequence of layers.

feature_extraction(x)

Defines the feature extraction part of the network (must be implemented).

classif(x)

Defines the classification part of the network (must be implemented).

forward(x: Tensor) Tensor

Defines the forward pass through the network.

Args: x (torch.Tensor): Input tensor.

Returns: torch.Tensor: Output tensor.

get_lin_size(layers: Sequential) int

Computes the output size of a sequence of layers.

Args: layers (nn.Sequential): Sequence of layers.

Returns: int: Output size of the sequence.

get_output_shape(layers: Sequential = None) tuple
class meegnet.network.EEGNet(input_size: tuple, n_outputs: int, filter_size: int = 64, n_filters: int = 16, dropout: float = 0.5, dropout_option: str = 'Dropout', depthwise_multiplier: int = 2)

Bases: CustomNet

EEGNet architecture implementation.

Parameters:
  • input_size (tuple) – Input shape (S, C, T) where S is the number of sensors.

  • n_outputs (int) – Number of output classes.

  • filter_size (int, optional) – Filter size. Defaults to 64.

  • n_filters (int, optional) – Number of filters. Defaults to 16.

  • dropout (float, optional) – Dropout rate. Defaults to 0.5.

  • dropout_option (str, optional) – Dropout type (“SpatialDropout2D” or “Dropout”). Defaults to “Dropout”.

  • depthwise_multiplier (int, optional) – Depthwise multiplier. Defaults to 2.

feature_extraction

Feature extraction layers.

Type:

nn.Sequential

classif

Classification layers.

Type:

nn.Sequential

class meegnet.network.EEGNetv4(input_size: tuple, n_outputs: int, final_conv_length: str | int = 'auto', pool_mode: str = 'mean', F1: int = 8, D: int = 2, F2: int | None = None, kernel_length: int = 64, depthwise_kernel_length: int = 16, pool1_kernel_size: int = 4, pool1_stride_size: int = 4, pool2_kernel_size: int = 8, pool2_stride_size: int = 8, conv_spatial_max_norm: float = 1, activation: Module = <class 'torch.nn.modules.activation.ELU'>, batch_norm_momentum: float = 0.01, batch_norm_affine: bool = True, batch_norm_eps: float = 0.001, drop_prob: float = 0.25)

Bases: CustomNet

class meegnet.network.FullNet(input_size: tuple, n_outputs: int, hlayers: int = 2, filter_size: int = 7, nchan: int = 5, n_linear: int = 150, dropout: float = 0.25, batchnorm: bool = False, maxpool: int = 0)

Bases: CustomNet

FullNet architecture implementation.

Parameters:
  • input_size (tuple) – Input shape (S, C, T) where S is the number of sensors.

  • n_outputs (int) – Number of output classes.

  • hlayers (int, optional) – Number of hidden layers. Defaults to 2.

  • filter_size (int, optional) – Filter size. Defaults to 7.

  • nchan (int, optional) – Number of channels. Defaults to 5.

  • n_linear (int, optional) – Number of neurons in the first linear layer. Defaults to 150.

  • dropout (float, optional) – Dropout rate. Defaults to 0.25.

  • batchnorm (bool, optional) – Whether to use batch normalization. Defaults to False.

  • maxpool (int, optional) – Max pooling size. Defaults to 0.

class meegnet.network.LfCNN(input_size: tuple, n_outputs: int, n_latent: int = 32, filter_length: int = 7, pooling: int = 2, stride: int = 2, pool_type: str = 'max', dropout: float = 0.5, nonlin: Module = <class 'torch.nn.modules.activation.ReLU'>)

Bases: CustomNet

LF-CNN architecture implementation.

Spatial demixing followed by depthwise (per-component) temporal convolution and pooling. Each latent component has its own temporal filter, making the model more interpretable than VAR-CNN. For details see [1].

Parameters:
  • input_size (tuple) – Input shape (S, C, T) where S is the number of sensor types, C the number of channels and T the number of time samples.

  • n_outputs (int) – Number of output classes.

  • n_latent (int, optional) – Number of latent components after spatial demixing. Defaults to 32.

  • filter_length (int, optional) – Length of temporal convolution kernels. Defaults to 7.

  • pooling (int, optional) – Temporal pooling factor. Defaults to 2.

  • stride (int, optional) – Temporal pooling stride. Defaults to 2.

  • pool_type (str, optional) – Pooling type (‘max’ or ‘avg’). Defaults to ‘max’.

  • dropout (float, optional) – Dropout rate. Defaults to 0.5.

  • nonlin (nn.Module, optional) – Activation function class. Defaults to nn.ReLU.

References

[1] I. Zubarev, et al., Adaptive neural network classifier for decoding MEG

signals. Neuroimage. (2019) May 4;197:425-434

class meegnet.network.MEEGNet(input_size: tuple, n_outputs: int, n_linear: int = 2000, dropout: float = 0.5)

Bases: CustomNet

MEEGNet architecture implementation.

Parameters:
  • input_size (tuple) – Input shape (S, C, T) where S is the number of sensors.

  • n_outputs (int) – Number of output classes.

  • n_linear (int, optional) – Number of neurons in the first linear layer. Defaults to 2000.

  • dropout (float, optional) – Dropout rate. Defaults to 0.5.

class meegnet.network.MLP(input_size: tuple, n_outputs: int, hparams: dict)

Bases: CustomNet

Multi-Layer Perceptron (MLP) implementation.

Parameters:
  • input_size (tuple) – Input shape.

  • n_outputs (int) – Number of output neurons.

  • hparams (dict) –

    Hyperparameters:
    • mlp_width (int): Number of neurons in each hidden layer.

    • mlp_depth (int): Number of hidden layers.

    • mlp_dropout (float): Dropout rate.

class meegnet.network.Model(name: str, net_option: str, input_size: tuple, n_outputs: int, save_path: str = None, learning_rate: float = 1e-05, optimizer: callable = <class 'torch.optim.adam.Adam'>, criterion: callable = CrossEntropyLoss(), n_folds: int = 5, device: str = 'cuda', net_params: dict = None)

Bases: object

compute_accuracy(y_pred, target)
display_progress(i, epoch, loss, n_batches, verbose=3)
evaluate(dataloader)
fit(*args, **kwargs)
from_pretrained(repo=None)
get_clf_weights()
get_feature_weights()
load(model_path=None)

Load model from file.

n_parameters(model: Module = None)
plot_accuracy(option='both')
plot_loss(option='both')
save(model_path: str = None)
test(dataset)
train(dataset, batch_size: int = 128, patience: int = 10, max_epoch: int = None, model_path: str = None, early_stop: str = 'loss', num_workers: int = 4, continue_training: bool = False, verbose: int = 3) None

Train the model on the provided dataset.

Parameters:
  • dataset – Dataset to train on.

  • batch_size (int, optional) – Batch size for training. Defaults to 128.

  • patience (int, optional) – Patience for early stopping. Defaults to 10.

  • max_epoch (int, optional) – Maximum number of epochs to train. Defaults to None.

  • model_path (str, optional) – Path to save the model. Defaults to None.

  • num_workers (int, optional) – Number of workers for data loading. Defaults to 4.

  • continue_training (bool, optional) – Wether to pick up training from last checkpoint. By default False. Use when training stopped abbruptly because of an error, or to re-train or fine-tune the model.

  • verbose (int, optional) – The verbosity level. By default 3. Values under 3 will display less detailed progress during training. Values under 2 will not display any update on performance epoch per epoch during training.

Notes

This method trains the model using the provided dataset and hyperparameters. It uses early stopping based on the validation loss and saves the model periodically.

train_batch(batch)
train_epoch(epoch, trainloader, verbose)
class meegnet.network.TrainingTracker(save_path, name, model_path: str = None)

Bases: object

load(mat_path)
plot_accuracy(option: str = 'both', early_stop: bool = True)
plot_loss(option: str = 'both', early_stop: bool = True)
plot_metric(metric_type: str, option: str = 'both', early_stop: bool = True)
save(checkpoint) None

Save model to file.

set_model_path(model_path: str = None) None
update(epoch, tloss, tacc, vloss, vacc, net, optimizer, early_stop: str = 'loss') None
class meegnet.network.VGG16(input_size: tuple, n_outputs: int)

Bases: CustomNet

VGG16 architecture implementation.

Parameters:
  • input_size (tuple) – Input shape (C, H, W) or (S, C, T) for M/EEG data. With S the number of sensors, C the number of channels and T the number of time samples

  • n_outputs (int) – Number of output classes.

class meegnet.network.VanPutNet(input_size: tuple, n_output: int, dropout: float = 0.25)

Bases: CustomNet

VanPutNet architecture implementation.

Parameters:
  • input_size (tuple) – Input shape (C, H, W) or (S, C, T) for M/EEG data.

  • n_output (int) – Number of output classes.

  • dropout (float, optional) – Dropout rate. Defaults to 0.25.

class meegnet.network.VarCNN(input_size: tuple, n_outputs: int, n_latent: int = 32, filter_length: int = 7, pooling: int = 2, stride: int = 2, pool_type: str = 'max', dropout: float = 0.5, nonlin: Module = <class 'torch.nn.modules.activation.ReLU'>)

Bases: CustomNet

VAR-CNN architecture implementation.

Spatial demixing followed by VAR temporal convolution and pooling. For details see [1].

Parameters:
  • input_size (tuple) – Input shape (S, C, T) where S is the number of sensor types, C the number of channels and T the number of time samples.

  • n_outputs (int) – Number of output classes.

  • n_latent (int, optional) – Number of latent components after spatial demixing. Defaults to 32.

  • filter_length (int, optional) – Length of temporal convolution kernels. Defaults to 7.

  • pooling (int, optional) – Temporal pooling factor. Defaults to 2.

  • stride (int, optional) – Temporal pooling stride. Defaults to 2.

  • pool_type (str, optional) – Pooling type (‘max’ or ‘avg’). Defaults to ‘max’.

  • dropout (float, optional) – Dropout rate. Defaults to 0.5.

  • nonlin (nn.Module, optional) – Activation function class. Defaults to nn.ReLU.

References

[1] I. Zubarev, et al., Adaptive neural network classifier for decoding MEG

signals. Neuroimage. (2019) May 4;197:425-434

meegnet.network.create_net(net_option: str, input_size: int, n_outputs: int, net_params: dict = None) Module

Creates a neural network based on the specified option.

Parameters:
  • net_option (str) – Network architecture option (“MLP”, “meegnet”, “custom”, “VGG”, “EEGNet”, “vanPutNet”).

  • input_size (int) – Input size of the network.

  • n_outputs (int) – Number of output neurons.

  • net_params (dict, optional) – Network parameters (required for “MLP” and “custom” options).

Returns:

Created neural network.

Return type:

nn.Module

Raises:

AttributeError – If net_option is invalid.

meegnet.layer module

class meegnet.layer.Conv2dWithConstraint(*args, max_norm=1, **kwargs)

Bases: Conv2d

forward(x)

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class meegnet.layer.DeMixing(size, nonlin=Identity(), axis=-1, regularize='l1_lambda', bias_trainable=True, bias_const=0, unitnorm=False)

Bases: Module

Spatial demixing Layer.

Parameters:
  • size (int) – Number of output features.

  • nonlin (callable) – Activation function.

  • axis (int) – Axis to apply demixing.

  • specs (dict) – Dictionary of layer specifications.

forward(x)

Applies demixing to input tensor.

Args: x (torch.Tensor): Input tensor.

Returns: torch.Tensor: Demixed tensor.

class meegnet.layer.DepthwiseConv2d(in_channels, kernel_size, depthwise_multiplier=1, **kwargs)

Bases: Module

Depthwise separable convolutional layer.

Parameters:
  • in_channels (int) – Number of input channels.

  • kernel_size (int or tuple) – Size of the convolutional kernel.

  • depthwise_multiplier (int, optional) – Multiplier for depthwise convolution. Defaults to 1.

depthwise

Depthwise convolutional layer.

Type:

nn.Conv2d

forward(x)

Applies depthwise convolution to the input tensor.

forward(x)

Applies depthwise convolution to the input tensor.

class meegnet.layer.Flatten(*args: Any, **kwargs: Any)

Bases: Module

Flatten layer to connect feature extraction and classification parts of a network.

Parameters:

None

None
forward(x)

Flattens the input tensor.

forward(x)

Flattens the input tensor.

class meegnet.layer.SeparableConv2d(in_channels, out_channels, kernel_size, **kwargs)

Bases: Module

Separable convolutional layer (depthwise + pointwise convolution).

Parameters:
  • in_channels (int) – Number of input channels.

  • out_channels (int) – Number of output channels.

  • kernel_size (int or tuple) – Size of the convolutional kernel.

depthwise

Depthwise convolutional layer.

Type:

DepthwiseConv2d

pointwise

Pointwise convolutional layer.

Type:

nn.Conv2d

forward(x)

Applies separable convolution to the input tensor.

forward(x)

Applies separable convolution to the input tensor.

meegnet.utils module

meegnet.utils.check_PD(mat)
meegnet.utils.compute_psd(data: array, fs: int, option: str = 'multitaper')

compute_psd.

Parameters:
  • data (np.array) – The data to compute psd on. Must be of shape n_channels x n_samples.

  • fs (int) – The sampling frequency.

  • option (str) – method option for the psd computation. Can be welch or multitaper.

meegnet.utils.cuda_check()

Checks if a CUDA device is available and returns it.

Returns:

A CUDA device if one is available, otherwise a CPU device.

Return type:

torch.device

meegnet.utils.elapsed_time(t0, t1)

Time lapsed between t0 and t1.

Returns the time (from time.time()) between t0 and t1 in a more readable fashion.

Parameters:
  • t0 (float,) – time.time() initial measure of time (eg. at the begining of the script)

  • t1 (float,) – time.time() time at the end of the script or the execution of a function.

meegnet.utils.extract_bands(data: array, f: list = None) array

extract_bands.

Parameters:
  • data (np.array) – the data after it has been transformed to frequency space. Of shape n_samples x n_channels x n_bins or n_channels x n_bins

  • f (list) – the list of bins. id set to None, a list of all bins every .5 from 0 to n_bins will be generated.

Returns:

data – the data after averaging for frequency bands of shape n_samples x n_channels x 7 or n_channels x 7 depending on input shape. bands are defined as follow:

delta: .5 to 4 Hz theta: 4 to 8 Hz alpha: 8 to 12 Hz beta1: 12 to 30 Hz beta2: 30 to 60 Hz gamma1: 60 to 90 Hz gamma2: 90 to 120 Hz

Return type:

np.array

meegnet.utils.load_psd_cc_subjects(PSD_PATH, sub_info_path, window, overlap)
meegnet.utils.nice_time(time)

Returns time in a humanly readable format.

meegnet.utils.stratified_sampling(data, targets, n_samples, subject=None, groups=None)

Performs stratified sampling for a single subject.

Parameters:
  • data (torch.Tensor) – The dataset containing all subjects’ data.

  • targets (torch.Tensor) – The target labels corresponding to the data.

  • n_samples (int) – The number of samples to draw. If None, all samples are used in a stratified manner.

  • groups (torch.Tensor, optional) – The group labels indicating the subject for each sample.

  • subject (int or str, optional) – The subject ID for which to perform stratified sampling.

Returns:

sampled_indices – The indices of the stratified sampled data for the subject.

Return type:

torch.Tensor

meegnet.utils.string_to_int(array, target_labels=None)

Converts an array of strings to integers based on unique labels.

Parameters:
  • array (array-like) – The input array of strings to be converted.

  • target_labels (list, optional) – A predefined list of unique labels. If None, unique labels will be inferred from the input array.

Returns:

A tuple containing: - An array of integers corresponding to the input strings. - A list of unique labels used for the mapping.

Return type:

tuple

Raises:

ValueError – If the input array is empty or if target_labels contains duplicates.

meegnet.utils.strip_string(string: str) str

meegnet.viz module

meegnet.viz.apply_colormap_on_image(org_im, activation, colormap_name)

Apply heatmap on image

Parameters:
  • org_img (PIL img) – Original image

  • activation_map (numpy arr) – Activation map (grayscale) 0-255

  • colormap_name (str) – Name of the colormap

meegnet.viz.avg_range(arr: list)
meegnet.viz.best_window(mask, w_size)
meegnet.viz.choose_best_window(data, fs=500, w_size=300)
data: array

Must be of size k x n_samples. k can be sensor dimension or trial dimension.

w_size: int

The size of the window in ms

meegnet.viz.colorFader(c1, c2, mix=0, alpha=0.5)
meegnet.viz.compute_cams(net, target_layers, dataset, verbose=3)
meegnet.viz.compute_saliency_based_psd(saliency, trial, w_size, fs)
meegnet.viz.compute_saliency_maps(dataset, net, sal_path, threshold=0.95, labels=None, epoched=False)
meegnet.viz.compute_single_sal_psd(index, trial, w_size, fs)
meegnet.viz.convert_to_grayscale(im_as_arr)

Converts 3d image to grayscale

Parameters:

im_as_arr (numpy arr) – RGB image with shape (D,W,H)

Returns:

Grayscale image with shape (1,W,D)

Return type:

grayscale_im (numpy_arr)

meegnet.viz.format_np_output(np_arr)

This is a (kind of) bandaid fix to streamline saving procedure. It converts all the outputs to the same format which is 3xWxH with using sucecssive if clauses.

Parameters:

im_as_arr (Numpy array) – Matrix of shape 1xWxH or WxH or 3xWxH

meegnet.viz.generate_saliency_figure(saliencies: dict, info, save_path: str = '', suffix: str = '', title: str = '', sensors: list = [''], sfreq=500, edge=100, cmap='coolwarm', stim_tick=None, show=False, outlines='head', topomap='window')

Generates a figure visualizing saliency maps for MEG data.

This function creates a grid of images showing the saliency maps for types of stimuli (e.g., image and sound) and sensor channels (e.g., MAG, GRAD1, GRAD2). It also plots a topomap for the maximum saliency index along with a color bar.

Parameters:
  • saliencies (dict) – Dictionary containing saliency maps. Keys should correspond to different types of stimuli (e.g., “image”, “sound”), and values should be numpy arrays of shape 3 x sensors x samples, representing the saliency maps for each channel.

  • save_path (str, optional) – Path to save the generated figure. Default is an empty string, which means the figure will not be saved automatically.

  • suffix (str, optional) – Suffix to append to the filename when saving the figure. Default is an empty string.

  • title (str, optional) – Title for the figure. Default is an empty string.

  • sensors (list, optional) – List of sensor types to include in the visualization. Default is [“”].

  • sfreq (int, optional) – Sampling frequency for computation of xticks. Default is 500.

  • cmap (str, optional) – Colormap to use for displaying the topo- and saliency maps. Default is “coolwarm”.

  • stim_tick (int, optional) – Tick position for the stimulus event in the time axis. Default is None,

  • show (bool, optional) – Wether to show the figure or not. Useful for ipynb.

  • topomap (str, optional) – Must be “window”, “average” or “timing”. if “ average, the average saliency across time is used. If timing, then the saliency at to the highest saliency timing is used. if “window” is used, then the saliency window around the max saliency timing is used.

Return type:

None

Notes

The function assumes that the input saliency maps are normalized to have zero mean and unit variance. The colormap used for displaying the saliency maps is “coolwarm” by default, but can be changed with the cmap parameter.

The function creates a grid layout with a subplot for each sensor channel and a subplot for the topomap. The grid layout is dynamically adjusted based on the number of sensors.

The function does not handle exceptions that may occur during the plotting process, such as issues with file I/O or invalid input data.

meegnet.viz.get_example_params(example_index)

Gets used variables for almost all visualizations, like the image, model etc.

Parameters:

example_index (int) – Image id to use from examples

Returns:

Original image read from the file prep_img (numpy_arr): Processed image target_class (int): Target class for the image file_name_to_export (string): File name to export the visualizations pretrained_model(Pytorch model): Model to use for the operations

Return type:

original_image (numpy arr)

meegnet.viz.get_positive_negative_saliency(gradient)

Generates positive and negative saliency maps based on the gradient

Parameters:

gradient (numpy arr) – Gradient of the operation to visualize

Returns:

pos_saliency ( )

meegnet.viz.make_gif(image_list, output=None, duration=100, loop=0)
meegnet.viz.plot_cam(input_tensor, cams, name, label, out_path='.', colorbar=True)
meegnet.viz.plot_epoch(data, title: str = None)
meegnet.viz.plot_masked_epoch(data, mask, c1='white', c2='green', alpha=0.3, title: str = None)
meegnet.viz.preprocess_image(pil_im, resize_im=True)

Processes image for CNNs

Parameters:
  • PIL_img (PIL_img) – PIL Image or numpy array to process

  • resize_im (bool) – Resize to 224 or not

Returns:

Variable that contains processed float tensor

Return type:

im_as_var (torch variable)

meegnet.viz.rec_avg_range(arr: list, length: int)
meegnet.viz.recreate_image(im_as_var)

Recreates images from a torch variable, sort of reverse preprocessing

Parameters:

im_as_var (torch variable) – Image to recreate

Returns:

Recreated image in array

Return type:

recreated_im (numpy arr)

meegnet.viz.save_class_activation_images(org_img, activation_map, file_name)

Saves cam activation map and activation map on the original image

Parameters:
  • org_img (PIL img) – Original image

  • activation_map (numpy arr) – Activation map (grayscale) 0-255

  • file_name (str) – File name of the exported image

meegnet.viz.save_gradient_images(gradient, file_name)

Exports the original gradient image

Parameters:
  • gradient (np arr) – Numpy array of the gradient with shape (3, 224, 224)

  • file_name (str) – File name to be exported

meegnet.viz.save_image(im, path)

Saves a numpy matrix or PIL image as an image

Parameters:
  • im_as_arr (Numpy array) – Matrix of shape DxWxH

  • path (str) – Path to the image