示例#1
0
def test_dimensions(
    n_features,
    n_components,
    n_individual_components,
    multiview_output,
    prewhiten,
    solver,
):
    n_samples = 10
    n_sources = 3
    noise_level = 0.1
    rng = np.random.RandomState(0)
    Xs, sources, mixings = generate_signals(n_samples, n_sources, n_features,
                                            noise_level, rng)
    ica_kwargs = dict(tol=1)
    ica = GroupICA(
        n_components=n_components,
        n_individual_components=n_individual_components,
        multiview_output=multiview_output,
        prewhiten=prewhiten,
        solver=solver,
        ica_kwargs=ica_kwargs,
        random_state=rng,
    )
    ica.fit(Xs)
    if n_components is None:
        n_components = min(n_features)
    assert ica.mixing_.shape == (n_components, n_components)
    assert ica.components_.shape == (n_components, n_components)
    for i, n_feature in enumerate(n_features):
        assert ica.means_[i].shape == (n_feature, )
        assert ica.individual_components_[i].shape == (n_components, n_feature)
        assert ica.individual_mixing_[i].shape == (n_feature, n_components)
示例#2
0
def test_source_recovery(n_individual_components, multiview_output, solver):
    rng = np.random.RandomState(0)
    n_samples = 500
    n_sources = 2
    n_features = [2, 3, 4]
    noise_level = 0.01
    Xs, sources, mixings = generate_signals(
        n_samples, n_sources, n_features, noise_level, rng
    )
    ica = GroupICA(
        n_components=2,
        n_individual_components=n_individual_components,
        multiview_output=multiview_output,
        solver=solver,
        random_state=rng,
    )
    ica.fit(Xs)
    estimated_sources = ica.transform(Xs)
    estimated_mixings = ica.individual_mixing_
    if multiview_output:
        for s in estimated_sources:
            C = np.dot(s.T, sources)
            assert amari_d(C) < 1e-3
        for A, A_ in zip(mixings, estimated_mixings):
            assert amari_d(np.linalg.pinv(A), A_) < 1e-3
示例#3
0
def test_inverse_transform(n_individual_components, multiview_output, solver):
    rng = np.random.RandomState(0)
    n_samples = 500
    n_sources = 2
    n_features = [2, 3, 4]
    noise_level = 0.0001
    Xs, sources, mixings = generate_signals(n_samples, n_sources, n_features,
                                            noise_level, rng)
    ica = GroupICA(
        n_components=2,
        n_individual_components=n_individual_components,
        multiview_output=multiview_output,
        solver=solver,
        random_state=rng,
    )
    ica.fit(Xs)
    estimated_sources = ica.transform(Xs)
    recovered_signals = ica.inverse_transform(estimated_sources)
    for X, X_ in zip(recovered_signals, Xs):
        assert_allclose(X, X_, atol=1e-2)
示例#4
0
def test_transform(
    n_components, n_individual_components, multiview_output, solver, whiten
):
    ica_kwargs = dict(tol=1, whiten=whiten)
    ica = GroupICA(
        n_components=n_components,
        n_individual_components=n_individual_components,
        multiview_output=multiview_output,
        solver=solver,
        ica_kwargs=ica_kwargs,
    )
    rng = np.random.RandomState(0)
    n_samples = 100
    Xs, _, _ = generate_signals(n_samples, 2, [4, 5, 6], 0.1, rng)
    # check the shape of fit.transform
    X_r = ica.fit(Xs).transform(Xs)
    if multiview_output:
        for X in X_r:
            assert X.shape[0] == n_samples
            if n_components is not None:
                assert X.shape[1] == n_components
    else:
        assert X_r.shape[0] == n_samples
        if n_components is not None:
            assert X_r.shape[1] == n_components
    X_r2 = ica.transform(Xs)
    if multiview_output:
        for X, X2 in zip(X_r, X_r2):
            assert_allclose(X, X2)
    else:
        assert_allclose(X_r, X_r2)
    X_r = ica.fit_transform(Xs)
    X_r2 = ica.transform(Xs)
    if multiview_output:
        for X, X2 in zip(X_r, X_r2):
            assert_allclose(X, X2)
    else:
        assert_allclose(X_r, X_r2)
mixings = [np.random.randn(3, 3) for _ in range(n_views)]
Xs = [np.dot(S, A.T) + 0.3 * np.random.randn(n_samples, 3) for A in mixings]

# We can visualize one dataset: it looks quite messy.
plot_sources(Xs[0])

###############################################################################
# Apply Group ICA
# ---------------
#
# Next, we can apply group ICA. The option `multiview_output=False` means that
# we want to recover the estimated sources when we do `.transform`. Here, we
# look at what the algorithm estimates as the sources from the multiview
# data.

groupica = GroupICA(multiview_output=False).fit(Xs)

estimated_sources = groupica.transform(Xs)
plot_sources(estimated_sources)

###############################################################################
# Inspect Estimated Mixings
# -------------------------
#
# We see they look pretty good! We can also wheck that it has correctly
# predicted each mixing matrix. The estimated mixing matrices are stored in
# the `.individual_mixing_` attribute.
#
# If :math:`\tilde{A}` is the estimated mixing matrix and :math:`A` is the
# true mixing matrix, we can look at :math:`\tilde{A}^{-1}A`. It should be
# close to a scale and permuation matrix: in this case, the sources are
示例#6
0
def test_badsolver():
    with pytest.raises(ValueError):
        ica = GroupICA(solver="bla")