Skip to content

Commit

Permalink
unify error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
RMeli committed Jan 28, 2022
1 parent 833a1f7 commit de7b0d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
8 changes: 8 additions & 0 deletions spyrmsd/graphs/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
warn_disconnected_graph: str = "Disconnected graph detected. Is this expected?"
warn_no_atomic_properties: str = (
"No atomic property information stored on nodes. Node matching is not performed..."
)

error_non_isomorphic_graphs: str = (
"Graphs are not isomorphic.\nMake sure graphs have the same connectivity."
)
18 changes: 9 additions & 9 deletions spyrmsd/graphs/gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import numpy as np
from graph_tool import generation, topology

from spyrmsd.graphs._common import (
error_non_isomorphic_graphs,
warn_disconnected_graph,
warn_no_atomic_properties,
)


# TODO: Implement all graph-tool supported types
def _c_type(numpy_dtype):
Expand Down Expand Up @@ -74,7 +80,7 @@ def graph_from_adjacency_matrix(
# 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?")
warnings.warn(warn_disconnected_graph)

if aprops is not None:
if not isinstance(aprops, np.ndarray):
Expand Down Expand Up @@ -122,20 +128,14 @@ def match_graphs(G1, G2) -> List[Tuple[List[int], List[int]]]:
subgraph=False,
)
except KeyError:
warnings.warn(
"No atomic property information stored on nodes. "
+ "Node matching is not performed..."
)
warnings.warn(warn_no_atomic_properties)

maps = topology.subgraph_isomorphism(G1, G2, subgraph=False)

# Check if graphs are actually isomorphic
if len(maps) == 0:
# TODO: Create a new exception
raise ValueError(
"Graphs are not isomorphic."
"\nMake sure graphs have the same connectivity."
)
raise ValueError(error_non_isomorphic_graphs)

n = num_vertices(G1)

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

from spyrmsd.graphs._common import (
error_non_isomorphic_graphs,
warn_disconnected_graph,
warn_no_atomic_properties,
)


def graph_from_adjacency_matrix(
adjacency_matrix: Union[np.ndarray, List[List[int]]],
Expand Down Expand Up @@ -32,7 +38,7 @@ def graph_from_adjacency_matrix(
G = nx.Graph(adjacency_matrix)

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

if aprops is not None:
attributes = {idx: aprops for idx, aprops in enumerate(aprops)}
Expand Down Expand Up @@ -77,10 +83,7 @@ def match_aprops(node1, node2):
# No node-matching check
node_match = None

warnings.warn(
"No atomic number information stored on nodes. "
+ "Node matching is not performed..."
)
warnings.warn(warn_no_atomic_properties)

else:
node_match = match_aprops
Expand All @@ -90,10 +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(
"Graphs are not isomorphic."
"\nMake sure graphs have the same connectivity."
)
raise ValueError(error_non_isomorphic_graphs)

return [
(list(isomorphism.keys()), list(isomorphism.values()))
Expand Down

0 comments on commit de7b0d7

Please sign in to comment.