Exemplo n.º 1
0
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 csp.nfilter == 4
    assert csp.metric == 'euclid'
    assert csp.log
    csp = CSP(3, 'riemann', False)
    assert csp.nfilter == 3
    assert csp.metric == 'riemann'
    assert not csp.log

    with pytest.raises(TypeError):
        CSP('foo')

    with pytest.raises(ValueError):
        CSP(metric='foo')

    with pytest.raises(TypeError):
        CSP(log='foo')

    # Test fit
    csp = CSP()
    csp.fit(X, labels % 2)  # two classes
    csp.fit(X, labels)  # 3 classes

    with pytest.raises(ValueError):
        csp.fit(X, labels * 0.)  # 1 class
    with pytest.raises(ValueError):
        csp.fit(X, labels[:1])  # unequal # of samples
    with pytest.raises(TypeError):
        csp.fit(X, 'foo')  # y must be an array
    with pytest.raises(TypeError):
        csp.fit('foo', labels)  # X must be an array
    with pytest.raises(ValueError):
        csp.fit(X[:, 0], labels)
    with pytest.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]])

    # Test transform
    Xt = csp.transform(X)
    assert_array_equal(Xt.shape, [len(X), X.shape[1]])

    with pytest.raises(TypeError):
        csp.transform('foo')
    with pytest.raises(ValueError):
        csp.transform(X[:, 1:, :])  # unequal # of chans

    csp.log = False
    Xt = csp.transform(X)
Exemplo n.º 2
0
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)
    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]])

    # Test transform
    Xt = csp.transform(X)
    assert_array_equal(Xt.shape, [len(X), X.shape[1]])
    assert_raises(TypeError, csp.transform, 'foo')
    assert_raises(ValueError, csp.transform, X[:, 1:, :])  # unequal # of chans
    csp.log = False
    Xt = csp.transform(X)