def run_graph(dot):
     """ Runs graphviz to see if the syntax is good. """
     graph = AGraph()
     graph = graph.from_string(dot)
     extension = 'png'
     graph.draw(path='output.png', prog='dot', format=extension)
     sys.exit(0)
 def run_graph(dot):
     """ Runs graphviz to see if the syntax is good. """
     graph = AGraph()
     graph = graph.from_string(dot)
     extension = 'png'
     graph.draw(path='output.png', prog='dot', format=extension)
     sys.exit(0)
示例#3
0
    def processLines(self):
        if len(self.lines) == 0:
            return
        
        self.identifier = self.lines[0][2:]

        s = '\n'.join(self.lines)
        A = AGraph()
        G = A.from_string(s)
        self.processGraph(G)
示例#4
0
    def processLines(self):
        if len(self.lines) == 0:
            return

        self.identifier = self.lines[0][2:]

        s = '\n'.join(self.lines)
        A = AGraph()
        G = A.from_string(s)
        self.processGraph(G)
示例#5
0
    def consensus_network_image(self, net, outfile, cm, node_positions):
        def colorize_edge(weight):
            colors = "9876543210"
            breakpoints = [.1, .2, .3, .4, .5, .6, .7, .8, .9]
            return "#" + str(colors[bisect(breakpoints, weight)]) * 6

        def node(n, position):
            s = "\t\"%s\"" % n.name
            if position:
                x, y = position
                s += " [pos=\"%d,%d\"]" % (x, y)
            return s + ";"

        nodes = net.nodes
        positions = node_positions

        dotstr = "\n".join(
            ["digraph G {"] +
            [node(n, pos) for n,pos in zip(nodes, positions)] +
            ["\t\"%s\" -> \"%s\" [color=\"%s\"];" % \
                (nodes[src].name, nodes[dest].name, colorize_edge(cm[src][dest])) \
                for src,dest in net.edges
            ] +
            ["}"]
        )

        fd, fname = tempfile.mkstemp()
        open(fname, 'w').write(dotstr)

        # needless repitition of network.as_gml() here:
        A = AGraph()
        net = A.from_string(string=dotstr)
        G = nx.from_agraph(net)
        nx.write_gml(G, outfile.replace(".png", ".gml"))

        os.system("dot -n1 -Tpng -o%s %s" % (outfile, fname))
        os.system("dot -n1 -Tsvg -o%s %s" %
                  (outfile.replace(".png", ".svg"), fname))
        os.remove(fname)
示例#6
0
 def as_gml(self, filename):
     dstr = self.as_dotstring()
     A = AGraph()
     net = A.from_string(string=dstr)
     G = nx.from_agraph(net)
     nx.write_gml(G, filename)
示例#7
0
    def consensus_network(self, filename, threshold, write_gml=False):
        """
        **Purpose**
            Draw a consensus network for a specified threshold
            
        **Arguments**
            filename (Required)
                filename to save the png and svg to. 
                
            threshold (Required)
                threshold value 0..1.0 for the consensus network
                
            write_gml (Optional, default=False)
                write the GML file for loadinging into e.g. Cytoscape
        
        
        """
        assert self.result, "bayes.consensus_network: Bayes network not learnt yet"
        assert filename, "You must specify a filename"
        assert threshold, "You must specify a threshold"
        assert threshold <= 1.0, "threshold is >= 1.0!"
        assert threshold >= 0.0, "threshold is <= 0.0!"
                   
        #print [n.score for n in list(reversed(self.result.networks))]

        this_posterior = posterior.from_sorted_scored_networks(self.result.nodes, list(reversed(self.result.networks)))
        net_this_threshold = this_posterior.consensus_network(threshold)

        cm = this_posterior.consensus_matrix # A method/property...
        top = this_posterior[0] # posterior object
        top.layout()
        nodes = net_this_threshold.nodes

        A = AGraph()
        net = A.from_string(string=net_this_threshold.as_dotstring()) # Load their Network into a AGraph and NX.
        G = nx.from_agraph(net)
        edge_cols = [colorize_edge(cm[src][dest]) for src, dest in net_this_threshold.edges]
        #pos = nx.graphviz_layout(G, 'dot')
        #fig = plot.figure()
        #ax = fig.add_subplot(111)
        #nx.draw_graphviz(G, 'dot', with_labels=True, edge_color=edge_cols, linewidths=0)
        #fig.tight_layout()
        #fig.savefig(filename.replace(".png", ".pgv.png"))

        dotstr = "\n".join(
            ["digraph G {"] + 
            [node(n, pos) for n, pos in zip(nodes, top.node_positions)] + 
            ["\t\"%s\" -> \"%s\" [color=\"%s\"];" % (nodes[src].name, nodes[dest].name, colorize_edge(cm[src][dest])) for src,dest in net_this_threshold.edges] + 
            ["}"])

        fd, fname = tempfile.mkstemp()
        open(fname, 'w').write(dotstr)

        if write_gml:
            nx.write_gml(G, filename.replace(".png", ".gml"))
        
        # Better done through pygraphviz gives different results...
        os.system("dot -n1 -Tpng -o%s %s" % (filename, fname))
        os.system("dot -n1 -Tsvg -o%s %s" % (filename.replace(".png", ".svg"), fname))
        os.remove(fname)
        config.log.info("bayes.consensus_network: Saved '%s'" % filename)