def erds_plot(epochs): # compute ERDS maps ########################################################### # idx = np.array(list(range(7, 11)) + list(range(12, 15)) + list(range(17, 21)) + list(range(32, 41))) # chans = np.array(epochs.ch_names)[idx].tolist() epochs.pick_channels(["C3","Cz","C4"]) event_ids = epochs.event_id tmin, tmax = -1, 4 freqs = np.arange(60, 90, 1) # frequencies from 2-35Hz n_cycles = freqs # use constant t/f resolution vmin, vmax = -1, 1.5 # set min and max ERDS values in plot # vmin, vmax = -0.5, 0.5 # set min and max ERDS values in plot baseline = [-1, 0.3] # baseline interval (in s) cmap = center_cmap(plt.cm.RdBu, vmin, vmax) # zero maps to white kwargs = dict(n_permutations=100, step_down_p=0.05, seed=1, buffer_size=None, out_type='mask') # for cluster test # Run TF decomposition overall epochs tfr = tfr_multitaper(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, average=False, decim=2) tfr.crop(tmin, tmax) tfr.apply_baseline(baseline, mode="percent") for event in event_ids: # select desired epochs for visualization tfr_ev = tfr[event] fig, axes = plt.subplots(1, 4, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 10, 10, 1]}) for ch, ax in enumerate(axes[:-1]): # for each channel # positive clusters _, c1, p1, _ = pcluster_test(tfr_ev.data[:, ch, ...], tail=1, **kwargs) # negative clusters _, c2, p2, _ = pcluster_test(tfr_ev.data[:, ch, ...], tail=-1, **kwargs) # note that we keep clusters with p <= 0.05 from the combined clusters # of two independent tests; in this example, we do not correct for # these two comparisons c = np.stack(c1 + c2, axis=2) # combined clusters p = np.concatenate((p1, p2)) # combined p-values mask = c[..., p <= 0.05].any(axis=-1) # plot TFR (ERDS map with masking) tfr_ev.average().plot([ch], vmin=vmin, vmax=vmax, cmap=(cmap, False), axes=ax, colorbar=False, show=False, mask=mask, mask_style="mask") ax.set_title(epochs.ch_names[ch], fontsize=10) ax.axvline(0, linewidth=1, color="black", linestyle=":") # event if not ax.is_first_col(): ax.set_ylabel("") ax.set_yticklabels("") fig.colorbar(axes[0].images[-1], cax=axes[-1]) fig.suptitle("ERDS ({})".format(event)) fig.show()
freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, average=False, decim=2) tfr.crop(tmin, tmax) tfr.apply_baseline(baseline, mode="percent") fig, axes = plt.subplots(1, 4, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 10, 10, 1]}) for ch, ax in enumerate(axes[:-1]): # for each channel # positive clusters _, c1, p1, _ = pcluster_test(tfr.data[:, ch, ...], tail=1, **kwargs) # negative clusters _, c2, p2, _ = pcluster_test(tfr.data[:, ch, ...], tail=-1, **kwargs) # note that we keep clusters with p <= 0.05 from the combined clusters # of two independent tests; in this example, we do not correct for # these two comparisons c = np.stack(c1 + c2, axis=2) # combined clusters p = np.concatenate((p1, p2)) # combined p-values mask = c[..., p <= 0.05].any(axis=-1) # plot TFR (ERDS map) tfr.average().plot([ch], vmin=vmin, vmax=vmax, cmap=(cmap, False),
buffer_size=None) # for cluster test # Run TF decomposition overall epochs tfr = tfr_multitaper(epochs, freqs=freqs, n_cycles=n_cycles, use_fft=True, return_itc=False, average=False, decim=2) tfr.crop(tmin, tmax) tfr.apply_baseline(baseline, mode="percent") for event in event_ids: # select desired epochs for visualization tfr_ev = tfr[event] fig, axes = plt.subplots(1, 4, figsize=(12, 4), gridspec_kw={"width_ratios": [10, 10, 10, 1]}) for ch, ax in enumerate(axes[:-1]): # for each channel # positive clusters _, c1, p1, _ = pcluster_test(tfr_ev.data[:, ch, ...], tail=1, **kwargs) # negative clusters _, c2, p2, _ = pcluster_test(tfr_ev.data[:, ch, ...], tail=-1, **kwargs) # note that we keep clusters with p <= 0.05 from the combined clusters # of two independent tests; in this example, we do not correct for # these two comparisons c = np.stack(c1 + c2, axis=2) # combined clusters p = np.concatenate((p1, p2)) # combined p-values mask = c[..., p <= 0.05].any(axis=-1) # plot TFR (ERDS map with masking) tfr_ev.average().plot([ch], vmin=vmin, vmax=vmax, cmap=(cmap, False), axes=ax, colorbar=False, show=False, mask=mask, mask_style="mask")