def __plot__(self, nodename: str, shape: str, rankdir=None): """ Acceptable to leave here :param rankdir: The visual direction of the DAG """ # Prep globals, passed through arguments self.xp_state.eg.serialize() self.xp_state.nodes = {} self.xp_state.edges = [] dot = Digraph() # diagram = {"dot": dot, "counter": 0, "sha": {}} if not util.isOrphan(self): # self.parent.__plotWalk__(diagram) vg = viz.VizGraph() self.parent.__plotWalk__(vg) # vg.bft() vg.to_graphViz() Source.from_file('output.gv').view() else: node_diagram_id = '0' dot.node(node_diagram_id, nodename, shape=shape) self.xp_state.nodes[nodename] = node_diagram_id dot.format = 'png' if rankdir == 'LR': dot.attr(rankdir='LR') dot.render('driver.gv', view=True) self.xp_state.eg.clean()
def export_matrix(self): G = Digraph(engine='neato', strict=False) G.graph_attr['splines'] = 'ortho' G.graph_attr['dpi'] = '300' elist = [] for i in self.sequence: a = (i[0], i[1]) elist.append(a) with G.subgraph(name='main') as d: d.edges(elist) d.node_attr['shape'] = 'box' d.node_attr['style'] = 'strocked' d.node_attr['color'] = 'red' d.attr(label='pyArchInit - Harris Matrix Export System') for i in self.periodi: with G.subgraph(name=i[1]) as c: # c.attr(bgcolor='lightgrey') for n in i[0]: c.attr('node', shape='square', label=str(n), color='blue') c.node(str(n)) c.attr(color='blue') c.attr(label=i[2]) matrix_path = '{}{}{}'.format(self.HOME, os.sep, "pyarchinit_Matrix_folder") filename = 'Harris_matrix' G.format = 'xdot' dot_file = G.render(directory=matrix_path, filename=filename) # For MS-Windows, we need to hide the console window. if Pyarchinit_OS_Utility.isWindows(): si = subprocess.STARTUPINFO() si.dwFlags |= subprocess.STARTF_USESHOWWINDOW si.wShowWindow = subprocess.SW_HIDE #cmd = ' '.join(['tred', dot_file]) #dotargs = shlex.split(cmd) with open(os.path.join(matrix_path, filename + '_tred.dot'), "w") as out, \ open(os.path.join(matrix_path, 'matrix_error.txt'), "w") as err: subprocess.Popen( ['tred', dot_file], #shell=True, stdout=out, stderr=err) #startupinfo=si if Pyarchinit_OS_Utility.isWindows()else None) tred_file = os.path.join(matrix_path, filename + '_tred.dot') g = Source.from_file(tred_file, format='svg') g.render() f = Source.from_file(tred_file, format='png') f.render() return g
def draw_transitions(self): G = nx.MultiDiGraph() edges = {} for t in self.transitions: edges[(t.origin.name, t.destination.name)] = t.probability states = [s.name for s in self.states] G.add_nodes_from(states) for k, v in edges.items(): origin, destination = k[0], k[1] G.add_edge(origin, destination, weight=v, label=f"{v:.2f}") pos = nx.drawing.nx_pydot.graphviz_layout(G, prog='dot') nx.draw_networkx(G, pos) nx.drawing.nx_pydot.write_dot(G, '../Chain.dot') Source.from_file('../Chain.dot').view()
def ExportFigs(self, data, feature_names, class_names, fig_name): export_graphviz(data, out_file=r"Figs/" + fig_name + ".dot", feature_names=feature_names, class_names=class_names, rounded=True, filled=True, leaves_parallel=True, node_ids=True, proportion=False, precision=2) Source.from_file(r"Figs/" + fig_name + ".dot").render(r"Figs/" + fig_name, view=True, cleanup=True)
def Forest(Xtrain, Ytrain, Xtest, Ytest): # Train rf = RandomForestClassifier(n_estimators=1000, oob_score=True, random_state=69) rf.fit(Xtrain, Ytrain) # Feature Importance rfFeature_importances = pd.DataFrame(rf.feature_importances_, index=dfXtrain.columns, columns=['importance']).sort_values( 'importance', ascending=False) # Test rfPredictions = rf.predict(Xtest) rfAccuracy = accuracy_score(Ytest, rfPredictions) print(f'Baggie score: {rf.oob_score_:.3}') print(f'Mean Accuracy: {rfAccuracy:.3}') #Visualize Tree tree = rf.estimators_[999] export_graphviz(tree, out_file='tree.dot', rounded=True, proportion=True, precision=2, filled=True) image = Source.from_file("/Users/antonis/PycharmProjects/OD14/tree.dot") image = image.view() return rf, rfFeature_importances, rfPredictions, rfAccuracy, print( f'Mean Accuracy: {rfAccuracy:.3}'), image
def show_powergrid_model(self): path_1 = "GLM/"+combo_smartgrid_model.get().replace(" ", "_")+'.glm' path_2 = "GLM/"+combo_smartgrid_model.get().replace(" ", "_") +'.dot' os.system('python3 ' + path + 'glmMap.py ' + path_1 + ' ' + path_2) #os.system('ruby glm2dot.rb ' + path_1 + ' ' + path_2+ " GridAttackAnalyer") s = Source.from_file(path_2) s.view()
def flor_plan(self, label): # Deprecated: keeping for backward compatibility pulls = self.__get_pulls__() with util.chinto(self.repo_path): for i, pull_d in enumerate(pulls): if label == pull_d['message'].split(':')[1]: util.__runProc__(['git', 'checkout', pull_d['commit']]) if not interactive: Source.from_file('output.gv').view() else: output_image = Source.from_file('output.gv') break util.__runProc__(['git', 'checkout', 'master']) return output_image
def readGraphFromGraphvizFromTrainData(fileName, vitualize=True): hornGraph = Source.from_file(fileName) #read gv to networkx #print(fileName) G = nx.DiGraph(nx.drawing.nx_pydot.read_dot(fileName)) #view by graphviz if (vitualize == True): hornGraph.view() print(hornGraph.source) ''' for node,content in nodes.items(): print("-----------") print(node,content['label']) print("Neighbors:") for neighbor in G.neighbors(node): print(neighbor,nodes[neighbor]['label']) print("Predecessors:") for predecessor in G.predecessors(node): print(predecessor,nodes[predecessor]['label']) print("Successors:") for successor in G.successors(node): print(successor, nodes[successor]['label']) ''' #G = nodeRelabel(G) #prerryPrintOneGraph(G) return G
def readGraphFromGraphviz(fileName, vitualize=True): path = '../graphs/' parentDirectory = os.path.abspath(os.path.dirname(os.getcwd())) hornGraph = Source.from_file(path + fileName) #read gv to networkx G = nx.DiGraph(nx.drawing.nx_pydot.read_dot(path + fileName)) #view by graphviz if (vitualize == True): hornGraph.view() print(hornGraph.source) nodes = G.nodes for node, content in nodes.items(): print("-----------") print(node, content['label']) print("Neighbors:") for neighbor in G.neighbors(node): print(neighbor, nodes[neighbor]['label']) print("Predecessors:") for predecessor in G.predecessors(node): print(predecessor, nodes[predecessor]['label']) print("Successors:") for successor in G.successors(node): print(successor, nodes[successor]['label']) G = nodeRelabel(G) #prerryPrintOneGraph(G) return G
def plot_graphclusters(concepttriplets, filename='unix.gv'): try: i = 1 allfiles = [] for cluster in concepttriplets: filename = filename.split(".")[0] + str(i) + '.gv' allfiles.append(filename) u = Digraph('unix', filename) u.attr(size='6,6') u.node_attr.update(color='lightblue2', style='filled') for triplet in cluster: firstedge = triplet[0]['text'] try: label = triplet[1]['text'] except: label = '' try: secondedge = triplet[2]['text'] except: secondedge = triplet[0]['text'] u.edge(firstedge, secondedge, label=label) i += 1 u.render(filename) u = None except: pass for file in allfiles: s = Source.from_file(file) s.view()
def createBlockchainGraph(self, outfilename): print("creating graph") self.blockchain.to_graphviz(filename=outfilename + '.gv', shape=u'box', graph=u'digraph') g = Source.from_file(outfilename + '.gv') g.render()
def write_file(filepath, method, clusters, targetMDG): # if file exists, remove and create new file. result_path = "test/result/" + filepath[0:-4] + "_" + method + "_result.gv" if os.path.exists(result_path): os.remove(result_path) f = open(result_path, "w") f.write('digraph "summary" {\n') for edge in targetMDG.edges: f.write(' ' + str(edge[0]) + ' -> ' + str(edge[1]) + ';\n') f.write("\n\n") cluster_num = len(clusters) for i in range(cluster_num): data = " subgraph cluster_" + str(i) + " {\n" f.write(data) for node in clusters[i].get_nodes(): data = " " + str(node) + "; " f.write(data) f.write("\n") f.write(" }\n") f.write("}") f.close() a = Source.from_file(result_path) a.render(result_path, view=True)
def readGraphsFromDot(): graphList = [] argumentList = [] path = "../../trainData/" suffix = ".c" # some file name include .horn print("graph file", len(sorted(glob.glob(path + '*' + suffix + '.gv')))) print("argument file", len(sorted(glob.glob(path + '*' + suffix + '.arguments')))) for fileGraph, fileArgument in zip( sorted(glob.glob(path + '*' + suffix + '.gv')), sorted(glob.glob(path + '*' + suffix + '.arguments'))): fileName = fileGraph[:fileGraph.find(suffix + ".gv") + len(suffix)] fileName = fileName[fileName.rindex("/") + 1:] print(fileName) # read graph print(fileGraph) hornGraph = Source.from_file(fileGraph) G = nx.DiGraph(nx.drawing.nx_pydot.read_dot(fileGraph)) graphList.append(G) #read argument print(fileArgument) f = open(fileArgument, "r") arguments = f.read() f.close() argumentList.append(arguments) return graphList, argumentList
def plot_transition_graph( data: Union[pd.DataFrame, traja.TrajaDataFrame, np.ndarray], outpath="markov.dot", interactive=True, ): """Plot transition graph with networkx. Args: data (trajectory or transition_matrix) .. note:: Modified from http://www.blackarbs.com/blog/introduction-hidden-markov-models-python-networkx-sklearn/2/9/2017 """ try: import networkx as nx import pydot import graphviz except ImportError as e: raise ImportError(f"{e} - please install it with pip") if (isinstance(data, (traja.TrajaDataFrame)) or isinstance(data, pd.DataFrame) and "x" in data): transition_matrix = traja.transitions(data) edges_wts = _get_markov_edges(pd.DataFrame(transition_matrix)) states_ = list(range(transition_matrix.shape[0])) # create graph object G = nx.MultiDiGraph() # nodes correspond to states G.add_nodes_from(states_) # edges represent transition probabilities for k, v in edges_wts.items(): tmp_origin, tmp_destination = k[0], k[1] G.add_edge(tmp_origin, tmp_destination, weight=v.round(4), label=v.round(4)) pos = nx.drawing.nx_pydot.graphviz_layout(G, prog="dot") nx.draw_networkx(G, pos) # create edge labels for jupyter plot but is not necessary edge_labels = {(n1, n2): d["label"] for n1, n2, d in G.edges(data=True)} nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels) if os.exists(outpath): logging.info(f"Overwriting {outpath}") nx.drawing.nx_pydot.write_dot(G, outpath) if interactive: # Plot from graphviz import Source s = Source.from_file(outpath) s.view()
def get_process_model(self, models_path, log_name, window, activity): map_file = self.model_type_definitions.get_model_filename(log_name, window) models_path = self.model_type_definitions.get_models_path(models_path, log_name, activity) if os.path.exists(os.path.join(models_path, map_file)): gviz = Source.from_file(filename=map_file, directory=models_path) return gviz.source return """
def read_and_render(filename): """ Read file and return source :param filename: Filename that wanted to read :return: Source of given file """ a = Source.from_file(filename) # print(a.source) # a.render(filename, view=True) return a
def show_model(): path_1 = database_path + combo_smartgrid_model.get().replace( " ", "_") + '/' + 'GridLab-D.glm' path_2 = database_path + combo_smartgrid_model.get().replace( " ", "_") + '/' + 'GridLab-D.dot' os.system('python glmMap.py ' + path_1 + ' ' + path_2) s = Source.from_file(path_2) s.view() print(os.getcwd())
def plot(self, rankdir=None): # Prep globals, passed through arguments self.xp_state.nodes = {} self.xp_state.edges = [] dot = Digraph() # diagram = {"dot": dot, "counter": 0, "sha": {}} if not util.isOrphan(self): # self.parent.__plotWalk__(diagram) vg = viz.VizGraph() self.parent.__plotWalk__(vg) # vg.bft() vg.to_graphViz() Source.from_file('output.gv').view() else: node_diagram_id = '0' dot.node(node_diagram_id, self.loc, shape="box") self.xp_state.nodes[self.loc] = node_diagram_id dot.format = 'png' if rankdir == 'LR': dot.attr(rankdir='LR') dot.render('driver.gv', view=True)
def add_style(filename: str) -> str: FONTNAME = "Roboto-Regular" FONTCOLOR = "#2F2F2F" SHAPECOLOR = "#2F2F2F" MARK = '"' style = fr"\1color={MARK}{SHAPECOLOR}{MARK}, fontname={MARK}{FONTNAME}{MARK}, fontcolor={MARK}{FONTCOLOR}{MARK}, " s = Source.from_file(filename).source s = re.sub(r"(\[)(?=label)", style, s) with open(filename, "w") as dot_file: dot_file.write(s) return "\n\N{sparkles} \N{memo} Done! \N{sparkles}\n"
def print_tree(rf, feature_list): # Pull out one tree from the forest tree = rf.estimators_[5] # Export the image to a dot file export_graphviz(tree, out_file='./reports/tree_deep3.dot', feature_names=feature_list, rounded=True, precision=1) # Use dot file to create a graph (graph, ) = pydot.graph_from_dot_file('./reports/tree_deep3.dot') # Write graph to a png file graph.write_png('./reports/tree_deep3.png') from graphviz import Source path = './reports/tree_deep3.dot' s = Source.from_file(path) s.view()
def show_config_requirements(experiment_type: str, format: str = 'text'): """ show experiment file requirements Arguments: - experiment_type : str - format : str """ if not format in ['text', 'figure']: raise RuntimeError( 'unsupported format %s, supported : `text`, `figure`' % (format)) config = __get_config(experiment_type) if format == 'text': return str(RenderTree(config)) else: filename = 'config_requirements.gv' from tempfile import NamedTemporaryFile try: from graphviz import Source except ImportError as e: raise ImportError( "unable to import graphviz, you can install it using `pip3 install graphviz`" ) with NamedTemporaryFile("wb", delete=False) as dotfile: dotfilename = dotfile.name def edgetypefunc(node, child): return '->' def nodeattrfunc(node): return 'label="%s", %s' % (node.name, "shape=box" if node.is_required() else "shape=oval") exporter = UniqueDotExporter(config, edgetypefunc=edgetypefunc, nodeattrfunc=nodeattrfunc) exporter.to_dotfile('test.dot') for line in exporter: dotfile.write(("%s\n" % line).encode("utf-8")) dotfile.flush() gv = Source.from_file(dotfilename) return gv
def visualize_MCTS(MCTS, fileName='MCTS.dot'): # each node it has information on robots' position # reward, n, and UCT mapping = {} for node in MCTS.digraph.nodes: state = MCTS.digraph.nodes[node]['state'].currNode reward = MCTS.digraph.nodes[node]['reward'] n = MCTS.digraph.nodes[node]['n'] uct = MCTS.digraph.nodes[node]['uct'] mapping[node] = '-Node:' + str(node) + \ ' r:' + str(reward) + \ ' n:' + str(n) + \ ' uct:' + str(uct) + '\n' + \ 's:' + str(state) G = copy.deepcopy(MCTS.digraph) G = nx.relabel_nodes(G, mapping) write_dot(G, fileName) s = Source.from_file(fileName) # s.view() return s
def visualize_tree(file_path, filename, data, tree_model): from sklearn.tree import export_graphviz file_path = file_path + filename + ".dot" with open(file_path, "w") as f: export_graphviz(tree_model, out_file=f, feature_names=data.feature_names[2:], class_names=data.target_names, rounded=True, filled=True) import os os.environ["PATH"] += os.pathsep + 'C:/Program Files/Graphviz 2.44.1/bin' from graphviz import Source output = Source.from_file(file_path, format="png") output.view()
def plot_pipeline(graph, dot_path=None): """ Dump the graph to a dot file and visualize it using Graphviz Args: graph: NetworkX graph instance dot_path: Path to .dot file location """ rm_path = False if dot_path is None: # crete temp dir to store the .dot file dot_path = tempfile.mkstemp() rm_path = True nx.drawing.nx_pydot.write_dot(graph, dot_path) s = Source.from_file(dot_path) s.view() if rm_path: os.remove(dot_path)
def nn_visualize(model,type="keras",title ="My neural network", verbose =False): """ nn_visualize is used to visualize neural networks in pictorial form(keras) or graphical form(graphviz) Parameters: model - reference of the model type - to return keras or graphviz visualization title - for graphviz visualization verbose(bool) - to display details of neural network architecture Return : Visualization object """ if type == "keras" or type[0]=="k": from keras.utils.vis_utils import plot_model obj = plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) return obj else: try: from ann_visualizer.visualize import ann_viz from graphviz import Source ann_viz(model,view=True, title=title) graph_source = Source.from_file("network.gv") if verbose: print(graph_source.source) return graph_source except: print("An error occur while using graphviz visualization. So keras visualization object is returned") from keras.utils.vis_utils import plot_model obj = plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) return obj
def visualize(file): dot.from_file(file).render(view=True, format="png")
tree_clf = DecisionTreeClassifier(max_depth=2) tree_clf.fit(X, y) """visualize using graphviz, need 1.pip install graphviz, 2.brew install graphviz""" from graphviz import Source from sklearn.tree import export_graphviz export_graphviz(tree_clf, out_file="iris_tree.dot", feature_names=iris.feature_names[2:], class_names=iris.target_names, rounded=True, filled=True ) Source.from_file("iris_tree.dot") tree_clf.predict_proba([[5, 1.5]]), tree_clf.predict([[5, 1.5]]) """criterion can switch from gini to entropy""" entropy_tree_clf = DecisionTreeClassifier(criterion="entropy", max_depth=3) """hyper-parameters for regularization""" regularized_tree_clf = DecisionTreeClassifier(max_depth=5, # maximum depth of that tree max_leaf_nodes=20, # maximum number of leaf nodes max_features=8, # maximum number of features when splitting each node min_samples_split=10, # min number of samples of a node before it can split min_samples_leaf=4, # min number of samples of a leaf node min_weight_fraction_leaf=0.01 # same as min_samples_leaf, but by weight frac )
def main(): s = Source.from_file(sys.argv[1]) s.view()
from graphviz import Digraph from graphviz import Source import os stream = os.popen('adr generate graph') graph_source = stream.read() source_file = './doc/architecture/ADR_Dependencies' with open(source_file, 'w') as graph_write: graph_write.writelines(graph_source) #dot = Digraph(comment='ADR Graph') #dot.source() s = Source.from_file(source_file) s.render(view=False, format='png')
def randomforest(model, featnames=None, num_trees=None, filepath='tree', export='png', resolution=100, figsize=(25, 25), verbose=3): """Plot tree based on a randomforest. Parameters ---------- model : model randomforest model. featnames : list, optional list of feature names. The default is None. num_trees : int, default 0 Specify the ordinal number of target tree filepath : str, optional filename to export. The default is 'tree'. export : list of str, optional Export type. The default is 'png'. Alternatives: 'pdf', 'png' resolution : int, optional resolution of the png file. The default is 100. figsize: tuple, default (25,25) Figure size, (height, width) verbose : int, optional Print progress to screen. The default is 3. 0: NONE, 1: ERROR, 2: WARNING, 3: INFO (default), 4: DEBUG, 5: TRACE Returns ------- ax : Figure axis Figure axis of the input model. """ ax = None dotfile = None pngfile = None if num_trees is None: num_trees = 0 # Check model _check_model(model, 'randomforest') # Set env _set_graphviz_path() if export is not None: dotfile = filepath + '.dot' pngfile = filepath + '.png' if featnames is None: featnames = np.arange(0, len(model.feature_importances_)).astype(str) # Get model parameters if ('gradientboosting' in str(model).lower()): estimator = model.estimators_[num_trees][0] else: if hasattr(model, 'estimators_'): estimator = model.estimators_[num_trees] else: estimator = model # Make dot file dot_data = export_graphviz( estimator, out_file=dotfile, feature_names=featnames, class_names=model.classes_.astype(str), rounded=True, proportion=False, precision=2, filled=True, ) # Save to pdf if export == 'pdf': s = Source.from_file(dotfile) s.view() # Save to png elif export == 'png': try: call([ 'dot', '-Tpng', dotfile, '-o', pngfile, '-Gdpi=' + str(resolution) ]) fig, ax = plt.subplots(1, 1, figsize=figsize) img = mpimg.imread(pngfile) plt.imshow(img) plt.axis('off') plt.show() except: if _get_platform() != "windows": print( '[treeplot] >Install graphviz first: <sudo apt install python-pydot python-pydot-ng graphviz>' ) else: graph = Source(dot_data) plt.show() return (ax)