def testAddEmptyGraph(self): gr1 = pygraph.digraph() gr1.generate(25, 100) gr1c = copy.copy(gr1) gr2 = pygraph.digraph() gr1.add_graph(gr2) self.assertTrue(gr1.nodes() == gr1c.nodes()) self.assertTrue(gr1.edges() == gr1c.edges())
def testAddGraph(self): gr1 = pygraph.digraph() gr1.generate(25, 100) gr2 = pygraph.digraph() gr2.generate(40, 200) gr1.add_graph(gr2) for each in gr2.nodes(): self.assertTrue(each in gr1) for each in gr2.edges(): self.assertTrue(each in gr1.edges())
def graphize(self): try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() modules = [self.topModule] + self.moduleList # first, we must add all the nodes. Only then can we add all the edges self.graph.add_nodes(modules) # here we have a strictly directed graph, so we need only insert directed edges for module in modules: def checkParent(child): if module.name == child.parent: return True else: return False children = filter(checkParent, modules) for child in children: # due to compatibility issues, we need these try catch to pick the # right function prototype. try: self.graph.add_edge(module, child) except TypeError: self.graph.add_edge((module, child))
def testNoCycleDigraph2(self): G = pygraph.digraph() G.add_nodes([1,2,3]) G.add_edge(1,2) G.add_edge(1,3) G.add_edge(2,3) assert G.find_cycle() == []
def testGraphComplete(self): gr = pygraph.digraph() gr.add_nodes(xrange(10)) gr.complete() for i in xrange(10): for j in range(10): self.assertTrue((i, j) in gr.edges() or i == j)
def testDigraph(self): def has_parent(node, list): for each in list: if gr.has_edge(each, node): return True return (ts == []) gr = pygraph.digraph() gr.add_nodes([0,1,2,3,4,5,6,7,8]) gr.add_edge(0,1) gr.add_edge(0,2) gr.add_edge(1,3) gr.add_edge(1,4) gr.add_edge(2,5) gr.add_edge(2,6) gr.add_edge(3,7) gr.add_edge(8,0) gr.add_edge(7,5) gr.add_edge(3,0) gr.add_edge(4,3) gr.add_edge(2,7) gr.add_edge(6,0) ts = topological_sorting(gr) while (ts): x = ts.pop() assert has_parent(x, ts)
def graphizeSynth(self): try: self.graphSynth = pygraph.digraph() except (NameError, AttributeError): self.graphSynth = digraph() modulesUnfiltered = [self.topModule] + self.moduleList # first, we must add all the nodes. Only then can we add all the edges # filter by synthesis boundaries modules = filter(checkSynth, modulesUnfiltered) self.graphSynth.add_nodes(modules) # here we have a strictly directed graph, so we need only insert directed edges for module in modules: def checkParent(child): if module.name == child.synthParent: return True else: return False children = filter(checkParent, modules) for child in children: #print "Adding p: " + module.name + " c: " + child.name try: self.graphSynth.add_edge(module,child) except TypeError: self.graphSynth.add_edge((module,child))
def graphize(self): try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() modules = [self.topModule] + self.moduleList # first, we must add all the nodes. Only then can we add all the edges self.graph.add_nodes(modules) # here we have a strictly directed graph, so we need only insert directed edges for module in modules: def checkParent(child): if module.name == child.parent: return True else: return False children = filter(checkParent, modules) for child in children: # due to compatibility issues, we need these try catch to pick the # right function prototype. try: self.graph.add_edge(module,child) except TypeError: self.graph.add_edge((module,child))
def testSanityDigraph(self): G = pygraph.digraph() G.generate(100, 500) st, lo = G.breadth_first_search() for each in G: if (st[each] != None): assert lo.index(each) > lo.index(st[each])
def graphizeSynth(self): try: self.graphSynth = pygraph.digraph() except (NameError, AttributeError): self.graphSynth = digraph() modulesUnfiltered = [self.topModule] + self.moduleList # first, we must add all the nodes. Only then can we add all the edges # filter by synthesis boundaries modules = filter(checkSynth, modulesUnfiltered) self.graphSynth.add_nodes(modules) # here we have a strictly directed graph, so we need only insert directed edges for module in modules: def checkParent(child): if module.name == child.synthParent: return True else: return False children = filter(checkParent, modules) for child in children: #print "Adding p: " + module.name + " c: " + child.name try: self.graphSynth.add_edge(module, child) except TypeError: self.graphSynth.add_edge((module, child))
def testReadDigraphDot(self): dot = ['digraph graphname {', '1;', '2;', '3;', '4;', '5;', '1 -> 2;', '4 -> 5;', '1 -> 5;', '2 -> 3;', '2 -> 4;', '3 -> 5;', '}', ''] dot = "\n".join(dot) gr = pygraph.digraph() gr.read(dot, 'dot') self._check_nodes(gr, dot) self._check_arrows(gr, dot)
def sort_out_covering_exons (cursor, exons): # havana is manually curated and gets priority is_ensembl = {} is_havana = {} for exon in exons: logic_name = get_logic_name(cursor, exon.analysis_id) is_ensembl[exon] = ('ensembl' in logic_name) is_havana [exon] = ('havana' in logic_name) dg = digraph() dg.add_nodes(exons) for exon1, exon2 in combinations(dg.nodes(),2): master, covered = find_master(cursor, exon1,exon2,is_ensembl,is_havana) if master is not None and covered is not None: dg.add_edge(master,covered) assert not find_cycle(dg) clusters = dict(((k,v) for k,v in accessibility(dg).iteritems() if not dg.incidents(k))) for k in clusters: clusters[k].remove(k) for master_exon, covered_list in clusters.iteritems(): master_exon.covering_exon = -1 # nobody's covering this guy master_exon.covering_exon_known = -1 # formal for covered_exon in covered_list: covered_exon.covering_exon = master_exon.exon_id covered_exon.covering_exon_known = master_exon.is_known
def testNodeRemoval(self): gr = pygraph.digraph() gr.generate(10, 90) gr.del_node(0) self.assertTrue(0 not in gr) for each, other in gr.edges(): self.assertTrue(each in gr) self.assertTrue(other in gr)
def testRandomGraph(self): gr = pygraph.digraph() gr.generate(100, 500) self.assertEqual(gr.nodes(), range(100)) self.assertEqual(len(gr.edges()), 500) for each, other in gr.edges(): self.assertTrue(each in gr) self.assertTrue(other in gr)
def testGraphInverse(self): gr = pygraph.digraph() gr.generate(50, 300) inv = gr.inverse() for each in gr.edges(): self.assertTrue(each not in inv.edges()) for each in inv.edges(): self.assertTrue(each not in gr.edges())
def testSanityDigraph(self): G = pygraph.digraph() G.generate(100, 500) st, pre, post = G.depth_first_search() for each in G: if (st[each] != None): assert pre.index(each) > pre.index(st[each]) assert post.index(each) < post.index(st[each])
def __init__(self, connections): self.unmatchedChannels = False self.modules = {} for connection in connections: # give channels unit weight connection.activity = 1 if (not connection.module_name in self.modules): # for now, type and name are the same if (DUMP_GRAPH_DEBUG): print "Adding module, Found channel " + connection.name + " with module " + connection.module_name self.modules[connection.module_name] = LIModule(connection.module_name,\ connection.module_name) if (isinstance(connection, LIChannel)): self.modules[connection.module_name].addChannel(connection) elif (isinstance(connection, LIService)): self.modules[connection.module_name].addService(connection) else: self.modules[connection.module_name].addChain(connection) # let's match up all those connections self.matchGraphChannels() # now that we have a dictionary, we can create a graph try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() self.graph.add_nodes(self.modules.values()) self.weights = {} # unfortunately pygraph does not support multiedges # add edges - for now all edges have unit weight for module in self.modules.values(): for channel in module.channels: # depending on what we are doing with the graph, # unmatched channels may not be an error. We will # instead mark the object in case the caller cares if (not (channel.matched or channel.optional)): self.unmatchedChannels = True continue # It is possible that optional channels do not have a match if (not channel.matched): continue #Only add an edge if the channel is a source. if (channel.isSource()): if (not self.graph.has_edge( (module, channel.partnerModule))): self.graph.add_edge((module, channel.partnerModule)) self.weights[( module, channel.partnerModule)] = channel.activity else: self.weights[( module, channel.partnerModule)] += channel.activity
def testNoCycleDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(3, 5) assert G.find_cycle() == []
def testMisleadingDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(3, 5) G.add_edge(3, 1) assert G.find_cycle() == [1, 2, 3]
def testSmallCycleDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(2, 1) # Cycle: 1-2 assert G.find_cycle() == [1,2]
def testDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(5, 1) # Cycle: 1-2-4-5 assert find_cycle(G) == [1,2,4,5]
def testDigraphDFS(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5, 6]) G.add_edge(1, 2) G.add_edge(1, 3) G.add_edge(2, 4) G.add_edge(4, 3) G.add_edge(5, 1) G.add_edge(3, 5) G.add_edge(5, 6) st, pre, post = depth_first_search(G, 1, filter=filters.find(5)) assert st == {1: None, 2: 1, 3: 4, 4: 2, 5: 3}
def testDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(1, 5) G.add_edge(3, 5) st, lo = G.breadth_first_search() assert st == {1: None, 2: 1, 3: 2, 4: 2, 5: 1} assert lo == [1, 2, 5, 3, 4]
def testWriteDigraphDot(self): gr = pygraph.digraph() gr.add_nodes([1, 2, 3, 4, 5]) gr.add_edge(1, 2) gr.add_edge(2, 3) gr.add_edge(2, 4) gr.add_edge(4, 5) gr.add_edge(1, 5) gr.add_edge(3, 5) dotstr = dot.write(gr) self._check_nodes(gr, dotstr) self._check_arrows(gr, dotstr)
def __init__(self, connections): self.unmatchedChannels = False self.modules = {} for connection in connections: # give channels unit weight connection.activity = 1 if (not connection.module_name in self.modules): # for now, type and name are the same if(DUMP_GRAPH_DEBUG): print "Adding module, Found channel " + connection.name + " with module " + connection.module_name self.modules[connection.module_name] = LIModule(connection.module_name,\ connection.module_name) if (isinstance(connection, LIChannel)): self.modules[connection.module_name].addChannel(connection) else: self.modules[connection.module_name].addChain(connection) # let's match up all those connections self.matchGraphChannels() # now that we have a dictionary, we can create a graph try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() self.graph.add_nodes(self.modules.values()) self.weights = {} # unfortunately pygraph does not support multiedges # add edges - for now all edges have unit weight for module in self.modules.values(): for channel in module.channels: # depending on what we are doing with the graph, # unmatched channels may not be an error. We will # instead mark the object in case the caller cares if (not (channel.matched or channel.optional)): self.unmatchedChannels = True continue # It is possible that optional channels do not have a match if (not channel.matched): continue #Only add an edge if the channel is a source. if (channel.isSource()): if (not self.graph.has_edge((module, channel.partnerModule))): self.graph.add_edge((module, channel.partnerModule)) self.weights[(module, channel.partnerModule)] = channel.activity else: self.weights[(module, channel.partnerModule)] += channel.activity
def testDigraphBFS(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5, 6]) G.add_edge(1, 2) G.add_edge(1, 3) G.add_edge(2, 4) G.add_edge(4, 3) G.add_edge(5, 1) G.add_edge(3, 5) G.add_edge(5, 6) st, lo = G.breadth_first_search(1, filter=filters.find(5)) assert st == {1: None, 2: 1, 3: 1, 4: 2, 5: 3}
def testDigraph(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5]) G.add_edge(1, 2) G.add_edge(2, 3) G.add_edge(2, 4) G.add_edge(4, 5) G.add_edge(1, 5) G.add_edge(3, 5) st, pre, post = G.depth_first_search() assert st == {1: None, 2: 1, 3: 2, 4: 2, 5: 3} assert pre == [1, 2, 3, 5, 4] assert post == [5, 3, 4, 2, 1]
def testWriteDigraphXML(self): gr = pygraph.digraph() gr.add_nodes([1, 2, 3, 4, 5]) gr.add_edge(1, 2) gr.add_edge(2, 3) gr.add_edge(2, 4) gr.add_edge(4, 5) gr.add_edge(1, 5) gr.add_edge(3, 5) xml = markup.write(gr) self._check_nodes(gr, xml) self._check_edges(gr, xml) print xml
def graphize(self): try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() # first, we must add all the nodes. Only then can we add all the edges for platform in self.platforms: self.graph.add_nodes([platform]) for platform in self.platforms: egresses = self.platforms[platform].getEgresses() ingresses = self.platforms[platform].getIngresses() for egress in egresses: # search for paired ingress - a specification is illegal if # egress/ingress pairing is not made error = 0 if(egresses[egress].endpointName in self.platforms): if(platform in (self.platforms[egresses[egress].endpointName]).ingresses): # we have a legal connection try: self.graph.add_edge(platform,egresses[egress].endpointName) except (TypeError, ValueError): self.graph.add_edge((platform,egresses[egress].endpointName)) #fill in the ingress with the egress and the egress with the ingress else: error = 1 else: error = 1 if(error == 1): print 'Illegal graph: egress ' + egresses[egress].endpointName + ' and ' + platform + ' improperly connected' raise SyntaxError(egresses[egress].endpointName + ' and ' + platform + ' improperly connected') # although we've already added a edge for the legal connections, we need to check the reverse # in case some ingress lacks a egress for ingress in ingresses: error = 0 if(ingresses[ingress].endpointName in self.platforms): if(platform in (self.platforms[ingresses[ingress].endpointName]).egresses): a = 0 # make python happy.... else: error = 1 else: error = 1 if(error == 1): print 'Illegal graph: ingress ' + ingresses[ingress].endpointName + ' and ' + platform + ' improperly connected' raise SyntaxError(ingresses[ingress].endpointName + ' and ' + platform + ' improperly connected')
def graphize(self): try: self.graph = pygraph.digraph() except (NameError, AttributeError): self.graph = digraph() # first, we must add all the nodes. Only then can we add all the edges for platform in self.platforms: self.graph.add_nodes([platform]) for platform in self.platforms: sinks = self.platforms[platform].getSinks() sources = self.platforms[platform].getSources() for sink in sinks: # search for paired source - a specification is illegal if # sink/source pairing is not made error = 0 if(sinks[sink].endpointName in self.platforms): if(platform in (self.platforms[sinks[sink].endpointName]).sources): # we have a legal connection try: self.graph.add_edge(platform,sinks[sink].endpointName) except (TypeError, ValueError): self.graph.add_edge((platform,sinks[sink].endpointName)) #fill in the source with the sink and the sink with the source else: error = 1 else: error = 1 if(error == 1): print 'Illegal graph: ' + sinks[sink].endpointName + ' and ' + platform + ' improperly connected' raise SyntaxError(sinks[sink].endpointName + ' and ' + platform + ' improperly connected') # although we've already added a edge for the legal connections, we need to check the reverse # in case some source lacks a sink for source in sources: error = 0 if(sources[source].endpointName in self.platforms): if(platform in (self.platforms[sources[source].endpointName]).sinks): a = 0 # make python happy.... else: error = 1 else: error = 1 if(error == 1): print 'Illegal graph: ' + sources[source].endpointName + ' and ' + platform + ' improperly connected' raise SyntaxError(sources[source].endpointName + ' and ' + platform + ' improperly connected')
def load_automaton(filename): """ Read a automaton described as a labelled transition system and build the equivalent graph. @type filename: string @param filename: Name of the file containing the LTS-described automaton. @rtype: graph @return: Automaton's graph. """ gr = pygraph.digraph() infile = file(filename,'r') line = infile.readline() final = [] while (line): line = line.replace("\n",'').split(' ') datatype = line[0] data = line[1:] if (datatype == 'Q'): # States for each in data: gr.add_node(each) if (datatype == 'A'): # Alphabet pass if (datatype == 'F'): # Final states final = final + data if (datatype == 's'): # Initial state gr.add_node('.',attrs=[('shape','point')]) gr.add_edge('.',data[0]) if (datatype == 't'): # Transitions if (gr.has_edge(data[1], data[2])): gr.set_edge_label(data[1], data[2], \ gr.edge_label(data[1], data[2]) + ', ' + data[0]) else: gr.add_edge(data[1], data[2], label=data[0]) line = infile.readline() for node in gr: if (node in final and node != '.'): gr.add_node_attribute(node, ('shape','doublecircle')) elif (node != '.'): gr.add_node_attribute(node, ('shape','circle')) return gr, final
def load_automaton(filename): """ Read a automaton described as a labelled transition system and build the equivalent graph. @type filename: string @param filename: Name of the file containing the LTS-described automaton. @rtype: graph @return: Automaton's graph. """ gr = pygraph.digraph() infile = file(filename, "r") line = infile.readline() final = [] while line: line = line.replace("\n", "").split(" ") datatype = line[0] data = line[1:] if datatype == "Q": # States for each in data: gr.add_node(each) if datatype == "A": # Alphabet pass if datatype == "F": # Final states final = final + data if datatype == "s": # Initial state gr.add_node(".", attrs=[("shape", "point")]) gr.add_edge(".", data[0]) if datatype == "t": # Transitions if gr.has_edge(data[1], data[2]): gr.set_edge_label(data[1], data[2], gr.edge_label(data[1], data[2]) + ", " + data[0]) else: gr.add_edge(data[1], data[2], label=data[0]) line = infile.readline() for node in gr: if node in final and node != ".": gr.add_node_attribute(node, ("shape", "doublecircle")) elif node != ".": gr.add_node_attribute(node, ("shape", "circle")) return gr, final
def testDigraphDFS(self): G = pygraph.digraph() G.add_nodes([1, 2, 3, 4, 5, 6, 7, 8, 9]) G.add_edge(1, 2) G.add_edge(1, 3) G.add_edge(2, 4) G.add_edge(3, 5) G.add_edge(4, 6) G.add_edge(5, 7) G.add_edge(1, 8, wt=3) G.add_edge(7, 8, wt=3) G.add_edge(8, 9) G.add_edge(3, 9) st, pre, post = depth_first_search(G, 1, filter=filters.radius(2)) assert st == {1: None, 2: 1, 3: 1, 4: 2, 5: 3, 9: 3} st, pre, post = depth_first_search(G, 7, filter=filters.radius(2)) assert st == {7: None}