Skip to content

Commit

Permalink
Updating docstring from is_isomorphic
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Ellison committed Jun 30, 2011
1 parent 5da17c3 commit 8c0b776
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
29 changes: 27 additions & 2 deletions networkx/algorithms/isomorphism/isomorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,36 @@ def is_isomorphic(G1, G2, node_match=None, edge_match=None):
Works for Graph, DiGraph, MultiGraph, and MultiDiGraph.
Examples
--------
>>> import networkx as nx
>>> import networkx.isomorphism as nxiso
For (di)graphs G1 and G2, using 'weight' edge attribute (default: 1)
>>> em = nxiso.numerical_edge_match('weight', 1)
>>> nx.is_isomorphic(G1, G2, edge_match=em)
For (multi)(di)graphs G1 and G2, using 'fill' node attribute (default: '')
>>> nm = nxiso.categorical_node_match('fill', 'red')
>>> nx.is_isomorphic(G1, G2, node_match=nm)
For multi(di)graphs G1 and G2, using 'weight' edge attribute (default: 2.3)
>>> em = nxiso.numerical_multiedge_match('weight', 2.3)
>>> nx.is_isomorphic(G1, G2, edge_match=em)
For (di)graphs G1 and G2, using 'weight' and 'linewidth' edge attributes
with default values 1 and 2.5. Also using 'fill' node attribute with
default value 'red'.
>>> em = nxiso.numerical_multiedge_match(['weight', 'lw'], [1, 2.5])
>>> nm = nxiso.categorical_node_match('fill', 'red')
>>> nx.is_isomorphic(G1, G2, edge_match=em, node_match=nm)
See Also
--------
:mod:`isomorphvf2`, :mod:`matchelpers`
numerical_node_match, numerical_edge_match, numerical_multiedge_match
categorical_node_match, categorical_edge_match, categorical_multiedge_match
numerical_node_match, numerical_edge_match, numerical_multiedge_match
categorical_node_match, categorical_edge_match, categorical_multiedge_match
"""
if G1.is_directed() and G2.is_directed():
Expand Down
36 changes: 18 additions & 18 deletions networkx/algorithms/isomorphism/matchhelpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Various functions which help end users define customize node_match and
Various functions which help end users define customize node_match and
edge_match functions to use during isomorphism checks.
"""

Expand Down Expand Up @@ -59,7 +59,7 @@ def close(x, y, rtol=1.0000000000000001e-05, atol=1e-08):

