def add_to_graph_recursively(g, node: MyBPlusTreeNode, father_count, node_index): CountNode.amount += 1 current_count = CountNode.amount nohtml_str = "" i = 0 if node.is_leaf: for i in range(node.count): nohtml_str = nohtml_str + "<%d>%d|" % (i, node.keys[i]) nohtml_str = nohtml_str[:-1] g.node(str(current_count), nohtml(nohtml_str)) if father_count >= 0: g.edge("%d:%d" % (father_count, node_index), "%d:%d" % (current_count, node.count // 2)) else: for i in range(node.count): nohtml_str = nohtml_str + "<%d>|" % (2 * i) add_to_graph_recursively(g, node.children[i], current_count, 2 * i) if i < node.count - 1: nohtml_str = nohtml_str + "<%d>%d|" % (2 * i + 1, node.keys[i]) nohtml_str = nohtml_str[:-1] g.node(str(current_count), nohtml(nohtml_str)) if father_count >= 0: g.edge("%d:%d" % (father_count, node_index), "%d:%d" % (current_count, node.count - 1))
def report_symbol(self, dot, symbol, intermediates, anonymise=False): if isinstance(symbol, Variable): if symbol.name in intermediates: return symbol.name, intermediates[symbol.name] var_name = self.get_next('Var') if anonymise else symbol.name dot.node(symbol.name, nohtml(var_name), shape='plaintext', fontsize='10.0') return symbol.name, symbol.shape if isinstance(symbol, Constant): const_name = self.get_next('Const') dot.node(const_name, 'Const' if anonymise else str(symbol.value[0]), shape='oval', fontsize='10.0') return const_name, None if len(symbol.shape) == 1 else symbol.shape ids_and_shapes = [ self.report_symbol(dot, sym, intermediates, anonymise=anonymise) for sym in symbol.contents ] func_label = self.get_next( 'Op') if anonymise else symbol.__class__.__name__ dot.node(symbol.name, nohtml(func_label), shape='record') for child_id, shape in ids_and_shapes: dot.edge(child_id, symbol.name, xlabel=f'{str_shape(shape)}') return symbol.name, symbol.shape
def dot(self, g): if self.array_spec: g.node( str(self.id), nohtml( f'{self.line}) typeref | {self.name} @ {str(self.array_spec)}' )) else: g.node(str(self.id), nohtml(f'{self.line}) typeref | {self.name}')) _add_type_node(g, self)
def node(self, dot, node): if node.color: # 红黑 dot.node('%s' % (node.key), nohtml('<f0> |<f1> %s|<f2>' % (node.value)), color='%s' % (node.color)) else: # 普通 dot.node('%s' % (node.key), nohtml('<f0> |<f1> %s|<f2>' % (node.value)))
def create_binary_node(graph, name, label, size, free_vars, lchild, rchild, operation): global MAX_LABEL label = label[:MAX_LABEL] graph.node(name, label=format_node(name, size, free_vars), tooltip=label) graph.edge(name, lchild, label=graphviz.nohtml(operation), arrowhead="none") graph.edge(name, rchild, label=graphviz.nohtml(operation), arrowhead="none")
def Populate(self, g): nameBinary = f"{self.n:b}".zfill(self.maxLevel) if (self.nodeType == NodeType.START): g.node(self.NameBinary, nohtml(f"<f0> |<f1> α{self.n}|<f2>")) elif (self.nodeType == NodeType.END): g.node(self.NameBinary, nohtml(f"<f0> |<f1> ω{self.n}|<f2>")) elif (self.nodeType == NodeType.ROUTER): g.node(self.NameBinary, nohtml(f"<f0> |<f1> {self.NameBinary}|<f2>")) if not self.left is None: g.edge(f"{self.NameBinary}:f1", f"{self.left.NameBinary}:f2") if not self.right is None: g.edge(f"{self.NameBinary}:f1", f"{self.right.NameBinary}:f0")
def node_render(node, g): label = str(node.key) name = str(id(node)) if node.val: g.node(name, nohtml(f'<f0> {label}|<f1> {chr(node.val)}'), shape='record') else: g.node(name, nohtml(label)) if node.left: node_render(node.left, g) g.edge(name, str(id(node.left)), label='0') if node.right: node_render(node.right, g) g.edge(name, str(id(node.right)), label='1')
def render(self, dot, rootNode, label): if (rootNode.order() == 4): dot.node(str(label), nohtml('<r0> .|<v0> ' + str(rootNode.data[0]) + '|<r1> .|<v1> '+ str(rootNode.data[1]) +'|<r2> .|<v2> '+ str(rootNode.data[2]) +'|<r3> .')) if (rootNode.order() == 3): dot.node(str(label), nohtml('<r0> .|<v0> ' + str(rootNode.data[0]) + '|<r1> .|<v1> '+ str(rootNode.data[1]) +'|<r2> .')) if (rootNode.order() == 2): dot.node(str(label), nohtml('<r0> .|<v0> ' + str(rootNode.data[0]) + '|<r1> .')) rootLabel = label if (rootNode.children != None): for ci in range(len(rootNode.children)): dot.edge(str(rootLabel)+':r'+str(ci), str(label + 1)) label = self.render(dot, rootNode.children[ci], label + 1) return label
def graph(n, ch): dg = Digraph('btree', engine='dot', filename='btree.gv', node_attr={'shape': 'record'}) for i in range(0, int(n)): dg.node('node%d' % i, nohtml('<f0>|<f1> %c|<f2>' % (ord(ch) + i))) # 字符转换为数字 # dg.node('node1', nohtml('<f0>|<f1> B|<f2>')) # dg.node('node2', nohtml('<f0>|<f1> C|<f2>')) # dg.node('node3', nohtml('<f0>|<f1> D|<f2>')) # dg.node('node4', nohtml('<f0>|<f1> E|<f2>')) # dg.node('node5', nohtml('<f0>|<f1> F|<f2>')) # dg.node('node6', nohtml('<f0>|<f1> G|<f2>')) # dg.node('node7', nohtml('<f0>|<f1> H|<f2>')) # dg.node('node8', nohtml('<f0>|<f1> I|<f2>')) dg.edge('node0:f0', 'node1:f1') dg.edge('node0:f2', 'node2:f1') dg.edge('node1:f0', 'node3:f1') dg.edge('node1:f2', 'node4:f1') dg.edge('node2:f0', 'node5:f1') dg.edge('node2:f2', 'node6:f1') dg.edge('node3:f0', 'node7:f1') dg.edge('node3:f2', 'node8:f1') dg.view()
def value_list_2htmltab(value_list: list, leaf_pointer: list = [], is_leaf: bool = False): nonlocal tree_order tab_body = '' writed_block = 0 it = iter(leaf_pointer) for value in value_list: tab_body += f'<f{str(writed_block)}>' tab_body += value_str_short(value) if writed_block != tree_order - 1: tab_body += '|' writed_block += 1 for i in range(tree_order - writed_block): tab_body += f'<f{str(writed_block)}>' if writed_block != tree_order - 1: tab_body += '|' writed_block += 1 if is_leaf: tab_body += nohtml(r'|<next>Next-\>') return f'''{{{tab_body}}}'''
def report_expression(self, dot: Digraph, G: NNGraph, node: ExpressionFusionParameters, report_quantized=False, anonymise=False): if report_quantized: nid = NodeId(node) if not G.quantization or nid not in G.quantization: raise ValueError( f'requested drawing quantized expression {node.name} but it is not quantized' ) func_col = G.quantization[nid].cache['qfunc_col'] else: func_col = node.func_col intermediates = {} with dot.subgraph(name=f'cluster{node.name}', node_attr={'style': 'solid(dashed)'}) as sub: for var, func in func_col.functions.items(): node_id, shape = self.report_symbol(sub, func, intermediates, anonymise=anonymise) var_name = self.get_next('Var') if anonymise else var.name if node_id == var.name: intermediates[node_id] = shape else: dot.node(var.name, nohtml(var_name), shape='plaintext', fontsize='10.0') sub.edge(node_id, var.name, xlabel=f'{str_shape(shape)}') return [node.input_symbols, node.output_symbols]
def visualize(self, name="tree", dot=None, valueToText=lambda value: str(value)): render = dot is None if dot is None: dot = graphviz.Digraph(engine="dot", node_attr={ 'shape': 'record', 'height': '.1' }) dot.node( name, graphviz.nohtml("<f0> |<f1> " + valueToText(self.value) + " |<f2>")) if self.left: self.left.visualize(name + "l", dot, valueToText) dot.edge(name + ":f0", name + "l:f1", "True") if self.right: self.right.visualize(name + "r", dot, valueToText) dot.edge(name + ":f2", name + "r:f1", "False") if render: dot.render(name, view=True, cleanup=True)
def report_fusion(self, dot: Digraph, G: NNGraph, node: FusionBase, all_ports, fake_idx, nodes=None, all_dims=False, anonymise=False, expressions=False, qrecs=None): inputs_by_idx = sorted( node.subgraph.nodes(node_classes=FusionInputParameters), key=lambda x: x.idx) outputs_by_idx = sorted( node.subgraph.nodes(node_classes=FusionOutputParameters), key=lambda x: x.idx) input_symbols = [] output_symbols = [] for input_node in inputs_by_idx: dot.node(input_node.name, nohtml(input_node.name), shape='plaintext', fontsize='10.0') all_ports[input_node] = [[input_node.name], [input_node.name]] input_symbols.append(input_node.name) for output_node in outputs_by_idx: dot.node(output_node.name, nohtml(output_node.name), shape='plaintext', fontsize='10.0') all_ports[output_node] = [[output_node.name], [output_node.name]] output_symbols.append(output_node.name) with dot.subgraph(name=f'cluster{node.name}', node_attr={'style': 'solid(dashed)'}) as sub: self.report_graph(node.subgraph, dot, all_ports, fake_idx, all_dims=all_dims, anonymise=anonymise, expressions=expressions, qrecs=qrecs, parent=node) return [input_symbols, output_symbols]
def dot(self, g): g.node(str(self.id), nohtml(f'{self.line}) fncall | {self.refname} | <f0> (...)'), **self.nodestyle()) for a in self.args: a.dot(g) g.edge(f'{str(self.id)}:<f0>', str(a.id)) _add_type_node(g, self)
def dot(self, g): g.node(str(self.id), nohtml(f'<f0> | {self.line}) {type(self).NAME} | <f1>'), **self.nodestyle()) self.left.dot(g) self.right.dot(g) g.edge(f'{str(self.id)}:<f0>', str(self.left.id)) g.edge(f'{str(self.id)}:<f1>', str(self.right.id)) _add_type_node(g, self)
def test_nohtml(string, expected, expected_quoted): result = graphviz.nohtml(string) assert isinstance(result, str) assert isinstance(result, quoting.NoHtml) assert result == expected quoted = quoting.quote(result) assert isinstance(quoted, str) assert quoted == expected_quoted
def render_graph(self, bot_nodes, bot_edges, bot_user_name): self.graph = Digraph('g', filename=bot_user_name, node_attr={'shape': 'record'}, format='png') graph_string_nodes = self.create_nodes_render_string(bot_nodes) graph_string_edges = self.create_edge_render_list(bot_edges) for node, buttons in graph_string_nodes: self.graph.node(node, nohtml(buttons)) for from_box, dest_box in graph_string_edges: self.graph.edge(from_box, dest_box) pic_path = self.graph.render(filename=bot_user_name, view=False, format='png') return pic_path
def dot(self, g): g.node( str(self.id), nohtml( f'{self.line}) vardecl | {self.stage or "*"} | {self.name} | <f0> {"T" if self.typeref else "?"} | {str("?" if self.index is None else self.index)}' ), **self.nodestyle()) if self.typeref: self.typeref.dot(g) g.edge(f'{str(self.id)}:<f0>', str(self.typeref.id)) _add_type_node(g, self)
def crearnodos(self, arbol, f, dir): if arbol != None: strclave = str(arbol.clave) f.node(strclave, nohtml('<f0> |<f1> ' + strclave + '|<f2>')) if arbol.padre != None: if dir == 0: f.edge(str(arbol.padre.clave)+':<f0>', str(arbol.clave)+':<f1>') else: f.edge(str(arbol.padre.clave)+':<f2>', str(arbol.clave)+':<f1>') self.crearnodos(arbol.Izq, f, 0) self.crearnodos(arbol.Der, f, 1)
def plot_tree(graph, tree, column, node_graph): from graphviz import nohtml # plot the node tname = 'node-{}'.format(id(tree)) node_graph.node(tname, nohtml(str(tree.key)), group='g-{}'.format(column)) # plot subtrees for c in tree.children(): cname, column = plot_tree(graph, c, column, graph) graph.edge(tname, cname) column += 1 return tname, column
def showBTreeSubNodes(header, pages, start, g): childs = pages[start-1].get_tree_childs() g.node('node%d' % start, nohtml('<f%d> %d' % (start, start))) if(len(childs) > 0): print(childs) if not(len(pages[childs[0]-1].get_tree_childs()) > 0): childstring = "{<f%d> Leaves: | %d" % (childs[0], childs[0]) for i, c in enumerate(childs[1:]): if(((i+1) % 16) == 0): childstring += "| %d" % c else: childstring += ", %d" % c childstring += "}" print(childstring) g.node('node%d' % childs[0], nohtml(childstring)) g.edge('node%d:f%d' % (start, start), 'node%d:f%d' % (childs[0], childs[0])) return g for c in childs: showBTreeSubNodes(header, pages, c, g) return g
def showFreeList(header, pages): g = Digraph('g', filename='freelist.gv', node_attr={'shape': 'record', 'height': '.1'}) f = header.get_first_free_page()[0] if(f != 0 and len(FreeTrunkPage(pages[f-1].pagebytes).get_cells()) != 0): if(len(FreeTrunkPage(pages[f-1].pagebytes).get_cells()) > 30): g.node('node%d' % f, nohtml('{<f%d> %d | Trunkpage } | %d Leaves' % ( f, f, len(FreeTrunkPage(pages[f-1].pagebytes).get_cells())))) else: g.node('node%d' % f, nohtml('{<f%d> %d | Trunkpage } | %s' % ( f, f, FreeTrunkPage(pages[f-1].pagebytes).get_cells()))) else: g.node('node%d' % f, nohtml('<f%d> %d' % (f, f))) while(f != 0): print(f) nf = FreeTrunkPage(pages[f-1].pagebytes).get_next_trunk_page()[0] if(nf != 0): if(len(FreeTrunkPage(pages[f-1].pagebytes).get_cells()) > 30): g.node('node%d' % nf, nohtml('{<f%d> %d | Trunkpage } | %d Leaves' % ( nf, nf, len(FreeTrunkPage(pages[nf-1].pagebytes).get_cells())))) else: g.node('node%d' % nf, nohtml('{<f%d> %d | Trunkpage } | %s' % ( nf, nf, FreeTrunkPage(pages[nf-1].pagebytes).get_cells()))) else: g.node('node%d' % nf, nohtml('<f%d> %d' % (nf, nf))) g.edge('node%d:f%d' % (f, f), 'node%d:f%d' % (nf, nf)) f = nf g.view()
def make_graph(h): from graphviz import Digraph, nohtml g = Digraph(node_attr={'shape': 'record', 'height': '.1'}) for i, k in enumerate(h): g.node('node-{}'.format(i), nohtml("<f0>|<f1> {}|<f2>".format(k))) n = len(h) for i, k in enumerate(h): l, r = heap_node_children(i) if l < n: g.edge("node-{}:f0".format(i), "node-{}:f1".format(l)) if r < n: g.edge("node-{}:f2".format(i), "node-{}:f1".format(r)) return g
def dot(self, g, dot_reals=True): return_type_string = str(self['T']) if 'T' in self else '' g.node( str(self.id), nohtml( f'{{{self.line}) {self.name}| {{{self.param_rep()}}} | <f0> {return_type_string}}}' ), **self.nodestyle()) self.body.dot(g) g.edge(str(self.id) + ":<f0>", str(self.body.id)) if dot_reals: with g.subgraph(name="Cluster_" + self.name, graph_attr={'label': self.name}) as sg: self.dot_reals(sg)
def tree_to_graph(tree, node_id, name, dot): feature = "" if header is not None: feature = header[tree.feature[node_id]] else: feature = tree.feature[node_id] prediction = tree.value[node_id] if tree.n_outputs == 1: prediction = prediction[0, :] if isinstance(self.decisionTree, sklearn.ensemble.GradientBoostingRegressor): prediction = ', '.join([ str(x * self.decisionTree.learning_rate) for x in prediction ]) else: prediction = ', '.join([str(x) for x in prediction]) if tree.children_left[node_id] == sklearn.tree._tree.TREE_LEAF: value = self.decisionTree.criterion + ": " + str(tree.impurity[node_id]) + \ "\\nsamples: " + str(tree.n_node_samples[node_id]) + "\\nvalue: " + str(prediction) dot.node(name, graphviz.nohtml("<f0> |<f1> " + value + " |<f2>")) return value = feature + "\\n\\<= " + str(tree.threshold[node_id]) + "\\n" + self.decisionTree.criterion + \ ": " + str(tree.impurity[node_id]) + "\\nsamples: " + str(tree.n_node_samples[node_id]) + "\\nvalue: " + str(prediction) dot.node(name, graphviz.nohtml("<f0> |<f1> " + value + " |<f2>")) left_child = tree.children_left[node_id] tree_to_graph(tree, left_child, name + "l", dot) dot.edge(name + ":f0", name + "l:f1", "True") right_child = tree.children_right[node_id] tree_to_graph(tree, right_child, name + "r", dot) dot.edge(name + ":f2", name + "r:f1", "False")
def _render_graph(self, g): queue = [self.root] while queue: root = queue.pop(0) g.node('%s' % (id(root), ), nohtml(self._render_node_str(root))) if root.next is not None: g.edge('%s' % (id(root), ), '%s' % (id(root.next), ), constraint='false') for i, child in enumerate(root.children): if child is not None: queue.append(child) g.edge('%s:f%d' % (id(root), i * 2), '%s:f%d' % (id(child), self.order))
def crearnodos(self, arbol, f, dir,tipo=""): if tipo =="": if arbol != None: strclave = str(arbol.clave) f.node(strclave, nohtml('<f0> |<f1> ' + strclave + '|<f2>')) if arbol.padre != None: if dir == 0: f.edge(str(arbol.padre.clave)+':<f0>', str(arbol.clave)+':<f1>') else: f.edge(str(arbol.padre.clave)+':<f2>', str(arbol.clave)+':<f1>') self.crearnodos(arbol.Izq, f, 0) self.crearnodos(arbol.Der, f, 1) else: for index in range(0, len(arbol)): try: f.edge(str(arbol[index]),str(arbol[index+1])) except: pass
def create_node(g, i, node): if node not in memory.mf_dict: best_forms = [] else: forms = [x for x in memory.mf_dict[node].items()] forms.sort(key=operator.itemgetter(1), reverse=True) best_forms = [] best_score = forms[0][1] while len(forms) > 0 and forms[0][1] == best_score: best_forms.append(forms.pop(0)) form_counts = [ '{} {}/{}'.format( x[0], memory.meaning_stats[node]['use_counts'][x[0]], x[1]) if x[0] in memory.meaning_stats[node]['use_counts'] else '{} {}/{}'.format(x[0], 0, x[1]) for x in best_forms ] form = ', '.join(form_counts) g.node(str(i), nohtml('{} {}'.format(form, node.range)))
def view_graph(self, graph_name='bplustree'): if self.root.is_empty(): print('The B+ Tree is empty!') return g = Digraph('g', filename=graph_name + '.gv', node_attr={ 'shape': 'record', 'height': '.1' }) g.format = 'svg' queue = [self.root] while len(queue) > 0: node = queue.pop(0) if not isinstance(node, LeafNode): queue += node.values design = '' if isinstance(node, LeafNode): for i in range(len(node.keys)): design += '<f' + str(i * 2) + '> | {{ <f' + str( i * 2 + 1) + '> {' + str(i) + '} | {{' + ', '.join( map(str, node.values[i])) + '}} }}| ' design += '<f' + str(len(node.keys) * 2) + '>' else: for i in range(len(node.keys)): design += '<f' + str( i * 2) + '> | <f' + str(i * 2 + 1) + '> {' + str(i) + '} | ' design += '<f' + str(len(node.keys) * 2) + '>' g.node('node' + str(node.uid), nohtml(design.format(*node.keys))) if not isinstance(node, LeafNode): for i, value in enumerate(node.values): mid_key = len(value.keys) g.edge('node{}:f{}'.format(node.uid, i * 2), 'node{}:f{}'.format(value.uid, mid_key)) else: pass g.view()
def show_graph( root, emphasized_message=None, f='/tmp/graphviz', sim_num=1500, ): dot = Digraph( node_attr=NORMAL_NODE_ATTR, format='svg', ) dot.attr(bgcolor='transparent', ) nodes_queue = [(root, None, 0, 0)] node_i = 0 while nodes_queue: node_ident = str(node_i) node, parent_ident, child_num, level = nodes_queue.pop() wrapped_node = TreeNodeWrapper(node) color_perc = (wrapped_node.node.visits / sim_num)**0.3 start_color = 'black' if wrapped_node.message == emphasized_message: start_color = '0, 0.8, 0.8' dot.attr( 'node', dict(NORMAL_NODE_ATTR, **{'fillcolor': f'{start_color}:0.66, 0.8, {color_perc}'})) dot.node(node_ident, nohtml(node_formatter(wrapped_node))) if parent_ident is not None: dot.edge( parent_ident, node_ident, label='send' if child_num == 1 else 'drop', color=arrows_color, fontcolor=arrows_color, ) for i, child in enumerate(node.children): nodes_queue.append((child, node_ident, i, level + 1)) node_i += 1 dot.render(f, view=False)
def add_state_node(self, key, t): # Get id of node node_id, readable_name = self.graph.get_node_id(key, t) # Get all fields (values of b) for the node field_initial_values = self.graph.get_delta_at_t( key, t) # Make the node contain all fields (b) already fields_values_str = self.get_field_values_str(field_initial_values) # Create the node in the graph visualization #half = (0x004a0b00 - 0x004a0a00) / 2 #xpos = 0 if type(key) is X86ConstEnum else (key - 0x004a0a00 - half) xpos = 0 self.subgraph[t].node(node_id, nohtml("<name>%s|%s" % (readable_name, fields_values_str)), pos="%d,%d!" % (xpos, -t * 8)) #self.subgraph[t].node(node_id, nohtml("<name>%s|%s" % (readable_name, fields_values_str))) self.nodes.add(node_id)