Beispiel #1
0
def test_transform(Xs):
    ica = MultiviewICA(n_components=2)
    with pytest.raises(ValueError):
        ica.transform(Xs)
    assert(ica.fit_transform(Xs).shape == (Xs.shape[0], Xs.shape[1], 2))

    ica = MultiviewICA()
    assert(ica.fit_transform(Xs).shape == Xs.shape)
Beispiel #2
0
def test_fit_errors(Xs):
    with pytest.raises(ValueError):
        ica = MultiviewICA()
        ica.fit(Xs[:,:5, :])
    with pytest.raises(ValueError):
        ica = MultiviewICA(init='WRONG')
        ica.fit(Xs)
    with pytest.raises(TypeError):
        ica = MultiviewICA(init=list())
        ica.fit(Xs)
Beispiel #3
0
def test_inverse_transform(Xs):
    ica = MultiviewICA(n_components=2)
    with pytest.raises(ValueError):
        ica.transform(Xs)
    ica = ica.fit(Xs)
    Xs_mixed = ica.inverse_transform()
    avg_mixed = np.mean([X @ C for X,C in zip(Xs, ica.components_)], axis=0)
    for X_mixed in Xs_mixed:
        assert np.linalg.norm(X_mixed - avg_mixed) < 0.5
Beispiel #4
0
def test_inverse_transform(Xs, multiview_output):
    ica = MultiviewICA(n_components=2, multiview_output=multiview_output)
    with pytest.raises(ValueError):
        ica.inverse_transform(Xs)
    S = ica.fit_transform(Xs)
    Xs_mixed = ica.inverse_transform(S)
    avg_mixed = np.mean(
        [X @ np.linalg.pinv(C) for X, C in zip(Xs, ica.pca_components_)],
        axis=0,
    )
    avg_mixed2 = np.mean(
        [X @ np.linalg.pinv(C) for X, C in zip(Xs_mixed, ica.pca_components_)],
        axis=0,
    )
    assert np.linalg.norm(avg_mixed2 - avg_mixed) < 0.2
Beispiel #5
0
def test_index(index, inverse_index, multiview_output):
    # Test that the projection of data can be inverted
    rng = np.random.RandomState(0)
    n, p = 50, 3
    X = rng.randn(n, p)  # spherical data
    X[:, 1] *= 0.00001  # make middle component relatively small
    X += [5, 4, 3]  # make a large mean

    X2 = np.copy(X)
    X2[:, 1] += rng.randn(n) * 0.00001
    X2 = X2.dot(rng.rand(p, p))

    X3 = np.copy(X)
    X3[:, 1] += rng.randn(n) * 0.00001
    X3 = X3.dot(rng.rand(p, p))

    Xs = [X, X2, X3]
    ica = MultiviewICA(
        n_components=2,
        init="groupica",
        multiview_output=multiview_output,
    ).fit(Xs)
    if index is not None:
        index_ = np.atleast_1d(index)
        Xs_transform = [Xs[i] for i in index_]
        len_index = len(index_)
    else:
        len_index = 3
        Xs_transform = np.copy(Xs)

    if inverse_index is not None:
        inverse_index_ = np.atleast_1d(inverse_index)
        Xs_inverse = [Xs[i] for i in inverse_index_]
        len_inverse_index = len(inverse_index_)
    else:
        len_inverse_index = 3
        Xs_inverse = np.copy(Xs)

    Y = ica.transform(Xs_transform, index=index)
    Y_inverse = ica.inverse_transform(Y, index=inverse_index)
    for X, X_estimated in zip(Xs_inverse, Y_inverse):
        np.testing.assert_allclose(X, X_estimated, atol=1e-3)
Beispiel #6
0
def test_fit(Xs, capfd):
    ica = MultiviewICA(verbose=True)
    ica.fit(Xs)
    out, err = capfd.readouterr()
    assert out[:2] == "it"
Beispiel #7
0
def test_inverse_transform_no_preproc(Xs):
    ica = MultiviewICA()
    S = ica.fit_transform(Xs)
    Xs_mixed = ica.inverse_transform(S)
    assert np.mean((Xs_mixed - Xs)**2) / np.mean(Xs**2) < 0.05