Example #1
0
def test_normalize_values(rndstate, get_covmats):
    """Test normalize values"""
    n_matrices, n_channels = 20, 3

    # after corr-normalization => diags = 1 and values in [-1, 1]
    mat = get_covmats(n_channels, n_channels)
    mat_cn = normalize(mat, "corr")
    assert_array_almost_equal(np.ones(mat_cn.shape[:-1]),
                              np.diagonal(mat_cn, axis1=-2, axis2=-1))
    assert np.all(-1 <= mat_cn) and np.all(mat_cn <= 1)

    # after trace-normalization => trace equal to 1
    mat = rndstate.randn(n_matrices, n_channels, n_channels)
    mat_tn = normalize(mat, "trace")
    assert_array_almost_equal(np.ones(mat_tn.shape[0]),
                              np.trace(mat_tn, axis1=-2, axis2=-1))

    # after determinant-normalization => determinant equal to +/- 1
    mat_dn = normalize(mat, "determinant")
    assert_array_almost_equal(np.ones(mat_dn.shape[0]),
                              np.abs(np.linalg.det(mat_dn)))

    with pytest.raises(ValueError):  # not at least 2d
        normalize(rndstate.randn(n_channels), "trace")
    with pytest.raises(ValueError):  # not square
        shape = (n_matrices, n_channels, n_channels + 2)
        normalize(rndstate.randn(*shape), "trace")
    with pytest.raises(ValueError):  # invalid normalization type
        normalize(rndstate.randn(n_matrices, n_channels, n_channels), "abc")
Example #2
0
def test_normalize_shapes(norm, rndstate):
    """Test normalize shapes"""
    n_conds, n_matrices, n_channels = 15, 10, 3

    # test a 2d array, ie a single square matrix
    mat = rndstate.randn(n_channels, n_channels)
    mat_n = normalize(mat, norm)
    assert mat.shape == mat_n.shape
    # test a 3d array, ie a group of square matrices
    mat = rndstate.randn(n_matrices, n_channels, n_channels)
    mat_n = normalize(mat, norm)
    assert mat.shape == mat_n.shape
    # test a 4d array, ie a group of groups of square matrices
    mat = rndstate.randn(n_conds, n_matrices, n_channels, n_channels)
    mat_n = normalize(mat, norm)
    assert mat.shape == mat_n.shape
                  n_potatoes=len(rpf_config))

# EEG processing for RPF
rpf_covs = []
for p in rpf_config.values():  # loop on potatoes
    rpf_sig = filter_bandpass(raw,
                              p.get('low_freq'),
                              p.get('high_freq'),
                              channels=p.get('ch_names'))
    rpf_epochs = make_fixed_length_epochs(rpf_sig,
                                          duration=duration,
                                          overlap=duration - interval,
                                          verbose=False)
    covs_ = Covariances(estimator='scm').transform(rpf_epochs.get_data())
    if p.get('cov_normalization'):
        covs_ = normalize(covs_, p.get('cov_normalization'))
    rpf_covs.append(covs_)

# RPF training
rpf.fit([c[train_set] for c in rpf_covs])

###############################################################################
# Online Artifact Detection with Potatoes
# ---------------------------------------
#
# Detect artifacts/outliers on test set, with an animation to imitate an online
# acquisition, processing and artifact detection of EEG time-series.
# Remark that all these potatoes are semi-dynamic: they are updated when EEG is
# not artifacted [1]_.

# Prepare data for online detection
def test_normalize(rndstate):
    """Test normalize"""
    n_conds, n_matrices, n_channels = 15, 20, 3

    # test a 2d array, ie a single square matrix
    mat = rndstate.randn(n_channels, n_channels)
    mat_n = normalize(mat, "trace")
    assert mat.shape == mat_n.shape
    # test a 3d array, ie a group of square matrices
    mat = rndstate.randn(n_matrices, n_channels, n_channels)
    mat_n = normalize(mat, "determinant")
    assert mat.shape == mat_n.shape
    # test a 4d array, ie a group of groups of square matrices
    mat = rndstate.randn(n_conds, n_matrices, n_channels, n_channels)
    mat_n = normalize(mat, "trace")
    assert mat.shape == mat_n.shape

    # after trace-normalization => trace equal to 1
    mat = rndstate.randn(n_matrices, n_channels, n_channels)
    mat_tn = normalize(mat, "trace")
    assert_array_almost_equal(np.ones(mat_tn.shape[0]),
                              np.trace(mat_tn, axis1=-2, axis2=-1))
    # after determinant-normalization => determinant equal to +/- 1
    mat_dn = normalize(mat, "determinant")
    assert_array_almost_equal(np.ones(mat_dn.shape[0]),
                              np.abs(np.linalg.det(mat_dn)))

    with pytest.raises(ValueError):  # not at least 2d
        normalize(rndstate.randn(n_channels), "trace")
    with pytest.raises(ValueError):  # not square
        shape = (n_matrices, n_channels, n_channels + 2)
        normalize(rndstate.randn(*shape), "trace")
    with pytest.raises(ValueError):  # invalid normalization type
        normalize(rndstate.randn(n_matrices, n_channels, n_channels), "abc")