TF image (ERDS map) with significance mask
See original GitHub issueI’m trying to plot a TF image (more specifically an ERDS map) with a significance mask. In other words, I would like to plot only significant t/f values, whereas not significant values should be white.
Here’s the example code I’m working with: https://martinos.org/mne/stable/auto_examples/time_frequency/plot_time_frequency_erds.html
I need the individual epochs to compute the significance, so I add the argument average=False
to tfr_multitaper
. I create a new object containing the average TFR with average_tfr = power.average()
.
I renamed power
to tfr
for consistency. Here is the modified code starting on line 103 (everything else above doesn’t change except for two additional imports:
from numpy.ma import masked
from mne.stats import permutation_cluster_1samp_test as pc_test
for event in event_ids:
tfr = tfr_multitaper(epochs[event], freqs=freqs, n_cycles=n_cycles,
use_fft=True, return_itc=False, average=False,
decim=2)
tfr.crop(tmin, tmax)
fig, ax = plt.subplots(1, 4, figsize=(12, 4),
gridspec_kw={"width_ratios": [10, 10, 10, 1]})
average_tfr = tfr.average()
for i in range(3):
t_obs, cl, ps, _ = pc_test(tfr.data[:, i, ...], n_permutations=100,
threshold=5)
shape = average_tfr.data[i].shape
mask = np.full(shape, True)
for c, p in zip(cl, ps):
if p <= 0.05:
mask[c] = False
# average_tfr.data[i] = tfr_plot
average_tfr.plot([i], baseline=[-1, 0], mode="percent", vmin=vmin,
vmax=vmax, cmap=(cmap, False), axes=ax[i],
colorbar=False, show=False)
arr = ax[i].collections[0].get_array().reshape(shape)
arr[mask] = masked
ax[i].collections[0].set_array(arr.flatten())
ax[i].set_title(epochs.ch_names[i], fontsize=10)
ax[i].axvline(0, linewidth=1, color="black", linestyle=":") # event
if i > 0:
ax[i].set_ylabel("")
ax[i].set_yticklabels("")
fig.colorbar(ax[0].collections[0], cax=ax[-1])
fig.suptitle("ERDS ({})".format(event))
fig.show()
This code is inspired by https://martinos.org/mne/stable/auto_tutorials/plot_stats_cluster_1samp_test_time_frequency.html. However, my modified example doesn’t look right. The masked regions do not fit the TFR at all, it seems like the non-significant patches are arbitrarily placed on the TFR image (I’ve used threshold=5
in the cluster permutation test):
Any idea what I’m missing here? Could this be related to the fact that I’m plotting the TFR image with baseline=[-1, 0], mode="percent"
, which might not be reflected in average_tfr.data
? If so, how can I run a significance test on the correct data? Or is there something wrong with how I’m performing the cluster permutation test?
Issue Analytics
- State:
- Created 6 years ago
- Comments:26 (26 by maintainers)
Top GitHub Comments
(…And by that I mean don’t use the phrase “significant clusters”, even though that’s how people tend to colloquially refer to them. The
step_down_p
stuff is fine for publication.)Also you can use e.g.
step_down_p=0.05
. It removes “significant clusters” (don’t ever say this in a publication) that could be pushing the null distribution larger than it should be.