コード例 #1
0
def test_weighted_GCCA_methods():
    # TODO we have view weighted GCCA and missing observation GCCA
    latent_dims = 2
    c = 0
    unweighted_gcca = GCCA(latent_dims=latent_dims, c=[c, c]).fit([X, Y])
    deweighted_gcca = GCCA(latent_dims=latent_dims,
                           c=[c, c],
                           view_weights=[0.5, 0.5]).fit([X, Y])
    corr_unweighted_gcca = unweighted_gcca.score((X, Y))
    corr_deweighted_gcca = deweighted_gcca.score((X, Y))
    # Check the correlations from each unregularized method are the same
    K = np.ones((2, X.shape[0]))
    K[0, 200:] = 0
    unobserved_gcca = GCCA(latent_dims=latent_dims, c=[c, c]).fit((X, Y), K=K)
    assert (np.testing.assert_array_almost_equal(
        corr_unweighted_gcca, corr_deweighted_gcca, decimal=1) is None)
コード例 #2
0
def test_unregularized_multi():
    # Tests unregularized CCA methods for more than 2 views. The idea is that all of these should give the same result.
    latent_dims = 2
    cca = rCCA(latent_dims=latent_dims).fit((X, Y, Z))
    iter = CCA_ALS(latent_dims=latent_dims,
                   stochastic=False,
                   tol=1e-12,
                   random_state=rng).fit((X, Y, Z))
    gcca = GCCA(latent_dims=latent_dims).fit((X, Y, Z))
    mcca = MCCA(latent_dims=latent_dims).fit((X, Y, Z))
    kcca = KCCA(latent_dims=latent_dims).fit((X, Y, Z))
    corr_cca = cca.score((X, Y, Z))
    corr_iter = iter.score((X, Y, Z))
    corr_gcca = gcca.score((X, Y, Z))
    corr_mcca = mcca.score((X, Y, Z))
    corr_kcca = kcca.score((X, Y, Z))
    # Check the correlations from each unregularized method are the same
    assert np.testing.assert_array_almost_equal(corr_cca, corr_iter,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_mcca,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_gcca,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_kcca,
                                                decimal=1) is None
コード例 #3
0
def test_sparse_input():
    # Tests unregularized CCA methods. The idea is that all of these should give the same result.
    latent_dims = 2
    cca = CCA(latent_dims=latent_dims, centre=False).fit((X_sp, Y_sp))
    iter = CCA_ALS(
        latent_dims=latent_dims,
        tol=1e-9,
        stochastic=False,
        centre=False,
        random_state=rng,
    ).fit((X_sp, Y_sp))
    iter_pls = PLS_ALS(latent_dims=latent_dims, tol=1e-9, centre=False).fit(
        (X_sp, Y_sp))
    gcca = GCCA(latent_dims=latent_dims, centre=False).fit((X_sp, Y_sp))
    mcca = MCCA(latent_dims=latent_dims, centre=False).fit((X_sp, Y_sp))
    kcca = KCCA(latent_dims=latent_dims, centre=False).fit((X_sp, Y_sp))
    scca = SCCA(latent_dims=latent_dims, centre=False, c=0.001).fit(
        (X_sp, Y_sp))
    corr_cca = cca.score((X, Y))
    corr_iter = iter.score((X, Y))
    corr_gcca = gcca.score((X, Y))
    corr_mcca = mcca.score((X, Y))
    corr_kcca = kcca.score((X, Y))
    # Check the correlations from each unregularized method are the same
    assert np.testing.assert_array_almost_equal(
        corr_iter, corr_mcca, decimal=1) is None
    assert np.testing.assert_array_almost_equal(
        corr_iter, corr_gcca, decimal=1) is None
    assert np.testing.assert_array_almost_equal(
        corr_iter, corr_kcca, decimal=1) is None
コード例 #4
0
def test_unregularized_methods():
    # Tests unregularized CCA methods. The idea is that all of these should give the same result.
    latent_dims = 2
    cca = CCA(latent_dims=latent_dims).fit([X, Y])
    iter = CCA_ALS(latent_dims=latent_dims,
                   tol=1e-9,
                   stochastic=False,
                   random_state=rng).fit([X, Y])
    gcca = GCCA(latent_dims=latent_dims).fit([X, Y])
    mcca = MCCA(latent_dims=latent_dims, eps=1e-9).fit([X, Y])
    kcca = KCCA(latent_dims=latent_dims).fit([X, Y])
    kgcca = KGCCA(latent_dims=latent_dims).fit([X, Y])
    tcca = TCCA(latent_dims=latent_dims).fit([X, Y])
    corr_cca = cca.score((X, Y))
    corr_iter = iter.score((X, Y))
    corr_gcca = gcca.score((X, Y))
    corr_mcca = mcca.score((X, Y))
    corr_kcca = kcca.score((X, Y))
    corr_kgcca = kgcca.score((X, Y))
    corr_tcca = tcca.score((X, Y))
    assert np.testing.assert_array_almost_equal(corr_cca, corr_iter,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_mcca,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_gcca,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_kcca,
                                                decimal=1) is None
    assert np.testing.assert_array_almost_equal(corr_cca, corr_tcca,
                                                decimal=1) is None
    assert (np.testing.assert_array_almost_equal(
        corr_kgcca, corr_gcca, decimal=1) is None)
    # Check standardized models have standard outputs
    assert (np.testing.assert_allclose(
        np.linalg.norm(iter.transform(
            (X, Y))[0], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(cca.transform(
            (X, Y))[0], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(mcca.transform(
            (X, Y))[0], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(kcca.transform(
            (X, Y))[0], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(iter.transform(
            (X, Y))[1], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(cca.transform(
            (X, Y))[1], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(mcca.transform(
            (X, Y))[1], axis=0)**2, n, rtol=0.2) is None)
    assert (np.testing.assert_allclose(
        np.linalg.norm(kcca.transform(
            (X, Y))[1], axis=0)**2, n, rtol=0.2) is None)
コード例 #5
0
def test_regularized_methods():
    # Test that linear regularized methods match PLS solution when using maximum regularisation.
    latent_dims = 2
    c = 1
    kernel = KCCA(latent_dims=latent_dims,
                  c=[c, c],
                  kernel=["linear", "linear"]).fit((X, Y))
    pls = PLS(latent_dims=latent_dims).fit([X, Y])
    gcca = GCCA(latent_dims=latent_dims, c=[c, c]).fit([X, Y])
    mcca = MCCA(latent_dims=latent_dims, c=[c, c]).fit([X, Y])
    rcca = rCCA(latent_dims=latent_dims, c=[c, c]).fit([X, Y])
    corr_gcca = gcca.score((X, Y))
    corr_mcca = mcca.score((X, Y))
    corr_kernel = kernel.score((X, Y))
    corr_pls = pls.score((X, Y))
    corr_rcca = rcca.score((X, Y))
    # Check the correlations from each unregularized method are the same
    assert np.testing.assert_array_almost_equal(corr_pls, corr_mcca,
                                                decimal=1) is None
    assert (np.testing.assert_array_almost_equal(
        corr_pls, corr_kernel, decimal=1) is None)
    assert np.testing.assert_array_almost_equal(corr_pls, corr_rcca,
                                                decimal=1) is None