#### Note

The choice of the threshold is more or less arbitrary. Choosing\n a t-value corresponding to p=0.05, p=0.01, or p=0.001 may often provide\n a good starting point. Depending on the specific dataset you are working\n with, you may need to adjust the threshold.

\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# We want a two-tailed test\ntail = 0\n\n# In this example, we wish to set the threshold for including data bins in\n# the cluster forming process to the t-value corresponding to p=0.001 for the\n# given data.\n#\n# Because we conduct a two-tailed test, we divide the p-value by 2 (which means\n# we're making use of both tails of the distribution).\n# As the degrees of freedom, we specify the number of observations\n# (here: epochs) minus 1.\n# Finally, we subtract 0.001 / 2 from 1, to get the critical t-value\n# on the right tail (this is needed for MNE-Python internals)\ndegrees_of_freedom = len(epochs) - 1\nt_thresh = scipy.stats.t.ppf(1 - 0.001 / 2, df=degrees_of_freedom)\n\n# Set the number of permutations to run.\n# Warning: 50 is way too small for a real-world analysis (where values of 5000\n# or higher are used), but here we use it to increase the computation speed.\nn_permutations = 50\n\n# Run the analysis\nT_obs, clusters, cluster_p_values, H0 = permutation_cluster_1samp_test(\n epochs_power,\n n_permutations=n_permutations,\n threshold=t_thresh,\n tail=tail,\n adjacency=adjacency,\n out_type=\"mask\",\n verbose=True,\n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## View time-frequency plots\nWe now visualize the observed clusters that are statistically significant\nunder our permutation distribution.\n\n#### Warning

Talking about \"significant clusters\" can be convenient, but\n you must be aware of all associated caveats! For example, it\n is **invalid** to interpret the cluster p value as being\n spatially or temporally specific. A cluster with sufficiently\n low (for example < 0.05) p value at specific location does not\n allow you to say that the significant effect is at that\n particular location. The p value only tells you about the\n probability of obtaining similar or stronger/larger cluster\n anywhere in the data if there were no differences between the\n compared conditions. So it only allows you to draw conclusions\n about the differences in the data \"in general\", not at specific\n locations. See the comprehensive\n [FieldTrip tutorial](ft_cluster_) for more information.\n [FieldTrip tutorial](ft_cluster_) for more information.

\n\n.. include:: ../../links.inc\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"evoked_data = evoked.data\ntimes = 1e3 * evoked.times\n\nfig, (ax, ax2) = plt.subplots(2, layout=\"constrained\")\n\nT_obs_plot = np.nan * np.ones_like(T_obs)\nfor c, p_val in zip(clusters, cluster_p_values):\n if p_val <= 0.05:\n T_obs_plot[c] = T_obs[c]\n\n# Just plot one channel's data\n# use the following to show a specific one:\n# ch_idx = tfr_epochs.ch_names.index('MEG 1332')\nch_idx, f_idx, t_idx = np.unravel_index(\n np.nanargmax(np.abs(T_obs_plot)), epochs_power.shape[1:]\n)\n\nvmax = np.max(np.abs(T_obs))\nvmin = -vmax\nax.imshow(\n T_obs[ch_idx],\n cmap=plt.cm.gray,\n extent=[times[0], times[-1], freqs[0], freqs[-1]],\n aspect=\"auto\",\n origin=\"lower\",\n vmin=vmin,\n vmax=vmax,\n)\nax.imshow(\n T_obs_plot[ch_idx],\n cmap=plt.cm.RdBu_r,\n extent=[times[0], times[-1], freqs[0], freqs[-1]],\n aspect=\"auto\",\n origin=\"lower\",\n vmin=vmin,\n vmax=vmax,\n)\nfig.colorbar(ax.images[0])\nax.set(xlabel=\"Time (ms)\", ylabel=\"Frequency (Hz)\")\nax.set(title=f\"Induced power ({tfr_epochs.ch_names[ch_idx]})\")\n\nevoked.plot(axes=[ax2], time_unit=\"s\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}