Beispiel #1
0
    def test_do_schur_sparse(self, example_matrix_mu: np.ndarray):
        N = 9
        P, sd = get_known_input(example_matrix_mu)

        X_k, RR_k, _ = _do_schur(csr_matrix(P), eta=sd, m=N, method="krylov")

        _assert_schur(P, X_k, RR_k, N)
Beispiel #2
0
    def test_coarse_grain_sparse_eq_dense(self, example_matrix_mu: np.ndarray):
        P, sd = get_known_input(example_matrix_mu)

        Pc_b = gpcca_coarsegrain(P, m=3, eta=sd, method="brandts")
        Pc_k = gpcca_coarsegrain(csr_matrix(P), m=3, eta=sd, method="krylov")

        assert_allclose(Pc_k, Pc_b)
Beispiel #3
0
 def test_schur_b_neg(self):
     mu0 = mu(0)
     P, sd = get_known_input(mu0)
     with pytest.raises(
             ValueError,
             match=
             "The number of clusters/states is not supposed to be negative",
     ):
         _do_schur(P, eta=sd, m=-3)
Beispiel #4
0
    def _generate_ground_truth_rot_matrices(self):
        # this function generates the data for "test_init_final_rotation_matrix"
        P, sd = get_known_input(mu(0))
        g_ks = GPCCA(csr_matrix(P), method="krylov").optimize(3)
        g_kd = GPCCA(P, method="krylov").optimize(3)

        for g in [g_ks, g_kd]:
            g.schur_vectors
            _initialize_rot_matrix(sd)
            g.rotation_matrix
Beispiel #5
0
    def test_gpcca_krylov_sparse_eq_dense_mu(self,
                                             example_matrix_mu: np.ndarray):
        mu = int(example_matrix_mu[2, 4])
        if mu == 1000:
            pytest.skip("rtol=0.03359514, atol=3.73976903e+14")
        opt_clust = {0: 3, 10: 3, 50: 3, 100: 3, 200: 2, 500: 2, 1000: 5}[mu]

        P, sd = get_known_input(example_matrix_mu)

        g_s = GPCCA(csr_matrix(P), eta=sd, method="krylov").optimize(opt_clust)
        g_d = GPCCA(P, eta=sd, method="krylov").optimize(opt_clust)
        g_b = GPCCA(P, eta=sd, method="brandts").optimize(opt_clust)

        assert issparse(g_s.transition_matrix)
        assert not issparse(g_d.transition_matrix)
        assert not issparse(g_b.transition_matrix)

        assert_allclose(g_s.memberships.sum(1), 1.0)
        assert_allclose(g_d.memberships.sum(1), 1.0)
        assert_allclose(g_b.memberships.sum(1), 1.0)

        X_k, X_kd, X_b = g_s.schur_vectors, g_d.schur_vectors, g_b.schur_vectors
        RR_k, RR_kd, RR_b = g_s.schur_matrix, g_d.schur_matrix, g_b.schur_matrix

        # check if it's a correct Schur form
        _assert_schur(P, X_k, RR_k, N=None)
        _assert_schur(P, X_kd, RR_kd, N=None)
        _assert_schur(P, X_b, RR_b, N=None)
        # check if they span the same subspace
        assert np.max(subspace_angles(X_k, X_kd)) < eps
        assert np.max(subspace_angles(X_kd, X_b)) < eps

        ms, md, mb = g_s.memberships, g_d.memberships, g_b.memberships
        cs, cd, cb = (
            g_s.coarse_grained_transition_matrix,
            g_d.coarse_grained_transition_matrix,
            g_b.coarse_grained_transition_matrix,
        )

        for left, right in combinations(
            ["brandts", "dense_krylov", "sparse_krylov"], r=2):
            ml, cl = locals()[f"m{left[0]}"], locals()[f"c{left[0]}"]
            mr, cr = locals()[f"m{right[0]}"], locals()[f"c{right[0]}"]

            perm = _find_permutation(ml, mr)

            mr = mr[:, perm]
            assert_allclose(mr, ml, atol=1e-4)

            cr = cr[perm, :][:, perm]
            try:
                assert_allclose(cr, cl, atol=1e-4)
            except AssertionError as e:
                raise RuntimeError(f"Comparing: {left} and {right}.") from e
