Пример #1
0
def parse_p2g(lines):
    """Parse p2g format graph from string or iterable. Returns an XDiGraph."""
    if is_string_like(lines): lines=iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines])

    description = lines.next()
    # are multiedges (parallel edges) allowed?
    G=networkx_v099.XDiGraph(name=description,selfloops=True)
    nnodes,nedges=map(int,lines.next().split())

    nodelabel={}
    nbrs={}
    # loop over the nodes keeping track of node labels and out neighbors
    # defer adding edges until all node labels are known
    for i in range(nnodes):
        n=lines.next()
        nodelabel[i]=n
        G.add_node(n)
        nbrs[n]=map(int,lines.next().split())

    # now we know all of the node labels so we can add the edges
    # with the correct labels        
    for n in G:
        for nbr in nbrs[n]:
            G.add_edge(n,nodelabel[nbr])
    return G
Пример #2
0
def parse_p2g(lines):
    """Parse p2g format graph from string or iterable. Returns an XDiGraph."""
    if is_string_like(lines): lines = iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines])

    description = lines.next()
    # are multiedges (parallel edges) allowed?
    G = networkx_v099.XDiGraph(name=description, selfloops=True)
    nnodes, nedges = map(int, lines.next().split())

    nodelabel = {}
    nbrs = {}
    # loop over the nodes keeping track of node labels and out neighbors
    # defer adding edges until all node labels are known
    for i in range(nnodes):
        n = lines.next()
        nodelabel[i] = n
        G.add_node(n)
        nbrs[n] = map(int, lines.next().split())

    # now we know all of the node labels so we can add the edges
    # with the correct labels
    for n in G:
        for nbr in nbrs[n]:
            G.add_edge(n, nodelabel[nbr])
    return G
Пример #3
0
def parse_pajek(lines):
    """Parse pajek format graph from string or iterable.."""
    import shlex
    if is_string_like(lines): lines = iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines])
    G = networkx_v099.XDiGraph(
        selfloops=True)  # are multiedges allowed in Pajek?
    G.node_attr = {}  # dictionary to hold node attributes
    directed = True  # assume this is a directed network for now
    while lines:
        try:
            l = lines.next()
        except:  #EOF
            break
        if l.startswith("*network"):
            label, name = l.split()
            G.name = name
        if l.startswith("*vertices"):
            nodelabels = {}
            l, nnodes = l.split()
            for i in range(int(nnodes)):
                splitline = shlex.split(lines.next())
                id, label, x, y, shape = splitline[0:5]
                G.add_node(label)
                nodelabels[id] = label
                G.node_attr[label] = {'id': id, 'x': x, 'y': y, 'shape': shape}
                extra_attr = zip(splitline[5::2], splitline[6::2])
                G.node_attr[label].update(extra_attr)
        if l.startswith("*edges") or l.startswith("*arcs"):
            if l.startswith("*edge"):
                G = networkx_v099.XGraph(G)  # switch from digraph to graph
            for l in lines:
                splitline = shlex.split(l)
                ui, vi, w = splitline[0:3]
                u = nodelabels.get(ui, ui)
                v = nodelabels.get(vi, vi)
                edge_data = {'value': float(w)}
                extra_attr = zip(splitline[3::2], splitline[4::2])
                edge_data.update(extra_attr)
                G.add_edge(u, v, edge_data)
    return G
Пример #4
0
def parse_pajek(lines):
    """Parse pajek format graph from string or iterable.."""
    import shlex
    if is_string_like(lines): lines=iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines])
    G=networkx_v099.XDiGraph(selfloops=True) # are multiedges allowed in Pajek?
    G.node_attr={} # dictionary to hold node attributes
    directed=True # assume this is a directed network for now
    while lines:
        try:
            l=lines.next()
        except: #EOF
            break
        if l.startswith("*network"):
            label,name=l.split()
            G.name=name
        if l.startswith("*vertices"):
            nodelabels={}
            l,nnodes=l.split()
            for i in range(int(nnodes)):
                splitline=shlex.split(lines.next())
                id,label,x,y,shape=splitline[0:5]
                G.add_node(label)
                nodelabels[id]=label
                G.node_attr[label]={'id':id,'x':x,'y':y,'shape':shape}
                extra_attr=zip(splitline[5::2],splitline[6::2])
                G.node_attr[label].update(extra_attr)
        if l.startswith("*edges") or l.startswith("*arcs"):
            if l.startswith("*edge"):
                G=networkx_v099.XGraph(G) # switch from digraph to graph
            for l in lines:
                splitline=shlex.split(l)
                ui,vi,w=splitline[0:3]
                u=nodelabels.get(ui,ui)
                v=nodelabels.get(vi,vi)
                edge_data={'value':float(w)}
                extra_attr=zip(splitline[3::2],splitline[4::2])
                edge_data.update(extra_attr)
                G.add_edge(u,v,edge_data)
    return G
