Beispiel #1
0
def dot_to_json(file_in, file_out, indent=1):
    import json
    try:
        import networkx
        import pygraphviz
        from networkx.readwrite import json_graph
    except ImportError:
        print("Install pygraphviz and networkx:")
        print("sudo apt-get install python-pygraphviz python-networkx")
        return 1

    graph_dot  = pygraphviz.AGraph( file_in )
    graph_netx = networkx.from_agraph( graph_dot )
    graph_json = json_graph.node_link_data( graph_netx )

    # fix formatting [graphviz to d3.js]
    for node in graph_json["nodes"]:
        # replace label by name
        node['name'] = node.pop('label')
        # id from string to integer
        node['id']   = int(node['id'])

    with open(file_out, 'w') as f:
        json.dump(graph_json, f, indent=indent)

    return 0
Beispiel #2
0
  def __init__(self, topology_file = "gates_topology.dot"):
    frenetic.App.__init__(self) 
 
    logging.info("---> Reading Topology from "+topology_file)
    self.agraph = pgv.AGraph(topology_file)
    for sw in self.agraph.nodes():
      dpid = str(sw.attr['dpid'])
      self.dpid_to_switch_dict[ dpid ] = str(sw)
      self.switch_to_dpid_dict[ str(sw) ] = dpid
      if sw.attr['core']:
        self.core_switches.add (str(sw))

    # It's faster to denormalize this now
    logging.info("---> Remembering internal ports")
    self.switch_internal_ports = { sw: set([]) for sw in self.switch_to_dpid_dict }
    for e in self.agraph.edges():
      source_sw = str(e[0])
      dest_sw = str(e[1])
      source_port = int(e.attr["src_port"])
      dest_port = int(e.attr["dport"])
      self.switch_internal_ports[ source_sw ].add( source_port )
      if source_sw not in self.port_mappings:
        self.port_mappings[source_sw] = {}
      self.port_mappings[source_sw][dest_sw] = source_port
      self.switch_internal_ports[ dest_sw ].add( dest_port )
      if dest_sw not in self.port_mappings:
        self.port_mappings[dest_sw] = {}
      self.port_mappings[dest_sw][source_sw] = dest_port

    logging.info("---> Calculating spanning tree")
    nxgraph = nx.from_agraph(self.agraph)
    self.nx_topo = nx.minimum_spanning_tree(nxgraph)
Beispiel #3
0
    def draw_lineage(self, recs, nodecolor="mediumseagreen",
                     edgecolor="lightslateblue", dpi=96,
                     lineage_img="GO_lineage.png", engine="pygraphviz",
                     gml=False, draw_parents=True, draw_children=True):
        """Draw GO DAG subplot."""
        assert engine in GraphEngines
        grph = None
        if engine == "pygraphviz":
            grph = self.make_graph_pygraphviz(recs, nodecolor, edgecolor, dpi,
                                              draw_parents=draw_parents,
                                              draw_children=draw_children)
        else:
            grph = self.make_graph_pydot(recs, nodecolor, edgecolor, dpi,
                                         draw_parents=draw_parents, draw_children=draw_children)

        if gml:
            import networkx as nx  # use networkx to do the conversion
            gmlbase = lineage_img.rsplit(".", 1)[0]
            NG = nx.from_agraph(grph) if engine == "pygraphviz" else nx.from_pydot(grph)

            del NG.graph['node']
            del NG.graph['edge']
            gmlfile = gmlbase + ".gml"
            nx.write_gml(Nself._label_wrapG, gmlfile)
            sys.stderr.write("GML graph written to {0}\n".format(gmlfile))

        sys.stderr.write(("lineage info for terms %s written to %s\n" %
                          ([rec.id for rec in recs], lineage_img)))

        if engine == "pygraphviz":
            grph.draw(lineage_img, prog="dot")
        else:
            grph.write_png(lineage_img)
    def draw_lineage(self, recs, nodecolor="mediumseagreen",
                     edgecolor="lightslateblue", dpi=96,
                     lineage_img="GO_lineage.png", engine="pygraphviz",
                     gml=False, draw_parents=True, draw_children=True):
        assert engine in GraphEngines
        if engine == "pygraphviz":
            G = self.make_graph_pygraphviz(recs, nodecolor, edgecolor, dpi,
                              draw_parents=draw_parents, draw_children=draw_children)
        else:
            G = self.make_graph_pydot(recs, nodecolor, edgecolor, dpi,
                              draw_parents=draw_parents, draw_children=draw_children)

        if gml:
            import networkx as nx  # use networkx to do the conversion
            pf = lineage_img.rsplit(".", 1)[0]
            NG = nx.from_agraph(G) if engine == "pygraphviz" else nx.from_pydot(G)

            del NG.graph['node']
            del NG.graph['edge']
            gmlfile = pf + ".gml"
            nx.write_gml(NG, gmlfile)
            print("GML graph written to {0}".format(gmlfile), file=sys.stderr)

        print(("lineage info for terms %s written to %s" %
                             ([rec.id for rec in recs], lineage_img)), file=sys.stderr)

        if engine == "pygraphviz":
            G.draw(lineage_img, prog="dot")
        else:
            G.write_png(lineage_img)
