-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest_graph.py
More file actions
211 lines (147 loc) Β· 6.1 KB
/
Copy pathtest_graph.py
File metadata and controls
211 lines (147 loc) Β· 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import numpy as np
import pytest
import spyrmsd
from spyrmsd import constants, graph, io
from spyrmsd.exceptions import NonIsomorphicGraphs
from spyrmsd.graphs import _common as gc
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_adjacency_matrix_from_atomic_coordinates(mol) -> None:
A = graph.adjacency_matrix_from_atomic_coordinates(
mol.mol.atomicnums, mol.mol.coordinates
)
G = graph.graph_from_adjacency_matrix(A)
assert graph.num_vertices(G) == mol.n_atoms
assert graph.num_edges(G) == mol.n_bonds
def test_adjacency_matrix_from_mol(rawmol) -> None:
natoms = io.numatoms(rawmol.rawmol)
nbonds = io.numbonds(rawmol.rawmol)
assert natoms == rawmol.n_atoms
assert nbonds == rawmol.n_bonds
A = io.adjacency_matrix(rawmol.rawmol)
assert A.shape == (natoms, natoms)
assert np.all(A == A.T)
assert np.sum(A) == nbonds * 2
for i, j in io.bonds(rawmol.rawmol):
assert A[i, j] == 1
def test_graph_from_adjacency_matrix(rawmol) -> None:
natoms = io.numatoms(rawmol.rawmol)
nbonds = io.numbonds(rawmol.rawmol)
assert natoms == rawmol.n_atoms
assert nbonds == rawmol.n_bonds
A = io.adjacency_matrix(rawmol.rawmol)
assert A.shape == (natoms, natoms)
assert np.all(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_graph_from_adjacency_matrix_atomicnums(rawmol) -> None:
mol = rawmol.mol
natoms = io.numatoms(rawmol.rawmol)
nbonds = io.numbonds(rawmol.rawmol)
A = io.adjacency_matrix(rawmol.rawmol)
assert len(mol) == natoms
assert mol.adjacency_matrix.shape == (natoms, natoms)
assert np.all(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, "aprops", idx) == atomicnum
@pytest.mark.parametrize("n", list(range(2, 5)))
def test_match_graphs_isomorphic_lattice(n) -> None:
G1 = graph.lattice(n, n)
G2 = graph.lattice(n, n)
with pytest.warns(UserWarning, match=gc.warn_no_atomic_properties):
isomorphisms = graph.match_graphs(G1, G2)
assert len(isomorphisms) != 0
@pytest.mark.parametrize("n", list(range(2, 5)))
def test_match_graphs_isomorphic_cycle(n) -> None:
G1 = graph.cycle(n)
G2 = graph.cycle(n)
with pytest.warns(UserWarning, match=gc.warn_no_atomic_properties):
isomorphisms = graph.match_graphs(G1, G2)
assert len(isomorphisms) != 0
@pytest.mark.parametrize("n", list(range(2, 5)))
def test_match_graphs_not_isomorphic_lattice(n) -> None:
G1 = graph.lattice(n, n)
G2 = graph.lattice(n + 1, n)
with (
pytest.raises(NonIsomorphicGraphs, match=gc.error_non_isomorphic_graphs),
pytest.warns(UserWarning, match=gc.warn_no_atomic_properties),
):
graph.match_graphs(G1, G2)
@pytest.mark.parametrize("n", range(2, 5))
def test_match_graphs_not_isomorphic_cycle(n) -> None:
G1 = graph.cycle(n)
G2 = graph.cycle(n + 1)
with (
pytest.raises(NonIsomorphicGraphs, match=gc.error_non_isomorphic_graphs),
pytest.warns(UserWarning, match=gc.warn_no_atomic_properties),
):
graph.match_graphs(G1, G2)
@pytest.mark.parametrize(
"property",
[
np.array([0, 1, 2], dtype=int),
np.array([0.1, 1.2, 2.3], dtype=float),
np.array(["H", "H", "H"], dtype=str),
np.array(["Csp3", "Csp3", "Csp3"], dtype=str),
["LongProperty", "LongPropertyProperty", "LongPropertyProperty"],
],
)
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
@pytest.mark.skipif(
spyrmsd.get_backend() != "graph_tool",
reason="NetworkX supports all Python objects as node properties.",
)
def test_build_graph_node_features_unsupported() -> None:
if spyrmsd.get_backend() != "graph-tool":
pytest.skip(
"NetworkX and RustworkX support all Python objects as node properties."
)
A = np.array([[0, 1, 1], [1, 0, 0], [1, 0, 1]])
property = [True, False, True]
with pytest.raises(ValueError, match="Unsupported property type:"):
_ = graph.graph_from_adjacency_matrix(A, property)
@pytest.mark.skipif(
# Run test if all supported backends are installed
not set(spyrmsd.graph._supported_backends) <= set(spyrmsd.available_backends),
reason="Not all of the required backends are installed",
)
def test_set_backend() -> None:
import graph_tool as gt
import networkx as nx
import rustworkx as rx
A = np.array([[0, 1, 1], [1, 0, 0], [1, 0, 1]])
spyrmsd.set_backend("networkx")
assert spyrmsd.get_backend() == "networkx"
Gnx = graph.graph_from_adjacency_matrix(A)
assert isinstance(Gnx, nx.Graph)
spyrmsd.set_backend("graph-tool")
assert spyrmsd.get_backend() == "graph_tool"
Ggt = graph.graph_from_adjacency_matrix(A)
assert isinstance(Ggt, gt.Graph)
spyrmsd.set_backend("rustworkx")
assert spyrmsd.get_backend() == "rustworkx"
Grx = graph.graph_from_adjacency_matrix(A)
assert isinstance(Grx, rx.PyGraph)
with pytest.raises(ValueError, match="backend is not recognized or supported"):
spyrmsd.set_backend("unknown")