Пример #5
0
def parse_leda(lines):
    """Parse LEDA.GRAPH format from string or iterable.
    Returns an Graph or DiGraph."""
    if is_string_like(lines): lines = iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines \
            if not (line.startswith('#') or line.startswith('\n') or line=='')])
    for i in range(3):
        lines.next()
    # Graph
    du = int(lines.next())  # -1 directed, -2 undirected
    if du == -1:
        G = networkx_v099.DiGraph()
    else:
        G = networkx_v099.Graph()

    # Nodes
    n = int(lines.next())  # number of vertices
    node = {}
    for i in range(1, n + 1):  # LEDA counts from 1 to n
        symbol = lines.next()[2:-3]  # strip out data from |{data}|
        if symbol == "":
            symbol = str(i)  # use int if no label - could be trouble
        node[i] = symbol

    G.add_nodes_from([s for i, s in node.items()])

    # Edges
    m = int(lines.next())  # number of edges
    for i in range(m):
        try:
            s, t, reversal, label = lines.next().split()
        except:
            raise NetworkXError,\
                  'Too few fields in LEDA.GRAPH edge %d' % (i+1)
        # BEWARE: no handling of reversal edges
        G.add_edge(node[int(s)], node[int(t)], label[2:-2])
    return G
Пример #6
0
def parse_leda(lines):
    """Parse LEDA.GRAPH format from string or iterable.
    Returns an Graph or DiGraph."""
    if is_string_like(lines): lines=iter(lines.split('\n'))
    lines = iter([line.rstrip('\n') for line in lines \
            if not (line.startswith('#') or line.startswith('\n') or line=='')])
    for i in range(3):
        lines.next()
    # Graph
    du = int(lines.next())	# -1 directed, -2 undirected
    if du==-1:
        G = networkx_v099.DiGraph()
    else:
        G = networkx_v099.Graph()
        
    # Nodes
    n =int(lines.next())	# number of vertices
    node={}
    for i in range(1,n+1):  # LEDA counts from 1 to n
        symbol=lines.next()[2:-3]  # strip out data from |{data}|
        if symbol=="": symbol=str(i) # use int if no label - could be trouble
        node[i]=symbol

    G.add_nodes_from([s for i,s in node.items()])
	
    # Edges
    m = int(lines.next())	# number of edges
    for i in range(m):
        try:
            s,t,reversal,label=lines.next().split()
        except:
            raise NetworkXError,\
                  'Too few fields in LEDA.GRAPH edge %d' % (i+1)
        # BEWARE: no handling of reversal edges
        G.add_edge(node[int(s)],node[int(t)],label[2:-2])
    return G
Пример #7
0
 def make_str(t):
     if is_string_like(t): return t
     return str(t)
