Create random epochs

Hello World!

I am trying to create random control Epochs from a Raw object. Ideally, the Epochs would be 3s length and minimally overlap with other Epochs which I have created (ecg and eog). Any advice on how I might do this?

Below is a code example:

# load data
raw = mne.io.read_raw_fif(file_path)

# create ecg epochs
ecg_epochs = mne.preprocessing.create_ecg_epochs(raw, tmin=-1.5, tmax=1.5, baseline=(-0.5, -0.2))

# create eog epochs
eog_epochs = mne.preprocessing.create_eog_epochs(raw, tmin=-1.5, tmax=1.5, baseline=(-0.5, -0.2))

# create random epochs
# this is where i need help. ideally, the random epochs will be 3s and not overlap with the above epochs 

Supplementary Information:

  • MNE-Python version: 0.22.0
  • operating system: Ubuntu 18.04 (WSL2)

At the top of my head; I don’t have an immediate data set to try and debug, so I’m giving the example code that is predominantly commented:

# Generate a list of random time stamps based on how many random epochs you need, with the random time stamp lasting no more than the len(raw.times).

# Make a loop to access every piece of data from your raw data
for createRandomEpoch in range(len(raw.times)):

     # Create an if statement here to see if createRandomEpoch matches one of the randomly generated entry in the random time stamp list created prior to the loop. If yes, proceed to the following.
     
          # Access all ecg_epochs time stamps using a loop here and check to see if any falls under the 
          # range of createRandomEpoch and createRandomEpoch + 3.0s. If no, pass this 
          # createRandomEpoch time stamp onto the next loop below. If yes, end loop with 'continue' to 
          # get on with the next  createRandomEpoch time stamp.

          # Access all eog_epochs time stamps using a loop here and check to see if it falls under the 
          # range of createRandomEpoch and createRandomEpoch + 3.0s. If no, append row of data   
          # linked to that     
          # time stamp into an events array. Create an empty events array the first time this happens. This  
          # should follow the array dimension of a typical mne.preprocessing.find_eog_events output. If 
          # events array has already been created, simply np.concatonate new data entry into the events 
          # array. If yes, end loop with 'continue' to get on with the next 
          # createRandomEpoch time stamp.

I don’t know about the “minimal overlap” part, but you could consider simulating raw data and then creating epochs based on that:

https://mne.tools/stable/generated/mne.simulation.simulate_raw.html#mne.simulation.simulate_raw

https://mne.tools/stable/generated/mne.make_fixed_length_epochs.html?highlight=fixed_length_epochs#mne.make_fixed_length_epochs

Hi aaron! thanks so much for your help - if I send you an example of the dataset, would you be able to walk be through it in more detail? While I understand what you’re trying to say, I still can’t seem to code it.

I can work on this with a random data set of mine when I get the time, but I won’t be able to get to this asap as I have some other stuff that I need to attend to at the moment. Maybe in a few days time?

By the way, an average heartbeat per minute count for an adult is about 60-100. It will be impossible to generate a single 3s event epoch that does not overlap with an ecg_epoch event. Are you sure this is what you need?

hmmm that is an excellent point! could we perhaps make it so that there is a maximum overlap percentage? say 50% (1.5 seconds)? What do you think?

And yes of course – a few day’s time is totally okay!

60/1.5 is still 40 heartbeats per minute. It is still impossible to form a trial unless your participants are professional athletes.

May I ask why are you so concerned with ECG and EOG noise polluting your random epoch data? Is this meant to be some form of control condition? Because the typical way of removing EOG and ECG noise from data would be to use PCA or ICA.

Is your raw.data simply a resting state data with no experimental events? If so, it is a lot easier to create a single loop that extracts random epoch off the raw data and then subsequently run MNE’s PCA or ICA function.

https://mne.tools/stable/auto_tutorials/preprocessing/plot_40_artifact_correction_ica.html