Beispiel #5
0
    def draw_lineage(self, recs, nodecolor="mediumseagreen",
                     edgecolor="lightslateblue", dpi=96,
                     lineage_img="GO_lineage.png", engine="pygraphviz",
                     gml=False, draw_parents=True, draw_children=True):
        assert engine in GraphEngines
        if engine == "pygraphviz":
            G = self.make_graph_pygraphviz(recs, nodecolor, edgecolor, dpi,
                              draw_parents=draw_parents, draw_children=draw_children)
        else:
            G = self.make_graph_pydot(recs, nodecolor, edgecolor, dpi,
                              draw_parents=draw_parents, draw_children=draw_children)

        if gml:
            import networkx as nx  # use networkx to do the conversion
            pf = lineage_img.rsplit(".", 1)[0]
            NG = nx.from_agraph(G) if engine == "pygraphviz" else nx.from_pydot(G)

            del NG.graph['node']
            del NG.graph['edge']
            gmlfile = pf + ".gml"
            nx.write_gml(NG, gmlfile)
            print("GML graph written to {0}".format(gmlfile), file=sys.stderr)

        print(("lineage info for terms %s written to %s" %
                             ([rec.id for rec in recs], lineage_img)), file=sys.stderr)

        if engine == "pygraphviz":
            G.draw(lineage_img, prog="dot")
        else:
            G.write_png(lineage_img)
Beispiel #6
0
    def draw_lineage(self, recs, nodecolor="mediumseagreen",
                     edgecolor="lightslateblue", dpi=96,
                     lineage_img="GO_lineage.png", engine="pygraphviz",
                     gml=False, draw_parents=True, draw_children=True):
        """Draw GO DAG subplot."""
        assert engine in GraphEngines
        grph = None
        if engine == "pygraphviz":
            grph = self.make_graph_pygraphviz(recs, nodecolor, edgecolor, dpi,
                                              draw_parents=draw_parents,
                                              draw_children=draw_children)
        else:
            grph = self.make_graph_pydot(recs, nodecolor, edgecolor, dpi,
                                         draw_parents=draw_parents, draw_children=draw_children)

        if gml:
            import networkx as nx  # use networkx to do the conversion
            gmlbase = lineage_img.rsplit(".", 1)[0]
            obj = nx.from_agraph(grph) if engine == "pygraphviz" else nx.from_pydot(grph)

            del obj.graph['node']
            del obj.graph['edge']
            gmlfile = gmlbase + ".gml"
            nx.write_gml(self.label_wrap, gmlfile)
            sys.stderr.write("GML graph written to {0}\n".format(gmlfile))

        sys.stderr.write(("lineage info for terms %s written to %s\n" %
                          ([rec.item_id for rec in recs], lineage_img)))

        if engine == "pygraphviz":
            grph.draw(lineage_img, prog="dot")
        else:
            grph.write_png(lineage_img)
Beispiel #7
0
    def draw_lineage(self,
                     recs,
                     nodecolor="mediumseagreen",
                     edgecolor="lightslateblue",
                     dpi=96,
                     lineage_img="GO_lineage.png",
                     gml=False):
        # draw AMIGO style network, lineage containing one query record
        try:
            import pygraphviz as pgv
        except:
            logging.error("pygraphviz not installed, lineage not drawn!")
            logging.error("try `easy_install pygraphviz`")
            return

        G = pgv.AGraph()
        edgeset = set()
        for rec in recs:
            edgeset.update(rec.get_all_parent_edges())
            edgeset.update(rec.get_all_child_edges())

        edgeset = [(self._label_wrap(a), self._label_wrap(b))
                   for (a, b) in edgeset]
        for src, target in edgeset:
            # default layout in graphviz is top->bottom, so we invert
            # the direction and plot using dir="back"
            G.add_edge(target, src)

        G.graph_attr.update(dpi="%d" % dpi)
        G.node_attr.update(shape="box",
                           style="rounded,filled",
                           fillcolor="beige",
                           color=nodecolor)
        G.edge_attr.update(shape="normal",
                           color=edgecolor,
                           dir="back",
                           label="is_a")
        # highlight the query terms
        for rec in recs:
            try:
                q = G.get_node(self._label_wrap(rec.id))
                q.attr.update(fillcolor="plum")
            except:
                continue

        if gml:
            import networkx as nx  # use networkx to do the conversion
            pf = lineage_img.rsplit(".", 1)[0]
            G.name = "GO tree"
            NG = nx.from_agraph(G)

            del NG.graph['node']
            del NG.graph['edge']
            gmlfile = pf + ".gml"
            nx.write_gml(NG, gmlfile)

        logging.info("lineage info for terms %s written to %s" %
                     ([rec.id for rec in recs], lineage_img))

        G.draw(lineage_img, prog="dot")
Beispiel #8
0
    def drawPlot(self):
        a = nx.to_agraph(self.g)
        self.g = nx.from_agraph(a)
        self.fig = pylab.figure()
        nx.draw_graphviz(self.g, prog='neato')
        plt.savefig("path.png")

        self.image.set_from_file("path.png")
Beispiel #9
0
  def handleTopology(self, rpc):
    dotStr     = rpc.Extensions[Topology.msg].dot
    pyGraph    = pydot.graph_from_dot_data(dotStr)
    aGraph     = pygraphviz.AGraph(string = dotStr)
    self.graph = networkx.from_agraph(aGraph)

    for node in pyGraph.get_nodes():
      self.attr[node.get_name()] = node.obj_dict['attributes']
Beispiel #10
0
    def test_graphviz():
        G=nx.complete_graph(5)   # start with K5 in networkx
        A=nx.to_agraph(G)        # convert to a graphviz graph
        X1=nx.from_agraph(A)     # convert back to networkx (but as Graph)
        X2=nx.Graph(A)          # fancy way to do conversion
        G1=nx.Graph(X1)          # now make it a Graph

        A.write('k5.dot')     # write to dot file
        X3=nx.read_dot('k5.dot') # read from dotfile
        pass
