def test_filterfit(self): """Test filtering test computing PAC.""" data = np.random.rand(2, 1024) p = Pac(idpac=(4, 0, 0)) p.filterfit(1024, data, n_jobs=1) p.idpac = (1, 1, 1) p.filterfit(1024, data, data, n_jobs=1, n_perm=2) p.dcomplex = 'wavelet' p.filter(1024, data, n_jobs=1)
def test_pac_meth(): """Test all Pac methods.""" pha = np.random.rand(2, 7, 1024) amp = np.random.rand(3, 7, 1024) nmeth, nsuro, nnorm = 5, 5, 5 p = Pac() for k in range(nmeth): for i in range(nsuro): for j in range(nnorm): p.idpac = (k + 1, i, j) p.fit(pha, amp, axis=2, traxis=1, njobs=1, nperm=2)
def test_fit(self): """Test all Pac methods.""" pha = np.random.rand(2, 7, 1024) amp = np.random.rand(3, 7, 1024) nmeth, nsuro, nnorm = 5, 4, 5 p = Pac() for k in range(nmeth): for i in range(nsuro): for j in range(nnorm): p.idpac = (k + 1, i, j) p.fit(pha, amp, n_jobs=1, n_perm=10)
def test_functional_pac(self): """Test functionnal pac.""" # generate a 10<->100hz ground truth coupling n_epochs, sf, n_times = 1, 512., 4000 data, time = pac_signals_wavelet(f_pha=10, f_amp=100, noise=.8, n_epochs=n_epochs, n_times=n_times, sf=sf) # phase / amplitude extraction (single time) p = Pac(f_pha='lres', f_amp='lres', dcomplex='wavelet', width=12) phases = p.filter(sf, data, ftype='phase', n_jobs=1) amplitudes = p.filter(sf, data, ftype='amplitude', n_jobs=1) # ground truth array construction n_pha, n_amp = len(p.xvec), len(p.yvec) n_pix = int(n_pha * n_amp) gt = np.zeros((n_amp, n_pha), dtype=bool) b_pha = np.abs(p.xvec.reshape(-1, 1) - np.array([[9, 11]])).argmin(0) b_amp = np.abs(p.yvec.reshape(-1, 1) - np.array([[95, 105]])).argmin(0) gt[b_amp[0]:b_amp[1] + 1, b_pha[0]:b_pha[1] + 1] = True plt.figure(figsize=(12, 9)) plt.subplot(2, 3, 1) p.comodulogram(gt, title='Gound truth', cmap='magma', colorbar=False) # loop over implemented methods for i, k in enumerate([1, 2, 3, 5, 6]): # compute only the pac p.idpac = (k, 2, 3) xpac = p.fit(phases, amplitudes, n_perm=200).squeeze() pval = p.pvalues.squeeze() is_coupling = pval <= .05 # count the number of correct pixels. This includes both true # positives and true negatives acc = 100 * (is_coupling == gt).sum() / n_pix assert acc > 95. # build title of the figure (for sanity check) meth = p.method.replace(' (', '\n(') title = f"Method={meth}\nAccuracy={np.around(acc, 2)}%" # set to nan everywhere it's not significant xpac[~is_coupling] = np.nan vmin, vmax = np.nanmin(xpac), np.nanmax(xpac) # plot the results plt.subplot(2, 3, i + 2) p.comodulogram(xpac, colorbar=False, vmin=vmin, vmax=vmax, title=title) plt.ylabel(''), plt.xlabel('') plt.tight_layout() plt.show() # show on demand
def test_fit(self): """Test all Pac methods.""" pha = np.random.rand(2, 7, 1024) amp = np.random.rand(3, 7, 1024) nmeth, nsuro, nnorm = 5, 4, 5 p = Pac(verbose=False) for k in range(nmeth): for i in range(nsuro): for j in range(nnorm): p.idpac = (k + 1, i, j) p.fit(pha, amp, n_jobs=1, n_perm=10) if (i >= 1) and (k + 1 != 4): for mcp in ['maxstat', 'fdr', 'bonferroni']: p.infer_pvalues(mcp=mcp)
def test_properties(self): """Test Pac properties.""" p = Pac() # Idpac : p.idpac p.idpac = (2, 1, 1) # Dcomplex : p.dcomplex p.dcomplex = 'wavelet' # Cycle : p.cycle p.cycle = (12, 24) # Width : p.width p.width = 12
def test_properties(self): """Test Pac properties.""" p = Pac() # Idpac : p.idpac p.idpac = (2, 1, 1) # Filt : p.filt p.filt = 'butter' # Dcomplex : p.dcomplex p.dcomplex = 'wavelet' # Cycle : p.cycle p.cycle = (12, 24) # Filtorder : p.filtorder p.filtorder = 6 # Width : p.width p.width = 12
# npts is the number of time points. n = 10 # number of datasets npts = 4000 # number of time points data, time = pac_signals_tort(fpha=10, famp=100, noise=3., ntrials=n, npts=npts, dpha=10, damp=5, chi=.8) # First, let's use the MVL, without any further correction by surrogates : p = Pac(idpac=(1, 0, 0), fpha=(2, 30, 1, .5), famp=(60, 150, 10, 1)) xpac = p.filterfit(1024, data, axis=1) t1 = p.method + '\n' + p.surro + '\n' + p.norm # Now, we still use the MVL method, but in addition we shuffle amplitude time # series and then, subtract then divide by the mean of surrogates : p.idpac = (1, 1, 1) xpac_corr = p.filterfit(1024, data, data, axis=1, nperm=10) t2 = p.method + '\n' + p.surro + '\n' + p.norm # Now, we plot the result by taking the mean across the dataset dimension. plt.figure(figsize=(20, 7)) plt.subplot(1, 2, 1) p.comodulogram(xpac.mean(-1), title=t1) plt.subplot(1, 2, 2) p.comodulogram(xpac_corr.mean(-1), title=t2) plt.show()
plt.style.use('seaborn-paper') # First, we generate a delta <-> low-gamma coupling. By default, this dataset # is organized as (n_epochs, n_times) where n_times is the number of time # points. n_epochs = 20 # number of datasets sf = 512. # sampling frequency data, time = pac_signals_wavelet(sf=sf, f_pha=6, f_amp=70, noise=3., n_epochs=n_epochs, n_times=4000) # First, let's use the MVL, without any further correction by surrogates : p = Pac(f_pha=(3, 10, 1, .2), f_amp=(50, 90, 5, 1), dcomplex='wavelet', width=12) # Now, we want to compare PAC methods, hence it's useless to systematically # filter the data. So we extract the phase and the amplitude only once : phases = p.filter(sf, data, ftype='phase') amplitudes = p.filter(sf, data, ftype='amplitude') plt.figure(figsize=(16, 12)) for i, k in enumerate(range(4)): # Change the pac method : p.idpac = (5, k, 1) # Compute only the PAC without filtering : xpac = p.fit(phases, amplitudes, n_perm=10) # Plot : plt.subplot(2, 2, k + 1) p.comodulogram(xpac.mean(-1), title=p.str_surro, cmap='Reds', vmin=0) plt.show()
# to extract all of the phases and amplitudes only once # define a pac object and extract high-resolution phases and amplitudes using # Morlet's wavelets p = Pac(f_pha='hres', f_amp='hres', dcomplex='wavelet') # etract all of the phases and amplitudes phases = p.filter(sf, data, ftype='phase', n_jobs=1) amplitudes = p.filter(sf, data, ftype='amplitude', n_jobs=1) ############################################################################### # Compute, plot and compare PAC ############################################################################### # Once all of the phases and amplitudes extracted we can compute PAC by # ietrating over the implemented methods. plt.figure(figsize=(14, 8)) for i, k in enumerate([1, 2, 3, 4, 5, 6]): # switch method of PAC p.idpac = (k, 0, 0) # compute only the pac without filtering xpac = p.fit(phases, amplitudes) # plot plt.subplot(2, 3, k) title = p.method.replace(' (', f' ({k})\n(') p.comodulogram(xpac.mean(-1), title=title, cmap='viridis') if k <= 3: plt.xlabel('') plt.tight_layout() plt.show()
# Test if the alpha-gamma PAC is significant during motor planning ############################################################################### # finally, here, we are going to test if the peak PAC that is occurring during # the planning period is significantly different for a surrogate distribution. # To this end, and as recommended by Aru et al. 2015, :cite:`aru2015untangling` # the surrogate distribution is obtained by cutting an amplitude at a random # time-point and then swap the two blocks of amplitudes (Bahramisharif et al. # 2013, :cite:`bahramisharif2013propagating`). This procedure is then repeated # multiple times (e.g 200 or 1000 times) in order to obtained the distribution. # Finally, the p-value is inferred by computing the proportion exceeded by the # true coupling. In addition, the correction for multiple comparison is # obtained using the FDR. # still using the Gaussian-Copula PAC but this time, we also select the method # for computing the permutations p_obj.idpac = (6, 2, 0) # compute pac and 200 surrogates pac_prep = p_obj.fit(pha_p[..., time_prep], amp_p[..., time_prep], n_perm=200, random_state=0) # get the p-values mcp = 'maxstat' pvalues = p_obj.infer_pvalues(p=0.05, mcp=mcp) ############################################################################### # sphinx_gallery_thumbnail_number = 7 plt.figure(figsize=(8, 6)) title = (r"Significant alpha$\Leftrightarrow$gamma coupling occurring during " f"the motor planning phase\n(p<0.05, {mcp}-corrected for multiple "
amplitudes = p.filter(sf, data, ftype='amplitude', n_jobs=1) # ground truth array construction n_pha, n_amp = len(p.xvec), len(p.yvec) n_pix = int(n_pha * n_amp) gt = np.zeros((n_amp, n_pha), dtype=bool) b_pha = np.abs(p.xvec.reshape(-1, 1) - np.array([[9, 11]])).argmin(0) b_amp = np.abs(p.yvec.reshape(-1, 1) - np.array([[95, 105]])).argmin(0) gt[b_amp[0]:b_amp[1] + 1, b_pha[0]:b_pha[1] + 1] = True plt.figure(figsize=(12, 9)) plt.subplot(2, 3, 1) p.comodulogram(gt, title='Gound truth', cmap='magma', colorbar=False) # loop over implemented methods for i, k in enumerate([1, 2, 3, 5, 6]): # compute only the pac p.idpac = (k, 2, 3) xpac = p.fit(phases, amplitudes, n_perm=200).squeeze() pval = p.pvalues.squeeze() is_coupling = pval <= .05 # count the number of correct pixels. This includes both true # positives and true negatives trpr = (is_coupling == gt).sum() / n_pix assert trpr > .95 # build title of the figure (for sanity check) meth = p.method.replace(' (', '\n(') title = f"Method={meth}\nAccuracy={np.around(trpr * 100, 2)}%" # set to nan everywhere it's not significant xpac[~is_coupling] = np.nan vmin, vmax = np.nanmin(xpac), np.nanmax(xpac) # plot the results plt.subplot(2, 3, i + 2)
f_pha=10, f_amp=100, noise=2., n_epochs=n_epochs, n_times=n_times) alphabet = string.ascii_uppercase p = Pac(idpac=(1, 2, 3), f_pha='hres', f_amp='hres') pha = p.filter(sf, data, ftype='phase', n_jobs=1) amp = p.filter(sf, data, ftype='amplitude', n_jobs=1) plt.figure(figsize=(18, 10)) gs = GridSpec(2, 7) for k in range(6): p.idpac = (k + 1, 2, 3) xpac = p.fit(pha.copy(), amp.copy(), n_perm=n_perm, p=.05, n_jobs=-1, random_state=k).mean(-1) sq = methods[titles[k]] plt.subplot(gs[sq[0], sq[1]]) p.comodulogram(xpac, cblabel="", title=p.method.replace(' (', '\n('), cmap=cfg["cmap"], vmin=0, colorbar=True,
data, time = pac_signals_wavelet(sf=sf, fpha=10, famp=100, noise=1., ntrials=n, npts=2000) # First, let's use the MVL, without any further correction by surrogates : p = Pac(fpha=(5, 16, 1, .1), famp=(80, 130, 5, 2), dcomplex='wavelet', width=12) # Now, we want to compare PAC methods, hence it's useless to systematically # filter the data. So we extract the phase and the amplitude only once : phases = p.filter(sf, data, axis=1, ftype='phase') amplitudes = p.filter(sf, data, axis=1, ftype='amplitude') plt.figure(figsize=(18, 9)) for i, k in enumerate(range(5)): # Change the pac method : p.idpac = (1, 4, k) print('-> Normalization using ' + p.norm) # Compute only the PAC without filtering : xpac = p.fit(phases, amplitudes, axis=2, nperm=100) # Plot : plt.subplot(2, 3, k + 1) p.comodulogram(xpac.mean(-1), title=p.norm, cmap='Spectral_r') plt.show()
# n_times is the number of time points. n_epochs = 10 # number of datasets sf = 512. # sampling frequency data, time = pac_signals_wavelet(sf=sf, f_pha=10, f_amp=100, noise=1., n_epochs=n_epochs, n_times=2000) # First, let's use the MVL, without any further correction by surrogates : p = Pac(f_pha=(5, 16, 1, .1), f_amp=(80, 130, 5, 2)) # Now, we want to compare PAC methods, hence it's useless to systematically # filter the data. So we extract the phase and the amplitude only once : phases = p.filter(sf, data, ftype='phase') amplitudes = p.filter(sf, data, ftype='amplitude') plt.figure(figsize=(18, 9)) for i, k in enumerate(range(5)): # Change the pac method : p.idpac = (1, 2, k) print('-> Normalization using ' + p.str_norm) # Compute only the PAC without filtering : xpac = p.fit(phases, amplitudes, n_perm=20) # Plot : plt.subplot(2, 3, k + 1) p.comodulogram(xpac.mean(-1), title=p.str_norm, cmap='Spectral_r') plt.show()