def build(self): g1 = self.g1 g2 = self.g2 # We will assume integer weights only. g1.add_edge('A', 'B', color='green', weight=0, size=.5) g1.add_edge('A', 'B', color='red', weight=1, size=.35) g1.add_edge('A', 'B', color='red', weight=2, size=.65) g2.add_edge('C', 'D', color='green', weight=1, size=.5) g2.add_edge('C', 'D', color='red', weight=0, size=.45) g2.add_edge('C', 'D', color='red', weight=2, size=.65) if g1.is_multigraph(): self.em = iso.numerical_multiedge_match('weight', 1) self.emc = iso.categorical_multiedge_match('color', '') self.emcm = iso.categorical_multiedge_match(['color', 'weight'], ['', 1]) self.emg1 = iso.generic_multiedge_match('color', 'red', eq) self.emg2 = iso.generic_multiedge_match( ['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, iso.matchhelpers.close]) else: self.em = iso.numerical_edge_match('weight', 1) self.emc = iso.categorical_edge_match('color', '') self.emcm = iso.categorical_edge_match(['color', 'weight'], ['', 1]) self.emg1 = iso.generic_multiedge_match('color', 'red', eq) self.emg2 = iso.generic_edge_match( ['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, iso.matchhelpers.close])
def build(self): g1 = self.g1 g2 = self.g2 # We will assume integer weights only. g1.add_edge('A', 'B', color='green', weight=0, size=.5) g1.add_edge('A', 'B', color='red', weight=1, size=.35) g1.add_edge('A', 'B', color='red', weight=2, size=.65) g2.add_edge('C', 'D', color='green', weight=1, size=.5) g2.add_edge('C', 'D', color='red', weight=0, size=.45) g2.add_edge('C', 'D', color='red', weight=2, size=.65) if g1.is_multigraph(): self.em = iso.numerical_multiedge_match('weight', 1) self.emc = iso.categorical_multiedge_match('color', '') self.emcm = iso.categorical_multiedge_match(['color', 'weight'], ['', 1]) self.emg1 = iso.generic_multiedge_match('color', 'red', eq) self.emg2 = iso.generic_multiedge_match(['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, iso.matchhelpers.close]) else: self.em = iso.numerical_edge_match('weight', 1) self.emc = iso.categorical_edge_match('color', '') self.emcm = iso.categorical_edge_match(['color', 'weight'], ['', 1]) self.emg1 = iso.generic_multiedge_match('color', 'red', eq) self.emg2 = iso.generic_edge_match(['color', 'weight', 'size'], ['red', 1, .5], [eq, eq, iso.matchhelpers.close])
def build(self): g1 = self.g1 g2 = self.g2 # We will assume integer weights only. g1.add_edge("A", "B", color="green", weight=0, size=0.5) g1.add_edge("A", "B", color="red", weight=1, size=0.35) g1.add_edge("A", "B", color="red", weight=2, size=0.65) g2.add_edge("C", "D", color="green", weight=1, size=0.5) g2.add_edge("C", "D", color="red", weight=0, size=0.45) g2.add_edge("C", "D", color="red", weight=2, size=0.65) if g1.is_multigraph(): self.em = iso.numerical_multiedge_match("weight", 1) self.emc = iso.categorical_multiedge_match("color", "") self.emcm = iso.categorical_multiedge_match(["color", "weight"], ["", 1]) self.emg1 = iso.generic_multiedge_match("color", "red", eq) self.emg2 = iso.generic_multiedge_match( ["color", "weight", "size"], ["red", 1, 0.5], [eq, eq, iso.matchhelpers.close], ) else: self.em = iso.numerical_edge_match("weight", 1) self.emc = iso.categorical_edge_match("color", "") self.emcm = iso.categorical_edge_match(["color", "weight"], ["", 1]) self.emg1 = iso.generic_multiedge_match("color", "red", eq) self.emg2 = iso.generic_edge_match( ["color", "weight", "size"], ["red", 1, 0.5], [eq, eq, iso.matchhelpers.close], )
def test_generic_multiedge_match(self): full_match = iso.generic_multiedge_match(['id', 'flowMin', 'flowMax'], [None]*3, [eq]*3) flow_match = iso.generic_multiedge_match(['flowMin', 'flowMax'], [None]*2, [eq]*2) min_flow_match = iso.generic_multiedge_match('flowMin', None, eq) id_match = iso.generic_multiedge_match('id', None, eq) assert_true(flow_match(self.G1[1][2], self.G2[2][3])) assert_true(min_flow_match(self.G1[1][2], self.G2[2][3])) assert_true(id_match(self.G1[1][2], self.G2[2][3])) assert_true(full_match(self.G1[1][2], self.G2[2][3])) assert_true(flow_match(self.G3[3][4], self.G4[4][5])) assert_true(min_flow_match(self.G3[3][4], self.G4[4][5])) assert_false(id_match(self.G3[3][4], self.G4[4][5])) assert_false(full_match(self.G3[3][4], self.G4[4][5]))
def test_generic_multiedge_match(self): full_match = iso.generic_multiedge_match(['id', 'flowMin', 'flowMax'], [None] * 3, [eq] * 3) flow_match = iso.generic_multiedge_match(['flowMin', 'flowMax'], [None] * 2, [eq] * 2) min_flow_match = iso.generic_multiedge_match('flowMin', None, eq) id_match = iso.generic_multiedge_match('id', None, eq) assert flow_match(self.G1[1][2], self.G2[2][3]) assert min_flow_match(self.G1[1][2], self.G2[2][3]) assert id_match(self.G1[1][2], self.G2[2][3]) assert full_match(self.G1[1][2], self.G2[2][3]) assert flow_match(self.G3[3][4], self.G4[4][5]) assert min_flow_match(self.G3[3][4], self.G4[4][5]) assert not id_match(self.G3[3][4], self.G4[4][5]) assert not full_match(self.G3[3][4], self.G4[4][5])
def recognize(self,graph,nodematchattr='function',edgematchattr='flowType'): ''' Return a list of node id tuples that match the rule Required arguments: graph -- a Networkx directed graph nodematchattr -- node attribute to match on edgematchattr -- edge attribute to match on ''' def node_function_match(label1,label2): function1,flow1 = label1.split('_') function2,flow2 = label2.split('_') functionIsWildcard = function1 == '*' or function2 == '*' flowIsWildcard = flow1 == '*' or flow2 == '*' if function1 == function2 and flow1 == flow2: return True elif function1 == function2 and flowIsWildcard: return True elif functionIsWildcard and flow1 == flow2: return True elif functionIsWildcard and flowIsWildcard: return True else: return False def edge_flow_match(label1,label2): flow1,*attr1 = label1.split('_') flow2,*attr2 = label2.split('_') flowIsWildcard = flow1 == '*' or flow2 == '*' if flow1 == flow2: return True elif flowIsWildcard: return True else: return False GM_content = iso.DiGraphMatcher(graph,self.lhs, node_match=iso.generic_node_match('function',None,node_function_match), edge_match=iso.generic_multiedge_match('flowType',None,edge_flow_match)) self.recognize_mappings = [im for im in GM_content.subgraph_isomorphisms_iter()]