def disassemble_sample_get_svg(sample_id, address): """ Gets SVG file data, with functions names. """ graph = disassemble_sample(sample_id, address) filename = Sample.query.get(sample_id).storage_file data = Source(graph, format='svg') out_file = filename + "_disass_" if address is not None: out_file += hex(address) out_file = data.render(out_file) beautify_svg(out_file) svg_data = open(out_file, 'rb').read() elements = re.findall("func_<!-- -->[0-9a-f]{3,}h", svg_data) for e in elements: et = e[13:-1] for i in Sample.query.get(sample_id).functions: if i.address == et: svg_data = svg_data.replace(e, i.name) elements = re.findall("loc_[0-9a-f]{3,}h", svg_data) for e in elements: et = e[4:-1] for i in Sample.query.get(sample_id).functions: if i.address == et: svg_data = svg_data.replace(e, i.name) return svg_data
def draw_graph(cwd): ui = GraphOptions() ui.edit() max_patchcount = 2 * ui.limit # changes = get_changes(cwd, None, ui.limit, author=Config.AUTHOR) from_upstream = get_changes_from_upstream(cwd, Config.UPSTREAM_REPO, None, max_patchcount) changes = get_changes(cwd, None, max_patchcount) # print('format picked: {}'.format(ui.format)) dotSrcLines = darcs_show(cwd, ui.limit, ui.filename).splitlines() def changeColor(i, color): dotSrcLines[i] = dotSrcLines[i][:-2] + 'color="{}"];'.format(color) for i, l in enumerate(dotSrcLines): m = PATCH_PATTERN.search(l) if not m: continue title = m.group(1).replace("\\n", " ").strip() print(title) def find_in(lst): try: return next(c for c in lst if title == c.title) except StopIteration: return None up = find_in(from_upstream) # type: Patch loc = find_in(changes) # type: Patch if up: if not loc: print("patch in upstream but not local?? {}".format(title)) if up.author == Config.AUTHOR: changeColor(i, ui.acolor) else: changeColor(i, ui.bcolor) elif loc: if loc.author == Config.AUTHOR: changeColor(i, ui.color) dotSrc = "\n".join(dotSrcLines) print(dotSrc) # for l in enumerate(dotSrc.splitlines()): # print("{}: {}".format(*l)) # dot = parse_dot_data(dotSrc) # print(dot) dot = Source(dotSrc) # # print(dotObj.source) # # dot.graph_attr['rankdir'] = 'LR' dot.format = ui.format dot.render(ui.filename, view=ui.open, cleanup=ui.cleanup)
def collate_xml(witness_a, witness_b): tokens_a = convert_xml_string_into_tokens(witness_a) tokens_b = convert_xml_string_into_tokens(witness_b) superwitness = align_tokens_and_return_superwitness(tokens_a, tokens_b) textgraph = convert_superwitness_to_textgraph(superwitness) dot_export = export_as_dot(textgraph, annotations=True) dot = Source(dot_export, format="svg") svg = dot.render() return display(SVG(svg))
def preview_dot(filename): from graphviz import Source with open(filename) as dot: src = Source(dot.read()) src.format = "png" outfile = src.render() with open(outfile, "rb") as content: data = content.read() return {"image/png": base64.b64encode(data).decode("ascii")}
def expression_graph_as_png(expr, output_file, view=True): """ Save a PNG of rendered graph (graphviz) of the symbolic expression. :param expr: sympy expression :param output_file: string with .png extension :param view: set to True if system default PNG viewer should pop up :return: None """ assert output_file.endswith('.png') graph = Source(dotprint(expr)) graph.format = 'png' graph.render(output_file.rpartition('.png')[0], view=view, cleanup=True)
def generate(engine, outformat, graphviz_code): src = Source(graphviz_code, engine=engine, format=outformat) app.logger.info("/".join(("request is ", engine, outformat, graphviz_code))) fb = io.BytesIO(src.pipe()) app.logger.debug(fb.getvalue()) app.logger.debug(outformat) return send_file(fb, mimetype=mimetypes.types_map['.'+outformat])
def csview(self, view=False): """View chemical shift values organized by amino acid residue. :param view: Open in default image viewer or save file in current working directory quietly. :type view: :py:obj:`True` or :py:obj:`False` :return: None :rtype: :py:obj:`None` """ for starfile in fileio.read_files(self.from_path): chains = starfile.chem_shifts_by_residue(amino_acids=self.amino_acids, atoms=self.atoms, amino_acids_and_atoms=self.amino_acids_and_atoms, nmrstar_version=self.nmrstar_version) for idx, chemshifts_dict in enumerate(chains): nodes = [] edges = [] for seq_id in chemshifts_dict: aaname = "{}_{}".format(chemshifts_dict[seq_id]["AA3Code"], seq_id) label = '"{{{}|{}}}"'.format(seq_id, chemshifts_dict[seq_id]["AA3Code"]) color = 8 aanode_entry = " {} [label={}, fillcolor={}]".format(aaname, label, color) nodes.append(aanode_entry) currnodename = aaname for atom_type in chemshifts_dict[seq_id]: if atom_type in ["AA3Code", "Seq_ID"]: continue else: atname = "{}_{}".format(aaname, atom_type) label = '"{{{}|{}}}"'.format(atom_type, chemshifts_dict[seq_id][atom_type]) if atom_type.startswith("H"): color = 4 elif atom_type.startswith("C"): color = 6 elif atom_type.startswith("N"): color = 10 else: color = 8 atnode_entry = "{} [label={}, fillcolor={}]".format(atname, label, color) nextnodename = atname nodes.append(atnode_entry) edges.append("{} -> {}".format(currnodename, nextnodename)) currnodename = nextnodename if self.filename is None: filename = "{}_{}".format(starfile.id, idx) else: filename = "{}_{}".format(self.filename, idx) src = Source(self.dot_template.format("\n".join(nodes), "\n".join(edges)), format=self.csview_format) src.render(filename=filename, view=view)
def collate_xml_svg(limit=1000): # convert XML files into tokens tokens1 = convert_xml_file_into_tokens("xml_source_transcriptions/ts-fol-test-small.xml") tokens2 = convert_xml_file_into_tokens("xml_source_transcriptions/tsq-test-small.xml") superwitness = align_tokens_and_return_superwitness(tokens1, tokens2) print(superwitness[0:20]) textgraph = convert_superwitness_to_textgraph(superwitness) dot_export = export_as_dot(textgraph, annotations=True, limit=limit) # print(dot_export) # render dot_export as SVG dot = Source(dot_export, format="svg") svg = dot.render() return display(SVG(svg))
def main(): """Main function.""" args = get_args() # Read the whole output file with open(args.heinz) as r: graph_dot = r.readlines() # Remove the redundant lines while not graph_dot[0].startswith('graph G {'): graph_dot.pop(0) src = Source(''.join(graph_dot)) data_pdf = src.pipe('pdf') # Redirect the output (very important) with open(args.output, 'wb') as w: w.write(data_pdf) print('The visualization is saved as PDF!') sys.exit(0)
def printChapterGraph(self,tlist,chapter): graphstr = "digraph { \n" graphstr += "bgcolor=transparent \n" graphstr += "node [style=rounded shape=box]\n" # Draw all nodes for inode in tlist : label = self.labels[self.nodes.index(inode)] graphstr += inode + ' [label=<' + label + '> URL="' + inode + '.html" target="_top"];\n' # Add the connections for inode in tlist : iind = self.nodes.index(inode) for jnode in tlist : jind = self.nodes.index(jnode) if self.graph[iind, jind] == 1 : graphstr += self.nodes[iind] + " -> " + self.nodes[jind] + '\n' # And finish printing the dot file graphstr += "}" src = Source( graphstr, format='svg' ) src.render("html/" + chapter) # Delete the dot file os.remove("html/" + chapter)
def printFullGraph(self,filename): if self.set_conn == 0 : sys.error("graph must be set before print") graphstr = "digraph { \n" graphstr += "bgcolor=transparent \n" graphstr += "node [style=rounded shape=box]\n" for name in self.nodes : label = self.labels[ self.nodes.index(name) ] # Label here should be label from node file graphstr += name + ' [label=<' + label + '> URL="' + name + '.html" target="_top"];\n' # And the connections for i in range(0,len(self.nodes)): for j in range(0,len(self.nodes)): if self.graph[i,j] == 1 : graphstr += self.nodes[i] + " -> " + self.nodes[j] + '\n' # And finish printing the dot file graphstr += "}" src = Source( graphstr, format='svg' ) src.render("html/" + filename) # Delete the dot file os.remove("html/" + filename)
def printNodeGraph(self,node): label = self.labels[self.nodes.index(node)] graphstr = "digraph { \n" graphstr += "bgcolor=transparent \n" graphstr += "node [style=rounded shape=box]\n" # Write this node graphstr += node + ' [style="bold,rounded" color=blue label=<' + label + '> URL="' + node + '.html" target="_top"];\n' # Write nodes with forward and backwards connections i = self.nodes.index(node) forwardnodes = [] backnodes = [] for j in range(0,len(self.nodes)): if self.graph[ i, j ] == 1 : graphstr += self.nodes[j] + ' [label=<' + self.labels[j] + '> URL="' + self.nodes[j] + '.html" target="_top"];\n' graphstr += "edge[style=solid]; \n" graphstr += node + " -> " + self.nodes[j] + '\n' if (len(forwardnodes)>0) & (len(forwardnodes)%2==0) : graphstr += "edge[style=invis];\n" graphstr += forwardnodes[len(forwardnodes)-2] + " -> " + self.nodes[j] + '\n' forwardnodes.append(self.nodes[j]) if self.graph[ j, i ] == 1 : graphstr += self.nodes[j] + ' [label=<' + self.labels[j] + '> URL="' + self.nodes[j] + '.html" target="_top"];\n' graphstr += "edge[style=solid]; \n" graphstr += self.nodes[j] + " -> " + node + '\n' if (len(backnodes)>0) & (len(backnodes)%2==0) : graphstr += "edge[style=invis];\n" graphstr += backnodes[len(backnodes)-2] + " -> " + self.nodes[j] + '\n' backnodes.append(self.nodes[j]) for i in range(0,math.floor(len(forwardnodes)/2)+1) : if len(forwardnodes)>2*i+2 : graphstr += "{ rank=same; " + forwardnodes[2*i] + ";" + forwardnodes[2*i+1] + "; } \n" for i in range(0,math.floor(len(backnodes)/2)+1) : if len(backnodes)>2*i+2 : graphstr += "{ rank=same; " + backnodes[2*i] + ";" + backnodes[2*i+1] + "; } \n" graphstr += "}" src = Source( graphstr, format='svg' ) src.render("html/" + node) # Delete the dot file os.remove("html/" + node)
def main(): """ Draws the topology of the SCION network in a gen folder. example: python scion_topology_graph -g "gen", -e, -n: will place a pdf file of the scion topology with edge and node labels into output/scion_topo.gv -g: path to the gen folder ex: SCION/gen -n: set this flag if address/port information should be drawn -l: set this flag if location labels should be drawn -o: path to the output file ex: output/scion_topo.gv """ parser = argparse.ArgumentParser() parser.add_argument('-gp', '--gen_folder_path', default="gen", help='path to the gen folder') parser.add_argument('-lp', '--label_file_path', default="", help='path to the gen folder') parser.add_argument('-n', '--node_labels', action='store_true', default=False, help='set this flag to add address/port information') parser.add_argument('-l', '--location_labels', action='store_true', default=False, help='set this flag if add location labels') parser.add_argument('-o', '--output_path', default="output/scion_topo.gv", help='path to the output topology file') args = parser.parse_args() if os.path.exists(args.gen_folder_path): topo = parse_gen_folder(args.gen_folder_path, args.output_path) else: print('Error: No gen folder found at ' + args.gen_folder_path) return if args.location_labels: labels = parse_desc_labels(args.label_file_path) else: labels = {} dot = draw_SCION_topology(topo, args.node_labels, args.location_labels, labels) s = Source(dot, filename=dot.filename, format="pdf") s.render(directory=args.output_path)
def printGraphWithPath(self,topicpath,modulename): graphstr = "digraph { \n" graphstr += "bgcolor=transparent \n" graphstr += "node [style=rounded shape=box]\n" for node_name in self.nodes : node_label = self.labels[self.nodes.index(node_name)] if node_name in topicpath : graphstr += node_name + ' [label=<' + node_label + '> URL="' + node_name + '.html" target="_top" penwidth=3 color=blue];\n' else : graphstr += node_name + ' [label=<' + node_label + '> URL="' + node_name + '.html" target="_top"];\n' # And the connections for j in range(0,len(self.nodes)): for k in range(0,len(self.nodes)): if self.graph[j,k] == 1 : if (self.nodes[j] in topicpath) & (self.nodes[k] in topicpath) : graphstr += self.nodes[j] + " -> " + self.nodes[k] + ' [penwidth=3 color=blue];\n' else : graphstr += self.nodes[j] + " -> " + self.nodes[k] + '\n' graphstr += "}" src = Source( graphstr, format='svg' ) src.render("html/" + modulename) # Delete the dot file os.remove("html/" + modulename)
boost.fit(train_features, train_labels) # boost.fit(train_features, np.ones(train_features.shape[0])) predictions = boost.predict(test_features) # errors = abs(predictions - test_labels) # mape = 100 * (errors / test_labels) # accuracy = 100 - np.mean(mape) # print('Accuracy:', round(accuracy, 2), '%.') tree = boost.estimators_[0, 0] # print(tree.tree_.value.shape) export_graphviz(tree, out_file='boost_tree.dot', feature_names=feature_list) (graph, ) = pydot.graph_from_dot_file('boost_tree.dot') s = Source.from_file('boost_tree.dot') s.view() importances = list(boost.feature_importances_) feature_importances = [ (feature, round(importance, 2)) for feature, importance in zip(feature_list, importances) ] feature_importances = sorted(feature_importances, key=lambda x: x[1], reverse=True) for pair in feature_importances: print('Variable: {:20} Importance: {}'.format(*pair))
plt.yticks(y_position , wqp_dt_feature_names) plt.xlabel('Relative Importance Score') plt.ylabel('Feature') t = plt.title('Feature Importances for Decision Tree') # ## Visualize the Decision Tree # In[21]: from graphviz import Source from sklearn import tree from IPython.display import Image graph = Source(tree.export_graphviz(wqp_dt, out_file=None, class_names=wqp_label_names, filled=True, rounded=True, special_characters=False, feature_names=wqp_feature_names, max_depth=3)) png_data = graph.pipe(format='png') with open('dtree_structure.png','wb') as f: f.write(png_data) Image(png_data) # ## Train, Predict & Evaluate Model using Random Forests # In[22]: from sklearn.ensemble import RandomForestClassifier # train the model wqp_rf = RandomForestClassifier()
# print("Example %d: label %s features %s" % (i, IRIS.target[i], IRIS.data[i])) # Build the classifier test_cohort = [0, 50, 100] train_target = numpy.delete(IRIS.target, test_cohort) train_data = numpy.delete(IRIS.data, test_cohort, axis=0) test_target = IRIS.target[test_cohort] test_data = IRIS.data[test_cohort] clf = tree.DecisionTreeClassifier().fit(train_data, train_target) # Print the expected labels and the predicted labels print(test_target) print(clf.predict(test_data)) # Generate a dot file and PDF 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) src = Source(dot_data.getvalue()) src.render(filename='2-tree_viz.gv') # Print a sample data point print(test_data[0], test_target[0]) # Print the structure of the data print(IRIS.feature_names, IRIS.target_names)
def __init__(self, m_desc, FuseEdges=False, max_stack=30, max_width=9.0, accept_color='chartreuse3', reject_color='red', neutral_color='dodgerblue2'): # Options self.color_accept = accept_color self.color_reject = reject_color self.color_neutral = neutral_color self.max_width = max_width self.fuse = FuseEdges # PDA specific options self.condition = 'ACCEPT_F' self.stack_size = 6 # initialize self.valid_input = True self.machine = m_desc self.machine_obj = dotObj_pda(self.machine, FuseEdges=FuseEdges) self.copy_source = reformat_edge_labels( set_graph_size(self.machine_obj.source, max_width)) # Set things we need for the animation self.machine_steps = [] self.feed_steps = [] self.stack_steps = [] self.from_nodes = self.machine['q0'] self.to_nodes = self.machine['q0'] self.animated = False self.is_back_step = False # Setup the widgets # Top row for user input self.user_input = widgets.Text(value='', placeholder='Sigma: {{{}}}'.format( ','.join( sorted(self.machine['Sigma']))), description='Input:', layout=Layout(width='500px')) self.user_input.observe(self.on_input_change, names='value') self.generate_button = widgets.Button(description="Animate", button_style='primary', disabled=False) self.generate_button.on_click(self.generate_animation) self.acceptance_toggle = widgets.Dropdown(options=[ ('State', 'ACCEPT_F'), ('Stack', 'ACCEPT_S') ], description='Acceptance:', disabled=False, layout=Layout(width='160px')) # Bottom row for player controls self.play_controls = widgets.Play(interval=950, value=0, min=0, max=100, step=1, description="Press play", disabled=True) self.play_controls.observe(self.on_play_step, names='value') self.speed_control = widgets.IntSlider(value=1, min=1, max=10, step=1, description='Speed:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d') self.speed_control.observe(self.on_speed_change, names='value') # Create the controls for stepping through the animation self.backward = widgets.Button(icon='step-backward', layout=Layout(width='40px'), disabled=True) self.forward = widgets.Button(icon='step-forward', layout=Layout(width='40px'), disabled=True) self.backward.on_click(self.on_backward_click) self.forward.on_click(self.on_forward_click) # set the widget to display the machine self.machine_display = widgets.Output() with self.machine_display: display(Source(self.copy_source)) # set a widget to display rejected output self.rejection_display = widgets.Output() self.rejection_text = widgets.HTML(value="") self.reject_msg_start = '<p style="color:{}; text-align:center"><b>\'<span style="font-family:monospace">'.format( self.color_reject) self.reject_msg_end = '</span>\' was REJECTED</b></br>(Try running with a larger stack size or changing acceptance)</p>' # set the widget to display the stack self.stack_display = widgets.Output() s_state = self.set_stack_display() with self.stack_display: display(Source(s_state)) self.stack_size_slider = widgets.IntSlider(value=self.stack_size, min=2, max=max_stack, step=1, description='Stack Size:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d') self.stack_size_slider.observe(self.on_stack_size_change, names='value') # set the widget to display the feed self.feed_display = widgets.Output() f_state, inspecting = self.generate_feed('', 0, 0, []) with self.feed_display: display(Source(f_state)) self.path_dropdown = widgets.Dropdown(options={}, value=None, description='Path:', disabled=True, layout=Layout(width='200px')) self.path_dropdown.observe(self.on_path_change, names='value') # TODO: REMOVE TESTING CODE self.test_output = widgets.Output() # arrange the widgets in the display area row1 = widgets.HBox( [self.user_input, self.acceptance_toggle, self.generate_button]) row2 = widgets.HBox([self.stack_size_slider]) ms_disp = widgets.HBox([self.stack_display, self.machine_display]) play_row = widgets.HBox([ self.path_dropdown, self.play_controls, self.backward, self.forward, self.speed_control ]) w = widgets.VBox([ row1, row2, self.rejection_display, ms_disp, self.feed_display, play_row, self.test_output ]) display(w) self.play_controls.disabled = True self.forward.disabled = True self.backward.disabled = True self.speed_control.disabled = True
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=5, shuffle=True) # Training # 1. Decision Tree clf = DecisionTreeClassifier() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print('Decision Tree Accuracy Score: {}'.format(accuracy_score(y_test, y_pred))) # Visualization graph = Source(export_graphviz(clf, out_file=None, feature_names=features)) png_bytes = graph.pipe(format='png') with open('dt.dot', 'wb') as f: f.write(png_bytes) Image(png_bytes) # 2. K-Nearest Neighbor clf = KNeighborsClassifier() clf.fit(X_train, y_train) y_pred = clf.predict(X_test) print('K-Nearest Neighbor Accuracy Score: {}'.format( accuracy_score(y_test, y_pred))) # 3. Neural Net clf = MLPClassifier(max_iter=2000) clf.fit(X_train, y_train)
def compilar_AST_png(self): img = Source.from_file("ast_reporte.dot", format="png") img.render() entrada = self.popup_reporte_png(self.ventana, "ast_reporte.dot.png")
def getSurrogateVisualization(self): self.certainty = "You can see how the tree performed on sample data when looking at the square brackets of each node." if not os.path.exists("temp/head.jpg"): if self.surrogate_dt is None: self.buildSurrogateDecisiontree() dot_data = export_graphviz( self.surrogate_dt, out_file=None, feature_names=self.featureNames, impurity=False, label='none', filled=True, rotate=True, class_names=[ self.dictionary["class"]["values"][0], self.dictionary["class"]["values"][1] ]) def categorize_dot(dot): new_dot = dot feature_list = self.getCategoricalFeatures() print(new_dot) for fe in feature_list: m = re.findall('label="' + fe + '(.+?)\\\\n', new_dot) print(m) if m != []: for k in m: if '<=' in m[0]: number = float(re.findall("\\d+\\.\\d+", k)[0]) instances = dict( filter( lambda elem: elem[0] <= number, self.dictionary[fe]["values"].items())) print(number) print(instances) else: number = float(re.findall("\\d+\\.\\d+", k)[0]) instances = dict( filter( lambda elem: elem[0] > number, self.dictionary[fe]["values"].items())) print(number) print(instances) print(list(instances.values())) new_dot = new_dot.replace( fe + k, fe + " in " + str(list(instances.values()))) # replace fe+k by fe+instances.values() return (new_dot) c = categorize_dot(dot_data) path = "temp/tree.png" s = Source(c, filename="temp/tree", format="png") s.render() #p.write_png("temp/tree.png") return (path) else: return (path)
def apply(log, aligned_traces, parameters=None): """ Gets the alignment table visualization from the alignments output Parameters ------------- log Event log aligned_traces Aligned traces parameters Parameters of the algorithm Returns ------------- gviz Graphviz object """ if parameters is None: parameters = {} variants_idx_dict = variants_get.get_variants_from_log_trace_idx( log, parameters=parameters) variants_idx_list = [] for variant in variants_idx_dict: variants_idx_list.append((variant, variants_idx_dict[variant])) variants_idx_list = sorted(variants_idx_list, key=lambda x: len(x[1]), reverse=True) image_format = parameters["format"] if "format" in parameters else "png" table_alignments_list = [ "digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n" ] table_alignments_list.append( "<table border='0' cellborder='1' color='blue' cellspacing='0'>\n") table_alignments_list.append( "<tr><td>Variant</td><td>Alignment</td></tr>\n") for index, variant in enumerate(variants_idx_list): al_tr = aligned_traces[variant[1][0]] table_alignments_list.append("<tr>") table_alignments_list.append("<td><font point-size='9'>Variant " + str(index + 1) + " (" + str(len(variant[1])) + " occurrences)</font></td>") table_alignments_list.append( "<td><font point-size='6'><table border='0'><tr>") for move in al_tr['alignment']: move_descr = str(move[1]).replace(">", ">") if not move[0][0] == ">>" or move[0][1] == ">>": table_alignments_list.append("<td bgcolor=\"green\">" + move_descr + "</td>") elif move[0][1] == ">>": table_alignments_list.append("<td bgcolor=\"violet\">" + move_descr + "</td>") elif move[0][0] == ">>": table_alignments_list.append("<td bgcolor=\"gray\">" + move_descr + "</td>") table_alignments_list.append("</tr></table></font></td>") table_alignments_list.append("</tr>") table_alignments_list.append("</table>\n") table_alignments_list.append(">];\n") table_alignments_list.append("}\n") table_alignments = "".join(table_alignments_list) filename = tempfile.NamedTemporaryFile(suffix='.gv') gviz = Source(table_alignments, filename=filename.name) gviz.format = image_format return gviz
print(classification_report(y_test, y_pred)) #%%% y_test.shape, y_pred.shape y_test.head() y_pred[0:6] #%%% from graphviz import Source from sklearn import tree from IPython.display import SVG #libraries & path of graphviz import os os.environ["PATH"] += os.pathsep + 'C:\Program Files\Graphviz 2.44.1\bin' #%% graph1 = Source(tree.export_graphviz(clf, out_file=None, class_names= ['0', '1'] , filled = True)) display(SVG(graph1.pipe(format='svg'))) #change labels names graph2 = Source( tree.export_graphviz(clf, out_file=None, feature_names=X.columns, filled=True, class_names=['NoDiabetis','Diabetis'])) graph2 #change max_depth : 1 to 4 Source(tree.export_graphviz(clf, out_file=None, max_depth=1, feature_names=X.columns, class_names=['NonDB','DB'], label='all', filled=True, leaves_parallel=True, impurity=True, node_ids=True, proportion=True, rotate=True, rounded=True, special_characters=False, precision=1)) #https://stackoverflow.com/questions/27817994/visualizing-decision-tree-in-scikit-learn # This is for saving image in file system #https://scikit-learn.org/stable/modules/generated/sklearn.tree.export_graphviz.html #visualise using dotfile #True should be returned. goto location and see the file #%%% Create Decision Tree classifer object
def export_matrix(self): G = Digraph(engine='dot', strict=False) G.attr(rankdir='TB') G.attr(compound='true') G.graph_attr['pad'] = "0.5" G.graph_attr['nodesep'] = "1" G.graph_attr['ranksep'] = "1.5" G.graph_attr['splines'] = 'ortho' G.graph_attr['dpi'] = str(self.dialog.lineEdit_dpi.text()) elist1 = [] elist2 = [] elist3 = [] elist4 = [] elist5 = [] if bool(self.dialog.checkBox_period.isChecked()): for aa in self.periodi: with G.subgraph(name=aa[1]) as c: for n in aa[0]: c.attr('node', shape='record', label=str(n)) c.node(str(n)) c.attr(color='blue') c.attr('node', shape='record', fillcolor='white', style='filled', gradientangle='90', label=aa[2]) c.node(aa[2]) for bb in self.sequence: a = (bb[0], bb[1]) elist1.append(a) with G.subgraph(name='main') as e: e.attr(rankdir='TB') e.edges(elist1) e.node_attr['shape'] = str(self.dialog.combo_box_3.currentText()) e.node_attr['style'] = str(self.dialog.combo_box_4.currentText()) e.node_attr.update(style='filled', fillcolor=str( self.dialog.combo_box.currentText())) e.node_attr['color'] = 'black' e.node_attr['penwidth'] = str( self.dialog.combo_box_5.currentText()) e.edge_attr['penwidth'] = str( self.dialog.combo_box_5.currentText()) e.edge_attr['style'] = str(self.dialog.combo_box_10.currentText()) e.edge_attr.update( arrowhead=str(self.dialog.combo_box_11.currentText()), arrowsize=str(self.dialog.combo_box_12.currentText())) for cc in self.conteporene: a = (cc[0], cc[1]) elist3.append(a) with G.subgraph(name='main1') as b: b.edges(elist3) b.node_attr['shape'] = str( self.dialog.combo_box_18.currentText()) b.node_attr['style'] = str( self.dialog.combo_box_22.currentText()) b.node_attr.update(style='filled', fillcolor=str( self.dialog.combo_box_17.currentText())) b.node_attr['color'] = 'black' b.node_attr['penwidth'] = str( self.dialog.combo_box_19.currentText()) b.edge_attr['penwidth'] = str( self.dialog.combo_box_19.currentText()) b.edge_attr['style'] = str( self.dialog.combo_box_23.currentText()) b.edge_attr.update( arrowhead=str(self.dialog.combo_box_21.currentText()), arrowsize=str(self.dialog.combo_box_24.currentText())) for dd in self.negative: a = (dd[0], dd[1]) elist2.append(a) with G.subgraph(name='main2') as a: #a.attr(rank='same') a.edges(elist2) a.node_attr['shape'] = str( self.dialog.combo_box_6.currentText()) a.node_attr['style'] = str( self.dialog.combo_box_8.currentText()) a.node_attr.update(style='filled', fillcolor=str( self.dialog.combo_box_2.currentText())) a.node_attr['color'] = 'black' a.node_attr['penwidth'] = str( self.dialog.combo_box_7.currentText()) a.edge_attr['penwidth'] = str( self.dialog.combo_box_7.currentText()) a.edge_attr['style'] = str( self.dialog.combo_box_15.currentText()) a.edge_attr.update( arrowhead=str(self.dialog.combo_box_14.currentText()), arrowsize=str(self.dialog.combo_box_16.currentText())) for ee in self.connection: a = (ee[0], ee[1]) elist4.append(a) with G.subgraph(name='main3') as tr: #a.attr(rank='same') a.edges(elist4) a.node_attr['shape'] = str( self.dialog.combo_box_3.currentText()) a.node_attr['style'] = str( self.dialog.combo_box_4.currentText()) a.node_attr.update(style='filled', fillcolor=str( self.dialog.combo_box_2.currentText())) a.node_attr['color'] = 'black' a.node_attr['penwidth'] = str( self.dialog.combo_box_5.currentText()) a.edge_attr['penwidth'] = str( self.dialog.combo_box_5.currentText()) a.edge_attr['style'] = str( self.dialog.combo_box_10.currentText()) a.edge_attr.update( arrowhead=str(self.dialog.combo_box_11.currentText()), arrowsize=str(self.dialog.combo_box_16.currentText())) for ff in self.connection_to: a = (ff[0], ff[1]) elist5.append(a) with G.subgraph(name='main4') as tb: #a.attr(rank='same') a.edges(elist5) a.node_attr['shape'] = str( self.dialog.combo_box_6.currentText()) a.node_attr['style'] = str( self.dialog.combo_box_8.currentText()) a.node_attr.update(style='filled', fillcolor=str( self.dialog.combo_box_2.currentText())) a.node_attr['color'] = 'black' a.node_attr['penwidth'] = str( self.dialog.combo_box_7.currentText()) a.edge_attr['penwidth'] = str( self.dialog.combo_box_7.currentText()) a.edge_attr['style'] = 'dashed' a.edge_attr.update( arrowhead=str(self.dialog.combo_box_14.currentText()), arrowsize=str(self.dialog.combo_box_16.currentText())) if bool(self.dialog.checkBox_legend.isChecked()): with G.subgraph(name='cluster3') as j: j.attr(rank='max') j.attr(fillcolor='white', label='Legend', fontcolor='black', fontsize='16', style='filled') with G.subgraph(name='cluster3') as i: i.attr(rank='max') if self.L == 'it': i.node( 'a0', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style='filled', gradientangle='90', label='Ante/Post') i.edge( 'a0', 'a1', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style=str(self.dialog.combo_box_10.currentText()), arrowhead=str( self.dialog.combo_box_11.currentText()), arrowsize=str( self.dialog.combo_box_12.currentText())) i.node('a1', shape=str( self.dialog.combo_box_6.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style='filled', gradientangle='90', label='Negative') i.edge( 'a1', 'a2', shape=str(self.dialog.combo_box_8.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style=str(self.dialog.combo_box_15.currentText()), arrowhead=str( self.dialog.combo_box_14.currentText()), arrowsize=str( self.dialog.combo_box_16.currentText())) i.node('a2', shape=str( self.dialog.combo_box_18.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1', label='Cont.') # i.node('node3', shape=str(self.dialog.combo_box_18.currentText()), fillcolor=str(self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1') i.edge( 'a2', 'a1', shape=str(self.dialog.combo_box_22.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style=str(self.dialog.combo_box_23.currentText()), arrowhead=str( self.dialog.combo_box_21.currentText()), arrowsize=str( self.dialog.combo_box_24.currentText())) elif self.L == 'de': i.node( 'a0', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style='filled', gradientangle='90', label='Ante/Post') i.edge( 'a0', 'a1', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style=str(self.dialog.combo_box_10.currentText()), arrowhead=str( self.dialog.combo_box_11.currentText()), arrowsize=str( self.dialog.combo_box_12.currentText())) i.node('a1', shape=str( self.dialog.combo_box_6.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style='filled', gradientangle='90', label='Negativ') i.edge( 'a1', 'a2', shape=str(self.dialog.combo_box_8.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style=str(self.dialog.combo_box_15.currentText()), arrowhead=str( self.dialog.combo_box_14.currentText()), arrowsize=str( self.dialog.combo_box_16.currentText())) i.node('a2', shape=str( self.dialog.combo_box_18.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1', label='Wie') #i.node('node3', shape=str(self.dialog.combo_box_18.currentText()), fillcolor=str(self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1') i.edge( 'a2', 'a1', shape=str(self.dialog.combo_box_22.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style=str(self.dialog.combo_box_23.currentText()), arrowhead=str( self.dialog.combo_box_21.currentText()), arrowsize=str( self.dialog.combo_box_24.currentText())) else: i.node( 'a0', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style='filled', gradientangle='90', label='Ante/Post') i.edge( 'a0', 'a1', shape=str(self.dialog.combo_box_3.currentText()), fillcolor=str(self.dialog.combo_box.currentText()), style=str(self.dialog.combo_box_10.currentText()), arrowhead=str( self.dialog.combo_box_11.currentText()), arrowsize=str( self.dialog.combo_box_12.currentText())) i.node('a1', shape=str( self.dialog.combo_box_6.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style='filled', gradientangle='90', label='Negative') i.edge( 'a1', 'a2', shape=str(self.dialog.combo_box_8.currentText()), fillcolor=str( self.dialog.combo_box_2.currentText()), style=str(self.dialog.combo_box_15.currentText()), arrowhead=str( self.dialog.combo_box_14.currentText()), arrowsize=str( self.dialog.combo_box_16.currentText())) i.node('a2', shape=str( self.dialog.combo_box_18.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1', label='Same') #i.node('node3', shape=str(self.dialog.combo_box_18.currentText()), fillcolor=str(self.dialog.combo_box_17.currentText()), style='filled', gradientangle='1') i.edge( 'a2', 'a1', shape=str(self.dialog.combo_box_22.currentText()), fillcolor=str( self.dialog.combo_box_17.currentText()), style=str(self.dialog.combo_box_23.currentText()), arrowhead=str( self.dialog.combo_box_21.currentText()), arrowsize=str( self.dialog.combo_box_24.currentText())) dt = datetime.datetime.now() matrix_path = '{}{}{}'.format(self.HOME, os.sep, "pyarchinit_Matrix_folder") filename = 'Harris_matrix' #f = open(filename, "w") G.format = 'dot' 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') ######################trasformazione in jsone xdot_jason########################################### # with open(os.path.join(matrix_path, filename + '_tred.gml'), "w") as out: # subprocess.Popen(['gv2gml',tred_file], # #shell=True, # stdout=out) #startupinfo=si if Pyarchinit_OS_Utility.isWindows()else None) # with open(os.path.join(matrix_path, filename + '_tred.xdot_json'), "w") as out: # subprocess.Popen(['tred',tred_file], # #shell=True, # stdout=out) # #startupinfo=si if Pyarchinit_OS_Utility.isWindows()else None) ############################################################################################## f = Source.from_file(tred_file, format='png') f.render() g = Source.from_file(tred_file, format='jpg') g.render() return g, f
#go through all modified memory and hook each one as an end node which is the state of the system at the end of slice. for i in range(len(memAddressNames)): #some of the mem addresses in the memAddressNames are not modified by the slice, they are only input sources of memory # this if statement makes sure we only add endpoint nodes/edges for modified memory addresses if '[0x' not in ((memAddressDict[memAddressNames[i]])): #add_endNode(memAddressDict[memAddressNames[i]]) addNode(memAddressNames[i]) addEdge(memAddressDict[(memAddressNames[i])], memAddressNames[i], 'EndofSliceValue') #datG.node(memAddressNames[i], label = str(memAddressNames[i]), shape='box', color = 'darkgreen', penwidth = edgePenWidth) #datG.edge(memAddressDict[(memAddressNames[i])], memAddressNames[i], label='(' + 'EndofSliceValue' +')', color='Green', penwidth = edgePenWidth) #print(memAddressDict[memAddressNames[i]]) #go through all the flags in the x86 FLAG register and hook each #one as an end node to the instruction that last modified it before the slice ended. for i in range(len(FlagRegList)): addNode(FlagRegListNames[i]) addEdge(FlagRegList[i], FlagRegListNames[i], 'EndofSliceValue') with open(outFileName, 'w') as outFile: for line in datG.source: outFile.write(line) #for e in datG.get_edge_list(): # print e src = Source(datG) src.render(renderFileName, view=True) print('done! check '+ outFileName)
def _repr_svg_(self): from graphviz import Source return Source(self._repr_gv_(), encoding='utf-8')._repr_svg_()
for i in range(len(s)): f_str += " <mc:Edge>\n" f_str += " <mc:sourceId>%i</mc:sourceId>\n" % s[i] f_str += " <mc:targetId>%i</mc:targetId>\n" % d[i] # f_str += " <mc:networkLoad>%0.2f</mc:networkLoad>\n"%loads[i] f_str += " <mc:networkLoad>%i</mc:networkLoad>\n" % loads[i] f_str += " </mc:Edge>\n" f_str += " </mc:EdgeList>\n\n" # Ranks f_str += " <mc:RankList>\n" for r in aranks: f_str += " <mc:RankGroup>\n" rnodes = np.where(nranks == r)[0] for n in rnodes: f_str += " <mc:Rank>%i</mc:Rank>\n" % n f_str += " </mc:RankGroup>\n" f_str += " </mc:RankList>\n\n" f_str += "</mc:Graph>\n" fid.write(f_str) fid.close() # Create pdf and view s = Source.from_file(fname + '.gv') s.view()
def draw_notebook_dependencies(notebooks, format='svg', transitive_reduction=True, clusters=True, project='fuzzingbook'): dot = Digraph(comment="Notebook dependencies") # dot.attr(size='20,30', rank='max') if project == 'debuggingbook': fontname = 'Raleway, Helvetica, Arial, sans-serif' fontcolor = '#6A0DAD' else: fontname = 'Patua One, Helvetica, sans-serif' fontcolor = '#B03A2E' node_attrs = { 'shape': 'note', # note, plain, none 'style': 'filled', 'fontname': fontname, 'fontcolor': fontcolor, 'fillcolor': 'white' } cluster = None cluster_attrs = { 'shape': 'plain', # note, plain, none 'style': 'filled', 'fontname': fontname, 'fontcolor': 'black', 'color': '#F0F0F0', } for notebook_name in notebooks: dirname = os.path.dirname(notebook_name) basename = os.path.splitext(os.path.basename(notebook_name))[0] title = get_title(notebook_name) if clusters: if title.startswith("Part"): if cluster is not None: cluster.attr(**cluster_attrs) dot.subgraph(cluster) cluster = Digraph(name='cluster_' + basename) cluster.node(basename, label=title, URL='%s.ipynb' % basename, tooltip=basename, shape='plain', fontname=fontname) elif cluster is not None: cluster.node(basename) for module in notebook_dependencies(notebook_name, include_minor_dependencies=False): module_file = os.path.join(dirname, module + ".ipynb") if module_file in notebooks: module_title = get_title(module_file) dot.node(basename, URL='%s.ipynb' % basename, label=title, tooltip=basename, **node_attrs) dot.node(module, URL='%s.ipynb' % module, label=module_title, tooltip=module, **node_attrs) dot.edge(module, basename) if cluster is not None: cluster.attr(**cluster_attrs) dot.subgraph(cluster) if transitive_reduction: dot.format = 'gv' dot.save('depend.gv') os.system('tred depend.gv > depend.gv~ && mv depend.gv~ depend.gv') dot = Source.from_file('depend.gv') os.remove('depend.gv') dot.format = format dot.render('depend') os.system('cat depend.' + format) os.remove('depend') os.remove('depend.' + format)
def __init__(self, m_desc, FuseEdges=False, pick_start=False, max_width=10.0, accept_color='chartreuse3', reject_color='red', neutral_color='dodgerblue2'): # Options self.color_accept = accept_color self.color_reject = reject_color self.color_neutral = neutral_color self.max_width = max_width self.fuse = FuseEdges # DFA specific option self.valid_input = True self.machine = m_desc self.machine_obj = dotObj_nfa(self.machine, FuseEdges=FuseEdges) self.copy_source = reformat_edge_labels(set_graph_size(self.machine_obj.source, max_width), additional=' color=black arrowsize=1 penwidth=1') # Set things we need for the animation self.machine_steps = [] self.feed_steps = [] self.from_nodes = self.machine['Q0'] self.to_nodes = self.machine['Q0'] self.animated = False # Setup the widgets # Top row for user input self.user_input = widgets.Text(value='', placeholder='Sigma: {{{}}}'.format(','.join(sorted(self.machine['Sigma']))), description='Input:', layout=Layout(width='500px') ) self.user_input.observe(self.on_input_change, names='value') self.alternate_start = widgets.SelectMultiple(options=sorted(self.machine['Q']), value=tuple(self.machine['Q0']), rows=5, description='Start States:', disabled=False, layout=Layout(width='200px') ) self.generate_button = widgets.Button(description="Animate", button_style='primary', disabled=False ) self.generate_button.on_click(self.generate_animation) # Bottom row for player controls self.play_controls = widgets.Play(interval=950, value=0, min=0, max=100, step=1, description="Press play" ) self.play_controls.observe(self.on_play_step, names='value') self.speed_control = widgets.IntSlider(value=1, min=1, max=10, step=1, description='Speed:', disabled=False, continuous_update=False, orientation='horizontal', readout=True, readout_format='d' ) self.speed_control.observe(self.on_speed_change, names='value') # Create the controls for stepping through the animation self.backward = widgets.Button(icon='step-backward', layout=Layout(width='40px'), disabled=True ) self.forward = widgets.Button(icon='step-forward', layout=Layout(width='40px'), disabled=True ) self.backward.on_click(self.on_backward_click) self.forward.on_click(self.on_forward_click) # set the widget to display the machine self.machine_display = widgets.Output() with self.machine_display: display(Source(self.copy_source)) # set the widget to display the feed self.feed_display = widgets.Output() # arrange the widgets in the display area row1 = widgets.HBox([self.user_input, self.generate_button]) if pick_start: row1 = widgets.HBox([self.user_input, self.alternate_start, self.generate_button]) row2 = widgets.HBox([self.play_controls, self.backward, self.forward, self.speed_control]) w = widgets.VBox([row1, self.machine_display, self.feed_display, row2]) display(w) self.play_controls.disabled = True self.forward.disabled = True self.backward.disabled = True self.speed_control.disabled = True
def main(args: Any = None) -> None: mctx, *_, migrators = initialize_migrators() if not os.path.exists("./status"): os.mkdir("./status") total_status = {} for migrator in migrators: if isinstance(migrator, GraphMigrator) or isinstance( migrator, Replacement): if hasattr(migrator, "name"): assert isinstance(migrator.name, str) migrator_name = migrator.name.lower().replace(" ", "") else: migrator_name = migrator.__class__.__name__.lower() total_status[migrator_name] = f"{migrator.name} Migration Status" status, build_order, gv = migrator_status(migrator, mctx.graph) with open(os.path.join(f"./status/{migrator_name}.json"), "w") as fo: json.dump(status, fo, indent=2) d = gv.pipe("dot") with tempfile.NamedTemporaryFile() as ntf, open( f"{ntf.name}.dot", "w") as f: f.write(d.decode("utf-8")) # make the graph a bit more compact d = Source( subprocess.check_output([ "unflatten", "-f", "-l", "5", "-c", "10", f"{ntf.name}.dot", ]).decode("utf-8")).pipe("svg") with open(os.path.join(f"./status/{migrator_name}.svg"), "wb") as fb: fb.write(d) with open("./status/total_status.json", "w") as f: json.dump(total_status, f, sort_keys=True) lst = [ k for k, v in mctx.graph.nodes.items() if len([ z for z in v.get("payload", {}).get("PRed", []) if z.get("PR", {}).get("state", "closed") == "open" and z.get("data", {}).get("migrator_name", "") == "Version" ], ) >= Version.max_num_prs ] with open("./status/could_use_help.json", "w") as f: json.dump( sorted( lst, key=lambda z: (len(nx.descendants(mctx.graph, z)), lst), reverse=True, ), f, indent=2, ) lm = LicenseMigrator() lst = [ k for k, v in mctx.graph.nodes.items() if not lm.filter(v.get("payload", {})) ] with open("./status/unlicensed.json", "w") as f: json.dump( sorted( lst, key=lambda z: (len(nx.descendants(mctx.graph, z)), lst), reverse=True, ), f, indent=2, )
def generate_animation(self, change): if self.animated: # switching to input mode # enable the input controls self.play_controls._playing = False self.animated = False self.user_input.disabled = False # self.alternate_start.disabled = False self.stack_size_slider.disabled = False self.acceptance_toggle.disabled = False # update the button to switch between modes self.generate_button.description = 'Animate' self.generate_button.button_style = 'primary' # disable the play controls self.play_controls.disabled = True self.forward.disabled = True self.backward.disabled = True self.speed_control.disabled = True self.path_dropdown.disabled = True self.path_dropdown.index = None # clean the machine display with self.machine_display: clear_output(wait=True) display(Source(self.copy_source)) with self.stack_display: clear_output(wait=True) display(Source(self.set_stack_display())) with self.feed_display: clear_output(wait=True) display(Source(self.generate_feed('', 0, 0, [])[0])) with self.rejection_display: clear_output() else: # switching to play mode # ignore invalid input if not self.valid_user_input(): return # disable the input controls self.animated = True self.user_input.disabled = True # self.alternate_start.disabled = True self.stack_size_slider.disabled = True self.acceptance_toggle.disabled = True self.generate_button.description = 'Change Input' # clean the current play displays self.feed_steps = [] self.stack_steps = [] self.machine_steps = [] # find the acceptance paths a = () paths = [] touched = [] with io.capture_output() as captured: a, paths, touched = run_pda( self.user_input.value, self.machine, acceptance=self.acceptance_toggle.value, STKMAX=self.stack_size) # if there are no acceptance paths we don't have any animations to build if len(paths) == 0: self.generate_button.button_style = 'danger' rejected_machine = set_graph_color(self.copy_source, self.color_reject) with self.rejection_display: self.rejection_text.value = '{}{}{}'.format( self.reject_msg_start, self.user_input.value, self.reject_msg_end) display(self.rejection_text) with self.machine_display: clear_output(wait=True) display(Source(rejected_machine)) return new_dropdown_options = {} # generate all the display steps for each path path_count = 1 for p in paths: path_states = p[1].copy() max_steps = (len(path_states)) * 2 + 1 path_states.append(p[0]) # generate the feed display path_feed_steps = [] inspecting = '' for step in range(max_steps): feed_step, inspecting = self.generate_feed( inspecting, step, max_steps, path_states) path_feed_steps.append(feed_step) # generate the machine steps path_obj_steps = [] for step in range(max_steps): path_obj_steps.append( self.generate_machine_steps(path_states, step, max_steps)) # generate the stack steps path_stack_steps = [] for step in range(max_steps): if step == max_steps: path_stack_steps.append( self.set_stack_display(path_states[-1][2])) else: path_stack_steps.append( self.set_stack_display(path_states[step // 2][2])) # add the path as an option in the dropdown new_dropdown_options['Path {}'.format(path_count)] = ( max_steps - 1, path_obj_steps, path_stack_steps, path_feed_steps) path_count += 1 # update the dropdown self.path_dropdown.options = new_dropdown_options self.path_dropdown.index = 0 # display the machine for this step with self.machine_display: clear_output(wait=True) display(Source(self.machine_steps[0])) # display the feed for this step with self.feed_display: clear_output(wait=True) display(Source(self.feed_steps[0])) # display the stack for this step with self.stack_display: clear_output(wait=True) display(Source(self.stack_steps[0])) # enable the controls self.backward.disabled = True if len(self.user_input.value) == 0: self.forward.disabled = True else: self.forward.disabled = False self.play_controls.disabled = False self.speed_control.disabled = False self.path_dropdown.disabled = False
#pip install graphviz from graphviz import Source from sklearn import tree tree.plot_tree(decision_tree=clsModel) tree.plot_tree(decision_tree=clsModel, feature_names=['Var', 'Skew', ' Kur', 'Ent'], class_names=['Org', 'Fake'], fontsize=12) #not a good way to draw graphs.. other methods to be experimented tree.plot_tree(decision_tree=clsModel, max_depth=2, feature_names=['Var', 'Skew', ' Kur', 'Ent'], class_names=['Org1', 'Fake1'], fontsize=12) Source(tree.export_graphviz(clsModel)) Source(tree.export_graphviz(clsModel, max_depth=3)) dot_data1 = tree.export_graphviz(clsModel, max_depth=3, out_file=None, filled=True, rounded=True, special_characters=True, feature_names=['Var', 'Skew', ' Kur', 'Ent'], class_names=['Org', 'Fake']) #check the folder location after installing the graphviz import os os.environ["PATH"] += os.pathsep + 'c:/Program Files (x86)/Graphviz2.38/bin/' import graphviz from subprocess import call call(['dot', '-Tpng', 'tree.dot', '-o', 'tree.png', '-Gdpi=600'])
preds # Accuracy = train from sklearn import metrics print("Accuracy:", metrics.accuracy_score(y_train, preds)) preds = model.predict(x_test) preds # Accuracy = train from sklearn import metrics print("Accuracy:", metrics.accuracy_score(y_test, preds)) #....1 #plot from matplotlib import pyplot as plt tree.plot_tree(model.fit(x_train, y_train)) from sklearn.externals.six import StringIO from IPython.display import Image from sklearn.tree import export_graphviz import graphviz from graphviz import Source from pydotplus import pydotplus #dot_data = StringIO() help(tree.export_graphviz()) dot_data = Source( tree.export_graphviz(model, out_file=None, feature_names=colnames)) graph = graphviz.Source(dot_data) graph
def callgraph(code, name="callgraph"): if not os.path.isfile(name + '.dot'): construct_callgraph(code, name) return Source.from_file(name + '.dot')
if tight_layout: plt.tight_layout() plt.savefig(path, format=fig_extension, dpi=resolution) from sklearn.datasets import load_iris from sklearn.tree import DecisionTreeClassifier iris = load_iris() X = iris.data[:, 2:] # petal length and width y = iris.target tree_clf = DecisionTreeClassifier(max_depth=2, random_state=42) tree_clf.fit(X, y) from graphviz import Source from sklearn.tree import export_graphviz export_graphviz( tree_clf, out_file=os.path.join(IMAGES_PATH, "iris_tree.dot"), feature_names=iris.feature_names[2:], class_names=iris.target_names, rounded=True, filled=True ) Source.from_file(os.path.join(IMAGES_PATH, "iris_tree.dot"))
if 'label' in d: node.attr['label'] = str(n) + "\n" + str(d['label']) for n,c in colors.items(): node = A.get_node(n) node.attr['color'] = c node.attr['style'] = 'filled' print n,c for n,c in edges_color.items(): node = A.get_edge(*n) node.attr['color'] = c node.attr['penwidth'] = 2 print n,c except Exception,e: print "Error on printing graph: ",e src = Source(A) src.format = 'png' src.render("test-output/"+name,view=True) def minimize(self): for n in self.nodes(): path_exist = False for m in self.graph['initial']: if nx.has_path(self,m,n): path_exist = True break if path_exist==False: self.remove_node_init_accept(n) def run_trace(self,state_trace): labels = nx.get_node_attributes(self,'label')
def apply(fp, parameters=None): """ Visualize a footprints table Parameters --------------- fp Footprints parameters Parameters of the algorithm, including: - Parameters.FORMAT => Format of the visualization Returns --------------- gviz Graphviz object """ if parameters is None: parameters = {} if type(fp) is list: raise Exception("footprints visualizer does not work on list of footprints!") activities = sorted(list(set(x[0] for x in fp["sequence"]).union(set(x[1] for x in fp["sequence"])).union( set(x[0] for x in fp["parallel"])).union(set(x[1] for x in fp["parallel"])))) fp_table = {} for a1 in activities: fp_table[a1] = {} for a2 in activities: fp_table[a1][a2] = XOR_SYMBOL for x in fp["sequence"]: if x not in fp["parallel"]: fp_table[x[0]][x[1]] = SEQUENCE_SYMBOL fp_table[x[1]][x[0]] = PREV_SYMBOL for x in fp["parallel"]: fp_table[x[0]][x[1]] = PARALLEL_SYMBOL image_format = exec_utils.get_param_value(Parameters.FORMAT, parameters, "png") filename = tempfile.NamedTemporaryFile(suffix='.gv') footprints_table = ["digraph {\n", "tbl [\n", "shape=plaintext\n", "label=<\n"] footprints_table.append("<table border='0' cellborder='1' color='blue' cellspacing='0'>\n") footprints_table.append("<tr><td></td>") for act in activities: footprints_table.append("<td><b>"+act+"</b></td>") footprints_table.append("</tr>\n") for a1 in activities: footprints_table.append("<tr><td><b>"+a1+"</b></td>") for a2 in activities: footprints_table.append("<td>"+fp_table[a1][a2]+"</td>") footprints_table.append("</tr>\n") footprints_table.append("</table>\n") footprints_table.append(">];\n") footprints_table.append("}\n") footprints_table = "".join(footprints_table) gviz = Source(footprints_table, filename=filename.name) gviz.format = image_format return gviz
if b != c: if a == c: return "Isosceles" else: return "Scalene" else: return "Isosceles" import inspect if __name__ == '__main__': graph = to_graph(gen_cfg(inspect.getsource(check_triangle))) if __name__ == '__main__': Source(graph) #### cgi_decode if __name__ == '__main__': print('\n#### cgi_decode') def cgi_decode(s): hex_values = { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5,
sdp = StanfordDependencyParser(path_to_jar='E:/stanford/stanford-parser-full-2015-04-20/stanford-parser.jar', path_to_models_jar='E:/stanford/stanford-parser-full-2015-04-20/stanford-parser-3.5.2-models.jar') result = list(sdp.raw_parse(sentence)) result[0] [item for item in result[0].triples()] dep_tree = [parse.tree() for parse in result][0] print dep_tree dep_tree.draw() # generation of annotated dependency tree shown in Figure 3-4 from graphviz import Source dep_tree_dot_repr = [parse for parse in result][0].to_dot() source = Source(dep_tree_dot_repr, filename="dep_tree", format="png") source.view() import nltk tokens = nltk.word_tokenize(sentence) dependency_rules = """ 'fox' -> 'The' | 'brown' 'quick' -> 'fox' | 'is' | 'and' | 'jumping' 'jumping' -> 'he' | 'is' | 'dog' 'dog' -> 'over' | 'the' | 'lazy' """ dependency_grammar = nltk.grammar.DependencyGrammar.fromstring(dependency_rules) print dependency_grammar
def main(args: Any = None) -> None: import requests r = requests.get( "https://raw.githubusercontent.com/conda-forge/" "conda-forge.github.io/master/img/anvil.svg", ) # cache these for later if os.path.exists("status/closed_status.json"): with open("status/closed_status.json") as fp: old_closed_status = json.load(fp) else: old_closed_status = {} with open("status/total_status.json") as fp: old_total_status = json.load(fp) mctx, *_, migrators = initialize_migrators() if not os.path.exists("./status"): os.mkdir("./status") regular_status = {} longterm_status = {} print(" ", flush=True) for migrator in migrators: if hasattr(migrator, "name"): assert isinstance(migrator.name, str) migrator_name = migrator.name.lower().replace(" ", "") else: migrator_name = migrator.__class__.__name__.lower() print( "================================================================", flush=True, ) print("name:", migrator_name, flush=True) if isinstance(migrator, GraphMigrator) or isinstance(migrator, Replacement): if isinstance(migrator, GraphMigrator): mgconf = yaml.safe_load(getattr(migrator, "yaml_contents", "{}")).get( "__migrator", {}, ) if ( mgconf.get("longterm", False) or isinstance(migrator, ArchRebuild) or isinstance(migrator, OSXArm) ): longterm_status[migrator_name] = f"{migrator.name} Migration Status" else: regular_status[migrator_name] = f"{migrator.name} Migration Status" else: regular_status[migrator_name] = f"{migrator.name} Migration Status" status, build_order, gv = graph_migrator_status(migrator, mctx.graph) num_viz = status.pop("_num_viz", 0) with open(os.path.join(f"./status/{migrator_name}.json"), "w") as fp: json.dump(status, fp, indent=2) if num_viz <= 500: d = gv.pipe("dot") with tempfile.NamedTemporaryFile(suffix=".dot") as ntf: ntf.write(d) # make the graph a bit more compact d = Source( subprocess.check_output( ["unflatten", "-f", "-l", "5", "-c", "10", f"{ntf.name}"], ).decode("utf-8"), ).pipe("svg") with open(os.path.join(f"./status/{migrator_name}.svg"), "wb") as fb: fb.write(d or gv.pipe("svg")) else: with open(os.path.join(f"./status/{migrator_name}.svg"), "wb") as fb: fb.write(r.content) elif isinstance(migrator, Version): write_version_migrator_status(migrator, mctx) print(" ", flush=True) print("writing data", flush=True) with open("./status/regular_status.json", "w") as f: json.dump(regular_status, f, sort_keys=True, indent=2) with open("./status/longterm_status.json", "w") as f: json.dump(longterm_status, f, sort_keys=True, indent=2) total_status = {} total_status.update(regular_status) total_status.update(longterm_status) with open("./status/total_status.json", "w") as f: json.dump(total_status, f, sort_keys=True, indent=2) closed_status = _compute_recently_closed( total_status, old_closed_status, old_total_status, ) with open("./status/closed_status.json", "w") as f: json.dump(closed_status, f, sort_keys=True, indent=2) print("\ncomputing feedstock and PR stats", flush=True) def _get_needs_help(k): v = mctx.graph.nodes[k] if ( len( [ z for z in v.get("payload", {}).get("PRed", []) if z.get("PR", {}).get("state", "closed") == "open" and z.get("data", {}).get("migrator_name", "") == "Version" ], ) >= Version.max_num_prs ): return k else: return None lst = _collect_items_from_nodes(mctx.graph, _get_needs_help) with open("./status/could_use_help.json", "w") as f: json.dump( sorted( lst, key=lambda z: (len(nx.descendants(mctx.graph, z)), lst), reverse=True, ), f, indent=2, ) lm = LicenseMigrator() def _get_needs_license(k): v = mctx.graph.nodes[k] if not lm.filter(v.get("payload", {})): return k else: return None lst = _collect_items_from_nodes(mctx.graph, _get_needs_license) with open("./status/unlicensed.json", "w") as f: json.dump( sorted( lst, key=lambda z: (len(nx.descendants(mctx.graph, z)), lst), reverse=True, ), f, indent=2, ) def _get_open_pr_states(k): attrs = mctx.graph.nodes[k] _open_prs = [] for pr in attrs.get("PRed", []): if pr.get("PR", {}).get("state", "closed") != "closed": _open_prs.append(pr["PR"]) return _open_prs open_prs = [] for op in _collect_items_from_nodes(mctx.graph, _get_open_pr_states): open_prs.extend(op) merge_state_count = Counter([o["mergeable_state"] for o in open_prs]) with open("./status/pr_state.csv", "a") as f: writer = csv.writer(f) writer.writerow([merge_state_count[k] for k in GH_MERGE_STATE_STATUS])
def to_image(self, all_data): graph = Source(all_data, format="svg") graph.render(view=True)
import zerosmq import sys import time zerosmq.init(sys.argv[0]) zerosmq.control_socket.send_string("REQUEST_MAP null null") dotsource=zerosmq.control_socket.recv_string() from graphviz import Source s = Source(dotsource, filename="temp.gv", format="png") s.view()
acc = accuracy_score(testY, predY) #confusion matrix calculation cfm = confusion_matrix(testY, predY, labels=[0, 1]) print('Confusion Matrix:\n', cfm) print('\nAccuracy: ', acc) # calculating per class precision and per class recall precision = [] recall = [] precision.append(cfm[0, 0] / (cfm[0, 0] + cfm[1, 0])) precision.append(cfm[1, 1] / (cfm[0, 1] + cfm[1, 1])) recall.append(cfm[0, 0] / (cfm[0, 0] + cfm[0, 1])) recall.append(cfm[1, 1] / (cfm[1, 0] + cfm[1, 1])) print('\nPrecision: ', precision) print('\nRecall: ', recall) # Decision tree visualization # Note: Decision tree is huge, # so some times it feels like there is no output, to see the whole tree please scroll down print("\nTree Visualization") graph = Source( tree.export_graphviz( clf, out_file=None, feature_names=['Occupation', 'Marital_Status', 'Age', 'Gender'])) SVG(graph.pipe(format='svg'))
def linea_Vertical(): c = fini.get() f1 = cini.get() f2 = ffi.get() if int(c) > int(self.colum1): messagebox.showerror(message="La columna " + str(c) + " no existe", title="Error") H = self.hora() self.errores.insertar(self.fecha, H, "La columna " + str(c) + " no existe", "Agregar linea horizontal", self.name1) self.ventana_data3.destroy() elif int(f1) > int(self.row1): messagebox.showerror(message="La columna " + str(f1) + " no existe", title="Error") H = self.hora() self.errores.insertar(self.fecha, H, "La columna " + str(f1) + " no existe", "Agregar linea horizontal", self.name1) self.ventana_data3.destroy() elif int(f2) > int(self.row1): messagebox.showerror(message="La columna " + str(f2) + " no existe", title="Error") H = self.hora() self.errores.insertar(self.fecha, H, "La columna " + str(f2) + " no existe", "Agregar linea horizontal", self.name1) self.ventana_data3.destroy() elif int(f1) > int(f2): messagebox.showerror( message="La Fila inicial es mas grande que la Fila final", title="Error") H = self.hora() self.errores.insertar( self.fecha, H, "La columna inicial es mas grande que la columna final", "Agregar linea horizontal", self.name1) self.ventana_data3.destroy() else: self.m.linea_vertical(int(c), int(f1), int(f2)) a = self.m.cadena_grap() s = Source(a, filename="edit.gv", format="png") s.render() self.img2 = ImageTk.PhotoImage( Image.open( "C:/Users/jezeh/OneDrive/Escritorio/IPC2/Proyecto2_ipc2/edit.gv.png" )) self.lab2 = Label(self.frame2, image=self.img2) self.lab2.grid(row=1, column=1, padx=10, pady=10, sticky="nsew") Hora = self.hora() self.operac.insertar(self.fecha, Hora, "Insertar Linea Vertical", self.name1) self.labE.config(text="Linea Vertical") self.ventana_data3.destroy()
def learnDiscreteBN(df, continous_columns, features_column_names, label_column='cat', draw_network=False): features_df = df.copy() features_df = features_df.drop(label_column, axis=1) labels_df = DataFrame() labels_df[label_column] = df[label_column].copy() for i in continous_columns: bins = np.arange((min(features_df[i])), (max(features_df[i])), ((max(features_df[i]) - min(features_df[i])) / 5.0)) features_df[i] = pandas.np.digitize(features_df[i], bins=bins) data = [] for index, row in features_df.iterrows(): dict = {} for i in features_column_names: dict[i] = row[i] dict[label_column] = labels_df[label_column][index] data.append(dict) print "Init done" learner = PGMLearner() test = learner.discrete_estimatebn(data=data, pvalparam=0.05, indegree=1) # print test.__dict__ f = open('heart_structure.txt', 'w') s = str(test.__dict__) f.write(s) f.flush() f.close() print "done learning" edges = test.E vertices = test.V probas = test.Vdata # print probas dot_string = 'digraph BN{\n' dot_string += 'node[fontname="Arial"];\n' dataframes = {} print "save data" for vertice in vertices: print "New vertice: " + str(vertice) dataframe = DataFrame() pp = pprint.PrettyPrinter(indent=4) # pp.pprint(probas[vertice]) dot_string += vertice.replace(" ", "_") + ' [label="' + vertice + '\n' + '" ]; \n' if len(probas[vertice]['parents']) == 0: dataframe['Outcome'] = None dataframe['Probability'] = None vertex_dict = {} for index_outcome, outcome in enumerate(probas[vertice]['vals']): vertex_dict[str(outcome)] = probas[vertice]["cprob"][index_outcome] od = collections.OrderedDict(sorted(vertex_dict.items())) # print "Vertice: " + str(vertice) # print "%-7s|%-11s" % ("Outcome", "Probability") # print "-------------------" for k, v in od.iteritems(): # print "%-7s|%-11s" % (str(k), str(round(v, 3))) dataframe.loc[len(dataframe)] = [k, v] dataframes[vertice] = dataframe else: # pp.pprint(probas[vertice]) dataframe['Outcome'] = None vertexen = {} for index_outcome, outcome in enumerate(probas[vertice]['vals']): temp = [] for parent_index, parent in enumerate(probas[vertice]["parents"]): # print str([str(float(index_outcome))]) temp = probas[vertice]["cprob"] dataframe[parent] = None vertexen[str(outcome)] = temp dataframe['Probability'] = None od = collections.OrderedDict(sorted(vertexen.items())) # [str(float(i)) for i in ast.literal_eval(key)] # str(v[key][int(float(k))-1]) # print "Vertice: " + str(vertice) + " with parents: " + str(probas[vertice]['parents']) # print "Outcome" + "\t\t" + '\t\t'.join(probas[vertice]['parents']) + "\t\tProbability" # print "------------" * len(probas[vertice]['parents']) *3 # pp.pprint(od.values()) counter = 0 # print number_of_cols for outcome, cprobs in od.iteritems(): for key in cprobs.keys(): array_frame = [] array_frame.append((outcome)) print_string = str(outcome) + "\t\t" for parent_value, parent in enumerate([i for i in ast.literal_eval(key)]): # print "parent-value:"+str(parent_value) # print "parten:"+str(parent) array_frame.append(int(float(parent))) # print "lengte array_frame: "+str(len(array_frame)) print_string += parent + "\t\t" array_frame.append(cprobs[key][counter]) # print "lengte array_frame (2): "+str(len(array_frame)) # print cprobs[key][counter] print_string += str(cprobs[key][counter]) + "\t" # for stront in [str(round(float(i), 3)) for i in ast.literal_eval(key)]: # print_string += stront + "\t\t" # print "print string: " + print_string # print "array_frame:" + str(array_frame) dataframe.loc[len(dataframe)] = array_frame counter += 1 print "Vertice " + str(vertice) + " done" dataframes[vertice] = dataframe for edge in edges: dot_string += edge[0].replace(" ", "_") + ' -> ' + edge[1].replace(" ", "_") + ';\n' dot_string += '}' src = Source(dot_string) if draw_network:src.render('../data/BN', view=draw_network) if draw_network:src.render('../data/BN', view=False) print "vizualisation done" return dataframes
def on_stack_size_change(self, change): self.stack_size = change['new'] with self.stack_display: clear_output(wait=True) display(Source(self.set_stack_display()))
process.setOrigin(origin) print("Origin accessor : process = ") print(process) process.setTimeGrid(ot.RegularGrid(0, 1, 20)) print("Time grid accessor : process = ") print(process) real = process.getRealization() print("Realization :") print(real) future = process.getFuture(20) print("One future :") print(future) futures = process.getFuture(20, 3) print("3 different futures :") print(futures) process.exportToDOTFile('markov.dot') with open('markov.dot') as f: print(f.read()) if 0: from graphviz import Source path = 'markov.dot' s = Source.from_file(path) s.view() os.remove('markov.dot')
parameters = { 'criterion': ['entropy', 'gini'], 'min_samples_split': [2, 3, 4], 'max_depth': range(1, 20, 2), 'max_leaf_nodes': list(range(2, 100)) } grid_search_cv = GridSearchCV(DecisionTreeClassifier(random_state=42), parameters, verbose=1, cv=3) grid_search_cv.fit(X_train, y_train) print(grid_search_cv.best_estimator_) tree1 = grid_search_cv.best_estimator_ y_pred = tree1.predict(X_test) result = accuracy_score(y_test, y_pred) print("Accuracy is", result * 100) from graphviz import Source from sklearn.tree import export_graphviz export_graphviz(tree1, out_file=os.path.join(IMAGES_PATH, "cancer_tree.dot"), feature_names=cancer.feature_names, class_names=cancer.target_names, rounded=True, filled=True) Source.from_file(os.path.join(IMAGES_PATH, "cancer_tree.dot"))
def _text_to_graphiz(self, text): """create a graphviz graph from text""" dot = Source(text, format='svg') return dot.pipe().decode('utf-8')
def to_graphviz(booster, fmap='', num_trees=0, rankdir=None, yes_color=None, no_color=None, condition_node_params=None, leaf_node_params=None, **kwargs): """Convert specified tree to graphviz instance. IPython can automatically plot the returned graphiz instance. Otherwise, you should call .render() method of the returned graphiz instance. Parameters ---------- booster : Booster, XGBModel Booster or XGBModel instance fmap: str (optional) The name of feature map file num_trees : int, default 0 Specify the ordinal number of target tree rankdir : str, default "UT" Passed to graphiz via graph_attr yes_color : str, default '#0000FF' Edge color when meets the node condition. no_color : str, default '#FF0000' Edge color when doesn't meet the node condition. condition_node_params : dict, optional Condition node configuration for for graphviz. Example: .. code-block:: python {'shape': 'box', 'style': 'filled,rounded', 'fillcolor': '#78bceb'} leaf_node_params : dict, optional Leaf node configuration for graphviz. Example: .. code-block:: python {'shape': 'box', 'style': 'filled', 'fillcolor': '#e48038'} \\*\\*kwargs: dict, optional Other keywords passed to graphviz graph_attr, e.g. ``graph [ {key} = {value} ]`` Returns ------- graph: graphviz.Source """ try: from graphviz import Source except ImportError as e: raise ImportError('You must install graphviz to plot tree') from e if isinstance(booster, XGBModel): booster = booster.get_booster() # squash everything back into kwargs again for compatibility parameters = 'dot' extra = {} for key, value in kwargs.items(): extra[key] = value if rankdir is not None: kwargs['graph_attrs'] = {} kwargs['graph_attrs']['rankdir'] = rankdir for key, value in extra.items(): if 'graph_attrs' in kwargs.keys(): kwargs['graph_attrs'][key] = value else: kwargs['graph_attrs'] = {} del kwargs[key] if yes_color is not None or no_color is not None: kwargs['edge'] = {} if yes_color is not None: kwargs['edge']['yes_color'] = yes_color if no_color is not None: kwargs['edge']['no_color'] = no_color if condition_node_params is not None: kwargs['condition_node_params'] = condition_node_params if leaf_node_params is not None: kwargs['leaf_node_params'] = leaf_node_params if kwargs: parameters += ':' parameters += str(kwargs) tree = booster.get_dump( fmap=fmap, dump_format=parameters)[num_trees] g = Source(tree) return g
return thisnode ############################################################################# X, y = make_data(m=200, n=10, k=5, make_identical_observations_consistent=False, seed=True) tree = Tree(metric='gini', max_depth=6).fit(X, y) ypred = tree.predict(X) acc = np.equal(y, ypred).mean() print("accuracy =", acc, "\tnumber of nodes =", tree._nodes_counter) #graphviz string = tree.graph try: from graphviz import Source except: print("Unable to import graphviz") else: graph = Source(string, filename="image", format="png") graph.view() from sklearn.tree import DecisionTreeClassifier md = DecisionTreeClassifier(max_depth=6).fit(X, y) acc = md.score(X, y) print("sklearn acc", acc)
def write_graph(fields, containments, nests, matched_references, clusters): # Start a digraph graph = "digraph UML {\n" # Set the image's size, in inches graph += "size= \"33,33\";\n" # Add a title graph += "labelloc=\"t\";\n" graph += "label=<<FONT POINT-SIZE=\"45\">GA4GH Schema Diagram</FONT>>;\n" # Define node properties: shaped like UML items. graph += "node [\n" graph += "\tshape=plaintext\n" graph += "]\n\n" # Draw each node/type/record as a table for type_name, field_list in fields.items(): graph += "{} [label=<\n".format(type_name) graph += "<TABLE BORDER='0' CELLBORDER='1' CELLSPACING='0' CELLPADDING='4' bgcolor='#002060' color='#002060'>\n" graph += "\t<TR>\n" graph += "\t\t<TD COLSPAN='2' bgcolor='#79A6FF' border='3'><FONT POINT-SIZE='20' color='white'>{}</FONT>".format(type_name) graph += "</TD>\n" graph += "\t</TR>\n" # Now draw the rows of fields for the type. A field_list of # [a, b, c, d, e, f, g] will have [a, e] in row 1, [b, f] in # row 2, [c, g] in row 3, and just [d] in row 4 num_fields = len(field_list) for i in range(0, num_fields//2 + num_fields%2): # Draw one row. graph += "\t<TR>\n" # Port number and displayed text will be the i'th field's # name graph += "\t\t<TD align='left' port='{}'><FONT color='white'>- {}</FONT></TD>\n".format(field_list[i][0], field_list[i][0]) if (num_fields%2) == 1 and (i == num_fields//2 + num_fields%2 - 1): # Don't draw the second cell in the row if you have an # odd number of fields and it is the last row pass else: graph += "\t\t<TD align='left' port='{}'><FONT color='white'>- {}</FONT></TD>\n".format(field_list[num_fields//2 + num_fields%2 + i][0], field_list[num_fields//2 + num_fields%2 + i][0]) graph += "\t</TR>\n" # Finish the table graph += "</TABLE>>];\n\n" # Now define the clusters/subgraphs for cluster_name, cluster_types in clusters.items(): graph += "subgraph cluster_{} {{\n".format(cluster_name.replace(".", "_").replace("/", "_")) graph += "\tstyle=\"rounded, filled\";\n" graph += "\tcolor=lightgrey;\n" graph += "\tnode [style=filled,color=white];\n" graph += "\tlabel = \"{}\";\n".format(cluster_name.replace(".", "_")) # After all the cluster formatting, define the cluster types for cluster_type in cluster_types: # cluster_type should match up with a type_name from fields graph += "\t{};\n".format(cluster_type) graph += "}\n\n" # Define edge properties for containments graph += "edge [\n" graph += "\tdir=both\n" graph += "\tarrowtail=odiamond\n" graph += "\tarrowhead=none\n" graph += "\tcolor=\"#C55A11\"\n" graph += "\tpenwidth=2\n" graph += "]\n\n" for container, containee, container_field_name in containments: # Now do the containment edges # Only write the edge if the containee is a top-level field in fields. if containee in fields: graph += "{}:{}:w -> {}\n".format(container, container_field_name, containee) # Define edge properties for references graph += "\nedge [\n" graph += "\tdir=both\n" graph += "\tarrowtail=none\n" graph += "\tarrowhead=vee\n" graph += "\tstyle=dashed\n" graph += "\tcolor=\"darkgreen\"\n" graph += "\tpenwidth=2\n" graph += "]\n\n" for referencer, referencer_field, referencee in matched_references: # Now do the reference edges graph += "{}:{}:w -> {}:id:w\n".format(referencer, referencer_field, referencee) # Close the digraph off. graph += "}\n" graph = graph.replace("\n", " ").replace("\t", " ") src = Source(graph, format='svg') src.render('_build/generated_images/schema_uml')