def test_plv(): """ Test PAC function: PLV. 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using firfls filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose(plv(data, data, (13, 30), (80, 200)), 0.25114, atol=10**-5) assert np.allclose(plv(data, data, (13, 30), (80, 200), filterfn=firfls), 0.24715, atol=10**-5) # Test that the PLV function outputs close to 0 and 1 when expected lo, hi = genPAC1() assert plv(lo, hi, (4, 6), (90, 110)) > 0.99 lo, hi = genPAC0() assert plv(lo, hi, (4, 6), (90, 110)) < 0.01 # Test that Filterfn = False works as expected datalo = firf(data, (13, 30)) datahi = firf(data, (80, 200)) datahiamp = np.abs(hilbert(datahi)) datahiamplo = firf(datahiamp, (13, 30)) pha1 = np.angle(hilbert(datalo)) pha2 = np.angle(hilbert(datahiamplo)) pha1, pha2 = _trim_edges(pha1, pha2) assert np.allclose(plv(pha1, pha2, (13, 30), (80, 200), filterfn=False), plv(data, data, (13, 30), (80, 200)), atol=10**-5)
def test_plv(): """ Test PAC function: PLV. 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using firfls filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( plv(data, data, (13, 30), (80, 200)), 0.25114, atol=10 ** -5) assert np.allclose( plv(data, data, (13, 30), (80, 200), filterfn=firfls), 0.24715, atol=10 ** -5) # Test that the PLV function outputs close to 0 and 1 when expected lo, hi = genPAC1() assert plv(lo, hi, (4, 6), (90, 110)) > 0.99 lo, hi = genPAC0() assert plv(lo, hi, (4, 6), (90, 110)) < 0.01 # Test that Filterfn = False works as expected datalo = firf(data, (13, 30)) datahi = firf(data, (80, 200)) datahiamp = np.abs(hilbert(datahi)) datahiamplo = firf(datahiamp, (13, 30)) pha1 = np.angle(hilbert(datalo)) pha2 = np.angle(hilbert(datahiamplo)) pha1, pha2 = _trim_edges(pha1, pha2) assert np.allclose( plv(pha1, pha2, (13, 30), (80, 200), filterfn=False), plv(data, data, (13, 30), (80, 200)), atol=10 ** -5)
def test_glm(): """ Test PAC function: GLM 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using iir filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( glm(data, data, (13, 30), (80, 200)), 0.03191, atol=10 ** -5) assert np.allclose( glm(data, data, (13, 30), (80, 200), filterfn=butterf), 0.03476, atol=10 ** -5) # Test that the GLM function outputs close to 0 and 1 when expected lo, hi = genPAC1(glm_bias=True) assert glm(lo, hi, (4, 6), (90, 110)) > 0.99 lo, hi = genPAC0() assert glm(lo, hi, (4, 6), (90, 110)) < 0.01 # Test that Filterfn = False works as expected datalo = firf(data, (13,30)) datahi = firf(data, (80,200)) pha = np.angle(hilbert(datalo)) amp = np.abs(hilbert(datahi)) assert np.allclose( glm(pha, amp, (13, 30), (80, 200), filterfn=False), glm(data, data, (13, 30), (80, 200)), atol=10 ** -5)
def test_ozkurt(): """ Test PAC function: Ozkurt 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using iir filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( ozkurt(data, data, (13, 30), (80, 200)), 0.07548, atol=10 ** -5) assert np.allclose( ozkurt(data, data, (13, 30), (80, 200), filterfn=butterf), 0.07555, atol=10 ** -5) # Test that the Ozkurt PAC function outputs close to 0 and 1 when expected lo, hi = genPAC1(phabias=.2, fhi=300) hif = firf(hi, (100, 400)) amp = np.abs(hilbert(hif)) weight = (np.sqrt(len(amp)) * np.sqrt(np.sum(amp ** 2))) / np.sum(amp) assert ozkurt(lo, hi, (4, 6), (100, 400)) * weight > 0.99 lo, hi = genPAC0() assert ozkurt(lo, hi, (4, 6), (90, 110)) < 0.001 # Test that Filterfn = False works as expected datalo = firf(data, (13,30)) datahi = firf(data, (80,200)) pha = np.angle(hilbert(datalo)) amp = np.abs(hilbert(datahi)) assert np.allclose( ozkurt(pha, amp, (13, 30), (80, 200), filterfn=False), ozkurt(data, data, (13, 30), (80, 200)), atol=10 ** -5)
def test_mi_canolty(): """ Test PAC function: Canolty MI 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using iir filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( mi_canolty(data, data, (13, 30), (80, 200)), 1.10063, atol=10 ** -5) assert np.allclose(mi_canolty( data, data, (13, 30), (80, 200), filterfn=butterf), 1.14300, atol=10 ** -5) # Test that the Canolty MI function outputs close to 0 and 1 when expected lo, hi = genPAC1(phabias=.2, fhi=300) hif = firf(hi, (100, 400)) amp = np.abs(hilbert(hif)) assert mi_canolty(lo, hi, (4, 6), (100, 400)) / np.mean(amp) > 0.99 lo, hi = genPAC0() assert mi_canolty(lo, hi, (4, 6), (90, 110)) < 0.001 # Test that Filterfn = False works as expected datalo = firf(data, (13,30)) datahi = firf(data, (80,200)) pha = np.angle(hilbert(datalo)) amp = np.abs(hilbert(datahi)) assert np.allclose( mi_canolty(pha, amp, (13, 30), (80, 200), filterfn=False), mi_canolty(data, data, (13, 30), (80, 200)), atol=10 ** -5)
def test_firf(): """ Confirm consistency in FIR filtering """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose(np.sum(np.abs(firf(data, (13, 30)))), 6421935.78673, atol=10**-5) assert len(firf(data, (13, 30))) == len(data)
def test_firf(): """ Confirm consistency in FIR filtering """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + "/tests/exampledata.npy") assert np.allclose(np.sum(np.abs(firf(data, (13, 30)))), 5517466.5857, atol=10 ** -5)
def test_ozkurt(): """ Test PAC function: Ozkurt 1. Confirm consistency of output with example data 2. Confirm PAC=1 when expected 3. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( ozkurt(data, data, (13, 30), (80, 200)), 0.07658, atol=10 ** -5) # Test that the Ozkurt PAC function outputs close to 0 and 1 when expected lo, hi = genPAC1(phabias=.2, fhi=300) hif = firf(hi, (100, 400)) amp = np.abs(hilbert(hif)) weight = (np.sqrt(len(amp)) * np.sqrt(np.sum(amp ** 2))) / np.sum(amp) assert ozkurt(lo, hi, (4, 6), (100, 400)) * weight > 0.98 lo, hi = genPAC0() assert ozkurt(lo, hi, (4, 6), (100, 400)) < 0.01
def test_ozkurt(): """ Test PAC function: Ozkurt 1. Confirm consistency of output with example data 2. Confirm PAC=1 when expected 3. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose(ozkurt(data, data, (13, 30), (80, 200)), 0.07658, atol=10**-5) # Test that the Ozkurt PAC function outputs close to 0 and 1 when expected lo, hi = genPAC1(phabias=.2, fhi=300) hif = firf(hi, (100, 400)) amp = np.abs(hilbert(hif)) weight = (np.sqrt(len(amp)) * np.sqrt(np.sum(amp**2))) / np.sum(amp) assert ozkurt(lo, hi, (4, 6), (100, 400)) * weight > 0.98 lo, hi = genPAC0() assert ozkurt(lo, hi, (4, 6), (100, 400)) < 0.01
def test_mi_canolty(): """ Test PAC function: Canolty MI 1. Confirm consistency of output with example data 2. Confirm consistency of output with example data using iir filter 3. Confirm PAC=1 when expected 4. Confirm PAC=0 when expected """ # Load data data = np.load(os.path.dirname(pacpy.__file__) + '/tests/exampledata.npy') assert np.allclose( mi_canolty(data, data, (13, 30), (80, 200)), 1.10063, atol=10 ** -5) assert np.allclose(mi_canolty( data, data, (13, 30), (80, 200), filterfn=butterf), 1.14300, atol=10 ** -5) # Test that the Canolty MI function outputs close to 0 and 1 when expected lo, hi = genPAC1(phabias=.2, fhi=300) hif = firf(hi, (100, 400)) amp = np.abs(hilbert(hif)) assert mi_canolty(lo, hi, (4, 6), (100, 400)) / np.mean(amp) > 0.99 lo, hi = genPAC0() assert mi_canolty(lo, hi, (4, 6), (90, 110)) < 0.001
def findpt(x, f_osc, Fs=1000., w=3, boundary=0): """ Calculate peaks and troughs over time series Parameters ---------- x : array-like 1d voltage time series f_osc : (low, high), Hz frequency range for narrowband signal of interest, used to find zerocrossings of the oscillation Fs : float The sampling rate (default = 1000Hz) w : float Number of cycles for the filter order of the band-pass filter boundary : int distance from edge of recording that an extrema must be in order to be accepted (in number of samples) Returns ------- Ps : array-like 1d indices at which oscillatory peaks occur in the input signal x Ts : array-like 1d indices at which oscillatory troughs occur in the input signal x """ # Filter in narrow band from pacpy.filt import firf xn = firf(x, f_osc, Fs, w=w, rmvedge=False) # Find zero crosses def fzerofall(data): pos = data > 0 return (pos[:-1] & ~pos[1:]).nonzero()[0] def fzerorise(data): pos = data < 0 return (pos[:-1] & ~pos[1:]).nonzero()[0] zeroriseN = fzerorise(xn) zerofallN = fzerofall(xn) # Calculate # peaks and troughs if zeroriseN[-1] > zerofallN[-1]: P = len(zeroriseN) - 1 T = len(zerofallN) else: P = len(zeroriseN) T = len(zerofallN) - 1 # Calculate peak samples Ps = np.zeros(P, dtype=int) for p in range(P): # Calculate the sample range between the most recent zero rise # and the next zero fall mrzerorise = zeroriseN[p] nfzerofall = zerofallN[zerofallN > mrzerorise][0] Ps[p] = np.argmax(x[mrzerorise:nfzerofall]) + mrzerorise # Calculate trough samples Ts = np.zeros(T, dtype=int) for tr in range(T): # Calculate the sample range between the most recent zero fall # and the next zero rise mrzerofall = zerofallN[tr] nfzerorise = zeroriseN[zeroriseN > mrzerofall][0] Ts[tr] = np.argmin(x[mrzerofall:nfzerorise]) + mrzerofall if boundary > 0: Ps = _removeboundaryextrema(x, Ps, boundary) Ts = _removeboundaryextrema(x, Ts, boundary) return Ps, Ts