.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_stats_plot_compare_cond_cluster.py: ======================================================================== Compare PAC of two experimental conditions with cluster-based statistics ======================================================================== This example illustrates how to statistically compare the phase-amplitude coupling results coming from two experimental conditions. In particular, the script below a the cluster-based approach to correct for the multiple comparisons. In order to work, this script requires MNE-Python package to be installed in order to perform the cluster-based correction (:func:`mne.stats.permutation_cluster_test`) .. code-block:: default import numpy as np from tensorpac import Pac from tensorpac.signals import pac_signals_wavelet from mne.stats import permutation_cluster_test import matplotlib.pyplot as plt Simulate the data coming from two experimental conditions ############################################################################## Let's start by simulating data coming from two experimental conditions. The first dataset is going to simulate a 10hz phase <-> 120hz amplitude coupling while the second dataset will not include any coupling (random data) .. code-block:: default # create the first dataset with a 10hz <-> 100hz coupling n_epochs = 30 # number of datasets sf = 512. # sampling frequency n_times = 4000 # Number of time points x_1, time = pac_signals_wavelet(sf=sf, f_pha=10, f_amp=120, noise=2., n_epochs=n_epochs, n_times=n_times) # create a second random dataset without any coupling x_2 = np.random.rand(n_epochs, n_times) Compute the single trial PAC on both datasets ############################################################################## once the datasets created, we can now extract the PAC, computed across time-points for each trials and across several phase and amplitude frequencies .. code-block:: default # create the pac object. Use the Gaussian-Copula PAC p = Pac(idpac=(6, 0, 0), f_pha='hres', f_amp='hres', dcomplex='wavelet') # compute pac for both dataset pac_1 = p.filterfit(sf, x_1, n_jobs=-1) pac_2 = p.filterfit(sf, x_2, n_jobs=-1) Correct for multiple-comparisons using a cluster-based approach ############################################################################## Then, we perform the cluster-based correction for multiple comparisons between the PAC coming from the two conditions. To this end we use the Python package MNE-Python and in particular, the function :func:`mne.stats.permutation_cluster_test` .. code-block:: default # mne requires that the first is represented by the number of trials (n_epochs) # Therefore, we transpose the output PACs of both conditions pac_r1 = np.transpose(pac_1, (2, 0, 1)) pac_r2 = np.transpose(pac_2, (2, 0, 1)) n_perm = 1000 # number of permutations tail = 1 # only inspect the upper tail of the distribution # perform the correction t_obs, clusters, cluster_p_values, h0 = permutation_cluster_test( [pac_r1, pac_r2], n_permutations=n_perm, tail=tail) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Using a threshold of 4.006873 /home/circleci/project/examples/stats/plot_compare_cond_cluster.py:71: DeprecationWarning: The default for "out_type" will change from "mask" to "indices" in version 0.22. To avoid this warning, explicitly set "out_type" to one of its string values. [pac_r1, pac_r2], n_permutations=n_perm, tail=tail) stat_fun(H1): min=0.000000 max=52.333180 Running initial clustering Found 13 clusters Permuting 999 times... 0%| | : 0/999 [00:00` .. container:: sphx-glr-download :download:`Download Jupyter notebook: plot_compare_cond_cluster.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_