Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_CSP():
"""Test CSP"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
# Test Init
csp = CSP()
assert_true(csp.nfilter == 4)
assert_true(csp.metric == 'euclid')
assert_true(csp.log)
csp = CSP(3, 'riemann', False)
assert_true(csp.nfilter == 3)
assert_true(csp.metric == 'riemann')
assert_true(not csp.log)
assert_raises(TypeError, CSP, 'foo')
assert_raises(ValueError, CSP, metric='foo')
assert_raises(TypeError, CSP, log='foo')
# Test fit
csp = CSP()
csp.fit(X, labels % 2) # two classes
csp.fit(X, labels) # 3 classes
assert_raises(ValueError, csp.fit, X, labels * 0.) # 1 class
"""Test CSP"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
# Test Init
csp = CSP()
assert_true(csp.nfilter == 4)
assert_true(csp.metric == 'euclid')
assert_true(csp.log)
csp = CSP(3, 'riemann', False)
assert_true(csp.nfilter == 3)
assert_true(csp.metric == 'riemann')
assert_true(not csp.log)
assert_raises(TypeError, CSP, 'foo')
assert_raises(ValueError, CSP, metric='foo')
assert_raises(TypeError, CSP, log='foo')
# Test fit
csp = CSP()
csp.fit(X, labels % 2) # two classes
csp.fit(X, labels) # 3 classes
assert_raises(ValueError, csp.fit, X, labels * 0.) # 1 class
assert_raises(ValueError, csp.fit, X, labels[:1]) # unequal # of samples
assert_raises(TypeError, csp.fit, X, 'foo') # y must be an array
assert_raises(TypeError, csp.fit, 'foo', labels) # X must be an array
assert_raises(ValueError, csp.fit, X[:, 0], labels)
assert_raises(ValueError, csp.fit, X, X)
assert_array_equal(csp.filters_.shape, [X.shape[1], X.shape[1]])
assert_array_equal(csp.patterns_.shape, [X.shape[1], X.shape[1]])
def test_CSP():
"""Test CSP"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
# Test Init
csp = CSP()
assert_true(csp.nfilter == 4)
assert_true(csp.metric == 'euclid')
assert_true(csp.log)
csp = CSP(3, 'riemann', False)
assert_true(csp.nfilter == 3)
assert_true(csp.metric == 'riemann')
assert_true(not csp.log)
assert_raises(TypeError, CSP, 'foo')
assert_raises(ValueError, CSP, metric='foo')
assert_raises(TypeError, CSP, log='foo')
# Test fit
csp = CSP()
csp.fit(X, labels % 2) # two classes
csp.fit(X, labels) # 3 classes
assert_raises(ValueError, csp.fit, X, labels * 0.) # 1 class
assert_raises(ValueError, csp.fit, X, labels[:1]) # unequal # of samples
assert_raises(TypeError, csp.fit, X, 'foo') # y must be an array
assert_raises(TypeError, csp.fit, 'foo', labels) # X must be an array
assert_raises(ValueError, csp.fit, X[:, 0], labels)
def test_BilinearFilter():
"""Test Bilinear filter"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
filters = np.eye(3)
# Test Init
bf = BilinearFilter(filters)
assert_false(bf.log)
assert_raises(TypeError, BilinearFilter, 'foo')
assert_raises(TypeError, BilinearFilter, np.eye(3), log='foo')
# test fit
bf = BilinearFilter(filters)
bf.fit(X, labels % 2)
# Test transform
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0], filters.shape[0]])
assert_raises(TypeError, bf.transform, 'foo')
assert_raises(ValueError, bf.transform, X[:, 1:, :]) # unequal # of chans
bf.log = True
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0]])
filters = filters[0:2, :]
bf = BilinearFilter(filters)
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0], filters.shape[0]])
def test_BilinearFilter():
"""Test Bilinear filter"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
filters = np.eye(3)
# Test Init
bf = BilinearFilter(filters)
assert_false(bf.log)
assert_raises(TypeError, BilinearFilter, 'foo')
assert_raises(TypeError, BilinearFilter, np.eye(3), log='foo')
# test fit
bf = BilinearFilter(filters)
bf.fit(X, labels % 2)
# Test transform
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0], filters.shape[0]])
assert_raises(TypeError, bf.transform, 'foo')
assert_raises(ValueError, bf.transform, X[:, 1:, :]) # unequal # of chans
bf.log = True
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0]])
filters = filters[0:2, :]
def test_BilinearFilter():
"""Test Bilinear filter"""
n_trials = 90
X = generate_cov(n_trials, 3)
labels = np.array([0, 1, 2]).repeat(n_trials // 3)
filters = np.eye(3)
# Test Init
bf = BilinearFilter(filters)
assert_false(bf.log)
assert_raises(TypeError, BilinearFilter, 'foo')
assert_raises(TypeError, BilinearFilter, np.eye(3), log='foo')
# test fit
bf = BilinearFilter(filters)
bf.fit(X, labels % 2)
# Test transform
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0], filters.shape[0]])
assert_raises(TypeError, bf.transform, 'foo')
assert_raises(ValueError, bf.transform, X[:, 1:, :]) # unequal # of chans
bf.log = True
Xt = bf.transform(X)
assert_array_equal(Xt.shape, [len(X), filters.shape[0]])
# window length for calculating thresholds
N = int(np.round(win_len * sfreq))
if method == 'euclid':
U = np.zeros((blocksize, nc, nc))
for k in range(blocksize):
rangevect = np.minimum(ns - 1, np.arange(k, ns + k, blocksize))
x = X[:, rangevect]
U[k, ...] = x @ x.T
Uavg = block_geometric_median(U.reshape((-1, nc * nc)) / blocksize, 2)
Uavg = Uavg.reshape((nc, nc))
elif method == 'riemann':
blocksize = int(ns // blocksize)
U = block_covariance(X, window=blocksize, overlap=win_overlap)
Uavg = pyriemann.utils.mean.mean_covariance(U, metric='riemann')
# get the mixing matrix M
M = linalg.sqrtm(np.real(Uavg))
D, Vtmp = linalg.eig(M)
# D, Vtmp = nonlinear_eigenspace(M, nc)
V = Vtmp[:, np.argsort(D)]
# get the threshold matrix T
x = np.abs(np.dot(V, X))
offsets = np.int_(np.arange(0, ns - N, np.round(N * (1 - win_overlap))))
mu = np.zeros(nc)
sig = np.zeros(nc)
for ichan in range(nc):
rms = x[ichan, :] ** 2
Y = []
def test_MDM_predict():
"""Test prediction of MDM"""
covset = generate_cov(100, 3)
labels = np.array([0, 1]).repeat(50)
mdm = MDM(metric='riemann')
mdm.fit(covset, labels)
mdm.predict(covset)
# test fit_predict
mdm = MDM(metric='riemann')
mdm.fit_predict(covset, labels)
# test transform
mdm.transform(covset)
# predict proba
mdm.predict_proba(covset)
# test n_jobs
mdm = MDM(metric='riemann', n_jobs=2)
mdm.fit(covset, labels)
mdm.predict(covset)
mdm = MDM(metric='riemann')
mdm.fit(covset, labels)
mdm.predict(covset)
# test fit_predict
mdm = MDM(metric='riemann')
mdm.fit_predict(covset, labels)
# test transform
mdm.transform(covset)
# predict proba
mdm.predict_proba(covset)
# test n_jobs
mdm = MDM(metric='riemann', n_jobs=2)
mdm.fit(covset, labels)
mdm.predict(covset)
def test_TangentSpace_inversetransform_without_fit():
"""Test inverse transform of Tangent Space without fit."""
covset = generate_cov(10, 3)
ts = TangentSpace(metric='identity')
tsv = ts.fit_transform(covset)
ts = TangentSpace(metric='riemann')
cov = ts.inverse_transform(tsv)
assert_array_almost_equal(covset, cov)