Grand average of evoked files?

Hi Jonathan,

I spotted a bug in your script here:

Hello Everybody,
During my experiment I recorded 2 blocs per subject. I am just started
with mne_python.

I have two questions in one ::
1) I would like to know how can I concatenate the blocs and above all
the events (corresponding to each bloc) to have just one bloc.
In brief, for each subject I have one bloc with its events and another
bloc with its events also.

raw_fname1= '...'
raw_fname2= '...'
event_fname1= '...'
event_fname2= '...'

raw1 = io.Raw(raw_fname1)
raw2 = io.Raw(raw_fname2)

events1 = mne.read_events(event_fname1)
events2 = mne.read_events(event_fname2)
events_list = [events1,events2]

events is a numpy array, not a list. So, you need to use np.concatenate()
to stitch the events together. Something like:

import numpy as np
events = np.concatenate((events1, events2))

Does this solve your problem?

Mainak

raw_conc= concatenate_raws([raw1,raw2], events_list=events_list)

Until here I think these several code lines work correctly...

2) But when I try to extract the epochs from raw_conc I have some error
messages (concerning I think the events_list)

picks =
mne.pick_types(raw1.info
,meg='mag',eeg=False,ecg=False,eog=False,stim=False,
exclude='bads')
epochs = mne.Epochs(raw, events_list, event_id, tmin, tmax, picks=picks,
baseline= baseline, reject=reject)

The aim of both questions is to use the tfr_morlet function which asks
in input the epochs.

power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles,
use_fft=False, return_itc=True, decim=3,n_jobs=1)

Someone has an idea about this?

Bests,

Jonathan
--
Dr Jonathan Entakli - Research Associate Post doctoral position

Department of Psychology
University of Cambridge, Downing St
Cambridge CB2 3EB

Sir William Hardy Building
Office : 314
Mobile : 07778 692 458

http://www.viscog.psychol.cam.ac.uk/
_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

The information in this e-mail is intended only for the person to whom it
is
addressed. If you believe this e-mail was sent to you in error and the
e-mail
contains patient information, please contact the Partners Compliance
HelpLine at
MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you in
error
but does not contain patient information, please contact the sender and
properly
dispose of the e-mail.

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

Hi all,

Many thanks for the advice!

I used Mainank's solution in a loop as:

sessions = ['s1','s2','s3']
for session in sessions:
        subjects = ['mk','ah','mh','li','kk','ak']
        flag = 1
        for subject in subjects:
                filename = subject+"_"+session+"-ave.fif"
                evoked = mne.read_evokeds(source_path+filename)
                if flag == 1:
                        evoked_all = evoked[0]
                        flag = 0
                else:
                        evoked_all = evoked_all+evoked[0]
        evoked_all.save(save_path+session+"ave.fif")

Each file contain only one condition (sessions), so I think that this works
correctly.

-Maria

2014-09-11 20:18 GMT+03:00 Mainak Jas <mainakjas at gmail.com>:

Hi Maria,

The simplest solution here is to do:

Hi,

Thank you for answers!

I tried Mainak's solution but it gives

grand_ave = evoked1+evoked2 +evoked3

grand_ave:

[<Evoked | comment : 'Unknown', time : [-0.099994, 0.499968], n_epochs
: 160, n_channels x n_times : 306 x 721>,
<Evoked | comment : 'Unknown', time : [-0.099994, 0.499968], n_epochs
: 160, n_channels x n_times : 306 x 721>,
<Evoked | comment : 'Unknown', time : [-0.099994, 0.499968], n_epochs
: 160, n_channels x n_times : 306 x 721>]

I think that I should get only one evoked if this works correctly.

grand_ave = evoked1[0] + evoked2[0] + evoked3[0]

so that you extract the first element of the list. Or you can read in your
evoked object using:

evoked = mne.read_evokeds('filename-ave.fif', 0)

and then do grand_ave = evoked1 + evoked2 + evoked3

This should be ok if you know that your files contain only one condition.
Is that the case?

Mainak

Maybe I should try Stephen's solution if there is no simpler ones.

-Maria

2014-09-11 14:58 GMT+03:00 Stephen Politzer-Ahles <spa268 at nyu.edu>:

Hello Maria,

I haven't done this with evoked sensor data, only with STCs. However, as
long as evoked data also have an 'add' method, I imagine something like
this should work for you as well:

stc_avgs = dict()

# get a grand average for each condition (I defined a dictionary of
conditions earlier)
for condition in conditions.keys():

# where all my STCs for each participant in this condition are stored
stcdir = os.environ['SAMPLE'] + '/stcs_ica/' + condition

# template for the names of the STCs (I'm only doing left hemi here)
fname = os.path.join( stcdir, '%s-' + condition + '-free-lh.stc' )

# use a list comprehension to read in the STC for each subject; I
defined a list of participants earlier)
stcs = [mne.read_source_estimate(fname % subject) for subject in
participants]