Пример #8
0
def union(G,H,create_using=None,rename=False,name=None):
    """ Return the union of graphs G and H.
    
    Graphs G and H must be disjoint, otherwise an exception is raised.

    Node names of G and H can be changed be specifying the tuple
    rename=('G-','H-') (for example).
    Node u in G is then renamed "G-u" and v in H is renamed "H-v".

    To force a disjoint union with node relabeling, use
    disjoint_union(G,H) or convert_node_labels_to integers().

    Optional create_using=R returns graph R filled in with the
    union of G and H.  Otherwise a new graph is created, of the
    same class as G.  It is recommended that G and H be either
    both directed or both undirected.

    A new name can be specified in the form
    X=union(G,H,name="new_name")

    Implemented for Graph, DiGraph, XGraph, XDiGraph.        

    """
    if name is None:
        name="union( %s, %s )"%(G.name,H.name)
    if create_using is None:
        R=G.__class__()
    else:
        R=create_using
        R.clear()
    R.name=name

    # rename graph to obtain disjoint node labels
    # note that for objects w/o succinct __name__,
    # the new labels can be quite verbose
    # See also disjoint_union
    if rename: # create new string labels
        Gname={}
        Hname={}
        for v in G:
            if is_string_like(v):
                Gname[v]=rename[0]+v
            else:
                Gname[v]=rename[0]+repr(v)
        for v in H:
            if is_string_like(v):
                Hname[v]=rename[1]+v
            else:
                Hname[v]=rename[1]+repr(v)
    else:
        Gname=dict( ((v,v) for v in G) )
        Hname=dict( ((v,v) for v in H) )

    # node name check
    Hnames=set(Hname.values())
    Gnames=set(Gname.values())
    if Gnames & Hnames:
            raise networkx_v099.NetworkXError,\
            "node sets of G and H are not disjoint. Use appropriate rename=('Gprefix','Hprefix')"
    # node names OK, now build union
    R.add_nodes_from(Gnames)
    R.add_edges_from( ( (Gname[u],Gname[v],x) for u,v,x in G.edges_iter(data=True) ))
    R.add_nodes_from(Hnames)
    R.add_edges_from( ( (Hname[u],Hname[v],x) for u,v,x in H.edges_iter(data=True) ))
    return R
Пример #9
0
 def make_str(t):
     if is_string_like(t): return t
     return str(t)
Пример #10
0
def write_gml(G, path):
    """
    Write the graph G in GML format to the file or file handle path.

    Examples
    ---------

    >>> G=nx.path_graph(4)
    >>> nx.write_gml(G,"test.gml")

    path can be a filehandle or a string with the name of the file.

    >>> fh=open("test.gml",'w')
    >>> nx.write_gml(G,fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_gml(G,"test.gml.gz")

    
    The output file will use the default text encoding on your system.
    It is possible to write files in other encodings by opening
    the file with the codecs module.  See doc/examples/unicode.py
    for hints.

    >>> import codecs
    >>> fh=codecs.open("test.gml",'w',encoding='iso8859-1')# use iso8859-1
    >>> nx.write_gml(G,fh)

    GML specifications indicate that the file should only use
    7bit ASCII text encoding.iso8859-1 (latin-1). 

    Only a single level of attributes for graphs, nodes, and edges,
    is supported.

    """
    fh = _get_fh(path, mode='w')
    #    comments="#"
    #    pargs=comments+" "+' '.join(sys.argv)
    #    fh.write("%s\n" % (pargs))
    #    fh.write(comments+" GMT %s\n" % (time.asctime(time.gmtime())))
    #    fh.write(comments+" %s\n" % (G.name))

    # check for attributes or assign empty dict
    if hasattr(G, 'graph_attr'):
        graph_attr = G.graph_attr
    else:
        graph_attr = {}
    if hasattr(G, 'node_attr'):
        node_attr = G.node_attr
    else:
        node_attr = {}

    indent = 2 * ' '
    count = iter(range(G.number_of_nodes()))
    node_id = {}

    fh.write("graph [\n")
    # write graph attributes
    for k, v in graph_attr.items():
        if is_string_like(v):
            v = '"' + v + '"'
        fh.write(indent + "%s %s\n" % (k, v))
    # write nodes
    for n in G:
        fh.write(indent + "node [\n")
        # get id or assign number
        if n in node_attr:
            nid = node_attr[n].get('id', count.next())
        else:
            nid = count.next()
        node_id[n] = nid
        fh.write(2 * indent + "id %s\n" % nid)
        fh.write(2 * indent + "label \"%s\"\n" % n)
        if n in node_attr:
            for k, v in node_attr[n].items():
                if is_string_like(v): v = '"' + v + '"'
                if k == 'id': continue
                fh.write(2 * indent + "%s %s\n" % (k, v))
        fh.write(indent + "]\n")
    # write edges
    for e in G.edges_iter():
        if len(e) == 3:
            u, v, d = e
            # try to guess what is on the edge and do something reasonable
            edgedata = {}
            if d is None:  # no data
                pass
            elif hasattr(d, 'iteritems'):  # try dict-like
                edgedata = dict(d.iteritems())
            elif hasattr(d, '__iter__'):  # some kind of container
                edgedata['data'] = d.__repr__()
            else:  # something else - string, number
                edgedata['data'] = d
        else:
            u, v = e
            edgedata = {}
        fh.write(indent + "edge [\n")
        fh.write(2 * indent + "source %s\n" % node_id[u])
        fh.write(2 * indent + "target %s\n" % node_id[v])
        for k, v in edgedata.items():
            if k == 'source': continue
            if k == 'target': continue
            if is_string_like(v): v = '"' + v + '"'
            fh.write(2 * indent + "%s %s\n" % (k, v))
        fh.write(indent + "]\n")
    fh.write("]\n")