Beispiel #11
0
    def draw_lineage(
        self,
        recs,
        nodecolor="mediumseagreen",
        edgecolor="lightslateblue",
        dpi=96,
        lineage_img="GO_lineage.png",
        gml=False,
    ):
        # draw AMIGO style network, lineage containing one query record
        try:
            import pygraphviz as pgv
        except:
            print >>sys.stderr, "pygraphviz not installed, lineage not drawn!"
            print >>sys.stderr, "try `easy_install pygraphviz`"
            return

        G = pgv.AGraph()
        edgeset = set()
        for rec in recs:
            edgeset.update(rec.get_all_parent_edges())
            edgeset.update(rec.get_all_child_edges())

        edgeset = [(self._label_wrap(a), self._label_wrap(b)) for (a, b) in edgeset]
        for src, target in edgeset:
            # default layout in graphviz is top->bottom, so we invert
            # the direction and plot using dir="back"
            G.add_edge(target, src)

        G.graph_attr.update(dpi="%d" % dpi)
        G.node_attr.update(shape="box", style="rounded,filled", fillcolor="beige", color=nodecolor)
        G.edge_attr.update(shape="normal", color=edgecolor, dir="back", label="is_a")
        # highlight the query terms
        for rec in recs:
            try:
                q = G.get_node(self._label_wrap(rec.id))
                q.attr.update(fillcolor="plum")
            except:
                continue

        if gml:
            import networkx as nx  # use networkx to do the conversion

            pf = lineage_img.rsplit(".", 1)[0]
            G.name = "GO tree"
            NG = nx.from_agraph(G)

            del NG.graph["node"]
            del NG.graph["edge"]
            gmlfile = pf + ".gml"
            nx.write_gml(NG, gmlfile)

        print >>sys.stderr, ("lineage info for terms %s written to %s" % ([rec.id for rec in recs], lineage_img))

        G.draw(lineage_img, prog="dot")
  def __init__(self, logger, topology_file="multiswitch_topo.dot"):
    self.logger = logger

    self.logger.info("---> Reading Topology from "+topology_file)
    self.agraph = pgv.AGraph(topology_file)

    # It's faster to denormalize this now
    self.logger.info("---> Remembering internal ports")
    switches = [ int(sw) for sw in self.agraph.nodes()]
    for e in self.agraph.edges():
      # Parse the source and destination switches
      source_dpid = int(e[0])
      dest_dpid = int(e[1])
      source_port = int(e.attr["src_port"])
      dest_port = int(e.attr["dport"])

      # Add a mapping for the forward directions source->dest
      self.add_port_mapping(source_dpid, dest_dpid, source_port)

      # Add a mapping for the reverse direction dest->source
      self.add_port_mapping(dest_dpid, source_dpid, dest_port)

    # Calculate the core/edge attribute.  Edge switches have only one 
    # connection to another switch
    for sw in self.port_mappings:
      if len(self.port_mappings[sw]) == 1:
        self.edge_switches.add(sw)
        # Edge switches have only one entry in port_mappings[dpid], so we get it here
        connected_sw = self.port_mappings[sw].keys()[0]
        self.uplink_port[sw] = self.port_mappings[sw][connected_sw]
      else:
        self.core_switches.add(sw)

    self.logger.info("---> Calculating spanning tree")
    nxgraph = nx.from_agraph(self.agraph)
    self.nx_topo = nx.minimum_spanning_tree(nxgraph)    
    nx.write_edgelist(self.nx_topo, sys.stdout)

    self.logger.info("---> Enabling only those ports on the spanning tree")
    for (from_dpid, to_dpid) in self.nx_topo.edges():
      # We look up the port mapping from the port-mapping dictionary instead of 
      # from the graph attributes because NetworkX flips the src and dest node
      # arbitrarily in an undirected graph
      from_dpid_int = int(from_dpid)
      to_dpid_int = int(to_dpid)
      from_port = self.port_mappings[from_dpid_int][to_dpid_int]
      if from_dpid_int not in self.enabled_ports:
        self.enabled_ports[from_dpid_int] = []
      self.enabled_ports[from_dpid_int].append(from_port)

      to_port = self.port_mappings[to_dpid_int][from_dpid_int]
      if to_dpid_int not in self.enabled_ports:
        self.enabled_ports[to_dpid_int] = []
      self.enabled_ports[to_dpid_int].append(to_port)
