[Mne_analysis] calculate adaptive mean amplitude
Aaron.Newman at dal.ca
Mon Feb 19 22:50:41 EST 2018
Fantastic, thanks Marijn! Very elegant and fast. And thanks for the links. I had my head stuck in pandas mode but I find the ndarray indexing actually much more intuitive.
All the best,
On February 19, 2018 at 7:05:51 PM, Marijn van Vliet (w.m.vanvliet at gmail.com<mailto:w.m.vanvliet at gmail.com>) wrote:
here is a super fast solution that vectorises everything. In order to do this, we have two use two super handy numpy features:
1. Array broadcasting: https://docs.scipy.org/doc/numpy-1.13.0/user/basics.broadcasting.html
2. Advanced integer indexing: https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing
I hope you can follow along with the comments. I encourage you to go through it line by line and carefully inspect all the arrays that are created.
You can find the code example here:
Marijn van Vliet
Department of Neuroscience and Biomedical Engineering
> On 20 Feb 2018, at 04:25, Aaron Newman <Aaron.Newman at dal.ca> wrote:
> Hi all
> This is perhaps more of a python question than MNE-specific, but it is needed to solve an MNE-specific problem based on an MNE data structure. I’m trying to compute ‘adaptive mean amplitude’ for individual epochs - i.e., the mean amplitude centred around the minimum (or max) value within a specified time window. To do this, I find the time point with the minimum value, then compute the mean including x number of points +/- that time point.
> I have a working solution, but it’s very slow because it uses nested for loops to iterate through every cell in the dataframe manually. I’m wondering if anyone can suggest a vectorized or otherwise more efficient solution?
> thanks in advance!
> # Create pandas dataFrame from MNE epochs
> df = epochs.to_data_frame(scaling_time=scaling_time, index=['epoch', 'time'])
> # Create multiIndex; see http://pandas.pydata.org/pandas-docs/version/0.15.2/advanced.html#advanced-reindexing-and-alignment
> idx = pd.IndexSlice
> epoch_start, epoch_end = 150, 250
> ama_width = 30 # tw around peak to get mean of, in ms
> # find time points with peak minimum values within time window
> t_indices = df.loc[idx[:, time_win], idx[:]].groupby('epoch').idxmin()
> # note: this produces tuples within each cell, of (epoch, time_point)
> # create empty df with rows = epochs and columns for each electrode
> peak_times = pd.DataFrame(columns=list(df.columns[1:].values))
> # fill empty df with just time_point from each tuple
> for electrode in list(df.columns[1:].values):
> peak_times.loc[idx[:],idx[electrode]] = t_indices.loc[idx[:],idx[electrode]].str
> # create empty df for output
> out_data = pd.DataFrame(columns=list(df.columns[:].values))
> # add condition labels
> out_data.loc[idx[:],idx['condition']] = list(df.loc[idx[:,0], idx['condition']])
> # Slow step - needs to iterate through each cell of array (epoch and electrode) individually
> for electrode in list(out_data.columns[1:].values):
> for trial in list(df.index.levels):
> # define time window around peak value
> peak_tp = peak_times.loc[idx[trial], idx[electrode]]
> amawin_start = peak_tp - ama_width/2
> amawin_end = peak_tp + ama_width/2
> amawin = list(arange(amawin_start,amawin_end))
> # compute mean amplitude around peak value and write to output df
> out_data.loc[idx[trial], idx[electrode]] = float(df.loc[idx[trial, amawin], idx[electrode]].groupby(['epoch']).mean())
> Mne_analysis mailing list
> Mne_analysis at nmr.mgh.harvard.edu
> 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
> http://www.partners.org/complianceline . 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
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
http://www.partners.org/complianceline . 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...
More information about the Mne_analysis