Skip to content

Commit

Permalink
Merge pull request #62 from RMeli/issue-61
Browse files Browse the repository at this point in the history
Fix number of nodes in graph-tool graphs with disconnected single nodes
  • Loading branch information
RMeli authored Jan 27, 2022
2 parents 3c11da5 + 6aa86c8 commit 9be79d7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

------------------------------------------------------------------------------

## Version 0.5.x

Date: XX/YY/20ZZ
Contributors: @RMeli

### Fixed

* Fixed inconsistent number of nodes with `graph-tool` for disconnected graphs [PR #61 | @RMeli]

### Added

* Warning for disconnected graphs [PR #61| @RMeli]

------------------------------------------------------------------------------

## Version 0.5.1

Date: 21/09/2021
Expand Down
10 changes: 10 additions & 0 deletions spyrmsd/graphs/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,20 @@ def graph_from_adjacency_matrix(
# Get upper triangular adjacency matrix
adj = np.triu(adjacency_matrix)

assert adj.shape[0] == adj.shape[1]
num_vertices = adj.shape[0]

G = gt.Graph(directed=False)
G.add_vertex(n=num_vertices)
G.add_edge_list(np.transpose(adj.nonzero()))

# Check if graph is connected, for warning
cc, _ = topology.label_components(G)
if set(cc.a) != {0}:
warnings.warn("Disconnected graph detected. Is this expected?")

if atomicnums is not None:
# TODO: Support more Python types
vprop = G.new_vertex_property("short") # Create property map (of C type short)
vprop.a = atomicnums # Assign atomic numbers to property map array
G.vertex_properties["atomicnum"] = vprop # Set property map
Expand Down
3 changes: 3 additions & 0 deletions spyrmsd/graphs/nx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def graph_from_adjacency_matrix(

G = nx.Graph(adjacency_matrix)

if not nx.is_connected(G):
warnings.warn("Disconnected graph detected. Is this expected?")

if atomicnums is not None:
attributes = {idx: atomicnum for idx, atomicnum in enumerate(atomicnums)}
nx.set_node_attributes(G, attributes, "atomicnum")
Expand Down
8 changes: 6 additions & 2 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def test_graph_from_adjacency_matrix_atomicnums(rawmol, mol) -> None:
)
def test_match_graphs_isomorphic(G1, G2) -> None:

with pytest.warns(UserWarning):
with pytest.warns(
UserWarning, match="No atomic number information stored on nodes."
):
isomorphisms = graph.match_graphs(G1, G2)

assert len(isomorphisms) != 0
Expand All @@ -124,5 +126,7 @@ def test_match_graphs_isomorphic(G1, G2) -> None:
)
def test_match_graphs_not_isomorphic(G1, G2) -> None:

with pytest.raises(ValueError), pytest.warns(UserWarning):
with pytest.raises(ValueError, match="Graphs are not isomorphic."), pytest.warns(
UserWarning, match="No atomic number information stored on nodes."
):
graph.match_graphs(G1, G2)
12 changes: 12 additions & 0 deletions tests/test_rmsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,18 @@ def test_rmsd_symmrmsd(index: int, RMSD: float, minimize: bool) -> None:
)


def test_rmsd_symmrmsd_disconnected_node() -> None:

c = np.array([[0.0, 1.0, 2.0], [1.0, 2.0, 3.0], [2.0, 3.0, 4.0]])
a = np.array([0, 1, 4])

# Adjacency matrix with disconnected node
A = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 0]])

with pytest.warns(UserWarning, match="Disconnected graph detected."):
assert rmsd.symmrmsd(c, c, a, a, A, A) == pytest.approx(0.0, abs=1e-5)


# Results obtained with OpenBabel
@pytest.mark.parametrize(
"minimize, referenceRMSDs",
Expand Down

0 comments on commit 9be79d7

Please sign in to comment.