categorical_doc = """
Returns a comparison function for a categorical node attribute.
The value(s) of the attr(s) must be hashable and comparable via the ==
operator since they are placed into a set([]) object. If the sets from
G1 and G2 are the same, then the constructed function returns True.
Expand All @@ -72,7 +72,7 @@ def close(x, y, rtol=1.0000000000000001e-05, atol=1e-08):
default : value | list
The default value for the categorical node attribute, or a list of
default values for the categorical node attributes.
Returns
-------
match : function
Expand All @@ -93,7 +93,7 @@ def match(data1, data2):
attrs = list(zip(attr, default)) # Python 3
def match(data1, data2):
values1 = set([data1.get(attr, d) for attr, d in attrs])
values2 = set([data2.get(attr, d) for attr, d in attrs])
values2 = set([data2.get(attr, d) for attr, d in attrs])
return values1 == values2
return match

Expand Down Expand Up @@ -129,8 +129,8 @@ def match(datasets1, datasets2):

numerical_doc = """
Returns a comparison function for a numerical node attribute.
The value(s) of the attr(s) must be numerical and sortable. If the
The value(s) of the attr(s) must be numerical and sortable. If the
sorted list of values from G1 and G2 are the same within some
tolerance, then the constructed function returns True.
Expand All @@ -146,7 +146,7 @@ def match(datasets1, datasets2):
The relative error tolerance.
atol : float
The absolute error tolerance.
Returns
-------
match : function
Expand All @@ -162,7 +162,7 @@ def match(datasets1, datasets2):
def numerical_node_match(attr, default, rtol=1.0000000000000001e-05, atol=1e-08):
if nx.utils.is_string_like(attr):
def match(data1, data2):
return close(data1.get(attr, default),
return close(data1.get(attr, default),
data2.get(attr, default),
rtol=rtol, atol=atol)
else:
Expand All @@ -182,7 +182,7 @@ def match(datasets1, datasets2):
values2 = sorted([data.get(attr, default) for data in datasets2.values()])
return allclose(values1, values2, rtol=rtol, atol=atol)
else:
attrs = list(zip(attr, default)) # Python 3
attrs = list(zip(attr, default)) # Python 3
def match(datasets1, datasets2):
values1 = []
for data1 in datasets1.values():
Expand Down Expand Up @@ -219,15 +219,15 @@ def match(datasets1, datasets2):
Parameters
----------
attr : string | list
The node attribute to compare, or a list of node attributes
The node attribute to compare, or a list of node attributes
to compare.
default : value | list
The default value for the node attribute, or a list of
default values for the node attributes.
op : callable | list
The operator to use when comparing attribute values, or a list
of operators to use when comparing values for each attribute.
Returns
-------
match : function
Expand All @@ -242,7 +242,7 @@ def match(datasets1, datasets2):
>>> nm = generic_node_match(['weight', 'color'], [1.0, 'red'], [close, eq])
"""

def generic_node_match(attr, default, op):
if nx.utils.is_string_like(attr):
def match(data1, data2):
Expand All @@ -264,22 +264,22 @@ def generic_multiedge_match(attr, default, op):
The value(s) of the attr(s) are compared using the specified
operators. If all the attributes are equal, then the constructed
function returns True. Potentially, the constructed edge_match
function returns True. Potentially, the constructed edge_match
function can be slow since it must verify that no isomorphism
exists between the multiedges before it returns False.
Parameters
----------
attr : string | list
The edge attribute to compare, or a list of node attributes
The edge attribute to compare, or a list of node attributes
to compare.
default : value | list
The default value for the edge attribute, or a list of
default values for the dgeattributes.
op : callable | list
The operator to use when comparing attribute values, or a list
of operators to use when comparing values for each attribute.
Returns
-------
match : function
Expand All @@ -291,8 +291,8 @@ def generic_multiedge_match(attr, default, op):
>>> from nx.isomorphism import close
>>> nm = generic_node_match('weight', 1.0, close)
>>> nm = generic_node_match('color', 'red', eq)
>>> nm = generic_node_match(['weight', 'color'],
... [1.0, 'red'],
>>> nm = generic_node_match(['weight', 'color'],
... [1.0, 'red'],
... [close, eq])
...
Expand All @@ -316,7 +316,7 @@ def match(datasets1, datasets2):
# Then there are no isomorphisms between the multiedges.
return False
else:
attrs = list(zip(attr, default)) # Python 3
attrs = list(zip(attr, default)) # Python 3
def match(datasets1, datasets2):
values1 = []
for data1 in datasets1.values():
Expand Down
12 changes: 6 additions & 6 deletions networkx/algorithms/isomorphism/tests/test_vf2userfunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ def setUp(self):
self.build()

def build(self):

self.nm = nxiso.categorical_node_match('color', '')
self.em = nxiso.numerical_edge_match('weight', 1)

self.g1.add_node('A', color='red')
self.g2.add_node('C', color='blue')

Expand Down Expand Up @@ -130,7 +130,7 @@ def setUp(self):
self.g2 = nx.MultiGraph()
self.GM = nx.MultiGraphMatcher
self.build()

def build(self):
g1 = self.g1
g2 = self.g2
Expand All @@ -154,8 +154,8 @@ def build(self):
self.em = nxiso.numerical_edge_match('weight', 1)
self.emc = nxiso.categorical_edge_match('color', '')
self.emcm = nxiso.categorical_edge_match(['color', 'weight'], ['', 1])
self.emg1 = nxiso.generic_multiedge_match('color', 'red', eq)
self.emg2 = nxiso.generic_edge_match(['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, nxiso.matchhelpers.close])
self.emg1 = nxiso.generic_multiedge_match('color', 'red', eq)
self.emg2 = nxiso.generic_edge_match(['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, nxiso.matchhelpers.close])

def test_weights_only(self):
assert_true( nx.is_isomorphic(self.g1, self.g2, edge_match=self.em) )
Expand All @@ -167,7 +167,7 @@ def test_colors_only(self):
def test_colorsandweights(self):
gm = self.GM(self.g1, self.g2, edge_match=self.emcm)
assert_false( gm.is_isomorphic() )

def test_generic1(self):
gm = self.GM(self.g1, self.g2, edge_match=self.emg1)
assert_true( gm.is_isomorphic() )
Expand Down

0 comments on commit 8c0b776

Please sign in to comment.