将graphviz.dot.Digraph转换为networkx.Graph [英] Convert graphviz.dot.Digraph to networkx.Graph

查看:458
本文介绍了将graphviz.dot.Digraph转换为networkx.Graph的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I convert a graphviz.dot.Digraph into a networkx.Graph (or any subclass of it)?

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

LightGBM, an implementation of a tree-based algorithm, has a function returning a graphviz.dot.Digraph object. This type of object can represent any directed graph, but my graph is specifically a tree, and thus can be represented in JSON via a simpler nested structure:

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

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

Another, longer example of the above JSON structure is here. I use this JSON format to create a tree visualization using d3 in javascript.

总共,我需要将此graphviz.dot.Digraph对象转换为上述嵌套JSON格式.

In total, I need to convert this graphviz.dot.Digraph object to the above nested-JSON format.

如果我可以将此graphviz.dot.Digraph对象转换为networkx.Graph对象,则可以使用可以使用的东西进行另一次转换.

If I can convert this graphviz.dot.Digraph object into a networkx.Graph object, I can use this method to convert it to the required JSON format. This intermediate conversion has been problematic for me. It seems I need another conversion to something networkx can use.

推荐答案

graphviz.dot.Digraph转换为networkx.Graph的一种方法是将其转换为pydotplus.graphviz.Dot对象,然后将其转换为networkx.classes.multidigraph.MultiDiGraph对象, graph的密文.

One method of converting graphviz.dot.Digraph to a networkx.Graph is to convert it to a pydotplus.graphviz.Dot object, and convert that into a networkx.classes.multidigraph.MultiDiGraph object which inherets from 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的人

The rest is just for people considering conversion to 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方法,可以将所有这些方法都用快捷方式...但是我将其留在这里.

Later I found out LightGBM actually has a dump_model method that shortcuts all of this...but I'll leave this here.

这篇关于将graphviz.dot.Digraph转换为networkx.Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