I am using pydotplus
to parse a dot file. The function graph = pydotplus.graph_from_dot_file(dot_file)
return a pydotplus.graphviz.Dot object, I want to print the graph's nodes, edges infomationn, save and use the content in my python program. It seems DOT is a special class, I cannot print its content. How to get the content(nodes, edges) so I can save and use it in my data structure with python?
The Dot class inherits from the Graph class which has the methods get_nodes() and get_edges(). Here's an example of getting data on the nodes and edges.
import pydotplus
from sklearn import tree
# Data Collection
X = [[180, 15, 0],
[177, 42, 0],
[136, 35, 1],
[174, 65, 0],
[141, 28, 1]]
Y = ['1', '2', '1', '2', '1']
data_feature_names = ['a', 'b', 'c']
# Training
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, Y)
dot_data = tree.export_graphviz(clf,
feature_names=data_feature_names,
out_file=None,
filled=True,
rounded=True)
graph = pydotplus.graph_from_dot_data(dot_data)
print(graph.to_string())
for node in graph.get_nodes():
print(node.get_name())
print(node.get_port())
print(node.to_string())
print(node.obj_dict)
for edge in graph.get_edges():
print(edge.get_source())
print(edge.get_destination())
print(edge.to_string())
print(edge.obj_dict)