Indexing the nth epoch of a condition

Dear MNE users,

I'm analysing an EEG dataset which contains event markers for four separate conditions. The data has been segmented into epochs defined by these event markers. Stimuli from each condition appeared ten times during the experiment - therefore my dataset contains 40 epochs, ten per condition.

I'm generating evokeds for each condition with the following code:

subjects = {'001','002','003','005','006','007','008','009','010'}
conditions = {'cond_1','cond_2','cond_3','cond_4'}
cond = {}

for subject in subjects:
    epochs_fname[subject] = data_path + subject + '-epo.fif'
    epochs[subject] = mne.read_epochs(epochs_fname[subject])
    evoked[subject] = {cond:(epochs[subject][cond]).average() for cond in (conditions)}

However I would like to base my evokeds on only the first instance of each condition (i.e. the first time participants saw a stimulus from cond_1, the first time they saw a stimulus from cond_2, and so on). To this end, is there a way to index the nth epoch from each condition that would fit in the above code?

Any help would be greatly appreciated!

Regards

Lyam.

epochs[subject][cond][0] (or maybe epochs[subject][cond][:1]) should work.
But be careful about dropped epochs by looking at `epochs.selection` to
make sure the first instance didn't get dropped e.g. due to reject/flat
params.

Is there a place you looked for this sort of information in the docs and
couldn't find it? Maybe we (hopefully you :slight_smile: ) could add something to the
epochs tutorial
<https://mne-tools.github.io/dev/auto_tutorials/plot_object_epochs.html>?

Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20170620/f2b8df79/attachment.html

Dear Eric,

Thanks for your email. Unfortunately this didn't work - it simply returned a single epoch per subject (the first epoch in the dataset, I suspect). I also tried epochs[subject][cond[0]] which returned the following error: 'Event "n" is not in epochs', which suggests to me that it may not actually be possible to index in this way..?

1 Like

Thanks for your email. Unfortunately this didn't work - it simply returned
a single epoch per subject (the first epoch in the dataset, I suspect).

This sounds very close to what you want. I probably should have been more
explicit and noted that the code I suggested would need to be looped over
conditions (cond), like:

evokeds[subject] = {cond: epochs[subject][cond][0].average() for cond
in conditions}

?
HTH,
Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20170621/f343f3a1/attachment.html

Dear Eric,

No worries about not being explicit. I actually did implement your suggestion as written below the first time around. So, to be clear,

evokeds[subject] = {cond: epochs[subject][cond][0].average() for cond in conditions}

...only returns a single epoch. Sorry for the miscommunication.

evokeds[subject] = {cond: epochs[subject][cond][0].average() for cond in
conditions}

...only returns a single epoch. Sorry for the miscommunication.

Can you be a bit clearer about what you mean by "returns"? Which variables
are what length? For example, if evokeds[subject] is a length-1 dictionary
(what I think you're saying), this is only possible if `conditions` is
itself also length 1. But every entry in the `evokeds[subject]` dict should
be an Evoked instance with `nave==1`, namely the first (valid) epoch from
that condition, which is what I think you're trying to get.

Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20170621/2405aacc/attachment-0001.html

Dear Eric,

Many apologies...please disregard my last email. It seems that I was misreading my output - your suggestion DID indeed work. In answer to your question, by 'returns' I was referring to which epochs were being selected for averaging. So epochs[subject][cond][0] selected/returned the first instance of each condition, as intended.

Thanks very much for the help, and again, sorry for the miscommunication.

Kind regards

Lyam

No problem, glad it's working!

Eric