Seizure Detection#

Seizure detection module available trained models - ‘modelA’, ‘modelB’ modelA is the model from the published work. modelB had extended training dataset. Optimal input for the model is 300 second. It is recommended to use only middle part of the signal.

The module provides two main ways to get seizure probabilities:

  1. Low-level processing: Use preprocess_input and infer_seizure_probability for batch processing of pre-windowed signals.

  2. High-level processing: Use predict_channel_seizure_probability to get a continuous probability trace from a single long signal.

Example (Low-level)#

from brainmaze_torch.seizure_detection import load_trained_model, preprocess_input, infer_seizure_probability
from numpy.random import rand

# load model
modelA = load_trained_model('modelA')

# load data
fs = 500
x_len = 300
channels = 3

# create fake data for 3 channels, 300s long
x_input = rand(channels, fs * x_len)

# preprocess; from raw data to spectrogram
x = preprocess_input(x_input, fs)

# get seizure probability; model has 4 output classes, seizure probability is class 4.
# output is in shape (batch_size, x_len * 2 - 1); probability value for every half-second
y = infer_seizure_probability(x, modelA)

Example (High-level for a single channel)#

from brainmaze_torch.seizure_detection import predict_channel_seizure_probability
from numpy.random import rand

# create fake data for a single channel, 10 minutes long
fs = 200
x_input = rand(fs * 600)

# get seizure probability over the entire signal
# this function handles windowing, preprocessing, and inference automatically
time_vector, probability_trace = predict_channel_seizure_probability(x_input, fs, model='modelA')

Sources#

The seizure detection and training of the model is described in add website.

brainmaze_torch.seizure_detection.infer_seizure_probability(x, model, use_cuda=False, cuda_number=0)#

infers seizure probability for a given input x; recommended signal len is 300 seconds. :param x: output from preprocess_input function, should be in shape [batch_size, 100, time_in_half-seconds] :param model: loaded seizure model :param use_cuda: if true x is loaded to cuda with cuda_number :param cuda_number: :return: seizure probability for input x in shape [batch_size, x.shape[2]] :rtype: np.array

brainmaze_torch.seizure_detection.load_trained_model(model_name)#

Load a trained model from the specified path. Two available trained models - ‘modelA’, ‘modelB’ - modelA: the model from the published work. - modelB: trained with extended training dataset.

Parameters:

model_name (str)

Returns:

pytorch_model

brainmaze_torch.seizure_detection.predict_channel_seizure_probability(x, fs, model='modelA', use_cuda=False, cuda_number=0, n_batch=128, window_s=300, step_s=20, discard_edges_s=10)#

Predict seizure probability for a single-channel signal.

The signal is buffered into overlapping windows, converted to spectrograms, run through the seizure model in batches, and the window-level probabilities are realigned to the original time axis.

Parameters:
  • x (array-like) – 1D single-channel signal (samples, not channels).

  • fs (int | float) – Sampling frequency in Hz.

  • model (str | object, optional) – Model identifier passed to load_trained_model() or a preloaded model object. Default is ‘modelA’.

  • use_cuda (bool, optional) – If True, move input to CUDA device for inference. Default is False.

  • cuda_number (int, optional) – CUDA device index when use_cuda is True. Default is 0.

  • n_batch (int, optional) – Number of buffered windows processed per batch. Default is 128.

  • window_s (int, optional) – Window length in seconds for buffering. Default is 300.

  • step_s (int, optional) – Step size in seconds between windows. Default is 20.

  • discard_edges_s (int, optional) – Seconds to discard at each window edge to avoid boundary artifacts introduced by buffering/spectrogram and a model. Default is 10.

Returns:

txx (numpy.ndarray): Time vector (in seconds) corresponding to the first

buffered window after alignment (1D array).

prob (numpy.ndarray): Per-half-second seizure probability values aligned

to the original time axis. Length is approximately ceil(t_max * 2).

Return type:

tuple

Raises:

ValueError – If x is not a 1D array (function expects a single channel signal).

Notes

  • Recommended input length is at least window_s seconds for reliable coverage.

  • Returned probabilities are in [0, 1].

  • The function uses preprocess_input, infer_seizure_probability and load_trained_model internally; model may be a model name or an already-loaded model.

Example

>>> t, p = predict_channel_seizure_probability(signal, 200, model='modelA')
>>> # t: time vector for first window; p: probability per half-second
brainmaze_torch.seizure_detection.preprocess_input(x, fs, return_axes=False)#

This function will calculate a spectrogram. The spectrogram has shape [batch_size, 100, len(x) in seconds * 2 - 1] :param x: raw data input in batch form [batch_size, n-samples] :type x: iterable :param fs: sampling rate of the input signal :return: batch of spectrograms from the input :rtype: np.array