Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# pairwise
p = PermutationDistance(100, mode='pairwise')
p.test(covset, labels)
# with group
p.test(covset, labels, groups=groups)
# t-test
p = PermutationDistance(100, mode='ttest')
p.test(covset, labels)
# f-test
p = PermutationDistance(100, mode='ftest')
p.test(covset, labels)
# with custom estimator
p = PermutationDistance(10, mode='pairwise', estimator=CSP(2, log=False))
p.test(covset, labels)
# unique perms
p = PermutationDistance(1000)
p.test(covset, labels)
p.plot(nbins=2)
def test_permutation_distance():
"""Test one way permutation test"""
covset = generate_cov(10, 5)
labels = np.array([0, 1]).repeat(5)
groups = np.array([0] * 5 + [1] * 5)
assert_raises(ValueError, PermutationDistance, mode='badmode')
# pairwise
p = PermutationDistance(100, mode='pairwise')
p.test(covset, labels)
# with group
p.test(covset, labels, groups=groups)
# t-test
p = PermutationDistance(100, mode='ttest')
p.test(covset, labels)
# f-test
p = PermutationDistance(100, mode='ftest')
p.test(covset, labels)
# with custom estimator
p = PermutationDistance(10, mode='pairwise', estimator=CSP(2, log=False))
p.test(covset, labels)
# unique perms
p = PermutationDistance(1000)
p.test(covset, labels)
p.plot(nbins=2)
def test_permutation_distance():
"""Test one way permutation test"""
covset = generate_cov(10, 5)
labels = np.array([0, 1]).repeat(5)
groups = np.array([0] * 5 + [1] * 5)
assert_raises(ValueError, PermutationDistance, mode='badmode')
# pairwise
p = PermutationDistance(100, mode='pairwise')
p.test(covset, labels)
# with group
p.test(covset, labels, groups=groups)
# t-test
p = PermutationDistance(100, mode='ttest')
p.test(covset, labels)
# f-test
p = PermutationDistance(100, mode='ftest')
p.test(covset, labels)
# with custom estimator
p = PermutationDistance(10, mode='pairwise', estimator=CSP(2, log=False))
p.test(covset, labels)
# unique perms
p = PermutationDistance(1000)
labels = np.array([0, 1]).repeat(5)
groups = np.array([0] * 5 + [1] * 5)
assert_raises(ValueError, PermutationDistance, mode='badmode')
# pairwise
p = PermutationDistance(100, mode='pairwise')
p.test(covset, labels)
# with group
p.test(covset, labels, groups=groups)
# t-test
p = PermutationDistance(100, mode='ttest')
p.test(covset, labels)
# f-test
p = PermutationDistance(100, mode='ftest')
p.test(covset, labels)
# with custom estimator
p = PermutationDistance(10, mode='pairwise', estimator=CSP(2, log=False))
p.test(covset, labels)
# unique perms
p = PermutationDistance(1000)
p.test(covset, labels)
p.plot(nbins=2)
cosp = CospCovariances(
window=128, overlap=0.98, fmin=fmin, fmax=fmax, fs=160.0)
covmats = cosp.fit_transform(epochs_data[:, ::4, :])
fr = np.fft.fftfreq(128)[0:64] * 160
fr = fr[(fr >= fmin) & (fr <= fmax)]
###############################################################################
# Pairwise distance based permutation test
###############################################################################
pv = []
Fv = []
# For each frequency bin, estimate the stats
t_init = time()
for i in range(covmats.shape[3]):
p_test = PermutationDistance(1000, metric='riemann', mode='pairwise')
p, F = p_test.test(covmats[:, :, :, i], labels, verbose=False)
pv.append(p)
Fv.append(F[0])
duration = time() - t_init
# plot result
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
sig = 0.05
axes.plot(fr, Fv, lw=2, c='k')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Score')
a = np.where(np.diff(np.array(pv) < sig))[0]
a = a.reshape(int(len(a) / 2), 2)
st = (fr[1] - fr[0]) / 2.0
for p in a:
duration = time() - t_init
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
p_test.plot(nbins=10, axes=axes)
plt.title('Pairwise distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()
###############################################################################
# F-test distance based permutation test
###############################################################################
t_init = time()
p_test = PermutationDistance(n_perms, metric='riemann', mode='ftest',
estimator=XdawnCovariances(2))
p, F = p_test.test(epochs_data, labels)
duration = time() - t_init
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
p_test.plot(nbins=10, axes=axes)
plt.title('Pairwise distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()
###############################################################################
# Classification based permutation test
###############################################################################
covest = Covariances()
Fs = 160
window = 2 * Fs
Nwindow = 20
Ns = epochs_data.shape[2]
step = int((Ns - window) / Nwindow)
time_bins = range(0, Ns - window, step)
pv = []
Fv = []
# For each frequency bin, estimate the stats
t_init = time()
for t in time_bins:
covmats = covest.fit_transform(epochs_data[:, ::1, t:(t + window)])
p_test = PermutationDistance(1000, metric='riemann', mode='pairwise')
p, F = p_test.test(covmats, labels, verbose=False)
pv.append(p)
Fv.append(F[0])
duration = time() - t_init
# plot result
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
sig = 0.05
times = np.array(time_bins) / float(Fs) + tmin
axes.plot(times, Fv, lw=2, c='k')
plt.xlabel('Time (sec)')
plt.ylabel('Score')
a = np.where(np.diff(np.array(pv) < sig))[0]
a = a.reshape(int(len(a) / 2), 2)
st = (times[1] - times[0]) / 2.0
duration = time() - t_init
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
p_test.plot(nbins=10, axes=axes)
plt.title('t-test distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()
###############################################################################
# F-test distance based permutation test
###############################################################################
t_init = time()
p_test = PermutationDistance(n_perms, metric='riemann', mode='ftest')
p, F = p_test.test(covmats, labels)
duration = time() - t_init
fig, axes = plt.subplots(1, 1, figsize=[6, 3], sharey=True)
p_test.plot(nbins=10, axes=axes)
plt.title('F-test distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()
###############################################################################
# Classification based permutation test
###############################################################################
clf = make_pipeline(CSP(4), LogisticRegression())