Exemple #1
0
def read_gexf(path, node_type=str, relabel=False):
    """Read graph in GEXF format from path.

    "GEXF (Graph Exchange XML Format) is a language for describing
    complex networks structures, their associated data and dynamics" [1]_.

    Parameters
    ----------
    path : file or string
       File or filename to write.  
       Filenames ending in .gz or .bz2 will be compressed.

    node_type: Python type (default: str)
       Convert node ids to this type 

    relabel : bool (default: False)       
       If True relabel the nodes to use the GEXF node "label" attribute 
       instead of the node "id" attribute as the NetworkX node label.

    Returns
    -------
    graph: NetworkX graph
        If no parallel edges are found a Graph or DiGraph is returned.
        Otherwise a MultiGraph or MultiDiGraph is returned.

    Notes
    -----
    This implementation does not support mixed graphs (directed and unidirected 
    edges together).

    References
    ----------
    .. [1] GEXF graph format, http://gexf.net/format/
    """
    fh = _get_fh(path, mode='rb')
    reader = GEXFReader(node_type=node_type)
    if relabel:
        G = relabel_gexf_graph(reader(fh))
    else:
        G = reader(fh)
    return G
Exemple #2
0
def read_gexf(path,node_type=str,relabel=False):
    """Read graph in GEXF format from path.

    "GEXF (Graph Exchange XML Format) is a language for describing
    complex networks structures, their associated data and dynamics" [1]_.

    Parameters
    ----------
    path : file or string
       File or filename to write.  
       Filenames ending in .gz or .bz2 will be compressed.

    node_type: Python type (default: str)
       Convert node ids to this type 

    relabel : bool (default: False)       
       If True relabel the nodes to use the GEXF node "label" attribute 
       instead of the node "id" attribute as the NetworkX node label.

    Returns
    -------
    graph: NetworkX graph
        If no parallel edges are found a Graph or DiGraph is returned.
        Otherwise a MultiGraph or MultiDiGraph is returned.

    Notes
    -----
    This implementation does not support mixed graphs (directed and unidirected 
    edges together).

    References
    ----------
    .. [1] GEXF graph format, http://gexf.net/format/
    """
    fh=_get_fh(path,mode='rb')
    reader = GEXFReader(node_type=node_type)
    if relabel:
        G=relabel_gexf_graph(reader(fh))
    else:
        G=reader(fh)
    return G
Exemple #3
0
def write_gexf(G, path, encoding='utf-8', prettyprint=True):
    """Write G in GEXF format to path.

    "GEXF (Graph Exchange XML Format) is a language for describing
    complex networks structures, their associated data and dynamics" [1]_.

    Parameters
    ----------
    G : graph
       A NetworkX graph
    path : file or string
       File or filename to write.  
       Filenames ending in .gz or .bz2 will be compressed.
    encoding : string (optional)
       Encoding for text data.
    prettyprint : bool (optional)
       If True use line breaks and indenting in output XML.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_gexf(G, "test.gexf")

    Notes
    -----
    This implementation does not support mixed graphs (directed and unidirected 
    edges together).
    
    The node id attribute is set to be the string of the node label.  
    If you want to specify an id use set it as node data, e.g.
    node['a']['id']=1 to set the id of node 'a' to 1.

    References
    ----------
    .. [1] GEXF graph format, http://gexf.net/format/
    """
    fh = _get_fh(path, mode='wb')
    writer = GEXFWriter(encoding=encoding, prettyprint=prettyprint)
    writer.add_graph(G)
    writer.write(fh)
Exemple #4
0
def write_gexf(G, path, encoding='utf-8',prettyprint=True):
    """Write G in GEXF format to path.

    "GEXF (Graph Exchange XML Format) is a language for describing
    complex networks structures, their associated data and dynamics" [1]_.

    Parameters
    ----------
    G : graph
       A NetworkX graph
    path : file or string
       File or filename to write.  
       Filenames ending in .gz or .bz2 will be compressed.
    encoding : string (optional)
       Encoding for text data.
    prettyprint : bool (optional)
       If True use line breaks and indenting in output XML.

    Examples
    --------
    >>> G=nx.path_graph(4)
    >>> nx.write_gexf(G, "test.gexf")

    Notes
    -----
    This implementation does not support mixed graphs (directed and unidirected 
    edges together).
    
    The node id attribute is set to be the string of the node label.  
    If you want to specify an id use set it as node data, e.g.
    node['a']['id']=1 to set the id of node 'a' to 1.

    References
    ----------
    .. [1] GEXF graph format, http://gexf.net/format/
    """
    fh = _get_fh(path, mode='wb')
    writer = GEXFWriter(encoding=encoding,prettyprint=prettyprint)
    writer.add_graph(G)
    writer.write(fh)