Randomizing epochs (for permutation test)

Hi,

I am running an analysis for inter-brain synchronization (analyzing data between two EEG files, each file comes from each person).

I would like to run a permutation test. However, I have difficulty in how to randomize my epochs. Could you help me with how can I randomize the epochs from each file ?, then I calculate the inter-brain synchrony again for each permutation?

  • MNE-Python version: 0.23
  • operating system: Ubuntu 20

Thanks
Ihshan

Hello @ihgumilar,

have you meanwhile been able to resolve this issue?

Hi @richard ,

Thank you for your reply. Not yet up to now :slight_smile:
Cheers

Not sure if I get your question correctly, but I think you can use something like this:

import itertools

import mne

fname = "path_to_my_epochs.fif"
epochs = mne.read_epochs(fname)

perms = itertools.permutations(range(len(epochs)))

for perm in perms:
    epochs_in_permuted_order = epochs[np.asarray(perm)]
    data = epochs_in_permuted_order.get_data()
    # ... do something with data

Note that depending on how many epochs you have, the length of perms will be huge. In that case you may want to do something like this:

import numpy as np
import mne

fname = "path_to_my_epochs.fif"
epochs = mne.read_epochs(fname)

rng = np.random.default_rng(42)  # set a random seed
n_perms = 100  # how many permutations you want

for iperm in range(n_perms):
    perm = rng.permutation(len(epochs))
    epochs_in_permuted_order = epochs[perm]
    data = epochs_in_permuted_order.get_data()
    # ... do something with data

2 Likes

@sappelhoff Thank you very much for this. I will immediately try this.
Sorry for my late reply.

Cheers,
IG