# math out the grand average
stc_avg = reduce(add, stcs)
stc_avg /= len(stcs)

# put the grand average for this condition into a dictionary of all the
conditions
stc_avg.subject = 'fsaverage'
stc_avgs[condition] = stc_avg

(however, Mainak's solution, if it works, looks much simpler!)

Stephen Politzer-Ahles
New York University, Abu Dhabi
Neuroscience of Language Lab
http://www.nyu.edu/projects/politzer-ahles/

Hi all,

I would like to compute a grand average of evoked files.

It seems to work as: grand_ave = np.mean([evoked1.data, evoked2.data,
evoked3.data],1)

However, the problem is that if the evoked data is saved as:

evoked.save("filename-ave.fif")

and loaded as:

evoked = mne.read_evokeds("filename-ave.fif")

evoked doesn't have attribute data.

I also tried:
grand_ave = np.mean([evoked1, evoked2, evoked3],1)

but this gives an error:

TypeError Traceback (most recent call
last)
/scratch/braindata/mhhakone/intell/<ipython-input-65-f6e26a212f6a> in
<module>()
----> 1 grand_ave = np.mean([evoked1, evoked2, evoked3],1)

/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in mean(a,
axis, dtype, out)
   2371 mean = a.mean
   2372 except AttributeError:
-> 2373 return _wrapit(a, 'mean', axis, dtype, out)
   2374 return mean(axis, dtype, out)
   2375

/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.pyc in
_wrapit(obj, method, *args, **kwds)
     35 except AttributeError:
     36 wrap = None
---> 37 result = getattr(asarray(obj),method)(*args, **kwds)
     38 if wrap:
     39 if not isinstance(result, mu.ndarray):

TypeError: unsupported operand type(s) for /: 'Evoked' and 'float'

How can I get data from -ave.fif files? (The -ave.fif files I have
saved seems to open correctly in xFit, Matlab and mne_analyze.)
Or is there some better way to calculate the grand average of -ave.fif
files?

Thanks already in advance!

Regards,
Maria

_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

The information in this e-mail is intended only for the person to whom
it is
addressed. If you believe this e-mail was sent to you in error and the
e-mail
contains patient information, please contact the Partners Compliance
HelpLine at
MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you
in error
but does not contain patient information, please contact the sender and
properly
dispose of the e-mail.

_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

The information in this e-mail is intended only for the person to whom
it is
addressed. If you believe this e-mail was sent to you in error and the
e-mail
contains patient information, please contact the Partners Compliance
HelpLine at
MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you
in error
but does not contain patient information, please contact the sender and
properly
dispose of the e-mail.

_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

The information in this e-mail is intended only for the person to whom it
is
addressed. If you believe this e-mail was sent to you in error and the
e-mail
contains patient information, please contact the Partners Compliance
HelpLine at
MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you
in error
but does not contain patient information, please contact the sender and
properly
dispose of the e-mail.

_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

The information in this e-mail is intended only for the person to whom it
is
addressed. If you believe this e-mail was sent to you in error and the
e-mail
contains patient information, please contact the Partners Compliance
HelpLine at
MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you in
error
but does not contain patient information, please contact the sender and
properly
dispose of the e-mail.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20140912/3b97a727/attachment.html

Hi,

1) Mainak : I've already try your idea, the concatenation works but the
samples are not added, so I can't use events concatenated in the big raw
file.

2) dgw : Actually I like all the 3 options. I tried the option 2. It
seems very long to do but it seems the most coherent for me. And above
all it works perfectly :slight_smile:

Thank you very much,
Cheers,

John

Hi Jonathan,

Hi,

1) Mainak : I've already try your idea, the concatenation works but the
samples are not added, so I can't use events concatenated in the big raw
file.

What I was suggesting was the following: concatenate the raws as you do
right now

raw_conc= concatenate_raws([raw1,raw2], events_list=events_list)

but when computing the epochs, you need to use

epochs = mne.Epochs(raw, np.concatenate((events1, events2)), event_id,
tmin, tmax, picks=picks, baseline= baseline, reject=reject)

because events is usually a numpy array when computing epochs. How were
these events computed? Maybe you need to do:

events2[0, :] += raw1.last_samp

or something of that sort before you do np.concatenate((events1, events2))?

Mainak

2) dgw : Actually I like all the 3 options. I tried the option 2. It
seems very long to do but it seems the most coherent for me. And above
all it works perfectly :slight_smile:

Thank you very much,
Cheers,

