def nmppc(x, flo, fhi, nm, Fs): """ Calculate n:m phase-phase coupling between two oscillations Method from Palva et al., 2005 J Neuro * Morlet filter for the two frequencies * Use Hilbert to calculate phase and amplitude Parameters ---------- x : np array time series of interest flo : 2-element list low and high cutoff frequencies for the low frequency band of interest fhi : 2-element list low and high cutoff frequencies for the high frequency band of interest nm : 2-element list of ints (n,m) n:m is the ratio of low frequency to high frequency (e.g. if flo ~= 8 and fhi ~= 24, then n:m = 1:3) Fs : float Sampling rate Returns ------- plf : float n:m phase-phase coupling value (phase-locking factor) """ from pacpy.pac import pa_series, _trim_edges phalo, _ = pa_series(x, x, flo, flo, fs = Fs) phahi, _ = pa_series(x, x, fhi, fhi, fs = Fs) phalo, phahi = _trim_edges(phalo, phahi) phadiffnm = phalo*nm[1] - phahi*nm[0] plf = np.abs(np.mean(np.exp(1j*phadiffnm))) return plf
def test_paseries(): """ Test calculation of phase and amplitude time series """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') # Confirm returns correct size p, a = pa_series(data, data, (13, 30), (80, 200)) assert np.allclose(np.mean(a), 9.41637, atol=10**-5) assert np.allclose(p[0], -1.83601, atol=10**-5)
def test_paseries(): """ Test calculation of phase and amplitude time series """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') # Confirm returns correct size p, a = pa_series(data, data, (13, 30), (80, 200)) assert np.allclose(np.mean(a), 9.41637, atol=10 ** -5) assert np.allclose(p[0], -1.83601, atol=10 ** -5)
def _ampthresh(ampPC, x, fosc, Fs, Es, metric): """ Restrict metric to data with sufficiently high amplitude """ if ampPC > 0: from pacpy.pac import pa_series _, bamp = pa_series(x, x, fosc, fosc, fs=Fs) bamp = _edgeadd_paseries(bamp,fosc,Fs) bamp = bamp[Es] bampTH = np.percentile(bamp,ampPC) metric = metric[bamp>=bampTH] return metric
def _ampthresh(ampPC, x, fosc, Fs, Es, metric): """ Restrict metric to data with sufficiently high amplitude """ if ampPC > 0: from pacpy.pac import pa_series _, bamp = pa_series(x, x, fosc, fosc, fs=Fs) bamp = _edgeadd_paseries(bamp, fosc, Fs) bamp = bamp[Es] bampTH = np.percentile(bamp, ampPC) metric = metric[bamp >= bampTH] return metric
def _ampthresh(ampTH, x, fosc, Fs, Es, metric): """ Restrict data to the time points at which the extrema """ if ampTH > 0: from pacpy.pac import pa_series _, bamp = pa_series(x, x, fosc, fosc, fs=Fs) bamp = _edgeadd_paseries(bamp, fosc, Fs) bamp = bamp[Es] bampTH = np.percentile(bamp, ampTH) metric = metric[bamp >= bampTH] return metric
def test_paseries(): """ Test calculation of phase and amplitude time series """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') # Confirm returns correct size p, a = pa_series(data, data, (13, 30), (80, 200)) assert np.shape(p) == np.shape(data) assert np.shape(a) == np.shape(data) # Confirm consistency assert np.allclose(np.mean(a), 11.43970, atol=10 ** -5) assert np.allclose(p[0], 1.57119, atol=10 ** -5)
def test_padist(): """ Test calculation of amplitude distribution as a function of phase """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') np.random.seed(0) Nbins = np.random.randint(2, 20) pha, amp = pa_series(data, data, (13, 30), (80, 200)) dist = pa_dist(pha, amp, Nbins=Nbins) assert len(dist) == Nbins # Confirm consistency dist = pa_dist(pha, amp, Nbins=10) assert np.allclose(dist[0], 12.13961, atol=10 ** -5)