Concatenate Epoch based on feature

I have a raw edf file regarding the EEG with 4 channels and one channel of Environmental Sound. First, I made fixed epochs. My problem is that I want to gather all those epochs which have the average Environmental sound above the average of this channel and have the other epoch with the environmental sound below the average. So in the end, I want to have two variables containing related epochs.

Hi @SoroushZrZ please provide a snippet with the code that you have written so far, and point out where exactly you are stuck. That will make it much easier for people to help you.

In general you will want to:

  1. iterate over your epochs (all steps below are in a loop)
  2. use the .get_data() method to obtain the channels x timepoints data for the epoch of the current iteration
  3. subset the data to select your environmental sound channel β†’ you will have a simple timeseries now
  4. average that timeseries and compare the resulting value to the average over all epochs (done outside of the loop we are currently in)
  5. save the result of the comparison as β€œ1” if above average, and β€œ0” otherwise in a list or an array

After this loop you will have a list or an array of zeros and ones that is of the same length as the epochs you have. The ones will indicate class 1 (sound above average) and the zeros will indicate class 2 (sound below average)

Thank you so much for your reply.
The following is how I load the data:

file = "Data\\APPLE_polysomnography\\apples-130001.edf"
data = mne.io.read_raw_edf(file,preload=True) 
data.set_eeg_reference()   #Normalize the EEG data
epochs=mne.make_fixed_length_epochs(data,duration=10.01,overlap=0,preload=False)
epochs=epochs.drop_bad()

Then I want to gather all the epoch which have the average above the overall average of the data. I wrote this code:

epochs.load_data()
Average_Snore=np.average(epochs.copy().pick_channels(['snore']).get_data())
EEG_Snore=[]
EEG_Not_Snore=[]
for i in range(len(epochs)):
    if np.average(epochs.copy().pick_channels(['snore']).get_data()[i])>Average_Snore:
        EEG_Snore.append(epochs[i])
    else:
        EEG_Not_Snore.append(epochs[i])   

The problem is that by this method, no longer my data are epochs so I cannot calculate the PSD use " compute_psd
So I want to try the following code but I got stucked:

Average_Snore
EEG_Snore=[]
EEG_Not_Snore=[]
for i in range(len(epochs)):
    if np.average(epochs.copy().pick_channels(['snore']).get_data()[i])>Average_Snore:
        EEG_Snore=mne.concatenate_epochs(epochs[i])
    else:
        EEG_Not_Snore=mne.concatenate_epochs(epochs[i]) 

you should follow the recipe I described above and then subset your epochs like: new_epochs = old_epochs[idx], where idx is an index variable (a numpy array … either a boolean mask, or an array of integer positions into old_epochs).

See the documentation for indexing epochs: mne.Epochs β€” MNE 1.4.2 documentation

Thanks for you help. I tried that, but I have problem in the for loop:

Epochs123=mne.concatenate_epochs([epochs[1],epochs[2],epochs[3]])

I want to use this method to create EEG_Snore and EEG_Not_Snore in the loop below:

for i in range(len(epochs)):
    if np.average(epochs.copy().pick_channels(['snore']).get_data()[i])>Average_Snore:
        EEG_Snore=mne.concatenate_epochs(epochs[i])
    else:
        EEG_Not_Snore=mne.concatenate_epochs(epochs[i]) 

So, in the end, I would like these two epochs would be like Epochs123 with the following format:
image

What I meant was something like this:

idx=np.zeros(len(epochs))
for i, epo in enumerate(epochs):
    if np.mean(epo.pick_channels(['snore']).get_data()) > Average_Snore:
        idx[i] = True
    else:
        idx[i] = False

epos_snore = epochs[idx]
epos_no_snore = epochs[~idx]

Not sure if it works, I haven’t tested it … but this is meant to point you into the right direction.

Thanks for your help. I tried that:

epochs.load_data()
Average_Snore=np.average(epochs.copy().pick_channels(['snore']).get_data())
idx=np.zeros(len(epochs))
for i in range(len(epochs)):
    if np.average(epochs.copy().pick_channels(['snore']).get_data()[i])>Average_Snore:
        idx[i] = True
    else:
        idx[i] = False
epochs_snore = epochs[idx]
epochs_no_snore = epochs[~idx]

But I got this error about the last two lines:

IndexError: arrays used as indices must be of integer (or boolean) type

use idx = idx.astype(bool) before the indexing.