def finish(res, dest): if not os.path.exists(dest): os.makedirs(dest) summaries = pd.DataFrame() sub_vs_orig_DF = pd.DataFrame() sub_vs_sub_DF = pd.DataFrame() trees = list() for t in res: try: graph = pydot.graph_from_dot_data(repr(t.tree)) graph.write_svg("{0}/CrossVal{1}_train.svg".format(dest, t.tree.treeNum)) graph = pydot.graph_from_dot_data(repr(t)) graph.write_svg("{0}/CrossVal{1}_test.svg".format(dest, t.tree.treeNum)) except: print("Unexpected error:", t.tree.treeNum, sys.exc_info()[0]) summaries = summaries.append(Summary(t, 1, t.tree.treeNum, "Root")) summaries = summaries.append(Summary(t.left, 2, t.tree.treeNum, "Left")) summaries = summaries.append(Summary(t.right, 2, t.tree.treeNum, "Right")) s_v_o, s_v_s = ptfd.patterns(t, 2) sub_vs_orig_DF = sub_vs_orig_DF.append(s_v_o) sub_vs_sub_DF = sub_vs_sub_DF.append(s_v_s) trees.append(t) print(summaries) summaries.to_csv("./{0}/summary.csv".format(dest)) sub_vs_orig_DF.to_csv("./{0}/pattern_sub_orig.csv".format(dest)) sub_vs_sub_DF.to_csv("./{0}/pattern_sub_sub.csv".format(dest)) with open("{0}/trees.pkl".format(dest), 'wb') as f: pickle.dump(trees, f, pickle.HIGHEST_PROTOCOL)
def visualize_network(model, filename, show_layer_names=False): from keras.utils.vis_utils import model_to_dot import pydot_ng as pydot d = model_to_dot(model, show_shapes=True, show_layer_names=show_layer_names) d.set_dpi(50) pydot.graph_from_dot_data(d.to_string().replace('None, ', '')).write(filename, format='svg')
def importGraph(self, text): '''Init text after an import. Argument(s): text (str): Textual representation of the graph ''' self.acceptUpdate = False self.setPlainText(text) pydotGraph = graph_from_dot_data(text) # Check that attributes are in valid form message = self.checkItemsAttributes(pydotGraph.get_nodes(), pydotGraph.get_edges()) if not message: self.rebuildTextModel(text, pydotGraph) # Send every elements to the model to build him for id, args in self.nodes.items(): self.controller.onCreateNode(id, args) for id, args in self.edges.items(): self.controller.onCreateEdge(args[EdgeArgs.sourceId], args[EdgeArgs.destId]) # Some attributes are in invalid form else: QMessageBox.warning(self, "Syntax error", message) self.acceptUpdate = True
def focusOutEvent(self, event): '''Handle focus out event. Argument(s): event (QFocusEvent ): Focus event ''' QGraphicsTextItem.focusOutEvent(self, event) # Create a fake node to test if label is valid with pydot fakeNode = ("fake[" + NodeDotAttrs.label.value + "=" + self.toPlainText() + "]") pydotGraph = graph_from_dot_data("graph{" + fakeNode + "}") # Label is valid: we can do the update if pydotGraph: dicDotAttrs = { NodeDotAttrs.label.value: NodeDotLabelUtils.formatLabel(self.toPlainText()) } # Update text in other views node = self.parentItem() node.graphicsGraphView.controller.onEditNode(node.id, dicDotAttrs) # Disable edit text self.setTextInteractionFlags(Qt.NoTextInteraction) # Label is invalid: force user to write a correct label else: QMessageBox.warning(None, "Syntax error", "The label is invalid.") self.setFocus()
def test_parse_multiple_graphs(): graph_data = dedent( """\ graph A { a->b }; graph B {c->d} """ ) graphs = pydot.graph_from_dot_data(graph_data) assert len(graphs) == 2 assert sorted(g.get_name() for g in graphs) == sorted(["A", "B"])
def findPosItem(self, id): '''return index of start and end of the item's declaration Argument(s): id (str): ID of the item we want to find ''' index = 0 text = self.toPlainText() text = [e + '{' for e in text.split('{') if e != ""] index += len(text[0]) + 1 text.pop(0) text = ''.join(text) text = [e + '}' for e in text.split('}') if e != ""] text.pop(len(text) - 1) stats = re.split(';', ''.join(text)) # Use pydot to get all statements of the graph (in order) for s in stats: # Parse current statement pydotG = graph_from_dot_data("graph {" + s + "}") if pydotG: # Ignore subgraph s2 = s while (re.match("\s*(subgraph)*\s*.*\{", s2) or re.match("\s*\}.*", s2)): if re.match("\s*(subgraph)*\s*.*\{", s2): s2 = re.split('{', s2, 1)[1] pydotG = graph_from_dot_data("graph {" + s2 + "}") elif re.match("\s*\}.*", s2): s2 = re.split('}', s2, 1)[1] pydotG = graph_from_dot_data("graph {" + s2 + "}") for node in pydotG.get_nodes(): if node.get_name() == id: return ([index, index + len(s)]) for edge in pydotG.get_edges(): if EdgeUtils.createEdgeId(edge.get_source(), edge.get_destination()) == id: return ([index, index + len(s)]) index += len(s) + 1
def findPosItem(self, id): '''return index of start and end of the item's declaration Argument(s): id (str): ID of the item we want to find ''' index = 0 text = self.toPlainText() text = [e + '{' for e in text.split('{') if e != ""] index += len(text[0]) + 1 text.pop(0) text = ''.join(text) text = [e + '}' for e in text.split('}') if e != ""] text.pop(len(text) - 1) stats = re.split(';', ''.join(text)) # Use pydot to get all statements of the graph (in order) for s in stats: # Parse current statement pydotG = graph_from_dot_data("graph {" + s + "}") if pydotG: # Ignore subgraph s2 = s while (re.match("\s*(subgraph)*\s*.*\{", s2) or re.match("\s*\}.*", s2)): if re.match("\s*(subgraph)*\s*.*\{", s2): s2 = re.split('{', s2, 1)[1] pydotG = graph_from_dot_data("graph {" + s2 + "}") elif re.match("\s*\}.*", s2): s2 = re.split('}', s2, 1)[1] pydotG = graph_from_dot_data("graph {" + s2 + "}") for node in pydotG.get_nodes(): if node.get_name() == id: return([index, index + len(s)]) for edge in pydotG.get_edges(): if EdgeUtils.createEdgeId(edge.get_source(), edge.get_destination()) == id: return([index, index + len(s)]) index += len(s) + 1
def test_attribute_with_implicit_value(): dot = dedent( """ digraph { na -> b[label="hi", decorate]; } """ ) graph = pydot.graph_from_dot_data(dot) edge = graph.get_edges()[0] attrs = edge.get_attributes() assert attrs == {"decorate": None, "label": '"hi"'}
def _render_with_pydot(input_data, output_path): """Renders graph using pydot_ng library.""" import pydot_ng as pydot graph = pydot.graph_from_dot_data(input_data) if not graph: raise error.BadDataException( "Passed data does not contain graph in DOT format") try: graph.write_png(output_path) except pydot.InvocationException as e: raise error.WrongEnvironmentError( "There was an error with rendering graph:\n{0}".format(e))
def save_temp_results(t, dest, summaries, sub_vs_orig_DF, sub_vs_sub_DF): dest = dest + '/temp' if not os.path.exists(dest): os.makedirs(dest) try: graph = pydot.graph_from_dot_data(repr(t.tree)) graph.write_svg("{0}/CrossVal{1}_train.svg".format(dest, t.tree.treeNum)) graph = pydot.graph_from_dot_data(repr(t)) graph.write_svg("{0}/CrossVal{1}_test.svg".format(dest, t.tree.treeNum)) except: print("Unexpected error:", t.tree.treeNum, sys.exc_info()[0]) summaries = summaries.append(Summary(t, 1, t.tree.treeNum, "Root")) summaries = summaries.append(Summary(t.left, 2, t.tree.treeNum, "Left")) summaries = summaries.append(Summary(t.right, 2, t.tree.treeNum, "Right")) s_v_o, s_v_s = ptfd.patterns(t, 2) sub_vs_orig_DF = sub_vs_orig_DF.append(s_v_o) sub_vs_sub_DF = sub_vs_sub_DF.append(s_v_s) summaries.to_csv("./{0}/summary.csv".format(dest)) sub_vs_orig_DF.to_csv("./{0}/pattern_sub_orig.csv".format(dest)) sub_vs_sub_DF.to_csv("./{0}/pattern_sub_sub.csv".format(dest))
def DT_Classify(predictor_var, outcome_var, traindf, Ptest_X): # Decision Tree model = DecisionTreeClassifier(random_state=22) classification_model(model,traindf,predictor_var,outcome_var) dot_data = StringIO() features_mean=list(traindf.columns[0:3]) print features_mean export_graphviz(model, feature_names = features_mean, filled = True, rounded = True, out_file = dot_data) # classification_model(model,testdf,predictor_var,outcome_var) prediction = model.predict(Ptest_X) # featimp = pd.Series(model.feature_importances_, index=predictor_var).sort_values(ascending=False) # print(featimp) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("lndc.pdf") get_prediction(prediction)
def test_graph_with_shapefiles(self): shapefile_dir = os.path.join(TEST_DIR, 'from-past-to-future') dot_file = os.path.join(shapefile_dir, 'from-past-to-future.dot') pngs = [ os.path.join(shapefile_dir, fname) for fname in os.listdir(shapefile_dir) if fname.endswith('.png')] f = open(dot_file, 'rt') graph_data = f.read() f.close() g = pydot.graph_from_dot_data(graph_data) g.set_shape_files(pngs) jpe_data = g.create(format='jpe') hexdigest = sha256(jpe_data).hexdigest() hexdigest_original = self._render_with_graphviz(dot_file) self.assertEqual(hexdigest, hexdigest_original)
def test_graph_with_shapefiles(): shapefile_dir = os.path.join(TEST_DIR, "from-past-to-future") dot_file = os.path.join(shapefile_dir, "from-past-to-future.dot") pngs = [ os.path.join(shapefile_dir, fname) for fname in os.listdir(shapefile_dir) if fname.endswith(".png") ] with open(dot_file, "rt") as f: graph_data = f.read() # g = pydot.graph_from_dot_data(graph_data) g.set_shape_files(pngs) jpe_data = g.create(format="jpe") hexdigest = sha256(jpe_data).hexdigest() hexdigest_original = _render_with_graphviz(dot_file) assert hexdigest == hexdigest_original
def test_unicode_ids(self): node1 = '"aánñoöüé€"' node2 = '"îôø®çßΩ"' g = pydot.Dot() g.set_charset('latin1') g.add_node(pydot.Node(node1)) g.add_node(pydot.Node(node2)) g.add_edge(pydot.Edge(node1, node2)) self.assertEqual(g.get_node(node1)[0].get_name(), node1) self.assertEqual(g.get_node(node2)[0].get_name(), node2) self.assertEqual(g.get_edges()[0].get_source(), node1) self.assertEqual(g.get_edges()[0].get_destination(), node2) g2 = pydot.graph_from_dot_data(g.to_string()) self.assertEqual(g2.get_node(node1)[0].get_name(), node1) self.assertEqual(g2.get_node(node2)[0].get_name(), node2) self.assertEqual(g2.get_edges()[0].get_source(), node1) self.assertEqual(g2.get_edges()[0].get_destination(), node2)
def test_unicode_ids(): node1 = '"aánñoöüé€"' node2 = '"îôø®çßΩ"' graph = pydot.Dot() graph.set_charset("latin1") graph.add_node(pydot.Node(node1)) graph.add_node(pydot.Node(node2)) graph.add_edge(pydot.Edge(node1, node2)) assert graph.get_node(node1)[0].get_name() == node1 assert graph.get_node(node2)[0].get_name() == node2 assert graph.get_edges()[0].get_source() == node1 assert graph.get_edges()[0].get_destination() == node2 graph2 = pydot.graph_from_dot_data(graph.to_string()) assert graph2.get_node(node1)[0].get_name() == node1 assert graph2.get_node(node2)[0].get_name() == node2 assert graph2.get_edges()[0].get_source() == node1 assert graph2.get_edges()[0].get_destination() == node2
def output_pdf(model, X_nlp): #add graphviz to path os.environ['PATH'] = os.environ['PATH'] + ';' + os.environ[ 'CONDA_PREFIX'] + r"\Library\bin\graphviz" feature_names = X_nlp.columns class_name = dtc_nlp.classes_.astype(str) dot_data = StringIO() tree.export_graphviz( model, out_file=dot_data, feature_names=feature_names, class_names=class_name, filled=True, rounded=True, special_characters=True, node_ids=1, ) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("{}Decision_Tree.pdf".format(output_path)) print('Decision Tree diagram saved')
def MakeTree(self, crit, name, depth=None, leaf_samples=1, leaf_nodes=None): rf = tree.DecisionTreeClassifier(criterion=crit, max_depth=depth, min_samples_leaf=leaf_samples, max_leaf_nodes=leaf_nodes) rf.fit(self.train[:,2:-2], self.train[:,-2]) print("Test:", name) print("5-way cross-val scores:", cross_val_score( rf, self.data.iloc[:, 2:-2], self.data.iloc[:,-2], cv = 5 )) print("Training score:", rf.score( self.train[:,2:-2], self.train[:,-2] )) print("Testing score:", rf.score( self.test[:,2:-2], self.test[:,-2] )) dot_data = StringIO() tree.export_graphviz(rf, out_file=dot_data, feature_names=list(self.data)[2:-2], class_names=["Alive", "Deceased"], filled=True, rounded=True, special_characters=True) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf(name+".pdf") graph.write_png(name+".png") return
def draw_cg(model): ''' Draws the computational graph for model ''' graph = pd.graph_from_dot_data( c.build_computational_graph((model, )).dump()) return display(Image(graph.create_png()))
from sklearn.datasets import load_iris from sklearn import tree iris = load_iris() print iris.feature_names print iris.target_names type(iris.target) iris.target.shape iris.data.shape test_idx = [0, 50, 100] # training data train_target = np.delete(iris.target, test_idx) train_data = np.delete(iris.data, test_idx, axis=0) # testing data test_target = iris.target[test_idx] test_data = iris.data[test_idx] clf = tree.DecisionTreeClassifier() clf.fit(train_data, train_target) from sklearn.externals.six import StringIO import pydot_ng as pydot dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data, feature_names=iris.feature_names, class_names=iris.target_names, filled=True, rounded=True, special_characters=True) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("iris.pdf")
def focusOutEvent(self, event): '''Handle focus out event. Argument(s): event (QFocusEvent): Focus event ''' self.acceptUpdate = False # Create pydot graph from text pydotGraph = graph_from_dot_data(self.toPlainText()) # If the pydot graph is valid we can rewrite the text and check changes if pydotGraph: # If attributes are in valid form message = self.checkItemsAttributes(pydotGraph.get_nodes(), pydotGraph.get_edges()) if not message: oldNodes = self.nodes oldEdges = self.edges self.nodes = {} self.edges = {} self.rebuildTextModel(self.toPlainText(), pydotGraph) # Compare old and new text and send changes to the model # Add nodes added added = self.nodes.keys() - oldNodes.keys() for idNode in added: self.controller.onCreateNode(idNode, self.nodes[idNode]) # Edit nodes changed intersect = set(self.nodes.keys()).intersection( set(oldNodes.keys())) for idNode in intersect: if self.nodes[idNode] != oldNodes[idNode]: self.controller.onEditNode(idNode, self.nodes[idNode]) # Remove nodes deleted removed = oldNodes.keys() - self.nodes.keys() for idNode in removed: self.controller.onRemoveNode(idNode) # Delete edges which contain the node edgeToRemove = [] for edge in self.edges: if (idNode == self.edges[edge][EdgeArgs.sourceId] or idNode == self.edges[edge][EdgeArgs.destId]): edgeToRemove.append(edge) self.acceptUpdate = True for edge in edgeToRemove: self.removeEdge({ EdgeArgs.id: edge, EdgeArgs.sourceId: self.edges[edge][EdgeArgs.sourceId], EdgeArgs.destId: self.edges[edge][EdgeArgs.destId] }) self.acceptUpdate = False # Remove edges deleted removed = oldEdges.keys() - self.edges.keys() for idEdge in removed: self.controller.onRemoveEdge( oldEdges[idEdge][EdgeArgs.sourceId], oldEdges[idEdge][EdgeArgs.destId]) # Add edges added added = self.edges.keys() - oldEdges.keys() for idEdge in added: nodeSource = self.edges[idEdge][EdgeArgs.sourceId] nodeDest = self.edges[idEdge][EdgeArgs.destId] self.controller.onCreateEdge(nodeSource, nodeDest) QTextEdit.focusOutEvent(self, event) # Some attributes are in invalid form: show an error window else: QMessageBox.warning(self, "Syntax error", message) self.setFocus() # Pydot graph invalid: show an error window else: QMessageBox.warning(self, "Syntax error", "The dot structure is invalid.") self.setFocus() self.acceptUpdate = True
def plot_tree(DecisionTreeClassificationModel, featureNames=None, categoryNames=None, classNames=None, filled=True, roundedCorners=True, roundLeaves=True): """ Draws a Spark's fitted DecisionTreeClassificationModel in png format. If you are using Jupyter, this function can be easily used alongside Ipython.display in order to draw a tree in Jupyter: from spark_tree_plotting import plot_tree from IPython.display import Image Image(plot_tree(my_DecisionTreeClassificationModel)) Note that this function requires pydot3 to be installed. Arguments: DecisionTreeClassificationModel -- a pyspark.ml.classification.DecisionTreeClassificationModel instance featureNames -- a list with the feature names. This is probably the same list you usually pass to your VectorAssembler constructor categoryNames -- a dictionary with the featureNames that are categorical as the keys, and the different categories as the values. This is probably the featureNames as key, StringIndexerModel.labels attribute as value for each categorical feature classNames -- a list with the class names for your target column. This is probably the StringIndexerModel.labels for your target column filled -- boolean which indicates whether to fill nodes with colour or not. Color gamma will be the prediction class for each node, and color intensity the impurity at such node roundedCorners -- boolean which indicates whether to round rectangle corners for the nodes roundLeaves -- boolean which indicates whether to represent leaf nodes as ellipses rather than rectangles Returns: a PNG string, which can be displayed in Jupyter/any webpage and/or be saved to disk """ try: from pydot_ng import graph_from_dot_data except ImportError: raise ImportError( "This function requires pydot3 dot be installed. You can easily install it with pip install pydot3" ) graph = graph_from_dot_data( export_graphviz(DecisionTreeClassificationModel, featureNames=featureNames, categoryNames=categoryNames, classNames=classNames, filled=filled, roundedCorners=roundedCorners, roundLeaves=roundLeaves)) if type(graph) is list: plot = graph[0].create_png() else: plot = graph.create_png() return plot
def test_multiple_graphs(self): graph_data = 'graph A { a->b };\ngraph B {c->d}' graphs = pydot.graph_from_dot_data(graph_data) self.assertEqual(len(graphs), 2) self.assertEqual([g.get_name() for g in graphs], ['A', 'B'])
def test_attribute_with_implicit_value(self): d = 'digraph {\na -> b[label="hi", decorate];\n}' g = pydot.graph_from_dot_data(d) attrs = g.get_edges()[0].get_attributes() self.assertEqual('decorate' in attrs, True)
iris = load_iris() print iris.feature_names print iris.target_names type(iris.target) iris.target.shape iris.data.shape test_idx = [0, 50, 100] # training data train_target = np.delete(iris.target, test_idx) train_data = np.delete(iris.data, test_idx, axis=0) # testing data test_target = iris.target[test_idx] test_data = iris.data[test_idx] clf = tree.DecisionTreeClassifier() clf.fit(train_data, train_target) from sklearn.externals.six import StringIO import pydot_ng as pydot dot_data = StringIO() tree.export_graphviz(clf, out_file=dot_data, feature_names=iris.feature_names, class_names=iris.target_names, filled=True, rounded=True, special_characters=True) graph = pydot.graph_from_dot_data(dot_data.getvalue()) graph.write_pdf("iris.pdf")
def transform(dot_data, f): graph = pydot.graph_from_dot_data(dot_data) f(graph) return graph.to_string()