~ubuntu-branches/ubuntu/utopic/python-networkx/utopic

« back to all changes in this revision

Viewing changes to networkx/readwrite/json_graph/node_link.py

  • Committer: Package Import Robot
  • Author(s): Sandro Tosi
  • Date: 2012-08-11 12:41:30 UTC
  • mfrom: (1.4.1) (5.1.13 sid)
  • Revision ID: package-import@ubuntu.com-20120811124130-whr6uso7fncyg8bi
Tags: 1.7-1
* New upstream release
* debian/patches/changeset_*
  - removed, included upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Copyright (C) 2011 by 
 
2
#    Aric Hagberg <hagberg@lanl.gov>
 
3
#    Dan Schult <dschult@colgate.edu>
 
4
#    Pieter Swart <swart@lanl.gov>
 
5
#    All rights reserved.
 
6
#    BSD license.
 
7
from itertools import count,repeat
 
8
import json
 
9
import networkx as nx
 
10
__author__ = """Aric Hagberg (hagberg@lanl.gov))"""
 
11
__all__ = ['node_link_data', 'node_link_graph']
 
12
 
 
13
def node_link_data(G): 
 
14
    """Return data in node-link format that is suitable for JSON serialization
 
15
    and use in Javascript documents.
 
16
 
 
17
    Parameters
 
18
    ----------
 
19
    G : NetworkX graph
 
20
    
 
21
    Returns
 
22
    -------
 
23
    data : dict
 
24
       A dictionary with node-link formatted data.
 
25
 
 
26
    Examples
 
27
    --------
 
28
    >>> from networkx.readwrite import json_graph
 
29
    >>> G = nx.Graph([(1,2)])
 
30
    >>> data = json_graph.node_link_data(G)
 
31
 
 
32
    To serialize with json
 
33
 
 
34
    >>> import json
 
35
    >>> s = json.dumps(data)
 
36
    
 
37
    Notes
 
38
    -----
 
39
    Graph, node, and link attributes are stored in this format but keys 
 
40
    for attributes must be strings if you want to serialize with JSON.
 
41
 
 
42
    See Also
 
43
    --------
 
44
    node_link_graph, adjacency_data, tree_data
 
45
    """
 
46
    mapping = dict(zip(G,count()))
 
47
    data = {}
 
48
    data['directed'] = G.is_directed()
 
49
    data['multigraph'] = G.is_multigraph()
 
50
    data['graph'] = list(G.graph.items())
 
51
    data['nodes'] = [ dict(id=n, **G.node[n]) for n in G ]
 
52
    data['links'] = [ dict(source=mapping[u], target=mapping[v], **d)
 
53
                      for u,v,d in G.edges(data=True) ]
 
54
    return data
 
55
 
 
56
def node_link_graph(data, directed=False, multigraph=True):
 
57
    """Return graph from node-link data format. 
 
58
 
 
59
    Parameters
 
60
    ----------
 
61
    data : dict
 
62
        node-link formatted graph data
 
63
    
 
64
    directed : bool        
 
65
        If True, and direction not specified in data, return a directed graph.
 
66
 
 
67
    multigraph : bool        
 
68
        If True, and multigraph not specified in data, return a multigraph.
 
69
 
 
70
    Returns
 
71
    -------
 
72
    G : NetworkX graph
 
73
       A NetworkX graph object
 
74
 
 
75
    Examples
 
76
    --------
 
77
    >>> from networkx.readwrite import json_graph
 
78
    >>> G = nx.Graph([(1,2)])
 
79
    >>> data = json_graph.node_link_data(G)
 
80
    >>> H = json_graph.node_link_graph(data)
 
81
 
 
82
    See Also
 
83
    --------
 
84
    node_link_data, adjacency_data, tree_data
 
85
    """
 
86
    multigraph = data.get('multigraph',multigraph)
 
87
    directed = data.get('directed',directed)
 
88
    if multigraph:
 
89
        graph = nx.MultiGraph()
 
90
    else:
 
91
        graph = nx.Graph()
 
92
    if directed:
 
93
        graph = graph.to_directed()
 
94
    mapping=[]
 
95
    graph.graph = dict(data.get('graph',[]))
 
96
    c = count()
 
97
    for d in data['nodes']:
 
98
        node = d.get('id',next(c))
 
99
        mapping.append(node)
 
100
        nodedata = dict((str(k),v) for k,v in d.items() if k!='id')
 
101
        graph.add_node(node, **nodedata)
 
102
    for d in data['links']:
 
103
        source = d.pop('source')
 
104
        target = d.pop('target')
 
105
        edgedata = dict((str(k),v) for k,v in d.items() 
 
106
                        if k!='source' and k!='target')
 
107
        graph.add_edge(mapping[source],mapping[target],**edgedata)
 
108
    return graph
 
109