Beispiel #13
0
    def __init__(self, logger, topology_file="multiswitch_topo.dot"):
        self.logger = logger

        self.logger.info("---> Reading Topology from " + topology_file)
        self.agraph = pgv.AGraph(topology_file)

        # It's faster to denormalize this now
        self.logger.info("---> Remembering internal ports")
        switches = [int(sw) for sw in self.agraph.nodes()]
        for e in self.agraph.edges():
            # Parse the source and destination switches
            source_dpid = int(e[0])
            dest_dpid = int(e[1])
            source_port = int(e.attr["src_port"])
            dest_port = int(e.attr["dport"])

            # Add a mapping for the forward directions source->dest
            self.add_port_mapping(source_dpid, dest_dpid, source_port)

            # Add a mapping for the reverse direction dest->source
            self.add_port_mapping(dest_dpid, source_dpid, dest_port)

        # Calculate the core/edge attribute.  Edge switches have only one
        # connection to another switch
        for sw in self.port_mappings:
            if len(self.port_mappings[sw]) == 1:
                self.edge_switches.add(sw)
                # Edge switches have only one entry in port_mappings[dpid], so we get it here
                connected_sw = self.port_mappings[sw].keys()[0]
                self.uplink_port[sw] = self.port_mappings[sw][connected_sw]
            else:
                self.core_switches.add(sw)

        self.logger.info("---> Calculating spanning tree")
        nxgraph = nx.from_agraph(self.agraph)
        self.nx_topo = nx.minimum_spanning_tree(nxgraph)
        nx.write_edgelist(self.nx_topo, sys.stdout)

        self.logger.info("---> Enabling only those ports on the spanning tree")
        for (from_dpid, to_dpid) in self.nx_topo.edges():
            # We look up the port mapping from the port-mapping dictionary instead of
            # from the graph attributes because NetworkX flips the src and dest node
            # arbitrarily in an undirected graph
            from_dpid_int = int(from_dpid)
            to_dpid_int = int(to_dpid)
            from_port = self.port_mappings[from_dpid_int][to_dpid_int]
            if from_dpid_int not in self.enabled_ports:
                self.enabled_ports[from_dpid_int] = []
            self.enabled_ports[from_dpid_int].append(from_port)

            to_port = self.port_mappings[to_dpid_int][from_dpid_int]
            if to_dpid_int not in self.enabled_ports:
                self.enabled_ports[to_dpid_int] = []
            self.enabled_ports[to_dpid_int].append(to_port)
Beispiel #14
0
def topo_parser(filename):
    """Parse dot lanague defined topologies and return a parsed graph.

    Keyword Arguments:
    ------------------
    filename -- .dot file for a defined topology.

    Returns:
    --------
    graph -- networkx graph, see networkx.
    """
    agraph = pgv.AGraph(filename)
    G = nx.from_agraph(agraph)
    G.name = agraph.graph_attr['label']
    return G
Beispiel #15
0
def topo_parser(filename):
    """Parse dot lanague defined topologies and return a parsed graph.

    Keyword Arguments:
    ------------------
    filename -- .dot file for a defined topology.

    Returns:
    --------
    graph -- networkx graph, see networkx.
    """
    agraph = pgv.AGraph(filename)
    G = nx.from_agraph(agraph)
    G.name = agraph.graph_attr['label']
    return G
 def addScan(self, mech, fileName, newDate):
     if self.myCrypto.disabled:
         newScan = scan(network=self.pluginName, time=newDate)
     else:
         newScan = scan(network=self.pluginName, time=newDate,
                 encrypted=True)
     try:
         topoFile = mech.retrieve(fileName)
     except: 
         self.logger.error("Could not retrieve file "+fileName)
         return
     try: 
         f = gzip.open(topoFile[0])
     except:
         self.logger.error("Could not open temporary file "+topoFile[0])
         return
     
     graphString = ""
     for l in f:
         graphString += l 
     G = pg.AGraph(encoding='UTF-8')
     Ga = G.from_string(graphString)
     pG = nx.from_agraph(Ga)
     # note that FFGraz nodes are taken from the .dot file, 
     # so we don't have numerical IDs. In the DB the node_Id 
     # will be just the node name
     toBeRemoved = []
     for e in pG.edges(data=True):
         if str(e[2]['label']) != 'HNA': 
             if e[2]['label'] == 'INFINITE':
                 e[2]['weight'] = 1000
             else:
                 e[2]['weight'] = float(e[2]['label'])
         else:
             toBeRemoved.append([e[0], e[1]])
     for l in toBeRemoved:
         pG.remove_edge(l[0], l[1])
     simpleG = self.aggregateNodesByName(pG)
     for e in simpleG.edges():
         if e[0] == e[1]:
             simpleG.remove_edge(e[0], e[1])
     newFile = topo_file(file_url=fileName, scan_Id_r=newScan, time=newDate)
     self.localSession.add(newFile)
     #FIXME NEed to add owners and emails here
     addGraphToDB(simpleG, self.localSession, newScan, self.myCrypto)
     f.close()
     os.remove(topoFile[0])
Beispiel #17
0
def draw_ugraph(k, fname, report):
    """ actually build a graph

          #A.graph_attr['label']='known universe topology'
          #nx.draw_random(G) #nx.draw_circular(G)
          #nx.draw_graphviz(G) #nx.write_dot(G,'file.dot')
          #nx.draw(A) #nx.draw_random(G) #nx.draw_spectral(G)

    """
    import networkx as nx
    from pygraphviz import *
    G = nx.Graph()
    G.add_edges_from(k)
    A=nx.to_agraph(G)
    H = nx.from_agraph(A)
    A.edge_attr['color']='red'
    A.layout()
    A.draw(fname)
Beispiel #18
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)
Beispiel #19
0
    def agraph_checks(self, G):
        G = self.build_graph(G)
        A = nx.to_agraph(G)
        H = nx.from_agraph(A)
        self.assert_equal(G, H)

        fname = tempfile.mktemp()
        nx.drawing.nx_agraph.write_dot(H, fname)
        Hin = nx.drawing.nx_agraph.read_dot(fname)
        os.unlink(fname)
        self.assert_equal(H, Hin)

        (fd, fname) = tempfile.mkstemp()
        fh = open(fname, 'w')
        nx.drawing.nx_agraph.write_dot(H, fh)
        fh.close()

        fh = open(fname, 'r')
        Hin = nx.drawing.nx_agraph.read_dot(fh)
        fh.close()
        os.unlink(fname)
        self.assert_equal(H, Hin)
    def agraph_checks(self, G):
        G = self.build_graph(G)
        A=nx.to_agraph(G)
        H=nx.from_agraph(A)
        self.assert_equal(G, H)

        fname=tempfile.mktemp()
        nx.drawing.nx_agraph.write_dot(H,fname)
        Hin=nx.drawing.nx_agraph.read_dot(fname)
        os.unlink(fname)
        self.assert_equal(H,Hin)


        (fd,fname)=tempfile.mkstemp()
        fh=open(fname,'w')
        nx.drawing.nx_agraph.write_dot(H,fh)
        fh.close()

        fh=open(fname,'r')
        Hin=nx.drawing.nx_agraph.read_dot(fh)
        fh.close()
        os.unlink(fname)
        self.assert_equal(H,Hin)
