Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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)
Esempio n. 4
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)
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)
Esempio n. 8
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])
Esempio n. 9
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)