How to change the time duration of an sliding window when doing decoding?

Hi! Guys,
I’m wondering if there is a way to built sliding window classifiers using responses from 20 ms windows, which were gradually shifted in 1 ms steps over the duration of the epoched response from the onset of the epoch up to the offset.

Best

Hi @YuZhou, you can probably use epochs.get_data() to obtain an array of shape (n_epochs, n_channels, n_times).

Then you can use numpy and indexing to

  1. get an epoch to look at: epoch0_data = data[0, ...]
  2. iterate over that data in windows.
    1. for that, use information from epochs.times and epochs.info["sfreq"] to design your windows

Your question is very general. To obtain more helpful answers, I recommend that you share code snippets where you are blocked, and specify your questions in more detail.

1 Like

Hi!Stefan,

Thank you so much for your reply! My apology that I didn’t state my problem clearly. Here are my code snippets:

epo_data_learn = epoch_learn.get_data()
epo_data_per = epoch_per.get_data()

labels_fea_col_learn = epoch_learn.metadata['feature_color']
labels_fea_col_per = epoch_per.metadata['feature_color']

n_cvsplits = 10
cv = StratifiedKFold(n_splits=n_cvsplits)

clf = make_pipeline(
    StandardScaler(), # Z-score data, because gradiometers and magnetometers have different scales
    LogisticRegression(random_state=0, n_jobs=12, max_iter=500)
)

sliding_fea_col = SlidingEstimator(clf, n_jobs=12)

sliding_fea_col.fit(epo_data_learn, labels_fea_col_learn)
dvalues_fea_col = cross_val_predict(sliding_fea_col, epo_data_per, labels_fea_col_per, cv=cv, n_jobs=12, method='decision_function')

In my case, the epoch was downsampled from 1000Hz to 200Hz and was smoothed with fwhm=10. The classifiers were trained using the data from one task called ‘learn’, and were tested using the data from another task called ‘perception’.
In my code snippets, the time window for training the classifier only has 1 time point, but what I want to achieve is to classify on more than one time point using a sliding window (https://www.jneurosci.org/content/33/18/7691.short), so that the classifier has access to all time points in the window (the number of features is increased). As a result, this operation can improve the decoding performance according to this paper (Decoding Dynamic Brain Patterns from Evoked Responses: A Tutorial on Multivariate Pattern Analysis Applied to Time Series Neuroimaging Data | Journal of Cognitive Neuroscience | MIT Press)


But sadly I don’t know how to achieve this in MNE, in my vague thought, the SlidingEstimator() might not work here, I might need to write some loops by hand to loop in the time domain, I might also need the function mne.decoding.Vectorizer? to reshape the data of every n seconds * n_channels.

yes SlidingEstimator works with single time points. You’ll have to write for loops yourself
to use time windows

Alex