Skip to content

Commit fe76a68

Browse files
committed
add has_same_labels function and test
1 parent 84eb829 commit fe76a68

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

‎rmgpy/molecule/graph.pxd‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,6 @@ cdef class Graph(object):
146146

147147
cpdef bint is_mapping_valid(self, Graph other, dict mapping, bint equivalent=?, bint strict=?, bint check_labels=?) except -2
148148

149+
cpdef bint has_same_labels(self, Graph other, list ignore_labels=?) except -2
150+
149151
cpdef list get_edges_in_cycle(self, list vertices, bint sort=?)

‎rmgpy/molecule/graph.pyx‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,36 @@ cdef class Graph(object):
914914
# mapping is valid
915915
return True
916916

917+
cpdef bint has_same_labels(self, Graph other, list ignore_labels=None) except -2:
918+
"""
919+
Returns ``True`` if `self` and `other` have the same labels on
920+
their vertices, with the same number of vertices bearing each
921+
label (i.e. the multisets of vertex labels are equal). Vertices
922+
without a label (an empty or unset `label` attribute) are ignored.
923+
Returns ``False`` otherwise.
924+
925+
If `ignore_labels` is given, vertices bearing any of those labels
926+
are excluded from the comparison entirely.
927+
"""
928+
cdef dict labels1, labels2
929+
cdef set skip
930+
931+
skip = set(ignore_labels) if ignore_labels else set()
932+
933+
labels1 = {}
934+
for vertex in self.vertices:
935+
label = vertex.label
936+
if label and label not in skip:
937+
labels1[label] = labels1.get(label, 0) + 1
938+
939+
labels2 = {}
940+
for vertex in other.vertices:
941+
label = vertex.label
942+
if label and label not in skip:
943+
labels2[label] = labels2.get(label, 0) + 1
944+
945+
return labels1 == labels2
946+
917947
cpdef list get_edges_in_cycle(self, list vertices, bint sort=False):
918948
"""
919949
For a given list of atoms comprising a ring, return the set of bonds

‎test/rmgpy/molecule/graphTest.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,52 @@ def test_subgraph_isomorphism(self):
528528
assert graph1.is_mapping_valid(graph2, mapping)
529529
assert graph1.is_mapping_valid(graph2, mapping)
530530

531+
def test_has_same_labels(self):
532+
"""
533+
Check the has_same_labels() function.
534+
"""
535+
from rmgpy.molecule.molecule import Atom
536+
537+
graph1 = Graph([Atom(label="*1"), Atom(label="*2"), Atom()])
538+
539+
# Same labels, same multiplicity of each label
540+
graph2 = Graph([Atom(label="*2"), Atom(label="*1"), Atom()])
541+
assert graph1.has_same_labels(graph2)
542+
assert graph2.has_same_labels(graph1)
543+
544+
# A graph always has the same labels as itself
545+
assert graph1.has_same_labels(graph1)
546+
547+
# Missing a label entirely
548+
graph3 = Graph([Atom(label="*1"), Atom(), Atom()])
549+
assert not graph1.has_same_labels(graph3)
550+
assert not graph3.has_same_labels(graph1)
551+
552+
# Same labels present, but with a different multiplicity
553+
# (two vertices labeled "*1" instead of one "*1" and one "*2")
554+
graph4 = Graph([Atom(label="*1"), Atom(label="*1"), Atom()])
555+
assert not graph1.has_same_labels(graph4)
556+
assert not graph4.has_same_labels(graph1)
557+
558+
# Extra labeled vertex on one side
559+
graph5 = Graph([Atom(label="*1"), Atom(label="*2"), Atom(label="*3")])
560+
assert not graph1.has_same_labels(graph5)
561+
assert not graph5.has_same_labels(graph1)
562+
563+
# ignore_labels excludes the given labels from the comparison entirely,
564+
# so a mismatch on an ignored label no longer counts against a match...
565+
assert graph1.has_same_labels(graph3, ignore_labels=["*2"])
566+
assert graph3.has_same_labels(graph1, ignore_labels=["*2"])
567+
assert graph1.has_same_labels(graph5, ignore_labels=["*3"])
568+
assert graph5.has_same_labels(graph1, ignore_labels=["*3"])
569+
570+
# ...but a mismatch on a label that wasn't ignored still counts
571+
assert not graph1.has_same_labels(graph3, ignore_labels=["*1"])
572+
assert not graph1.has_same_labels(graph5, ignore_labels=["*1"])
573+
574+
# ignoring every labeled vertex trivially matches any graph
575+
assert graph1.has_same_labels(graph4, ignore_labels=["*1", "*2"])
576+
531577
def test_intersection_isomorphism(self):
532578
"""
533579
Check the intersection isomorphism functions.

0 commit comments

Comments
 (0)