[Mne_analysis] calculate adaptive mean amplitude

Aaron Newman Aaron.Newman at dal.ca
Mon Feb 19 12:25:45 EST 2018
Search archives:

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!
Aaron

# 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[1]

# 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[0]):
        # 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())


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


More information about the Mne_analysis mailing list