def test1(g, u, v): H0, H1, H2, H0d, H1d, H2d = g.hodge_star() P0, P1, P2, P0d, P1d, P2d = g.projection() eq(H1(P1(lambda x, y: (u(x, y), v(x, y)))), P1d(lambda x, y: (-v(x, y), u(x, y)))) eq(H1d(P1d(lambda x, y: (-v(x, y), u(x, y)))), P1(lambda x, y: (-u(x, y), -v(x, y))))
def test1(g, u, v): H0, H1, H2, H0d, H1d, H2d = g.hodge_star() P0, P1, P2, P0d, P1d, P2d = g.projection() eq(H1(P1(lambda x, y: (u(x,y), v(x,y)))), P1d(lambda x, y: (-v(x,y), u(x,y)))) eq(H1d(P1d(lambda x, y: (-v(x,y), u(x,y)))), P1(lambda x, y: (-u(x,y), -v(x,y))))
def test_wedge_chebyshev(): def α(x): return x**2 def β(x): return (x + 1 / 2)**3 def γ(x): return x**2 * (x + 1 / 2)**3 g = Grid_1D.chebyshev(13) P0, P1, P0d, P1d = g.projection() W = g.wedge() W00 = W[(0, True), (0, True), True] W01 = W[(0, True), (1, True), True] a0 = P0(α) b0 = P0(β) c0 = P0(γ) eq(W00(a0, b0), c0) a0 = P0(α) b1 = P1(β) c1 = P1(γ) eq(W01(a0, b1), c1)
def test_normal_pdb(): fn = get_fn('4lzt/4lzt.not_les.pdb') tn = get_fn('4lzt/4lzt.parm7') rst7 = get_fn('4lzt/4lzt.rst7') pdb_input = pdb.input(fn) symm = pdb_input.crystal_symmetry() pdb_hc = pdb_input.construct_hierarchy() phenix_coords_uc = expand_coord_to_unit_cell(pdb_hc.atoms().extract_xyz(), symm) indices_dict = get_indices_convert_dict(fn) p2a_indices = indices_dict['p2a'] a2p_indices = indices_dict['a2p'] parm = pmd.load_file(tn, rst7) a0 = reorder_coords_phenix_to_amber(phenix_coords_uc, p2a_indices) # make sure phenix and amber read the same coordinates aa_eq(pdb_input.atoms().extract_xyz(), parm.coordinates, decimal=2) aa_eq(a0, parm.coordinates, decimal=2) # make sure the a2p_indices and p2a_indices are identical for non-LES indices_dict = get_indices_convert_dict_from_array( pdb_input.atoms().extract_xyz(), parm.coordinates) p2a_indices = indices_dict['p2a'] a2p_indices = indices_dict['a2p'] eq(a2p_indices, p2a_indices)
def test_wedge(): def α(x): return sin(4 * x) def β(x): return cos(4 * x) def γ(x): return sin(4 * x) * cos(4 * x) g = Grid_1D.periodic(13) P0, P1, P0d, P1d = g.projection() W = g.wedge() W00 = W[(0, True), (0, True), True] W01 = W[(0, True), (1, True), True] a0 = P0(α) b0 = P0(β) c0 = P0(γ) eq(W00(a0, b0), c0) a0 = P0(α) b1 = P1(β) c1 = P1(γ) eq(W01(a0, b1), c1)
def test_mi(): a = np.random.uniform(low=0., high=360., size=1000).reshape(-1, 1) b = np.random.uniform(low=0., high=360., size=1000).reshape(-1, 1) MI1 = mi(10, a, b) MI2 = mi(10, b, a) eq(MI1, MI2, 6)
def test_nmutinf(): a = np.random.uniform(low=0., high=360., size=1000).reshape(-1, 1) b = np.random.uniform(low=0., high=360., size=1000).reshape(-1, 1) MI1 = nmutinf(24, a, b) MI2 = nmutinf(24, b, a) eq(MI1, MI2, 6)
def check_equal(g): h0, h1, h0d, h1d = g.hodge_star() H0 = to_matrix(h0, len(g.verts)) H1 = to_matrix(h1, len(g.delta)) H0d = to_matrix(h0d, len(g.delta)) H1d = to_matrix(h1d, len(g.verts)) eq(H1d, linalg.inv(H0)) eq(H0d, linalg.inv(H1))
def check_equal(g): h0, h1, h0d, h1d = g.hodge_star() H0 = to_matrix(h0, len(g.verts)) H1 = to_matrix(h1, len(g.delta)) H0d = to_matrix(h0d, len(g.delta)) H1d = to_matrix(h1d, len(g.verts)) eq( H1d, linalg.inv(H0) ) eq( H0d, linalg.inv(H1) )
def test_ncmi(): a = np.random.uniform(low=0, high=360, size=1000).reshape(-1, 1) b = np.random.uniform(low=0, high=360, size=1000).reshape(-1, 1) c = np.random.uniform(low=0, high=360, size=1000).reshape(-1, 1) NCMI_REF = (cmi(10, a, b, c) / ce(10, a, c)) NCMI = ncmi(10, a, b, c) eq(NCMI, NCMI_REF, 6)
def test_ncmutinf(): a = rs.uniform(low=0, high=360, size=1000).reshape(-1, 1) b = rs.uniform(low=0, high=360, size=1000).reshape(-1, 1) c = rs.uniform(low=0, high=360, size=1000).reshape(-1, 1) NCMI_REF = (cmutinf(10, a, b, c) / centropy(10, a, c)) NCMI = ncmutinf(10, a, b, c) eq(NCMI, NCMI_REF, 5)
def test0(g, f): H0, H1, H2, H0d, H1d, H2d = g.hodge_star() P0, P1, P2, P0d, P1d, P2d = g.projection() eq(H0(P0(f)), P2d(f)) eq(H2(P2(f)), P0d(f)) eq(H0d(P0d(f)), P2(f)) eq(H2d(P2d(f)), P0(f))
def test_leibniz(): N = 7 g = Grid_1D.periodic(N) D, DD = g.derivative() W = g.wedge() W00 = W[(0,True),(0,True), True] W01 = W[(0,True),(1,True), True] a0 = random.random_sample(N) b0 = random.random_sample(N) lhs = D(W00(a0, b0)) rhs = W01(a0, D(b0)) + W01(b0, D(a0)) eq(lhs, rhs)
def test_leibniz(): N = 7 g = Grid_1D.periodic(N) D, DD = g.derivative() W = g.wedge() W00 = W[(0, True), (0, True), True] W01 = W[(0, True), (1, True), True] a0 = random.random_sample(N) b0 = random.random_sample(N) lhs = D(W00(a0, b0)) rhs = W01(a0, D(b0)) + W01(b0, D(a0)) eq(lhs, rhs)
def newton_sqrt2(n, guess=2): from numpy.testing import assert_almost_equal as eq if eq(newton_sqrt2(n, guess)**2, n): return guess else: guess = 0.5 * (guess + n) return newton_sqrt(n, guess)
def test_min_cost_flow_pixel(): input_signal_U = np.asfortranarray(np.array([[1.0, 2.0, 3.0, 4.0, 5.0], [2.0, 3.0, 2.0, 1.0, 4.0], [5.0, 2.0, 2.0, 4.0, 3.0], [8.0, 8.0, 3.0, 2.0, 5.0], [2.0, 3.0, 2.0, 1.0, 4.0], [5.0, 2.0, 2.0, 4.0, 3.0]]), dtype=project_float) graph = build_graph([3, 2], [2, 2]) prox = lsd.min_cost_flow(input_signal_U, graph, 0.1) eq( prox, np.array([[1.0, 2.0, 2.9, 3.95, 4.9], [2.0, 3.0, 2.0, 1.0, 4.0], [5.0, 2.0, 2.0, 3.95, 3.0], [7.8, 7.8, 2.9, 1.9, 4.9], [2.0, 3.0, 2.0, 1.0, 4.0], [5.0, 2.0, 2.0, 4.0, 3.0]], dtype=project_float))
def test_min_cost_flow_l1(): input_signal_U = np.asfortranarray(np.array([[1.0, 2.0], [3.0, 4.0]]), dtype=project_float) groups_dimensions = (input_signal_U.shape[0], input_signal_U.shape[0]) singleton_graph = { 'eta_g': np.ones(input_signal_U.shape[0], dtype=project_float), 'groups': sp.csc_matrix(np.zeros(groups_dimensions), dtype=np.bool), 'groups_var': sp.csc_matrix(np.eye(input_signal_U.shape[0], dtype=np.bool), dtype=np.bool) } prox_l1 = lsd.min_cost_flow(input_signal_U, singleton_graph, 2.1) eq(prox_l1, np.array([[0.0, 0.0], [0.9, 1.9]], dtype=project_float), rtol=1e-6)
def test_min_cost_flow(): input_signal_U = np.asfortranarray(np.array([[1.0, 2.0], [3.0, 4.0]]), dtype=project_float) groups_dimensions = (1, 1) graph = { 'eta_g': np.ones(1, dtype=project_float), 'groups': sp.csc_matrix(np.zeros(groups_dimensions), dtype=np.bool), 'groups_var': sp.csc_matrix(np.ones((input_signal_U.shape[0], 1), dtype=np.bool), dtype=np.bool) } prox = lsd.min_cost_flow(input_signal_U, graph, 0.1) eq(prox, np.array([[1.0, 2.0], [2.9, 3.9]], dtype=project_float), rtol=1e-6)
def newton_sqrt2(x, guess=2): """Danger! Something's not quite right...""" from numpy.testing import assert_almost_equal as eq if eq(newton_sqrt2(x, guess)**2, x): return guess else: guess = 0.5 * (guess + x) return newton_sqrt2(x, guess)
def test_compare_chebyshev_and_lagrange_polynomials(): ''' The Chebyshev basis functions are equivalent to the Lagrange basis functions for the grid points. ''' for n in (10, 11, 12, 13): g = Grid_1D.chebyshev(n, -1, +1) x = linspace(g.xmin, g.xmax, 100) L = lagrange_polynomials(g.verts) for i in range(len(L)): eq(L[i](x), psi0(n, i, x)) L = lagrange_polynomials(g.verts_dual) for i in range(len(L)): eq(L[i](x), psid0(n, i, x))
def test_hodge(): F = lambda x: sin(4 * x) g = Grid_1D.periodic(10) H0, H1, _, _ = g.hodge_star() P0, P1, P0d, P1d = g.projection() eq(H0(P0(F)), P1d(F)) eq(H1(P1(F)), P0d(F)) #eq(H0(P0(F)), dot(g.hodge_star_toeplitz(), P0(F))) eq(H0(P0(F)), P1d(F)) eq(H1(P1(F)), P0d(F))
def test_wedge(): def α(x): return sin(4 * x) def β(x): return cos(4 * x) def γ(x): return sin(4 * x) * cos(4 * x) g = Grid_1D.periodic(13) P0, P1, P0d, P1d = g.projection() W = g.wedge() W00 = W[(0,True),(0,True), True] W01 = W[(0,True),(1,True), True] a0 = P0(α) b0 = P0(β) c0 = P0(γ) eq(W00(a0, b0), c0) a0 = P0(α) b1 = P1(β) c1 = P1(γ) eq(W01(a0, b1), c1)
def test_wedge_chebyshev(): def α(x): return x**2 def β(x): return (x+1/2)**3 def γ(x): return x**2 * (x+1/2)**3 g = Grid_1D.chebyshev(13) P0, P1, P0d, P1d = g.projection() W = g.wedge() W00 = W[(0,True),(0,True), True] W01 = W[(0,True),(1,True), True] a0 = P0(α) b0 = P0(β) c0 = P0(γ) eq(W00(a0, b0), c0) a0 = P0(α) b1 = P1(β) c1 = P1(γ) eq(W01(a0, b1), c1)
def _test_mi_alpha(traj): mi = AlphaAngleMutualInformation() M = mi.partial_transform(traj) eq(M - M.T, 0)
def check_d1_bndry(g, f, df): P0, P1, P2, P0d, P1d, P2d = g.projection() D0, D1, D0d, D1d = g.derivative() BC0, BC1 = g.boundary_condition() eq(D1(P1(f)), P2(df)) eq(D1d(P1d(f)) + BC1(f), P2d(df))
def test__soft_thresh(): eq(lsd._soft_thresh(np.ones(5), 0.9), 0.1 * np.ones(5)) eq(lsd._soft_thresh(np.ones(5), 1), np.zeros(5)) eq(lsd._soft_thresh(np.array([0.1, 2, 0.2]), 1), np.array([0, 1.0, 0]))
def test_adaptive(): eq(entropy(None, RNG, 'grassberger', a, b), TRUE_ENTROPY, rtol=.4)
def test_chaowangjost(): eq(entropy(8, RNG, 'chaowangjost', a, b), TRUE_ENTROPY, rtol=.2)
def test_kde(): eq(entropy(8, RNG, 'kde', a, b), TRUE_ENTROPY, rtol=.4)
def check_d1(g, f, df): D0, D1, D0d, D1d = g.derivative() P0, P1, P2, P0d, P1d, P2d = g.projection() eq(D1(P1(f)), P2(df)) eq(D1d(P1d(f)), P2d(df))
def check_d0(g, f, df): D0, D1, D0d, D1d = g.derivative() P0, P1, P2, P0d, P1d, P2d = g.projection() eq( D0(P0(f)), P1(df)) eq(D0d(P0d(f)), P1d(df))
def newton_sqrt2(x, guess=2): '''Danger! Something's not quite right''' if eq(newton_sqrt2(x, guess)**2, x): return guess else: guess = 0.5*(guess+x)
def check_grid(g): for P, B in zip(g.projection(), g.basis_fn()): eq(vstack(P(b) for b in B), eye(len(B)))
def test_hodge_star_basis_fn(): for n in range(2,4): g = Grid_1D.periodic(n) H0, H1, H0d, H1d = hodge_star_matrix(g.projection(), g.basis_fn()) h0, h1, h0d, h1d = g.hodge_star() eq(H0d, to_matrix(h0d, n)) eq(H1, to_matrix(h1, n)) eq(H0, to_matrix(h0, n)) eq(H1d, to_matrix(h1d, n)) for n in range(2,5): g = Grid_1D.regular(n, 0, pi) H0, H1, H0d, H1d = hodge_star_matrix(g.projection(), g.basis_fn()) h0, h1, h0d, h1d = g.hodge_star() eq(H0d, to_matrix(h0d, n-1)) eq(H1, to_matrix(h1, n-1)) eq(H0, to_matrix(h0, n)) eq(H1d, to_matrix(h1d, n)) for n in range(2,4): g = Grid_1D.chebyshev(n, -1, +1) H0, H1, H0d, H1d = hodge_star_matrix(g.projection(), g.basis_fn()) h0, h1, h0d, h1d = g.hodge_star() eq(H0d, to_matrix(h0d, n-1)) eq(H1, to_matrix(h1, n-1)) eq(H0, to_matrix(h0, n)) eq(H1d, to_matrix(h1d, n))
def _test_mi_contact(traj): mi = ContactMutualInformation() M = mi.partial_transform(traj) eq(M - M.T, 0)
def _test_mi_dihedral(traj): mi = DihedralMutualInformation() M = mi.partial_transform(traj) eq(M - M.T, 0) _test_mi_shuffle(mi, traj)
def check_grid(g): for P, R, B in zip(g.projection(), g.reconstruction(), g.basis_fn()): y = random.rand(len(B)) eq( P(R(y)), y )
def test_knn(): eq(entropy(3, [None], 'knn', a, b), TRUE_ENTROPY, rtol=.2)
def check_d0(g, f, df): D0, D1, D0d, D1d = g.derivative() P0, P1, P2, P0d, P1d, P2d = g.projection() eq(D0(P0(f)), P1(df)) eq(D0d(P0d(f)), P1d(df))
def test_grassberger(): eq(entropy(8, RNG, 'grassberger', a, b), TRUE_ENTROPY, rtol=.2)
def test_transforms(): x = random.random(11) eq(x, Sinv(S(x))) eq(Hinv(x), S(Hinv(Sinv(x)))) eq(H(S(x)), S(H(x))) x = linspace(0, 2 * pi, 13)[:-1] h = diff(x)[0] f = lambda x: sin(x) fp = lambda x: cos(x) eq(H(fp(x)), f(x + h / 2) - f(x - h / 2)) eq(fp(x), Hinv(f(x + h / 2) - f(x - h / 2))) eq(I(fp(x), -1, 1), f(x + 1) - f(x - 1)) eq(I(fp(x), 0, 1), f(x + 1) - f(x)) eq(fp(x), Iinv(f(x + 1) - f(x - 1), -1, 1)) eq(fp(x), Iinv(f(x + 1) - f(x), 0, 1)) eq(S(f(x)), f(x + h / 2)) eq(Sinv(f(x)), f(x - h / 2))
def test_naive(): eq(entropy(8, RNG, None, a, b), TRUE_ENTROPY, rtol=.2)
def check_d_bnd(g, f, f_prime): D, DD = g.derivative() P0, P1, P0d, P1d = g.projection() eq(D(P0(f)), P1(f_prime)) bc = g.boundary_condition(f) eq( DD(P0d(f))+bc, P1d(f_prime) )
def test_shrink(): eq(lsd.shrink(np.eye(5), 1, 1)[0], np.zeros((5, 5))) eq(lsd.shrink(np.ones((5, 5)), 1, 5)[0], np.ones((5, 5)) * 0.8) eq( lsd.shrink(np.array([[1, 2], [3, 4]]), 1, 2)[0], np.array([[1.04053125, 1.47651896], [2.3521747, 3.33774745]]))
def test_nmutinf_reversible(): MI1 = nmutinf(24, X, Y) MI2 = nmutinf(24, Y, X) eq(MI1, MI2, 5)
def check_grid(g): for P, B in zip(g.projection(), g.basis_fn()): eq( vstack(P(b) for b in B), eye(len(B)) )
def test_integrals(): for N in (10, 11, 12, 13): g = Grid_1D.periodic(N, 0, 2*pi) pnts = concatenate([g.verts, [g.xmax]]) for f in (sin, cos): reference = deci.slow_integration(g.edges[0], g.edges[1], f) eq( deci.integrate_boole1(pnts, f), reference ) eq( integrate_spectral_coarse(pnts, f), reference ) eq( integrate_spectral(pnts, f), reference ) g = Grid_1D.chebyshev(N, -1, +1) for f in ((lambda x: x), (lambda x: x**3), (lambda x: exp(x))): reference = deci.slow_integration(g.edges[0], g.edges[1], f) eq( deci.integrate_boole1(g.verts, f), reference ) eq( integrate_chebyshev(g.verts, f), reference ) g = Grid_1D.chebyshev(N, -1, +1) for f in ((lambda x: x), (lambda x: x**3), (lambda x: exp(x))): reference = deci.slow_integration(g.edges_dual[0], g.edges_dual[1], f) x = concatenate(([-1], g.verts_dual, [+1])) eq( deci.integrate_boole1(x, f), reference ) eq( integrate_chebyshev_dual(x, f), reference )
def newton_sqrt2(x, guess=2): if eq(newton_sqrt2(x, guess)**2, x): return guess else: guess = 0.5 * dv(guess + x, guess) return newton_sqrt2(x, guess)