Beispiel #21
0
def draw_network(graph,sg=None):

    G = nx.from_agraph(graph)
    pos=nx.spring_layout(G)
    ax=plt.gca()
    ax.set_xmargin(0.25)
    ax.set_ymargin(0.25)
    ax.autoscale()
    plt.axis('equal')
    plt.axis('off')

    for n in G:
        c=Circle(pos[n],radius=0.05,alpha=0.5)
        ax.add_patch(c)
        ax.text(pos[n][0]+0.15, pos[n][1], n, verticalalignment='center')
        G.node[n]['patch']=c
        x,y=pos[n]
    seen={}
    for (u,v,d) in G.edges(data=True):
        n1=G.node[u]['patch']
        n2=G.node[v]['patch']
        rad=random.random()
        if (u,v) in seen:
            rad=seen.get((u,v))
            rad=(rad+np.sign(rad)*0.1)*-1
        alpha=0.5
        color='k'

        e = FancyArrowPatch(n1.center,n2.center,patchA=n1,patchB=n2,
                            arrowstyle='-|>',
                            connectionstyle='arc3,rad=%s'%rad,
                            mutation_scale=10.0,
                            lw=2,
                            alpha=alpha,
                            color=color)
        seen[(u,v)]=rad
        ax.add_patch(e)
Beispiel #22
0
def tred(graph):
    A=nx.to_agraph(graph)
    I=A.tred(copy=true)
    new_graph=nx.from_agraph(I)
    return new_graph
 def test_from_agraph_name(self):
     G=nx.Graph(name='test')
     A=nx.to_agraph(G)
     H=nx.from_agraph(A)
     assert_equal(G.name,'test')
Beispiel #24
0
 def test_from_agraph_name(self):
     G = nx.Graph(name='test')
     A = nx.to_agraph(G)
     H = nx.from_agraph(A)
     assert_equal(G.name, 'test')
