def test_initialize_A_condition(self): dummy = hilbert(14) dummy = dummy[:, :-1] dummy[:, 0] = 1.0 with pytest.raises(ValueError, match="The condition number .*"): _initialize_rot_matrix(dummy)
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_first_is_not_constant(self): X = np.zeros((4, 4)) X[0, 0] = 1.0 with pytest.raises( ValueError, match= "First Schur vector is not constant 1. This indicates that the Schur vectors are incorrectly sorted. " "Cannot search for a simplex structure in the data.", ): _initialize_rot_matrix(X)
def test_initialize_A_shape_error_1(self): X = np.zeros((3, 4)) X[:, 0] = 1.0 with pytest.raises( ValueError, match= r"The Schur vector matrix of shape \(\d+, \d+\) has more columns than rows. " r"You can't get a \d+-dimensional simplex from \d+ data vectors.", ): _initialize_rot_matrix(X)
def test_initialize_A_second_and_rest_are_constant(self): X = np.zeros((3, 3)) X[:, 0] = 1.0 X[:, 2] = 2 with pytest.raises( ValueError, match= r"2 Schur vector\(s\) after the first one are constant. Probably the Schur vectors are incorrectly " "sorted. Cannot search for a simplex structure in the data.", ): _initialize_rot_matrix(X)
def test_initialize_A_condition_warning(self): dummy = hilbert(6) dummy = dummy[:, :-1] dummy[:, 0] = 1.0 with pytest.warns(UserWarning): _ = _initialize_rot_matrix(dummy)
def test_init_final_rot_matrix_krylov_dense( self, svecs_mu0_krylov_dense: np.ndarray, A_mu0_krylov_dense_init: np.ndarray, A_mu0_krylov_dense: np.ndarray, ): init_rot = _initialize_rot_matrix(svecs_mu0_krylov_dense) _, final_rot, _ = _gpcca_core(svecs_mu0_krylov_dense) assert_allclose(init_rot, A_mu0_krylov_dense_init) assert_allclose(final_rot, A_mu0_krylov_dense)
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_init_final_rot_matrix_brandts( self, svecs_mu0: np.ndarray, A_mu0_init: np.ndarray, A_mu0: np.ndarray, ): init_rot = _initialize_rot_matrix(svecs_mu0) _, final_rot, _ = _gpcca_core(svecs_mu0) assert_allclose(init_rot, A_mu0_init) assert_allclose(final_rot, A_mu0)
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])