2017-02-11 3 views
0

documentation hereに従って、read_dot()メソッドはファイルハンドルを入力として受け取ります。networkxのドットグラフをファイルではなく文字列から読み取る

私がしなければしても、それは正常に動作し、dotFileはファイルハンドルである

g = nx.drawing.nx_agraph.read_dot(dotFile) 

。しかし、変数にdotのフォーマットされたグラフがある場合、どのようにnxグラフ形式でインポートできますか?

編集 - 私の変数は

dotFormat = """ 
digraph G{ 
edge [dir=forward] 
node [shape=plaintext] 
0 [label="0 (None)"] 
0 -> 7 [label="root"] 
1 [label="1 (The)"] 
4 [label="4 (great Indian Circus)"] 
4 -> 4 [label="compound"] 
4 -> 1 [label="det"] 
4 -> 4 [label="amod"] 
5 [label="5 (is)"] 
6 [label="6 (in)"] 
7 [label="7 (Mumbai)"] 
7 -> 6 [label="case"] 
7 -> 5 [label="cop"] 
7 -> 4 [label="nsubj"] 
} 
""" 

EDIT 2、このようになります - 私もpy_dotを読んで、このようnetworkxグラフに変換してみました。その後

pg = pydot.graph_from_dot_data(dotFormat) 
g = nx.nx_pydot.from_pydot(pg) 

私は何をすべきエラー

if P.get_strict(None): # pydot bug: get_strict() shouldn't take argument 
AttributeError: 'list' object has no attribute 'get_strict' 

次のですか?

答えて

1

pygraphvizを明示的に使用してグラフを読み込んだ後、networkxグラフ(この場合はMultiDiGraph)に変換することで、これを行うことができます。

In [1]: import pygraphviz 

In [2]: from networkx.drawing import nx_agraph 

In [3]: dotFormat = """ 
digraph G{ 
edge [dir=forward] 
node [shape=plaintext] 
0 [label="0 (None)"] 
0 -> 7 [label="root"] 
1 [label="1 (The)"] 
4 [label="4 (great Indian Circus)"] 
4 -> 4 [label="compound"] 
4 -> 1 [label="det"] 
4 -> 4 [label="amod"] 
5 [label="5 (is)"] 
6 [label="6 (in)"] 
7 [label="7 (Mumbai)"] 
7 -> 6 [label="case"] 
7 -> 5 [label="cop"] 
7 -> 4 [label="nsubj"] 
} 
""" 

In [4]: G = nx_agraph.from_agraph(pygraphviz.AGraph(dotFormat)) 
関連する問題