将graphviz.dot.Digraph转换为networkx.Graph

问题描述 投票:1回答:1

如何将graphviz.dot.Digraph转换为networkx.Graph(或其任何子类)?

动机

LightGBM是基于树的算法的实现,具有返回graphviz.dot.Digraph对象的函数。这种类型的对象可以表示任何有向图,但我的图特别是树,因此可以通过更简单的嵌套结构以JSON表示:

var tree = {
    "name": "parent",
    "children": [
         {
         "name": "child_1"
         }
         {
         "name": "child_2"
         }
    [
}

上述JSON结构的另一个较长的例子是here。我使用这种JSON格式在d3中使用javascript创建树形可视化。

总的来说,我需要将这个graphviz.dot.Digraph对象转换为上面嵌套的JSON格式。

如果我可以将这个graphviz.dot.Digraph对象转换为networkx.Graph对象,我可以使用this method将其转换为所需的JSON格式。这种中间转换对我来说是个问题。我似乎需要另一种转换为networkx can use

python json tree networkx graphviz
1个回答
1
投票

graphviz.dot.Digraph转换为networkx.Graph的一种方法是将其转换为pydotplus.graphviz.Dot对象,并将其转换为networkx.classes.multidigraph.MultiDiGraph对象,该对象来自graph

#--------------------
# Do whatever it is you do to create the graphviz.Digraph object
import lightgbm as lgbm

# .......
digraph = lgbm.create_tree_digraph(model)
print(type(digraph)) # prints <class 'graphviz.dot.Digraph'>


#--------------------
# Perform the conversion to networkx
import pydotplus
import networkx

dotplus = pydotplus.graph_from_dot_data(digraph.source)
print(type(dotplus)) # prints <class 'pydotplus.graphviz.Dot'>
# if graph doesn't have multiedges, use dotplus.set_strict(true)
nx_graph = networkx.nx_pydot.from_pydot(dotplus)
print(type(nx_graph)) # prints <class 'networkx.classes.multidigraph.MultiDiGraph'>

其余的只是考虑转换为JSON的人

#--------------------
# Optionally, convert to json
import json

# must specify whatever node is the root of the tree, here its 'split0'
#     or you can try list(graph)[0] if you're not sure.
data = networkx.readwrite.json_graph.tree_data(nx_graph,root='split0')
# data is just a dictionary. Dump results in proper json format.
json.dump(data,open(file_path,'w'))

后来我发现LightGBM实际上有一个dump_model方法,可以快速切换所有这些......但是我会把它留在这里。

© www.soinside.com 2019 - 2024. All rights reserved.