Example #1
0
def test_permutation_mode(mode, get_covmats, get_labels):
    """Test one way permutation test"""
    n_matrices, n_channels, n_classes = 6, 3, 2
    covmats = get_covmats(n_matrices, n_channels)
    labels = get_labels(n_matrices, n_classes)
    p = PermutationDistance(100, mode=mode)
    p.test(covmats, labels)
Example #2
0
def test_permutation_pairwise_unique(get_covmats, get_labels):
    """Test one way permutation with estimator"""
    n_matrices, n_channels, n_classes = 6, 3, 2
    covmats = get_covmats(n_matrices, n_channels)
    labels = get_labels(n_matrices, n_classes)
    # unique perms
    p = PermutationDistance(1000)
    p.test(covmats, labels)
Example #3
0
def test_permutation_pairwise_estimator(get_covmats, get_labels):
    """Test one way permutation with estimator"""
    n_matrices, n_channels, n_classes = 6, 3, 2
    covmats = get_covmats(n_matrices, n_channels)
    labels = get_labels(n_matrices, n_classes)
    # with custom estimator
    p = PermutationDistance(10, mode="pairwise", estimator=CSP(2, log=False))
    p.test(covmats, labels)
Example #4
0
def test_permutation_pairwise_plot(get_covmats, get_labels):
    """Test one way permutation with estimator"""
    n_matrices, n_channels, n_classes = 6, 3, 2
    covmats = get_covmats(n_matrices, n_channels)
    labels = get_labels(n_matrices, n_classes)
    p = PermutationDistance(100, mode="pairwise")
    p.test(covmats, labels)
    p.plot(nbins=2)
Example #5
0
def test_permutation_pairwise(get_covmats, get_labels):
    """Test one way permutation pairwise test"""
    n_matrices, n_channels, n_classes = 6, 3, 2
    covmats = get_covmats(n_matrices, n_channels)
    labels = get_labels(n_matrices, n_classes)
    groups = np.array([0] * 3 + [1] * 3)
    # pairwise
    p = PermutationDistance(100, mode="pairwise")
    p.test(covmats, labels)
    # with group
    p.test(covmats, labels, groups=groups)
Example #6
0
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)
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)
Example #8
0
                verbose=False)
labels = epochs.events[:, -1] - 2

# get epochs
epochs_data = epochs.get_data()

# compute covariance matrices
covmats = Covariances().fit_transform(epochs_data)

n_perms = 500
###############################################################################
# Pairwise distance based permutation test
###############################################################################

t_init = time()
p_test = PermutationDistance(n_perms, metric='riemann', mode='pairwise')
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('Pairwise distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()

###############################################################################
# t-test distance based permutation test
###############################################################################
Example #9
0
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
Example #10
0
def test_permutation_distance():
    """Test one way permutation test"""
    covset = generate_cov(10, 5)
    labels = np.array([0, 1]).repeat(5)
    assert_raises(ValueError, PermutationDistance, mode='badmode')
    # pairwise
    p = PermutationDistance(100, mode='pairwise')
    p.test(covset, labels)
    # 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)
Example #11
0
def test_permutation_badmode():
    """Test one way permutation test"""
    with pytest.raises(ValueError):
        PermutationDistance(mode="badmode")
                baseline=None, preload=True, verbose=False)
labels = epochs.events[:, -1] - 2

# get epochs
epochs_data = epochs.get_data()

# compute covariance matrices
covmats = Covariances().fit_transform(epochs_data)

n_perms = 500
###############################################################################
# Pairwise distance based permutation test
###############################################################################

t_init = time()
p_test = PermutationDistance(n_perms, metric='riemann', mode='pairwise')
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('Pairwise distance - %.2f sec.' % duration)
print('p-value: %.3f' % p)
sns.despine()
plt.tight_layout()
plt.show()

###############################################################################
# t-test distance based permutation test
###############################################################################