def bootstrap_comparison(list_of_epochs, stat_fun=None, color_coding=None, title=None, subtitles=None, vline=None, ci=None): """ Compare different evoked responses """ if stat_fun is None: stat_fun = gfp cNorm = colors.Normalize(vmin=min(color_coding.values()), vmax=max(color_coding.values())) cmap = cmx.ScalarMappable(norm=cNorm, cmap="cool") fig, ax = plt.subplots(len(list_of_epochs), sharex="all", sharey="all") fig.colorbar(cmap, ax=ax) if title is not None: fig.suptitle("%s mode: %s" % (title)) for i, epochs in enumerate(list_of_epochs): for event in epochs.event_id.keys(): data = epochs[event].get_data() data_to_plot = stat_fun(data) ax[i].plot(epochs.times, data_to_plot, color=cmap.to_rgba(color_coding[event]), label=event) if ci is not None: ci_low, ci_high = bootstrap_confidence_interval( data, ci=ci, stat_fun=stat_fun) ax[i].fill_between(epochs.times, ci_low, ci_high, color=cmap.to_rgba(color_coding[event]), alpha=0.3) if subtitles is not None: ax[i].set_title(subtitles[i]) if vline is not None: ax[i].axvline(vline, linestyle="--", c="black")
print(frequency_map) del raw # Plot fig, axes = plt.subplots(4, 1, figsize=(10, 7), sharex=True, sharey=True) colors = plt.get_cmap('winter_r')(np.linspace(0, 1, 4)) for ((freq_name, fmin, fmax), average), color, ax in zip(frequency_map, colors, axes.ravel()[::-1]): times = average.times * 1e3 gfp = np.sum(average.data**2, axis=0) gfp = mne.baseline.rescale(gfp, times, baseline=(None, 0)) ax.plot(times, gfp, label=freq_name, color=color, linewidth=2.5) ax.axhline(0, linestyle='--', color='grey', linewidth=2) ci_low, ci_up = bootstrap_confidence_interval(average.data, random_state=0, stat_fun=stat_fun) ci_low = rescale(ci_low, average.times, baseline=(None, 0)) ci_up = rescale(ci_up, average.times, baseline=(None, 0)) ax.fill_between(times, gfp + ci_up, gfp - ci_low, color=color, alpha=0.3) ax.grid(True) ax.set_ylabel('GFP') ax.annotate('%s (%d-%dHz)' % (freq_name, fmin, fmax), xy=(0.95, 0.8), horizontalalignment='right', xycoords='axes fraction') ax.set_xlim(tmin * 1000, tmax * 1000)
def compare_evokeds(dataset, groups, mode="gfp", title=None, subtitles=None, vline=None, color_coding=None, ci=None): """ Compare different evoked responses """ cNorm = colors.Normalize(vmin=min(color_coding.values()), vmax=max(color_coding.values())) cmap = cmx.ScalarMappable(norm=cNorm, cmap="cool") if mode == "gfp": stat_fun = gfp elif mode == "rms": stat_fun = rms else: raise ValueError("Mode must be either 'rms' or 'gfp'!") if isinstance(dataset, list) and all( isinstance(d, Evoked) for d in dataset): if ci is not None: raise ValueError("Need Epochs to compute Confidence Interval") names = [evoked.comment for evoked in dataset] elif isinstance(dataset, Epochs) or isinstance(dataset, BaseEpochs): names = list(dataset.event_id.keys()) else: raise ValueError( "Input must be either a list of evoked responses or epoched data") fig, ax = plt.subplots(len(groups), sharex="all", sharey="all") fig.colorbar(cmap, ax=ax) if title is not None: fig.suptitle("%s mode: %s" % (title, mode)) for i, group in enumerate(groups): for g in group: idx = numpy.where(numpy.array(names) == g)[0][0] if isinstance(dataset, list): # data is evoked data = dataset[idx] else: # data is epochs data = dataset[names[idx]] times = data.times if isinstance(dataset, list): # data is evoked raw_data = data.data else: raw_data = data._data data = stat_fun(raw_data) ax[i].plot(times, data, color=cmap.to_rgba(color_coding[names[idx]]), label=g) if ci is not None: ci_low, ci_high = bootstrap_confidence_interval( raw_data, ci=ci, stat_fun=stat_fun) ax[i].fill_between(times, ci_low, ci_high, color=cmap.to_rgba( color_coding[names[idx]]), alpha=0.3) if subtitles is not None: ax[i].set_title(subtitles[i]) if vline is not None: ax[i].axvline(vline, linestyle="--", c="black")