Пример #11
0
def write_gml(G, path):
    """
    Write the graph G in GML format to the file or file handle path.

    Examples
    ---------

    >>> G=nx.path_graph(4)
    >>> nx.write_gml(G,"test.gml")

    path can be a filehandle or a string with the name of the file.

    >>> fh=open("test.gml",'w')
    >>> nx.write_gml(G,fh)

    Filenames ending in .gz or .bz2 will be compressed.

    >>> nx.write_gml(G,"test.gml.gz")

    
    The output file will use the default text encoding on your system.
    It is possible to write files in other encodings by opening
    the file with the codecs module.  See doc/examples/unicode.py
    for hints.

    >>> import codecs
    >>> fh=codecs.open("test.gml",'w',encoding='iso8859-1')# use iso8859-1
    >>> nx.write_gml(G,fh)

    GML specifications indicate that the file should only use
    7bit ASCII text encoding.iso8859-1 (latin-1). 

    Only a single level of attributes for graphs, nodes, and edges,
    is supported.

    """
    fh=_get_fh(path,mode='w')        
#    comments="#"
#    pargs=comments+" "+' '.join(sys.argv)
#    fh.write("%s\n" % (pargs))
#    fh.write(comments+" GMT %s\n" % (time.asctime(time.gmtime())))
#    fh.write(comments+" %s\n" % (G.name))

    # check for attributes or assign empty dict
    if hasattr(G,'graph_attr'):
        graph_attr=G.graph_attr
    else:
        graph_attr={}
    if hasattr(G,'node_attr'):
        node_attr=G.node_attr
    else:
        node_attr={}

    indent=2*' '
    count=iter(range(G.number_of_nodes()))
    node_id={}

    fh.write("graph [\n")
    # write graph attributes 
    for k,v in graph_attr.items():
        if is_string_like(v):
            v='"'+v+'"'
        fh.write(indent+"%s %s\n"%(k,v))
    # write nodes        
    for n in G:
        fh.write(indent+"node [\n")
        # get id or assign number
        if n in node_attr:
            nid=node_attr[n].get('id',count.next())
        else:
            nid=count.next()
        node_id[n]=nid
        fh.write(2*indent+"id %s\n"%nid)
        fh.write(2*indent+"label \"%s\"\n"%n)
        if n in node_attr:
          for k,v in node_attr[n].items():
              if is_string_like(v): v='"'+v+'"'
              if k=='id': continue
              fh.write(2*indent+"%s %s\n"%(k,v))
        fh.write(indent+"]\n")
    # write edges
    for e in G.edges_iter():
        if len(e)==3:
            u,v,d=e
            # try to guess what is on the edge and do something reasonable
            edgedata={}
            if d is None:  # no data 
                pass
            elif hasattr(d,'iteritems'): # try dict-like
                edgedata=dict(d.iteritems())
            elif hasattr(d,'__iter__'): # some kind of container
                edgedata['data']=d.__repr__()
            else: # something else - string, number
                edgedata['data']=d
        else:
            u,v=e
            edgedata={}
        fh.write(indent+"edge [\n")
        fh.write(2*indent+"source %s\n"%node_id[u])
        fh.write(2*indent+"target %s\n"%node_id[v])
        for k,v in edgedata.items():
            if k=='source': continue
            if k=='target': continue
            if is_string_like(v): v='"'+v+'"'
            fh.write(2*indent+"%s %s\n"%(k,v))
        fh.write(indent+"]\n")
    fh.write("]\n")