def render_fm_tree(root_node, filename='tree.png'): # Function to format nodes in FM tree def format_box(node): # https://graphviz.org/doc/info/shapes.html if node.level_id == 0: # Item Level Node return "fixedsize=false, shape=rect, fillcolor=lightgrey, style=filled" else: if not node.policy_tc: # Error? missing policy_tc entry for this Node return "fixedsize=false, shape=ellipse, fillcolor=pink, style=filled" elif len(node.policy_tc) > 1: # Node with multiple layers return "fixedsize=false, shape=rect, fillcolor=orange, style=filled" else: # Cov or loc nodes return "fixedsize=false, shape=ellipse, fillcolor=lightblue, style=filled" # Function to add weighted 'by layer number' edges def layered_edge(node, child): # https://anytree.readthedocs.io/en/latest/tricks/weightededges.html if hasattr(child, 'layer_max'): if child.layer_max > 1: return 'dir=back, style=bold, label=" {} Layers"'.format(child.layer_max) return "dir=back" # Render tree to png dot_data = DotExporter( root_node, edgeattrfunc=layered_edge, nodeattrfunc=format_box) dot_data.to_picture(filename)
def render_tree(opts, tree): """Render tree in ASCII and graphviz""" with codecs.open( f"{opts.output_dir}/{constants.FEATURE_IMPORTANCE_HIERARCHY}.txt", "w", encoding="utf8") as txt_file: for pre, _, node in anytree.RenderTree(tree): txt_file.write("%s%s: %s (%s: %s)\n" % (pre, node.name, node.description.title(), opts.effect_name, str(node.effect_size))) graph_options = [ ] # Example: graph_options = ["dpi=300.0;", "style=filled;", "bgcolor=yellow;"] exporter = DotExporter(tree, options=graph_options, nodeattrfunc=lambda node: nodeattrfunc(opts, node)) exporter.to_dotfile( f"{opts.output_dir}/{constants.FEATURE_IMPORTANCE_HIERARCHY}.dot") try: output_filename = f"{opts.output_dir}/{constants.FEATURE_IMPORTANCE_HIERARCHY}.png" exporter.to_picture(output_filename) print(f"Feature importance visualization: {output_filename}") except FileNotFoundError: print( "Feature importance visualization: error during tree rendering. Is Graphviz installed on your system?" )
def export(self, full=False): """ Export tree into graphviz format in both *.dot file and image *.png file Parameters ---------- full: boolean value, if True also the regex mappings are printed in the resulting file """ def nodeattr_fn(node): return f'style=filled color={COLOR_SCHEME[node.depth]}' def nodename_fn(node): nl = "\n" name = node.name if hasattr(node, "regex"): name = f""" {node.name} ----------- {nl.join(node.regex)} """ return name root = self.nodes[self.root_key] dot = DotExporter( root, nodenamefunc=nodename_fn if full else None, nodeattrfunc=nodeattr_fn, options=[ 'graph [layout = dot, ranksep="1.5", nodesep="0.7"]', 'rankdir ="LR"', 'node [fontname="Arial"]' ] ) dot.to_dotfile(f"./{self.root_key}.dot") dot.to_picture(f"./{self.root_key}.png")
def print_node(node): node.update_parent() root = AnyTreeVisitor().visit(node) for pre, fill, node in RenderTree(root): print('%s%s' % (pre, node.name)) # export graphviz files exporter = DotExporter(root) exporter.to_dotfile('graphviz.dot') exporter.to_picture('graphviz.png')
def export_tree(self, filename: str) -> None: """Exports a graphical representation of the DSL tree. This method can be called from any tree node. The tree root will always be used. Parameters: filename: Name for the exported image file. """ from anytree.exporter import DotExporter exporter = DotExporter(self.root, nodenamefunc=lambda node: node._name_func(), nodeattrfunc=lambda node: node._shape()) exporter.to_picture(filename)
def export_page_tree(fig: DotExporter, file_name: str) -> None: img_name = file_name.replace('.csv', '.svg') fig.to_picture(img_name) st.write(f"exported page tree image to '{img_name}'")