def test_pac_signals_dtrials(self):
     """Definition of artificially coupled signals using dPha/dAmp."""
     assert pac_signals_tort(f_pha=5,
                             f_amp=130,
                             sf=512,
                             n_epochs=23,
                             chi=0.9,
                             noise=2,
                             dpha=35,
                             damp=46)
 def test_pac_signals_bandwidth(self):
     """Definition of artificially coupled signals using bandwidth."""
     assert pac_signals_tort(f_pha=[5, 7],
                             f_amp=[30, 60],
                             sf=200.,
                             n_epochs=100,
                             chi=0.5,
                             noise=3.,
                             n_times=1000)
     assert pac_signals_wavelet(f_pha=10,
                                f_amp=57.,
                                n_times=1240,
                                sf=256,
                                noise=.7,
                                n_epochs=33,
                                pp=np.pi / 4,
                                rnd_state=23)
Exemple #3
0
(PSD) of an electrophysiological dataset. The PSD should be used to check that
there is the presence of a clear peak, espacially for frequency phase.
"""
from tensorpac.utils import PSD
from tensorpac.signals import pac_signals_tort

# Dataset of signals artificially coupled between 10hz and 100hz :
n_epochs = 20
n_times = 4000
sf = 512.  # sampling frequency

# Create artificially coupled signals using Tort method :
data, time = pac_signals_tort(f_pha=10,
                              f_amp=100,
                              noise=4,
                              n_epochs=n_epochs,
                              dpha=10,
                              damp=10,
                              sf=sf,
                              n_times=n_times)

# compute the PSD
psd = PSD(data, sf)

# plot the mean PSD across trials between [5, 150]Hz with a 95th confidence
# interval. Also, use a log x-scale
ax = psd.plot(confidence=95, f_min=5, f_max=150, log=True, grid=True)

# finally display the figure
psd.show()
and xamp have to be set to data.
"""
from tensorpac import Pac
from tensorpac.signals import pac_signals_tort

import matplotlib.pyplot as plt

plt.style.use('seaborn-poster')

# First, we generate 2 datasets of signals artificially coupled between 10hz
# and 100hz. By default, those datasets are organized as (n_epochs, n_times)
# where n_times is the number of time points.
n_epochs = 20  # number of datasets
d1, time = pac_signals_tort(f_pha=10,
                            f_amp=100,
                            noise=1,
                            n_epochs=n_epochs,
                            n_times=3000)
d2, time = pac_signals_tort(f_pha=10,
                            f_amp=100,
                            noise=3,
                            n_epochs=n_epochs,
                            dpha=20,
                            damp=5,
                            chi=.3,
                            n_times=3000)