Beispiel #25
0
def to_networkx_graph(data, create_using=None, multigraph_input=False):
    """Make a NetworkX graph from a known data structure.

    The preferred way to call this is automatically
    from the class constructor

    >>> d={0: {1: {'weight':1}}} # dict-of-dicts single edge (0,1)
    >>> G=nx.Graph(d)
    
    instead of the equivalent

    >>> G=nx.from_dict_of_dicts(d)

    Parameters
    ----------
    data : a object to be converted
       Current known types are:
         any NetworkX graph
         dict-of-dicts
         dist-of-lists
         list of edges
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    multigraph_input : bool (default False)
      If True and  data is a dict_of_dicts,
      try to create a multigraph assuming dict_of_dict_of_lists.
      If data and create_using are both multigraphs then create
      a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(data, "adj"):
        try:
            result= from_dict_of_dicts(data.adj,\
                    create_using=create_using,\
                    multigraph_input=data.is_multigraph())
            if hasattr(data, 'graph') and isinstance(data.graph, dict):
                result.graph = data.graph.copy()
            if hasattr(data, 'node') and isinstance(data.node, dict):
                result.node = dict(
                    (n, dd.copy()) for n, dd in data.node.items())
            return result
        except:
            raise nx.NetworkXError("Input is not a correct NetworkX graph.")

    # pygraphviz  agraph
    if hasattr(data, "is_strict"):
        try:
            return nx.from_agraph(data, create_using=create_using)
        except:
            raise nx.NetworkXError("Input is not a correct pygraphviz graph.")

    # dict of dicts/lists
    if isinstance(data, dict):
        try:
            return from_dict_of_dicts(data,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(data, create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # list or generator of edges
    if (isinstance(data, list) or hasattr(data, 'next')
            or hasattr(data, '__next__')):
        try:
            return from_edgelist(data, create_using=create_using)
        except:
            raise nx.NetworkXError("Input is not a valid edge list")

    # numpy matrix or ndarray
    try:
        import numpy
        if isinstance(data,numpy.matrix) or \
               isinstance(data,numpy.ndarray):
            try:
                return from_numpy_matrix(data, create_using=create_using)
            except:
                raise nx.NetworkXError(\
                  "Input is not a correct numpy matrix or array.")
    except ImportError:
        warnings.warn('numpy not found, skipping conversion test.',
                      ImportWarning)

    # scipy sparse matrix - any format
    try:
        import scipy
        if hasattr(data, "format"):
            try:
                return from_scipy_sparse_matrix(data,
                                                create_using=create_using)
            except:
                raise nx.NetworkXError(\
                      "Input is not a correct scipy sparse matrix type.")
    except ImportError:
        warnings.warn('scipy not found, skipping conversion test.',
                      ImportWarning)


    raise nx.NetworkXError(\
          "Input is not a known data type for conversion.")

    return
Beispiel #26
0
def from_whatever(thing, create_using=None, multigraph_input=False):
    """Make a NetworkX graph from an known type.

    The preferred way to call this is automatically
    from the class constructor

    >>> d={0: {1: {'weight':1}}} # dict-of-dicts single edge (0,1)
    >>> G=nx.Graph(d)
    
    instead of the equivalent

    >>> G=nx.from_dict_of_dicts(d)

    Parameters
    ----------
    thing : a object to be converted
       Current known types are:
         any NetworkX graph
         dict-of-dicts
         dist-of-lists
         list of edges
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    multigraph_input : bool (default False)
      If True and  thing is a dict_of_dicts,
      try to create a multigraph assuming dict_of_dict_of_lists.
      If thing and create_using are both multigraphs then create
      a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(thing, "adj"):
        try:
            result= from_dict_of_dicts(thing.adj,\
                    create_using=create_using,\
                    multigraph_input=thing.is_multigraph())
            if hasattr(thing, 'graph') and isinstance(thing.graph, dict):
                result.graph = thing.graph.copy()
            if hasattr(thing, 'node') and isinstance(thing.node, dict):
                result.node = dict(
                    (n, dd.copy()) for n, dd in thing.node.iteritems())
            return result
        except:
            raise networkx.NetworkXError,\
                "Input is not a correct NetworkX graph."

    # pygraphviz  agraph
    if hasattr(thing, "is_strict"):
        try:
            return networkx.from_agraph(thing, create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct pygraphviz graph."

    # dict of dicts/lists
    if isinstance(thing, dict):
        try:
            return from_dict_of_dicts(thing,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(thing, create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # list or generator of edges
    if isinstance(thing, list) or hasattr(thing, 'next'):
        try:
            return from_edgelist(thing, create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a valid edge list"

    # numpy matrix or ndarray
#    try:
#        import numpy
    if isinstance(thing,numpy.core.defmatrix.matrix) or \
           isinstance(thing,numpy.ndarray):
        try:
            return from_numpy_matrix(thing, create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct numpy matrix or array."
#    except ImportError:
#        pass # fail silently

# scipy sparse matrix - any format
#    try:
#        import scipy
    if hasattr(thing, "format"):
        try:
            return from_scipy_sparse_matrix(thing, create_using=create_using)
        except:
            raise networkx.NetworkXError, \
                  "Input is not a correct scipy sparse matrix type."


#    except ImportError:
#        pass # fail silently


    raise networkx.NetworkXError, \
          "Input is not a known data type for conversion."

    return
Beispiel #27
0
 def read_dot_file(self, filename):
   graph = pgv.AGraph(filename)
   network = nx.Graph(nx.from_agraph(graph))
   mapping = dict([(n,int(n)) for n in network.nodes()])
   result = nx.relabel_nodes(network, mapping)
   return result
Beispiel #28
0
def to_networkx_graph(data,create_using=None,multigraph_input=False):
    """Make a NetworkX graph from a known data structure.

    The preferred way to call this is automatically
    from the class constructor

    >>> d={0: {1: {'weight':1}}} # dict-of-dicts single edge (0,1)
    >>> G=nx.Graph(d)

    instead of the equivalent

    >>> G=nx.from_dict_of_dicts(d)

    Parameters
    ----------
    data : a object to be converted
       Current known types are:
         any NetworkX graph
         dict-of-dicts
         dist-of-lists
         list of edges
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    multigraph_input : bool (default False)
      If True and  data is a dict_of_dicts,
      try to create a multigraph assuming dict_of_dict_of_lists.
      If data and create_using are both multigraphs then create
      a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(data,"adj"):
        try:
            result= from_dict_of_dicts(data.adj,\
                    create_using=create_using,\
                    multigraph_input=data.is_multigraph())
            if hasattr(data,'graph') and isinstance(data.graph,dict):
                result.graph=data.graph.copy()
            if hasattr(data,'node') and isinstance(data.node,dict):
                result.node=dict( (n,dd.copy()) for n,dd in data.node.items() )
            return result
        except:
            raise nx.NetworkXError("Input is not a correct NetworkX graph.")

    # pygraphviz  agraph
    if hasattr(data,"is_strict"):
        try:
            return nx.from_agraph(data,create_using=create_using)
        except:
            raise nx.NetworkXError("Input is not a correct pygraphviz graph.")

    # dict of dicts/lists
    if isinstance(data,dict):
        try:
            return from_dict_of_dicts(data,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(data,create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # list or generator of edges
    if (isinstance(data,list)
        or hasattr(data,'next')
        or hasattr(data, '__next__')): 
        try:
            return from_edgelist(data,create_using=create_using)
        except:
            raise nx.NetworkXError("Input is not a valid edge list")

    # numpy matrix or ndarray 
    try:
        import numpy
        if isinstance(data,numpy.matrix) or \
               isinstance(data,numpy.ndarray):
            try:
                return from_numpy_matrix(data,create_using=create_using)
            except:
                raise nx.NetworkXError(\
                  "Input is not a correct numpy matrix or array.")
    except ImportError:
        warnings.warn('numpy not found, skipping conversion test.',
                      ImportWarning)

    # scipy sparse matrix - any format
    try:
        import scipy
        if hasattr(data,"format"):
            try:
                return from_scipy_sparse_matrix(data,create_using=create_using)
            except:
                raise nx.NetworkXError(\
                      "Input is not a correct scipy sparse matrix type.")
    except ImportError:
        warnings.warn('scipy not found, skipping conversion test.',
                      ImportWarning)


    raise nx.NetworkXError(\
          "Input is not a known data type for conversion.")

    return 
Beispiel #29
0
def from_whatever(thing, create_using=None, multigraph_input=False):
    """Attempt to make a NetworkX graph from an known type.

    Current known types are:

       any NetworkX graph
       dict-of-dicts
       dist-of-lists
       numpy matrix
       numpy ndarray
       scipy sparse matrix
       pygraphviz agraph

    If multigraph_input is True and thing is a dict_of_dicts,
    try to create a multigraph assuming dict_of_dict_of_lists.
    If thing and create_using are both multigraphs then create
    a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(thing, "adj"):
        try:
            result= from_dict_of_dicts(thing.adj,\
                    create_using=create_using,\
                    multigraph_input=thing.multigraph)
            result.weighted = thing.weighted
            return result
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct NetworkX graph."

    # pygraphviz  agraph
    if hasattr(thing, "is_strict"):
        try:
            return networkx.from_agraph(thing, create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct pygraphviz graph."

    # dict of dicts/lists
    if isinstance(thing, dict):
        try:
            return from_dict_of_dicts(thing,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(thing, create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # numpy matrix or ndarray

    try:
        import numpy
        if isinstance(thing,numpy.core.defmatrix.matrix) or \
               isinstance(thing,numpy.ndarray):
            try:
                return from_numpy_matrix(thing, create_using=create_using)
            except:
                raise networkx.NetworkXError,\
                      "Input is not a correct numpy matrix or array."
    except ImportError:
        pass  # fail silently

    # scipy sparse matrix - any format
    try:
        import scipy
        if hasattr(thing, "format"):
            try:
                return from_scipy_sparse_matrix(thing,
                                                create_using=create_using)
            except:
                raise networkx.NetworkXError, \
                      "Input is not a correct scipy sparse matrix type."
    except ImportError:
        pass  # fail silently


    raise networkx.NetworkXError, \
          "Input is not a known data type for conversion."

    return
Beispiel #30
0
G = pgv.AGraph(directed=True)

for node in nodes:
    G.add_node(node, label=node.id + "\n" + node.title)

for edge in edges:
    G.add_edge(edge[0], edge[1])

static_path = "webapp/static/webapp/"
G.write("graph.dot")

# Generating basic svg
G.draw(path=static_path + "graph.svg", prog="dot")

# Generating .dot with coordinate information for nodes
os.system(
    "ccomps -x graph.dot | dot -Nshape=point -Granksep=1.0 -Gnodesep=1.0 -Gsize=10 | gvpack -array1 | neato -Tdot -n2 -o graphcoord.dot"
)

# Generating JSON from graph with coordinates
G = pgv.AGraph("graphcoord.dot")
nx_G = nx.from_agraph(G)
graph_json = json_graph.node_link_data(nx_G)
with open(static_path + "graph.json", "w") as f:
    json.dump(graph_json, f, indent=1)

# Cleaning up
os.remove("graph.dot")
os.remove("graphcoord.dot")
Beispiel #31
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)
import networkx as nx
g = nx.DiGraph()

g.add_node("A:a", type="A", name="a")
g.add_node("B", type="B")
g.add_edge("A:a", "B", label="0,1")

g.add_node("B:b", type="B", name="b")
g.add_edge("A:a", "B:b", label="1,1")


dot = nx.to_agraph(g);
print "g:",dot

g2 = nx.from_agraph(dot)
print "g2:",nx.to_agraph(g2);

print g2["A:a"]["B:b"]

nx.write_dot(g,"test_nxdot.dot")
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import networkx as nx

# networkx graph
G=nx.Graph()
# ad edges with red color
G.add_edge(1,2,color='red')
G.add_edge(2,3,color='red')
# add nodes 3 and 4
G.add_node(3)
G.add_node(4)

# convert to a graphviz agraph 
A=nx.to_agraph(G)   

# write to dot file
A.write('k5_attributes.dot')   

# convert back to networkx Graph with attributes on edges and
# default attributes as dictionary data
X=nx.from_agraph(A)
print("edges")
print(X.edges(data=True))
print("default graph attributes")
print(X.graph)
print("node node attributes")
print(X.node)
#    Pieter Swart <*****@*****.**>
#    All rights reserved.
#    BSD license.

import networkx as nx

# networkx graph
G = nx.Graph()
# ad edges with red color
G.add_edge(1, 2, color='red')
G.add_edge(2, 3, color='red')
# add nodes 3 and 4
G.add_node(3)
G.add_node(4)

# convert to a graphviz agraph
A = nx.to_agraph(G)

# write to dot file
A.write('k5_attributes.dot')

# convert back to networkx Graph with attributes on edges and
# default attributes as dictionary data
X = nx.from_agraph(A)
print("edges")
print(X.edges(data=True))
print("default graph attributes")
print(X.graph)
print("node node attributes")
print(X.node)
Beispiel #35
0
def from_whatever(thing,create_using=None,multigraph_input=False):
    """Make a NetworkX graph from an known type.

    The preferred way to call this is automatically
    from the class constructor

    >>> d={0: {1: {'weight':1}}} # dict-of-dicts single edge (0,1)
    >>> G=nx.Graph(d)
    
    instead of the equivalent

    >>> G=nx.from_dict_of_dicts(d)

    Parameters
    ----------
    thing : a object to be converted
       Current known types are:
         any NetworkX graph
         dict-of-dicts
         dist-of-lists
         list of edges
         numpy matrix
         numpy ndarray
         scipy sparse matrix
         pygraphviz agraph

    create_using : NetworkX graph
       Use specified graph for result.  Otherwise a new graph is created.

    multigraph_input : bool (default False)
      If True and  thing is a dict_of_dicts,
      try to create a multigraph assuming dict_of_dict_of_lists.
      If thing and create_using are both multigraphs then create
      a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(thing,"adj"):
        try:
            result= from_dict_of_dicts(thing.adj,\
                    create_using=create_using,\
                    multigraph_input=thing.is_multigraph())
            if hasattr(thing,'graph') and isinstance(thing.graph,dict):
                result.graph=thing.graph.copy()
            if hasattr(thing,'node') and isinstance(thing.node,dict):
                result.node=dict( (n,dd.copy()) for n,dd in thing.node.iteritems() )
            return result
        except:
            raise networkx.NetworkXError,\
                "Input is not a correct NetworkX graph."

    # pygraphviz  agraph
    if hasattr(thing,"is_strict"):
        try:
            return networkx.from_agraph(thing,create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct pygraphviz graph."

    # dict of dicts/lists
    if isinstance(thing,dict):
        try:
            return from_dict_of_dicts(thing,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(thing,create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # list or generator of edges
    if isinstance(thing,list) or hasattr(thing,'next'): 
        try:
            return from_edgelist(thing,create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a valid edge list"

    # numpy matrix or ndarray 
#    try:
#        import numpy
    if isinstance(thing,numpy.core.defmatrix.matrix) or \
           isinstance(thing,numpy.ndarray):
        try:
            return from_numpy_matrix(thing,create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct numpy matrix or array."
#    except ImportError:
#        pass # fail silently

    # scipy sparse matrix - any format
#    try:
#        import scipy
    if hasattr(thing,"format"):
        try:
            return from_scipy_sparse_matrix(thing,create_using=create_using)
        except:
            raise networkx.NetworkXError, \
                  "Input is not a correct scipy sparse matrix type."
#    except ImportError:
#        pass # fail silently


    raise networkx.NetworkXError, \
          "Input is not a known data type for conversion."

    return 
Beispiel #36
0
def from_whatever(thing,create_using=None,multigraph_input=False):
    """Attempt to make a NetworkX graph from an known type.

    Current known types are:

       any NetworkX graph
       dict-of-dicts
       dist-of-lists
       numpy matrix
       numpy ndarray
       scipy sparse matrix
       pygraphviz agraph

    If multigraph_input is True and thing is a dict_of_dicts,
    try to create a multigraph assuming dict_of_dict_of_lists.
    If thing and create_using are both multigraphs then create
    a multigraph from a multigraph.

    """
    # NX graph
    if hasattr(thing,"adj"):
        try:
            result= from_dict_of_dicts(thing.adj,\
                    create_using=create_using,\
                    multigraph_input=thing.multigraph)
            result.weighted=thing.weighted
            return result
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct NetworkX graph."

    # pygraphviz  agraph
    if hasattr(thing,"is_strict"):
        try:
            return networkx.from_agraph(thing,create_using=create_using)
        except:
            raise networkx.NetworkXError,\
                  "Input is not a correct pygraphviz graph."

    # dict of dicts/lists
    if isinstance(thing,dict):
        try:
            return from_dict_of_dicts(thing,create_using=create_using,\
                    multigraph_input=multigraph_input)
        except:
            try:
                return from_dict_of_lists(thing,create_using=create_using)
            except:
                raise TypeError("Input is not known type.")

    # numpy matrix or ndarray 

    try:
        import numpy
        if isinstance(thing,numpy.core.defmatrix.matrix) or \
               isinstance(thing,numpy.ndarray):
            try:
                return from_numpy_matrix(thing,create_using=create_using)
            except:
                raise networkx.NetworkXError,\
                      "Input is not a correct numpy matrix or array."
    except ImportError:
        pass # fail silently

    # scipy sparse matrix - any format
    try:
        import scipy
        if hasattr(thing,"format"):
            try:
                return from_scipy_sparse_matrix(thing,create_using=create_using)
            except:
                raise networkx.NetworkXError, \
                      "Input is not a correct scipy sparse matrix type."
    except ImportError:
        pass # fail silently


    raise networkx.NetworkXError, \
          "Input is not a known data type for conversion."

    return 
Beispiel #37
0
def tred(graph):
    A = nx.to_agraph(graph)
    I = A.tred(copy=true)
    new_graph = nx.from_agraph(I)
    return new_graph
Beispiel #38
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)
Beispiel #39
0
                }
    def hypernode_subgraph(self, node):
        label = self.hypergraph.node_label(node)
        if label.span[0] == label.span[1]:
            return [("clust_terminals", label.span[0] + (0.5 if label.dir == Right else 0))]
        return []
    def subgraph_format(self, subgraph):
        return {"rank": "same"}
    def hyperedge_node_attrs(self, edge):
        return {"shape": "point"}
    def hyperedge_attrs(self, edge):
        return {"arrowhead": "none", 
                "color": "orange" if edge in self.path else "black",
                "penwidth": 5 if edge in self.path else 1}

ParseFormat(hypergraph, sentence, path).to_ipython()


# In[ ]:

import networkx as nx
from networkx.readwrite import json_graph
import json
G = ParseFormat(hypergraph, sentence, path).to_graphviz()
G2 = nx.from_agraph(G)
d = json_graph.node_link_data(G2) # node-link format to serialize
# write json 
json.dump(d, open('force.json','w'))
#nx.write_gexf(G2, "test_graph.gexf")