def test_hitting1(self): P = np.array([[0., 1., 0.], [0., 1., 0.], [0., 0., 1.]]) sol = np.array([1, 0, 0]) assert_allclose(hitting_probability(P, 1), sol) assert_allclose(hitting_probability(P, [1, 2]), sol)
def test_connected_count_matrix(self): """Directed""" C_cc = largest_connected_submatrix(self.C) assert_allclose(C_cc, self.C_cc_directed) """Undirected""" C_cc = largest_connected_submatrix(self.C, directed=False) assert_allclose(C_cc, self.C_cc_undirected)
def test_relaxation(fingerprints_data): T = fingerprints_data.transition_matrix R, D, L = rdl_decomposition(T, k=4 if fingerprints_data.sparse else None) times = np.array([1, 5, 10, 20, 100]) ev = np.diagonal(D) ev_t = ev[np.newaxis, :]**times[:, np.newaxis] obs = np.zeros(10) obs[0] = 1 obs[1] = 1 p0 = np.zeros(10) p0[:4] = 0.25 if not fingerprints_data.sparse: """k=None""" relax_amp = np.dot(p0, R) * np.dot(L, obs) relax = np.dot(ev_t, relax_amp) relaxn = relaxation(T, p0, obs, times=times) assert_allclose(relaxn, relax) """k=4""" k = 4 relax_amp = np.dot(p0, R[:, 0:k]) * np.dot(L[0:k, :], obs) relax = np.dot(ev_t[:, 0:k], relax_amp) relaxn = relaxation(T, p0, obs, k=k, times=times) assert_allclose(relaxn, relax)
def test_hitting2(self): P = np.array([[1.0, 0.0, 0.0, 0.0], [0.1, 0.8, 0.1, 0.0], [0.0, 0.0, 0.8, 0.2], [0.0, 0.0, 0.2, 0.8]]) sol = np.array([0., 0.5, 1., 1.]) assert_allclose(hitting_probability(P, [2, 3]), sol)
def test_timescales_rev(self): P_dense = self.bdc.transition_matrix P = self.bdc.transition_matrix_sparse mu = self.bdc.stationary_distribution ev = eigvals(P_dense) """Sort with decreasing magnitude""" ev = ev[np.argsort(np.abs(ev))[::-1]] ts = -1.0 / np.log(np.abs(ev)) """k=None""" with self.assertRaises(ValueError): tsn = timescales(P, reversible=True) """k is not None""" tsn = timescales(P, k=self.k, reversible=True) assert_allclose(ts[1:self.k], tsn[1:]) """k is not None, ncv is not None""" tsn = timescales(P, k=self.k, ncv=self.ncv, reversible=True) assert_allclose(ts[1:self.k], tsn[1:]) """k is not None, mu is not None""" tsn = timescales(P, k=self.k, reversible=True, mu=mu) assert_allclose(ts[1:self.k], tsn[1:]) """k is not None, mu is not None, ncv is not None""" tsn = timescales(P, k=self.k, ncv=self.ncv, reversible=True, mu=mu) assert_allclose(ts[1:self.k], tsn[1:]) """tau=7""" """k is not None""" tsn = timescales(P, k=self.k, tau=7, reversible=True) assert_allclose(7 * ts[1:self.k], tsn[1:])
def test_expectation(fingerprints_data): obs1 = np.zeros(10) obs1[0] = 1 obs1[1] = 1 exp = np.dot(fingerprints_data.stationary_distribution, obs1) expn = expectation(fingerprints_data.transition_matrix, obs1) assert_allclose(exp, expn)
def test_eigenvectors_reversible(scenario, ncv_values): k, bdc = scenario P = bdc.transition_matrix ev = eigvals(P) ev = ev[np.argsort(np.abs(ev))[::-1]] Dn = np.diag(ev) Dnk = Dn[:, :k][:k, :] with assert_raises(ValueError) if bdc.sparse and k is None else nullcontext(): # right eigenvectors Rn = eigenvectors(P, k=k, reversible=True, ncv=ncv_values) assert_allclose(P @ Rn, Rn @ Dnk) # left eigenvectors Ln = eigenvectors(P, right=False, k=k, reversible=True, ncv=ncv_values).T assert_allclose(Ln.T @ P, Dnk @ Ln.T) # orthogonality Xn = Ln.T @ Rn di = np.diag_indices(Xn.shape[0] if k is None else k) Xn[di] = 0.0 assert_allclose(Xn, 0) Rn = eigenvectors(P, k=k, ncv=ncv_values, reversible=True, mu=bdc.stationary_distribution) assert_allclose(ev[:k][np.newaxis, :] * Rn, P.dot(Rn)) Ln = eigenvectors(P, right=False, k=k, ncv=ncv_values, reversible=True, mu=bdc.stationary_distribution).T assert_allclose(P.transpose().dot(Ln), ev[:k][np.newaxis, :] * Ln)
def test_noninteger_counts_dense(self): C = np.loadtxt(testpath + 'C_1_lag.dat') T_dense_reference = impl_dense(C) T_dense_scaled_1 = impl_dense(C*10.0) T_dense_scaled_2 = impl_dense(C*0.1) assert_allclose(T_dense_reference, T_dense_scaled_1) assert_allclose(T_dense_reference, T_dense_scaled_2)
def test_noninteger_counts_sparse(self): C = np.loadtxt(testpath + 'C_1_lag.dat') T_sparse_reference = impl_sparse(scipy.sparse.csr_matrix(C)).toarray() T_sparse_scaled_1 = impl_sparse(scipy.sparse.csr_matrix(C*10.0)).toarray() T_sparse_scaled_2 = impl_sparse(scipy.sparse.csr_matrix(C*0.1)).toarray() assert_allclose(T_sparse_reference, T_sparse_scaled_1) assert_allclose(T_sparse_reference, T_sparse_scaled_2)
def test_hitting3(self): P = np.array([[0.9, 0.1, 0.0, 0.0, 0.0], [0.1, 0.9, 0.0, 0.0, 0.0], [0.0, 0.1, 0.4, 0.5, 0.0], [0.0, 0.0, 0.0, 0.8, 0.2], [0.0, 0.0, 0.0, 0.2, 0.8]]) sol = np.array([0.0, 0.0, 8.33333333e-01, 1.0, 1.0]) assert_allclose(hitting_probability(P, 3), sol) assert_allclose(hitting_probability(P, [3, 4]), sol)
def test_forward_committor(self): qplus = self.qplus qplusn = self.tpt.forward_committor assert_allclose(qplusn, qplus) qplusn = self.tpt_fast.forward_committor assert_allclose(qplusn, qplus)
def test_rate(self): k = self.bdc.rate(self.a, self.b) kn = self.tpt.rate assert_allclose(kn, k) kn = self.tpt_fast.rate assert_allclose(kn, k)
def test_totalflux(self): F = self.bdc.totalflux(self.a, self.b) Fn = self.tpt.total_flux assert_allclose(Fn, F) Fn = self.tpt_fast.total_flux assert_allclose(Fn, F)
def test_netflux(self): netflux = self.bdc.netflux(self.a, self.b) netfluxn = self.tpt.net_flux assert_allclose(netfluxn, netflux) netfluxn = self.tpt_fast.net_flux assert_allclose(netfluxn, netflux)
def test_grossflux(self): flux = self.bdc.flux(self.a, self.b) fluxn = self.tpt.gross_flux assert_allclose(fluxn, flux) fluxn = self.tpt_fast.gross_flux assert_allclose(fluxn, flux)
def test_stationary_distribution(self): mu = self.mu mun = self.tpt.stationary_distribution assert_allclose(mun, mu) mun = self.tpt_fast.stationary_distribution assert_allclose(mun, mu)
def test_pathways_sparse(self): paths, capacities = pathways(self.F_sparse, self.A, self.B) self.assertTrue(len(paths) == len(self.paths)) self.assertTrue(len(capacities) == len(self.capacities)) for i in range(len(paths)): assert_allclose(paths[i], self.paths[i]) assert_allclose(capacities[i], self.capacities[i])
def test_backward_committor(self): qminus = self.qminus qminusn = self.tpt.backward_committor assert_allclose(qminusn, qminus) qminusn = self.tpt_fast.backward_committor assert_allclose(qminusn, qminus)
def test_prior_rev(self): with warnings.catch_warnings(record=True) as w: Bn = prior_rev(self.C) assert_allclose(Bn, -1.0 * self.B_rev) with warnings.catch_warnings(record=True) as w: Bn = prior_rev(self.C, alpha=self.alpha) assert_allclose(Bn, self.alpha * self.B_rev)
def test_prior_const(self): with warnings.catch_warnings(record=True) as w: Bn = prior_const(self.C) assert_allclose(Bn, self.alpha_def * self.B_const) with warnings.catch_warnings(record=True) as w: Bn = prior_const(self.C, alpha=self.alpha) assert_allclose(Bn, self.alpha * self.B_const)
def test_relaxation(self): relax_amp = np.dot(self.p0, self.R) * np.dot(self.L, self.obs) relax = np.dot(self.ev_t, relax_amp) relaxn = relaxation(self.T, self.p0, self.obs, k=self.k, times=self.times) assert_allclose(relaxn, relax)
def test_relaxation_matvec(self): times = self.times P = self.T.toarray() relax = np.zeros(len(times)) for i in range(len(times)): P_t = np.linalg.matrix_power(P, times[i]) relax[i] = np.dot(self.p0, np.dot(P_t, self.obs)) relaxn = relaxation_matvec(self.T, self.p0, self.obs, times=self.times) assert_allclose(relaxn, relax)
def test_count_matrix(self): """Small test cases""" T = transition_matrix.transition_matrix_non_reversible( self.C1).toarray() assert_allclose(T, self.T1.toarray()) T = transition_matrix.transition_matrix_non_reversible( self.C1).toarray() assert_allclose(T, self.T1.toarray())
def test_error_perturbation_sparse(self): Csparse = scipy.sparse.csr_matrix(self.C) with warnings.catch_warnings(record=True) as w: xn = error_perturbation(Csparse, self.S1) assert_allclose(xn, self.x) Xn = error_perturbation(Csparse, self.S2) assert_allclose(Xn, self.X)
def test_expected_counts_stationary(setting, sparse_mode, statdist): if sparse_mode: setting = to_sparse_setting(setting, 500) T, mu, v, L, R = setting N = 20 D_mu = diags(mu, 0) EC_n = expected_counts_stationary(T, N, mu=mu if statdist else None) EC_true = N * D_mu.dot(T) assert_allclose(EC_true, EC_n)
def test_timescales_2(self): """Eigenvalues with non-zero imaginary part""" ts = np.array([np.inf, 0.971044, 0.971044]) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") tsn = timescales(0.5 * self.T + 0.5 * self.P) assert_allclose(tsn, ts) assert issubclass(w[-1].category, ImaginaryEigenValueWarning)
def test_fingerprint_relaxation(self): one_vec = np.ones(self.T.shape[0]) relax_amp = np.dot(self.p0, self.R) * np.dot(self.L, self.obs1) tsn, relax_ampn = fingerprint_relaxation(self.T, self.p0, self.obs1, k=self.k) assert_allclose(tsn, self.ts) assert_allclose(relax_ampn, relax_amp)
def test_fingerprint(self): k = self.k amp = np.dot(self.p0 * self.obs1, self.R) * np.dot(self.L, self.obs2) tsn, ampn = fingerprint(self.T, self.obs1, obs2=self.obs2, p0=self.p0, k=k) assert_allclose(tsn, self.ts) assert_allclose(ampn, amp)
def test_timescales_inf(): """Multiple eigenvalues of magnitude one, eigenvalues with non-zero imaginary part""" W = np.array([[0, 1], [1, 0]]) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('ignore') warnings.simplefilter('always', category=SpectralWarning) tsn = timescales(W) assert_allclose(tsn, np.array([np.inf, np.inf])) assert issubclass(w[-1].category, SpectralWarning)
def test_timescales_1(self): """Multiple eigenvalues of magnitude one, eigenvalues with non-zero imaginary part""" ts = np.array([np.inf, np.inf]) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") tsn = timescales(self.W) assert_allclose(tsn, ts) assert issubclass(w[-1].category, SpectralWarning)