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)
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
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)
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)
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])
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)
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)
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])