Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exception specifically for graph isomorphism #65

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ Contributors: @RMeli

* Support for more types of node properties (including strings) with `graph-tool` [PR #64 | @RMeli]

### Changed

* `ValueError` exception into `NonIsomorphicGraphs(ValueError)` exception [PR #65 | @RMeli]

### Added

* Warning for disconnected graphs [PR #61| @RMeli]
Expand Down
6 changes: 6 additions & 0 deletions spyrmsd/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class NonIsomorphicGraphs(ValueError):
"""
Raised when graphs are not isomorphic
"""

pass
11 changes: 8 additions & 3 deletions spyrmsd/graphs/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from graph_tool import generation, topology

from spyrmsd.exceptions import NonIsomorphicGraphs
from spyrmsd.graphs._common import (
error_non_isomorphic_graphs,
warn_disconnected_graph,
Expand All @@ -27,6 +28,11 @@ def _c_type(numpy_dtype):
str
C type

Raises
------
ValueError
If the data type is not supported

Notes
-----
https://graph-tool.skewed.de/static/doc/quickstart.html#sec-property-maps
Expand Down Expand Up @@ -113,7 +119,7 @@ def match_graphs(G1, G2) -> List[Tuple[List[int], List[int]]]:

Raises
------
ValueError
NonIsomorphicGraphs
If the graphs `G1` and `G2` are not isomorphic
"""

Expand All @@ -134,8 +140,7 @@ def match_graphs(G1, G2) -> List[Tuple[List[int], List[int]]]:

# Check if graphs are actually isomorphic
if len(maps) == 0:
# TODO: Create a new exception
raise ValueError(error_non_isomorphic_graphs)
raise NonIsomorphicGraphs(error_non_isomorphic_graphs)

n = num_vertices(G1)

Expand Down
6 changes: 3 additions & 3 deletions spyrmsd/graphs/nx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import networkx as nx
import numpy as np

from spyrmsd.exceptions import NonIsomorphicGraphs
from spyrmsd.graphs._common import (
error_non_isomorphic_graphs,
warn_disconnected_graph,
Expand Down Expand Up @@ -65,7 +66,7 @@ def match_graphs(G1, G2) -> List[Tuple[List[int], List[int]]]:

Raises
------
ValueError
NonIsomorphicGraphs
If the graphs `G1` and `G2` are not isomorphic
"""

Expand All @@ -92,8 +93,7 @@ def match_aprops(node1, node2):

# Check if graphs are actually isomorphic
if not GM.is_isomorphic():
# TODO: Create a new exception
raise ValueError(error_non_isomorphic_graphs)
raise NonIsomorphicGraphs(error_non_isomorphic_graphs)

return [
(list(isomorphism.keys()), list(isomorphism.values()))
Expand Down
7 changes: 4 additions & 3 deletions tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

from spyrmsd import constants, graph, io, molecule
from spyrmsd.exceptions import NonIsomorphicGraphs
from tests import molecules


Expand Down Expand Up @@ -126,9 +127,9 @@ def test_match_graphs_isomorphic(G1, G2) -> None:
)
def test_match_graphs_not_isomorphic(G1, G2) -> None:

with pytest.raises(ValueError, match="Graphs are not isomorphic."), pytest.warns(
UserWarning, match="No atomic number information stored on nodes."
):
with pytest.raises(
NonIsomorphicGraphs, match="Graphs are not isomorphic."
), pytest.warns(UserWarning, match="No atomic number information stored on nodes."):
graph.match_graphs(G1, G2)


Expand Down