Beispiel #6
0
    def test_do_schur_krylov_eq_brandts(self, example_matrix_mu: np.ndarray):
        P, sd = get_known_input(example_matrix_mu)

        X_b, RR_b, _ = _do_schur(P, eta=sd, m=3, method="brandts")
        X_k, RR_k, _ = _do_schur(P, eta=sd, m=3, method="krylov")

        # check if it's a correct Schur form
        _assert_schur(P, X_b, RR_b, N=None)
        _assert_schur(P, X_k, RR_k, N=None)
        # check if they span the same subspace
        assert np.max(subspace_angles(X_b, X_k)) < eps
Beispiel #7
0
    def test_initialize_A(self):
        mu0 = mu(0)
        P, sd = get_known_input(mu0)
        X, _, _ = _do_schur(P, sd, m=4)
        evs = X[:, :4]

        A = _initialize_rot_matrix(evs)
        index = _indexsearch(evs)
        A_exp = pinv(X[index, :4])

        assert_allclose(A, A_exp)
Beispiel #8
0
    def test_schur_b_pos(self):
        N = 9
        mu0 = mu(0)
        P, sd = get_known_input(mu0)
        X, RR, _ = _do_schur(P, eta=sd, m=3)

        np.testing.assert_array_equal(P.shape, [N, N])
        np.testing.assert_array_equal(X.shape, [9, 3])
        np.testing.assert_array_equal(RR.shape, [3, 3])

        _assert_schur(P, X, RR, N=None)
Beispiel #9
0
    def test_use_minChi(self):
        kmin, kmax = 2, 9
        kopt = []

        for mu_ in [10, 50, 100, 200, 500, 1000]:
            P, sd = get_known_input(mu(mu_))
            g = GPCCA(P, eta=sd)
            minChi = g.minChi(kmin, kmax)

            kopt.append(kmax - 1 - np.argmax(np.flipud(minChi[1:-1])))

        np.testing.assert_array_equal(kopt, [3] * 5 + [7])
Beispiel #10
0
    def test_cluster_by_isa(self, chi_isa_mu0_n3: np.ndarray,
                            chi_isa_mu100_n3: np.ndarray):
        # chi_sa_mu0_n3 has permuted 2nd and 3d columns when compared to the matlab version
        for mu_, chi_exp in zip([0, 100], [chi_isa_mu0_n3, chi_isa_mu100_n3]):
            P, sd = get_known_input(mu(mu_))
            X, _, _ = _do_schur(P, sd, m=3)
            chi, _ = _cluster_by_isa(X[:, :3])

            chi = chi[:, _find_permutation(chi_exp, chi)]

            assert_allclose(chi.T @ chi, chi_exp.T @ chi_exp)
            assert_allclose(chi, chi_exp)
Beispiel #11
0
    def test_objective_1st_col(self, mocker):
        # check_in_matlab: _objective
        P, _ = get_known_input(mu(0))
        N, M = P.shape[0], 4

        _, L, _ = lu(P[:, :M])
        mocker.patch(
            "pygpcca._sorted_schur",
            return_value=(np.eye(M), L, np.array([np.nan] * M)),
        )
        mocker.patch("pygpcca._gpcca._gram_schmidt_mod", return_value=L)

        with pytest.raises(
                ValueError,
                match=r"The first column X\[:, 0\] of the Schur "
                r"vector matrix isn't constantly equal 1.",
        ):
            _do_schur(P,
                      eta=np.true_divide(np.ones((N, ), dtype=np.float64), N),
                      m=M)
Beispiel #12
0
    def test_opt_soft_nelder_mead_more(self):
        kmin, kmax = 2, 8
        kopt = []
        ks = np.arange(kmin, kmax)

        for mu_ in [10, 50, 100, 200, 500, 1000]:
            mu_ = mu(mu_)
            P, sd = get_known_input(mu_)
            X, _, _ = _do_schur(P, eta=sd, m=kmax)

            crisp = [-np.inf] * (kmax - kmin)
            for j, k in enumerate(range(kmin, kmax)):
                svecs = X[:, :k]
                A = _initialize_rot_matrix(svecs)

                _, _, fopt = _opt_soft(svecs, A)
                crisp[j] = (k - fopt) / k

            kopt.append(ks[np.argmax(crisp)])

        np.testing.assert_array_equal(kopt, [3, 3, 3, 2, 2, 7])
Beispiel #13
0
    def test_do_schur(self, example_matrix_mu: np.ndarray):
        N = 9
        P, sd = get_known_input(example_matrix_mu)
        X, RR, _ = _do_schur(P, eta=sd, m=N)

        _assert_schur(P, X, RR, N)