Logo Search packages:      
Sourcecode: python-networkx version File versions  Download package

def networkx::drawing::nx_pygraphviz::from_pygraphviz (   A,
  create_using = None,
  graph_attr = None,
  node_attr = None,
  edge_attr = None 
)

Creates an networkx graph from an pygraphviz graph A.

This simplest use creates a netwrkx graph and ignores graphviz
attributes.
 
Optional arguments graph_attr, node_attr, edge_attr are used
to get graph properties.
http://www.graphviz.org/doc/info/attrs.html

The graph_attr, node_attr, and edge_attr  arguments can be either
a dictionary or function as follows:

graph_attr:
    empty dictionary will be filled with keys as attribute names and
    values as attribute values
    or
    A function that takes two arguments -
    the NetworkX graph and attributes::

      def graph_attr(G,a):
          print a # just print attributes
          return
    
node_attr:
    empty dictionary will be filled with keys as nodes with
    value set to a dictionary of attribute names and values.
    or
    A function that takes three arguments - the graph name, node name
    and attributes::

      def node_attr(G,n,a):
          print n,a # print node and attributes
          return a

edge_attr:
    empty dictionary will be filled with keys as  edge tuples (u,v)
    with a value set to a dictionary of attribute names and values.
    or
    A function that takes three arguments - the graph name, edge tuple,
    and attributes::

      def edge_attr(G,n,e):
          print e,a # print node and attributes
          return a


create_using is an optional networkx graph type.
The default is to use a Graph or DiGraph depending on the
type of the pygraphviz graph A

Definition at line 197 of file nx_pygraphviz.py.

                                   :
    """Creates an networkx graph from an pygraphviz graph A.

    This simplest use creates a netwrkx graph and ignores graphviz
    attributes.
 
    Optional arguments graph_attr, node_attr, edge_attr are used
    to get graph properties.
    http://www.graphviz.org/doc/info/attrs.html

    The graph_attr, node_attr, and edge_attr  arguments can be either
    a dictionary or function as follows:

    graph_attr:
        empty dictionary will be filled with keys as attribute names and
        values as attribute values
        or
        A function that takes two arguments -
        the NetworkX graph and attributes::

          def graph_attr(G,a):
              print a # just print attributes
              return
        
    node_attr:
        empty dictionary will be filled with keys as nodes with
        value set to a dictionary of attribute names and values.
        or
        A function that takes three arguments - the graph name, node name
        and attributes::

          def node_attr(G,n,a):
              print n,a # print node and attributes
              return a

    edge_attr:
        empty dictionary will be filled with keys as  edge tuples (u,v)
        with a value set to a dictionary of attribute names and values.
        or
        A function that takes three arguments - the graph name, edge tuple,
        and attributes::

          def edge_attr(G,n,e):
              print e,a # print node and attributes
              return a


    create_using is an optional networkx graph type.
    The default is to use a Graph or DiGraph depending on the
    type of the pygraphviz graph A

    """
    import networkx

    # set graph type or user defined
    if create_using is None:
        if A.is_undirected():
            create_using=networkx.Graph()
        else:
            create_using=networkx.DiGraph()

    N=networkx.empty_graph(0,create_using)
    N.name=str(A)

    # handle graph attributes,
    if hasattr(graph_attr,"__setitem__"):   # if we are a dict
        add_graph=graph_attr.update   # call update as a function
        # user function                
    elif hasattr(graph_attr,"__call__"): 
        def add_graph(a):
            return graph_attr(N,a)
    else:
        def add_graph(a):
            return

    # add node function with optional attributes
    if hasattr(node_attr,"__setitem__"):  
        # update user dictionary 
        def add_node(n,a):
            N.add_node(n)
            node_attr[n]=a
            return
    elif hasattr(node_attr,"__call__"): 
        # call user function
        def add_node(n,a):
            node_attr(N,n,a)
            return 
    else:
        # just add node
        def add_node(n,a):
            N.add_node(n)
            return
                

    # add edge function with optional attributes
    if hasattr(edge_attr,"__setitem__"):   
        # update user dictionary
        def add_edge(e,a):
            N.add_edge(e[0],e[1])
            edge_attr[e]=a
            return
    elif hasattr(edge_attr,"__call__"): 
        # call user function
        def add_edge(e,a):
            return edge_attr(N,e,a)
    else:
        # just add edge
        def add_edge(e,a):
            N.add_edge(e[0],e[1])

    # graph                
    add_graph(dict(A.get_all_attr()))
    # loop through nodes            
    for node in A.nodes():
        name=pygraphviz.graphviz.agnameof(node.anode)
        add_node(name,A.get_all_attr(node=node))
    # loop through edges
    edges_seen = {}
    for edge in A.edges():
        if edge in edges_seen: continue
        source=pygraphviz.graphviz.agnameof(edge.source().anode)
        target=pygraphviz.graphviz.agnameof(edge.target().anode)
        edges_seen[edge]=1
        add_edge((source,target),A.get_all_attr(edge=(source,target)))
    return N

networkx_from_pygraphviz=from_pygraphviz
pygraphviz_from_networkx=to_pygraphviz

def xgraph_from_pygraphviz(A):


Generated by  Doxygen 1.6.0   Back to index