def test_graph_from_adjacency_matrix(mol: molecule.Molecule, n_bonds: int) -> None: G = mol.to_graph() assert graph.num_vertices(G) == len(mol) assert graph.num_edges(G) == n_bonds for idx, atomicnum in enumerate(mol.atomicnums): assert graph.vertex_property(G, "atomicnum", idx) == atomicnum
def test_adjacency_matrix_from_atomic_coordinates(mol: molecule.Molecule, n_bonds: int) -> None: A = graph.adjacency_matrix_from_atomic_coordinates(mol.atomicnums, mol.coordinates) G = graph.graph_from_adjacency_matrix(A) assert graph.num_vertices(G) == len(mol) assert graph.num_edges(G) == n_bonds
def test_graph_from_adjacency_matrix(mol) -> None: natoms = io.numatoms(mol) nbonds = io.numbonds(mol) A = io.adjacency_matrix(mol) assert A.shape == (natoms, natoms) assert np.alltrue(A == A.T) assert np.sum(A) == nbonds * 2 G = graph.graph_from_adjacency_matrix(A) assert graph.num_vertices(G) == natoms assert graph.num_edges(G) == nbonds
def test_adjacency_matrix_from_atomic_coordinates_distance() -> None: # Lithium hydride (LiH) # H and Li have very different covalent radii atomicnums = np.array([1, 3]) # Distance is the sum of covalent radii d = sum([constants.anum_to_covalentradius[anum] for anum in atomicnums]) # Distance between two atoms is barely enough to create a bond # If the covalent radii are not correct, no bond will be created coordinates = np.array( [[0, 0, 0], [0, 0, d + constants.connectivity_tolerance - 0.01]]) A = graph.adjacency_matrix_from_atomic_coordinates(atomicnums, coordinates) G = graph.graph_from_adjacency_matrix(A) assert graph.num_edges(G) == 1
def test_graph_from_atomic_coordinates_perception(mol: molecule.Molecule, n_bonds: int) -> None: m = copy.deepcopy(mol) delattr(m, "adjacency_matrix") m.G = None with pytest.warns(UserWarning): # Uses automatic bond perception G = m.to_graph() assert graph.num_vertices(G) == len(m) assert graph.num_edges(G) == n_bonds for idx, atomicnum in enumerate(mol.atomicnums): assert graph.vertex_property(G, "aprops", idx) == atomicnum
def test_graph_from_adjacency_matrix_atomicnums(rawmol, mol) -> None: natoms = io.numatoms(rawmol) nbonds = io.numbonds(rawmol) A = io.adjacency_matrix(rawmol) assert len(mol) == natoms assert mol.adjacency_matrix.shape == (natoms, natoms) assert np.alltrue(mol.adjacency_matrix == A) assert np.sum(mol.adjacency_matrix) == nbonds * 2 G = mol.to_graph() assert graph.num_vertices(G) == natoms assert graph.num_edges(G) == nbonds for idx, atomicnum in enumerate(mol.atomicnums): assert graph.vertex_property(G, "atomicnum", idx) == atomicnum
def test_build_graph_node_features(property) -> None: A = np.array([[0, 1, 1], [1, 0, 0], [1, 0, 1]]) G = graph.graph_from_adjacency_matrix(A, property) assert graph.num_edges(G) == 3