# Define the model of PAC to use :
p = Pac(idpac=(4, 0, 0),
        f_pha=(2, 30, 1, 1),
        f_amp=(60, 150, 5, 5),
Exemple #5
0
Find the optimal bandwidth
==========================

Instead of looking for phase and amplitude frequency pairs (as in a
comodulogram) this example illustrate how it is possible to find starting,
ending and therefore, bandwidth coupling.
"""
from tensorpac import Pac
from tensorpac.signals import pac_signals_tort
from tensorpac.utils import pac_trivec

sf = 256.
data, time = pac_signals_tort(f_pha=[5, 7],
                              f_amp=[60, 80],
                              noise=2,
                              n_epochs=5,
                              n_times=3000,
                              sf=sf,
                              dpha=10)

trif, tridx = pac_trivec(f_start=30, f_end=140, f_width=3)

p = Pac(idpac=(1, 0, 0), f_pha=[5, 7], f_amp=trif)
pac = p.filterfit(sf, data)

p.triplot(pac.mean(-1),
          trif,
          tridx,
          cmap='Spectral_r',
          rmaxis=True,
          title=r'Optimal $[Fmin; Fmax]hz$ band for amplitude')
Exemple #6
0
"""
=====================================
Generate artificially coupled signals
=====================================

Use the pac_signals_tort function to generate artificial PAC.
"""
from tensorpac.signals import pac_signals_tort
import matplotlib.pyplot as plt

# Generate one signal containing PAC. By default, this signal present a
# coupling between a 2hz phase and a 100hz amplitude (2 <-> 100) :
sig, time = pac_signals_tort(n_epochs=1, n_times=1000)

# Now, we generate a longer and weaker 4 <-> 60 coupling using the chi
#  parameter. In addition, we increase the amount of noise :
sig2, time2 = pac_signals_tort(f_pha=4,
                               f_amp=60,
                               n_epochs=1,
                               chi=.9,
                               noise=3,
                               n_times=3000)

# Alternatively, you can generate multiple coupled signals :
sig3, time3 = pac_signals_tort(f_pha=10,
                               f_amp=150,
                               n_epochs=3,
                               chi=0.5,
                               noise=2)

# Finally, if you want to add variability across generated signals, use the
    plt.axis('tight')
    for loc, spine in ax.spines.items():
        if loc in torm:
            spine.set_color('none')
            ax.tick_params(**{loc: False})


def savefig(save_as):
    """Save the figure."""
    plt.savefig(f"{save_as}.pdf", dpi=600, bbox_inches='tight')


# random signals
data, time = pac_signals_tort(n_epochs=1,
                              f_pha=10,
                              f_amp=100,
                              sf=1024,
                              n_times=1025)
data = normalize(data, -1, 1)

# extract phase / amplitude
p = Pac(idpac=(1, 2, 3), f_pha=[9, 11], f_amp=[90, 110])
phase = p.filter(1024, data, ftype='phase')
x10hz = p.filter(1024, data, ftype='phase', keepfilt=True)
amplitude = p.filter(
    1024,
    data,
    ftype='amplitude',
)
x100hz = p.filter(1024, data, ftype='amplitude', keepfilt=True)
 def test_default_args(self):
     """Test default aurguments for pac_vec."""
     assert pac_signals_tort(chi=2., noise=11., dpha=120., damp=200.)
plt.style.use('seaborn-poster')
sns.set_style("whitegrid")
plt.rc('font', family=cfg['font'])


###############################################################################
sf = 512
f_pha, f_amp = 5, 120
n_epochs = 5
n_times = 1000
###############################################################################


# generate the random dataset
sig_t, time = pac_signals_tort(f_pha=f_pha, f_amp=f_amp, n_epochs=n_epochs,
                               noise=2, n_times=n_times, sf=sf)
sig_w, time = pac_signals_wavelet(sf=sf, f_pha=f_pha, f_amp=f_amp, noise=.5,
                                  n_epochs=n_epochs, n_times=n_times)

# compute pac
p = Pac(idpac=(6, 0, 0), f_pha=cfg["phres"], f_amp=cfg["ahres"],
        dcomplex='wavelet', width=12)
xpac_t = p.filterfit(sf, sig_t, n_jobs=1).mean(-1)
xpac_w = p.filterfit(sf, sig_w, n_jobs=1).mean(-1)

plt.figure(figsize=(17, 14))

# ----------------------------------------- Tort et al. 2010
plt.subplot(221)
plt.plot(time, sig_t[0, :], color='black', lw=.8)
plt.ylabel("Amplitude (V)")
"""
from __future__ import print_function

from tensorpac import Pac
from tensorpac.signals import pac_signals_tort

import matplotlib.pyplot as plt
plt.style.use('seaborn-paper')

# First, we generate a dataset of signals artificially coupled between 10hz
# and 100hz. By default, this dataset is organized as (n_epochs, n_times) where
# n_times is the number of time points.
n_epochs = 10  # number of datasets
sf = 512.  # sampling frequency
n_times = 4000  # Number of time points
data, time = pac_signals_tort(sf=sf, f_pha=5, f_amp=100, noise=2.,
                              n_epochs=n_epochs, n_times=n_times)

# First, let's use the MVL, without any further correction by surrogates :
p = Pac(f_pha=(2, 20, 1, 1), f_amp=(60, 150, 5, 5))

# 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', n_jobs=1)
amplitudes = p.filter(sf, data, ftype='amplitude', n_jobs=1)

plt.figure(figsize=(18, 9))
for i, k in enumerate([1, 2, 3, 4, 5, 6]):
    # Change the pac method :
    p.idpac = (k, 0, 0)
    print('-> PAC using ' + str(p))
    # Compute only the PAC without filtering :