John
> Hi Jonathan,
>
> I spotted a bug in your script here:
>
>
>> Hello Everybody,
>> During my experiment I recorded 2 blocs per subject. I am just
>> started
>> with mne_python.
>>
>> I have two questions in one ::
>> 1) I would like to know how can I concatenate the blocs and above
>> all
>> the events (corresponding to each bloc) to have just one bloc.
>> In brief, for each subject I have one bloc with its events and
>> another
>> bloc with its events also.
>>
>> raw_fname1= '...'
>> raw_fname2= '...'
>> event_fname1= '...'
>> event_fname2= '...'
>>
>> raw1 = io.Raw(raw_fname1)
>> raw2 = io.Raw(raw_fname2)
>>
>> events1 = mne.read_events(event_fname1)
>> events2 = mne.read_events(event_fname2)
>> events_list = [events1,events2]
>
> events is a numpy array, not a list. So, you need to use
> np.concatenate() to stitch the events together. Something like:
>
> import numpy as np
> events = np.concatenate((events1, events2))
>
> Does this solve your problem?
>
> Mainak
>
>> raw_conc= concatenate_raws([raw1,raw2], events_list=events_list)
>>
>> Until here I think these several code lines work correctly...
>>
>> 2) But when I try to extract the epochs from raw_conc I have some
>> error
>> messages (concerning I think the events_list)
>>
>> picks =
>> mne.pick_types(raw1.info
>> [1],meg='mag',eeg=False,ecg=False,eog=False,stim=False,
>> exclude='bads')
>> epochs = mne.Epochs(raw, events_list, event_id, tmin, tmax,
>> picks=picks,
>> baseline= baseline, reject=reject)
>>
>> The aim of both questions is to use the tfr_morlet function which
>> asks
>> in input the epochs.
>>
>> power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles,
>> use_fft=False, return_itc=True, decim=3,n_jobs=1)
>>
>> Someone has an idea about this?
>>
>> Bests,
>>
>> Jonathan
>> --
>> Dr Jonathan Entakli - Research Associate Post doctoral position
>>
>> Department of Psychology
>> University of Cambridge, Downing St
>> Cambridge CB2 3EB
>>
>> Sir William Hardy Building
>> Office : 314
>> Mobile : 07778 692 458
>>
>> http://www.viscog.psychol.cam.ac.uk/ [2]
>> _______________________________________________
>> Mne_analysis mailing list
>> Mne_analysis at nmr.mgh.harvard.edu
>> Mne_analysis Info Page [3]
>>
>> The information in this e-mail is intended only for the person to
>> whom it is
>> addressed. If you believe this e-mail was sent to you in error and
>> the e-mail
>> contains patient information, please contact the Partners
>> Compliance HelpLine at
>> MyComplianceReport.com: Compliance and Ethics Reporting [4] . If the e-mail was sent
>> to you in error
>> but does not contain patient information, please contact the sender
>> and properly
>> dispose of the e-mail.
>
>
>
> Links:
> ------
> [1] http://raw1.info
> [2] http://www.viscog.psychol.cam.ac.uk/
> [3] Mne_analysis Info Page
> [4] MyComplianceReport.com: Compliance and Ethics Reporting
>
> _______________________________________________
> Mne_analysis mailing list
> Mne_analysis at nmr.mgh.harvard.edu
> Mne_analysis Info Page
>
>
> The information in this e-mail is intended only for the person to whom
> it is
> addressed. If you believe this e-mail was sent to you in error and the
> e-mail
> contains patient information, please contact the Partners Compliance
> HelpLine at
> MyComplianceReport.com: Compliance and Ethics Reporting . If the e-mail was sent to you
> in error
> but does not contain patient information, please contact the sender and
> properly
> dispose of the e-mail.

--
Dr Jonathan Entakli - Research Associate

Department of Psychology
University of Cambridge, Downing St
Cambridge CB2 3EB

Sir William Hardy Building
Office : 314
Mobile : 07778 692 458

http://www.viscog.psychol.cam.ac.uk/
_______________________________________________
Mne_analysis mailing list
Mne_analysis at nmr.mgh.harvard.edu
Mne_analysis Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20140912/27812a96/attachment.html

Hello Mainak,

Yes you definitely right, the events used for the epochs are usually a
numpy array. I tried what you said (in different way) and it works
perfectly. What I did exactly :

I concatenated raw1 and raw2 such as raws = mne.io.Raw([raw1,raw2].

For the events I created manually a "big" empty numpy array (3D, in my
case) of the length events1+events2. I copied and pasted the events1 in
the big numpy array and and before to copy and paste events2 I added,
for each sample of time, the difference between the last sample of the
"Old" events (coming from raws) and the last sample of events2:

events_Old = mne.find_events(raws,stim_channel='...')
load events2 and events1(from my file)
Last_Old = events_Old[-1,0]
Last_events2 = events2[-1,0]
Diff = Last_Old - Last_events2

for i in range(0,events2.shape[0]):
events2[i,0] = events2[i,0] + Diff

big_events = np.zeros(shape = (len(events1+2[:,1]),3))
big_events = [0:len(events1)] = events1
big_events = [len(events2)::] = events2

I think there is easier but for the moment it works :slight_smile:

Thank you everybody for your help.

John.