将networkx图保存为json图的方法?

发布于 2024-09-07 13:10:58 字数 106 浏览 8 评论 0原文

似乎networkx中应该有一个方法来导出json图形格式,但我没有看到它。我想这应该很容易用 nx.to_dict_of_dicts() 完成,但需要一些操作。有人知道一个简单而优雅的解决方案吗?

Seems like there should be a method in networkx to export the json graph format, but I don't see it. I imagine this should be easy to do with nx.to_dict_of_dicts(), but would require a bit of manipulation. Anyone know of a simple and elegant solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

っ〆星空下的拥抱 2024-09-14 13:10:58

这个文档包含完整的描述

一个简单的例子是这样的:

import networkx as nx
from networkx.readwrite import json_graph

DG = nx.DiGraph()
DG.add_edge('a', 'b')
print json_graph.dumps(DG)

您还可以查看 Javascript/SVG/D3 不错将物理添加到图形可视化的示例。

This documentation contains a full description

A simple example is this:

import networkx as nx
from networkx.readwrite import json_graph

DG = nx.DiGraph()
DG.add_edge('a', 'b')
print json_graph.dumps(DG)

You can also take a look at the Javascript/SVG/D3 nice example on adding physics to the graph visualization.

你げ笑在眉眼 2024-09-14 13:10:58

通常我使用以下代码:

import networkx as nx; 
from networkx.readwrite import json_graph;
G = nx.Graph();
G.add_node(...)
G.add_edge(...)
....
json_graph.node_link_data(G)

它将创建 json 格式的图形,其中节点位于 nodes 中,边位于 links
除了有关图表的其他信息(方向性等)

Generally I use the following code :

import networkx as nx; 
from networkx.readwrite import json_graph;
G = nx.Graph();
G.add_node(...)
G.add_edge(...)
....
json_graph.node_link_data(G)

it will create json formatted graph in which the nodes are in nodes and edges in links
in addition to other information about the graph (directionality, ... etc)

梦里的微风 2024-09-14 13:10:58

这是我刚刚完成的 JSON 方法,以及用于读回结果的代码。它保存节点和边缘属性,以备您需要时使用。

import simplejson as json
import networkx as nx
G = nx.DiGraph()
# add nodes, edges, etc to G ...

def save(G, fname):
    json.dump(dict(nodes=[[n, G.node[n]] for n in G.nodes()],
                   edges=[[u, v, G.edge[u][v]] for u,v in G.edges()]),
              open(fname, 'w'), indent=2)

def load(fname):
    G = nx.DiGraph()
    d = json.load(open(fname))
    G.add_nodes_from(d['nodes'])
    G.add_edges_from(d['edges'])
    return G

Here is a JSON approach that I just did, together with code to read the results back in. It saves the node and edge attributes, in case you need that.

import simplejson as json
import networkx as nx
G = nx.DiGraph()
# add nodes, edges, etc to G ...

def save(G, fname):
    json.dump(dict(nodes=[[n, G.node[n]] for n in G.nodes()],
                   edges=[[u, v, G.edge[u][v]] for u,v in G.edges()]),
              open(fname, 'w'), indent=2)

def load(fname):
    G = nx.DiGraph()
    d = json.load(open(fname))
    G.add_nodes_from(d['nodes'])
    G.add_edges_from(d['edges'])
    return G
农村范ル 2024-09-14 13:10:58

试试这个:

# Save graph
nx.write_gml(G, "path_where_graph_should_be_saved.gml")

# Read graph
G = nx.read_gml('path_to_graph_graph.gml')

Try this:

# Save graph
nx.write_gml(G, "path_where_graph_should_be_saved.gml")

# Read graph
G = nx.read_gml('path_to_graph_graph.gml')
み格子的夏天 2024-09-14 13:10:58

其余的解决方案对我来说不起作用。来自 networkx 2.2 文档

nx.write_gpickle(G, "test.gpickle")
G = nx.read_gpickle("test.gpickle")

The rest of the solutions didn't work for me. From the networkx 2.2 documentation:

nx.write_gpickle(G, "test.gpickle")
G = nx.read_gpickle("test.gpickle")
回心转意 2024-09-14 13:10:58

这是 Abraham Flaxman 对 networkx 2.7 的回答的修改版本:

def save(G, fname):
    nodeArr = []
    for node in list(G.nodes(data=True)):
        print('node:', node)
        nodeArr.append(node)

    edgeArr = []
    for edge in list(G.edges()):
        print('edge:', edge)
        edgeArr.append(edge)

    graphDict = { 'nodes': nodeArr,
                  'edges': edgeArr}
    json.dump(graphDict, open(fname, 'w'), indent=2)

def load(fname):
    G = nx.DiGraph()
    d = json.load(open(fname))
    G.add_nodes_from(d['nodes'])
    G.add_edges_from(d['edges'])
    return G

Here's a modified version of Abraham Flaxman's answer for networkx 2.7:

def save(G, fname):
    nodeArr = []
    for node in list(G.nodes(data=True)):
        print('node:', node)
        nodeArr.append(node)

    edgeArr = []
    for edge in list(G.edges()):
        print('edge:', edge)
        edgeArr.append(edge)

    graphDict = { 'nodes': nodeArr,
                  'edges': edgeArr}
    json.dump(graphDict, open(fname, 'w'), indent=2)

def load(fname):
    G = nx.DiGraph()
    d = json.load(open(fname))
    G.add_nodes_from(d['nodes'])
    G.add_edges_from(d['edges'])
    return G
路弥 2024-09-14 13:10:58

节点和边的信息是否足够?如果是这样,您可以编写自己的函数:

json.dumps(dict(nodes=graph.nodes(), edges=graph.edges()))

Are the nodes and edges enough information? If so, you could write your own function:

json.dumps(dict(nodes=graph.nodes(), edges=graph.edges()))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文