我们从Python开源项目中,提取了以下39个代码示例,用于说明如何使用pydot.Node()。
def _to_pydot(self, graph): import pydot color = hash(self.head) & 0xffffff if not (color & 0x808080): color |= 0x808080 def new_leaf(leaf): node = pydot.Node(id(leaf), label=repr(leaf)) graph.add_node(node) return node subnodes = [kid._to_pydot(graph) if is_stree(kid) else new_leaf(kid) for kid in self.tail] node = pydot.Node(id(self), style="filled", fillcolor="#%x"%color, label=self.head) graph.add_node(node) for subnode in subnodes: graph.add_edge(pydot.Edge(node, subnode)) return node
def debugPlot(self, outfile): graph = pydot.Dot(graph_type='digraph') nodes = dict() for layerCfg in self.layerCfgs: node = pydot.Node(layerCfg.name) graph.add_node(node) nodes[layerCfg.name] = node for inp in layerCfg._inputs: if isinstance(inp,int): node = pydot.Node("input[{}]".format(inp)) graph.add_node(node) nodes[str(inp)] = node for layerCfg in self.layerCfgs: for inp in layerCfg._inputs: if isinstance(inp,int): inp = str(inp) edge = pydot.Edge(nodes[inp],nodes[layerCfg.name]) graph.add_edge(edge) graph.write_png(outfile)
def graph_from_idx_edges(edges): graph = pydot.Dot(graph_type = 'digraph') for edge in edges: idx1 = str(edge[0][0]) label1 = str(edge[0][1]) idx2 = str(edge[1][0]) label2 = str(edge[1][1]) graph.add_node(pydot.Node(idx1, label=label1)) graph.add_node(pydot.Node(idx2, label=label2)) graph.add_edge(pydot.Edge(idx1, idx2)) return graph
def render_node_graphviz (self, graph): ''' Render a node suitable for use in a Pydot graph using the set internal attributes. @type graph: pgraph.graph @param graph: Top level graph object containing the current node @rtype: pydot.Node @return: Pydot object representing node ''' import pydot dot_node = pydot.Node(self.id) dot_node.label = '<<font face="lucida console">%s</font>>' % self.label.rstrip("\r\n") dot_node.label = dot_node.label.replace("\\n", '<br/>') dot_node.shape = self.shape dot_node.color = "#%06x" % self.color dot_node.fillcolor = "#%06x" % self.color return dot_node ####################################################################################################################
def to_pydot(self): """Return a pydot digraph from a StepTree.""" graph = pydot.Dot(graph_type='digraph') # make sure Root shows up in single node trees if self.root_name: graph.add_node(pydot.Node(self._step_graph_label(self.root_name))) for parent, children in self.children.items(): for child in children: graph.add_edge(pydot.Edge( self._step_graph_label(parent), self._step_graph_label(child) )) return graph
def drawGraphFromSM2(SM, names, outFile, Cut): graph = pydot.Dot(graph_type='graph') # THRESHOLD SM: nonZeroMean = np.mean(SM[SM.nonzero()]) if Cut: T = 5.0 * nonZeroMean else: T = 0.0; for i in range(SM.shape[0]): for j in range(SM.shape[0]): if SM[i,j] <= T: SM[i,j] = 0.0 else: SM[i,j] = 1.0 numOfConnections = sum(SM, axis = 0) #fig = plt.figure(1) #plot1 = plt.imshow(SM, origin='upper', cmap=cm.gray, interpolation='nearest') #plt.show() numOfConnections = 9*numOfConnections / max(numOfConnections) for i,f in enumerate(names): if sum(SM[i,:])>0: fillColorCurrent = "{0:d}".format(int(ceil(numOfConnections[i]))) # NOTE: SEE http://www.graphviz.org/doc/info/colors.html for color schemes node = pydot.Node(f, style="filled", fontsize="8", shape="egg", fillcolor=fillColorCurrent, colorscheme = "reds9") graph.add_node(node) for i in range(len(names)): for j in range(len(names)): if i<j: if SM[i][j] > 0: #gr.add_edge((names[i], names[j])) edge = pydot.Edge(names[i], names[j]) graph.add_edge(edge) graph.write_png(outFile)
def addnodes(self, var): if var not in self.seen: if isinstance(var, Variable): value = '(' + (', ').join(['%d' % v for v in var.size()]) + ')' self.style_params["label"] = value self.dot.add_node(pydot.Node(str(id(var)), **self.style_params)) else: value = str(type(var).__name__) self.style_layers["label"] = value if value == "ConvNd": self.style_layers["fillcolor"] = "cyan" elif value == "BatchNorm": self.style_layers["fillcolor"] = "darkseagreen" elif value == "Threshold": self.style_layers["fillcolor"] = "crimson" self.style_layers["label"] = "ReLU" elif value == "Add": self.style_layers["fillcolor"] = "darkorchid" elif value == "AvgPool2d": self.style_layers["fillcolor"] = "gold" elif value == "Linear": self.style_layers["fillcolor"] = "chartreuse" elif value == "View": self.style_layers["fillcolor"] = "brown" else: self.style_layers["fillcolor"] = "aquamarine" self.dot.add_node(pydot.Node(str(id(var)), **self.style_layers)) self.seen.add(var) if hasattr(var, 'previous_functions'): for u in var.previous_functions: # if not isinstance(u[0], Variable): self.dot.add_edge(pydot.Edge(str(id(u[0])), str(id(var)))) self.addnodes(u[0])
def add_child(graph, child_id, child_label, parent_id, colour): """ http://www.graphviz.org/doc/info/shapes.html#polygon """ node = pydot.Node(child_id, style="filled", fillcolor=colour, label=child_label, shape="polygon", fontname=FONT) graph.add_node(node) graph.add_edge(pydot.Edge(parent_id, node))
def graph_from_edges_ex(edge_list, title, directed=False): if directed: graph = pydot.Dot(graph_type='digraph') else: graph = pydot.Dot(graph_type='graph') for edge in edge_list: id1 = str(edge[0][0]) label1 = str(edge[0][1]) id2 = str(edge[1][0]) label2 = str(edge[1][1]) # add node graph.add_node(pydot.Node(id1, label=label1)) graph.add_node(pydot.Node(id2, label=label2)) # add edege graph.add_edge(pydot.Edge(id1, id2)) graph.set_label( title ) return graph
def knock_44(Sent): tree_graph=pydot.Dot(graph_type='digraph',fontname="Microsoft YaHei") nodes=[] for ind, p in enumerate(Sent.phrases): nodes.append(pydot.Node(p.str,fontname="Microsoft YaHei")) tree_graph.add_node(nodes[ind]) for ind, p in enumerate(Sent.phrases): if p.father!=-1: tree_graph.add_edge(pydot.Edge(nodes[ind],nodes[p.father])) tree_graph.write_png("knock_44.png") # In[152]:
def visualize_tree(f_name,number_trees=3): tree = etree.parse(f_name) root = tree.getroot() sentences = root.xpath("/root/document/sentences/sentence") for idx, sentence in enumerate(sentences): tree_graph = pydot.Dot(graph_type='digraph') basic_dependencies = sentence.xpath( "./dependencies[@type='basic-dependencies']/dep") for dep in basic_dependencies: father = "{no}_{word}".format( no=dep[0].get("idx"), word=dep[0].text) child = "{no}_{word}".format( no=dep[1].get("idx"), word=dep[1].text) tree_graph.add_node(pydot.Node(father)) tree_graph.add_node(pydot.Node(child)) tree_graph.add_edge(pydot.Edge(child, father)) tree_graph.write_png("sentence_{no}.png".format(no=idx)) if idx > number_trees-1: break # In[91]:
def create_node(self, name, isleaf=False): if isleaf: node = pydot.Node(name, style="filled", fillcolor="red") else: node = pydot.Node(name, style="filled", fillcolor="green") self.graph.add_node(node) return node
def get_node(self, name, isleaf=False): """creates or returns (if the node already exists) the node :param isleaf: Defines wether this Node has further children :param name: Name of node """ # check all of the graph nodes for node in self.graph.get_nodes(): if name == node.get_name(): return node return self.create_node(name, isleaf=isleaf)
def add_edge(self, a, b): """checks if the edge already exists, if not, creates one from a2b :param a: Node a :param b: Node b """ if a is None or b is None: return for edge_obj in self.graph.get_edge_list(): if a.get_name() in edge_obj.obj_dict["points"] and \ b.get_name() in edge_obj.obj_dict["points"]: break else: # such an edge doesn't exist. create it self.graph.add_edge(pydot.Edge(a, b))
def pydot__tree_to_png(tree, filename): import pydot graph = pydot.Dot(graph_type='digraph', rankdir="LR") i = [0] def new_leaf(leaf): node = pydot.Node(i[0], label=repr(leaf)) i[0] += 1 graph.add_node(node) return node def _to_pydot(subtree): color = hash(subtree.data) & 0xffffff color |= 0x808080 subnodes = [_to_pydot(child) if isinstance(child, Tree) else new_leaf(child) for child in subtree.children] node = pydot.Node(i[0], style="filled", fillcolor="#%x"%color, label=subtree.data) i[0] += 1 graph.add_node(node) for subnode in subnodes: graph.add_edge(pydot.Edge(node, subnode)) return node _to_pydot(tree) graph.write_png(filename)
def plot(model, to_file='model.png'): graph = pydot.Dot(graph_type='digraph') if type(model) == Sequential: previous_node = None written_nodes = [] n = 1 for node in model.get_config()['layers']: # append number in case layers have same name to differentiate if (node['name'] + str(n)) in written_nodes: n += 1 current_node = pydot.Node(node['name'] + str(n)) written_nodes.append(node['name'] + str(n)) graph.add_node(current_node) if previous_node: graph.add_edge(pydot.Edge(previous_node, current_node)) previous_node = current_node graph.write_png(to_file) elif type(model) == Graph: # don't need to append number for names since all nodes labeled for input_node in model.input_config: graph.add_node(pydot.Node(input_node['name'])) # intermediate and output nodes have input defined for layer_config in [model.node_config, model.output_config]: for node in layer_config: graph.add_node(pydot.Node(node['name'])) # possible to have multiple 'inputs' vs 1 'input' if node['inputs']: for e in node['inputs']: graph.add_edge(pydot.Edge(e, node['name'])) else: graph.add_edge(pydot.Edge(node['input'], node['name'])) graph.write_png(to_file)
def createModulesGraph(moddb): graph = pydot.Dot(graph_type='digraph') nodes = dict() # add modules as nodes for mod in moddb.getModules(): tt = ", ".join(mod.provides) + " " node = pydot.Node(mod.name, tooltip=tt) nodes[mod.name] = node graph.add_node(node) # add directed edges from modules to modules that satisfy at least one dependency for src in moddb.getModules(): dstmods = moddb.getSolutionCandidates(src) for dst in dstmods: tt = ", ".join(dstmods[dst]) + " " edge = pydot.Edge(src.name, dst.name, tooltip=tt) graph.add_edge(edge) # add special directed edges for "modules" inclusion for src in moddb.getModules(): for dstname in src.modules: dst = moddb[dstname] edge = pydot.Edge(src.name, dst.name, color="green") graph.add_edge(edge) # add undirected edges for conflicts for src in moddb.getModules(): conflicts = moddb.getConflictingModules(src) for dst in conflicts: if (dst.name < src.name): tt = ", ".join(conflicts[dst]) + " " edge = pydot.Edge(src.name, dst.name, color="red", dir="none", tooltip=tt) graph.add_edge(edge) graph.write('dependencies.dot') # TODO rewrite as mako template and drop pydot dependency
def add_elements(g, toks, defaults_graph=None, defaults_node=None, defaults_edge=None): if defaults_graph is None: defaults_graph = {} if defaults_node is None: defaults_node = {} if defaults_edge is None: defaults_edge = {} for elm_idx, element in enumerate(toks): if isinstance(element, (pydot.Subgraph, pydot.Cluster)): add_defaults(element, defaults_graph) g.add_subgraph(element) elif isinstance(element, pydot.Node): add_defaults(element, defaults_node) g.add_node(element) elif isinstance(element, pydot.Edge): add_defaults(element, defaults_edge) g.add_edge(element) elif isinstance(element, ParseResults): for e in element: add_elements(g, [e], defaults_graph, defaults_node, defaults_edge) elif isinstance(element, DefaultStatement): if element.default_type == 'graph': default_graph_attrs = pydot.Node('graph', **element.attrs) g.add_node(default_graph_attrs) elif element.default_type == 'node': default_node_attrs = pydot.Node('node', **element.attrs) g.add_node(default_node_attrs) elif element.default_type == 'edge': default_edge_attrs = pydot.Node('edge', **element.attrs) g.add_node(default_edge_attrs) defaults_edge.update(element.attrs) else: raise ValueError, "Unknown DefaultStatement: %s " % element.default_type elif isinstance(element, P_AttrList): g.obj_dict['attributes'].update(element.attrs) else: raise ValueError, "Unknown element statement: %r" % element
def push_node_stmt(s, loc, toks): if len(toks) == 2: attrs = toks[1].attrs else: attrs = {} node_name = toks[0] if isinstance(node_name, list) or isinstance(node_name, tuple): if len(node_name)>0: node_name = node_name[0] n = pydot.Node(str(node_name), **attrs) return n
def model_picture(model, to_file='local/model.png'): graph = pydot.Dot(graph_type='digraph') if isinstance(model,Sequential): previous_node = None written_nodes = [] n = 1 for node in model.get_config()['layers']: # append number in case layers have same name to differentiate if (node['name'] + str(n)) in written_nodes: n += 1 current_node = pydot.Node(node['name'] + str(n)) written_nodes.append(node['name'] + str(n)) graph.add_node(current_node) if previous_node: graph.add_edge(pydot.Edge(previous_node, current_node)) previous_node = current_node graph.write_png(to_file) elif isinstance(model,Graph): # don't need to append number for names since all nodes labeled for input_node in model.input_config: graph.add_node(pydot.Node(input_node['name'])) # intermediate and output nodes have input defined for layer_config in [model.node_config, model.output_config]: for node in layer_config: graph.add_node(pydot.Node(node['name'])) # possible to have multiple 'inputs' vs 1 'input' if node['inputs']: for e in node['inputs']: graph.add_edge(pydot.Edge(e, node['name'])) else: graph.add_edge(pydot.Edge(node['input'], node['name'])) graph.write_png(to_file)
def create_schema_graph(tables=None, metadata=None, show_indexes=True, show_datatypes=True, font="Bitstream-Vera Sans", concentrate=True, relation_options={}, rankdir='TB'): relation_kwargs = { 'fontsize':"7.0" } relation_kwargs.update(relation_options) if metadata is None and tables is not None and len(tables): metadata = tables[0].metadata elif tables is None and metadata is not None: if not len(metadata.tables): metadata.reflect() tables = list(metadata.tables.values()) else: raise ValueError("You need to specify at least tables or metadata") graph = pydot.Dot(prog="dot",mode="ipsep",overlap="ipsep",sep="0.01",concentrate=str(concentrate), rankdir=rankdir) for table in tables: graph.add_node(pydot.Node(str(table.name), shape="plaintext", label=_render_table_html(table, metadata, show_indexes, show_datatypes), fontname=font, fontsize="7.0" )) for table in tables: for fk in table.foreign_keys: if fk.column.table not in tables: continue edge = [table.name, fk.column.table.name] is_inheritance = fk.parent.primary_key and fk.column.primary_key if is_inheritance: edge = edge[::-1] graph_edge = pydot.Edge( headlabel="+ %s"%fk.column.name, taillabel='+ %s'%fk.parent.name, arrowhead=is_inheritance and 'none' or 'odot' , arrowtail=(fk.parent.primary_key or fk.parent.unique) and 'empty' or 'crow' , fontname=font, #samehead=fk.column.name, sametail=fk.parent.name, *edge, **relation_kwargs ) graph.add_edge(graph_edge) # not sure what this part is for, doesn't work with pydot 1.0.2 # graph_edge.parent_graph = graph.parent_graph # if table.name not in [e.get_source() for e in graph.get_edge_list()]: # graph.edge_src_list.append(table.name) # if fk.column.table.name not in graph.edge_dst_list: # graph.edge_dst_list.append(fk.column.table.name) # graph.sorted_graph_elements.append(graph_edge) return graph
def model_to_dot(model, show_shapes=False, show_layer_names=True): dot = pydot.Dot() dot.set('rankdir', 'TB') dot.set('concentrate', True) dot.set_node_defaults(shape='record') if isinstance(model, Sequential): if not model.built: model.build() model = model.model layers = model.layers # Create graph nodes. for layer in layers: layer_id = str(id(layer)) # Append a wrapped layer's label to node's label, if it exists. layer_name = layer.name class_name = layer.__class__.__name__ if isinstance(layer, Wrapper): layer_name = '{}({})'.format(layer_name, layer.layer.name) child_class_name = layer.layer.__class__.__name__ class_name = '{}({})'.format(class_name, child_class_name) # Create node's label. if show_layer_names: label = '{}: {}'.format(layer_name, class_name) else: label = class_name # Rebuild the label as a table including input/output shapes. if show_shapes: try: outputlabels = str(layer.output_shape) except: outputlabels = 'multiple' if hasattr(layer, 'input_shape'): inputlabels = str(layer.input_shape) elif hasattr(layer, 'input_shapes'): inputlabels = ', '.join( [str(ishape) for ishape in layer.input_shapes]) else: inputlabels = 'multiple' label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label, inputlabels, outputlabels) node = pydot.Node(layer_id, label=label) dot.add_node(node) # Connect nodes with edges. for layer in layers: layer_id = str(id(layer)) for i, node in enumerate(layer.inbound_nodes): node_key = layer.name + '_ib-' + str(i) if node_key in model.container_nodes: for inbound_layer in node.inbound_layers: inbound_layer_id = str(id(inbound_layer)) layer_id = str(id(layer)) dot.add_edge(pydot.Edge(inbound_layer_id, layer_id)) return dot
def write_hypergraph(hgr, colored = False): """ Return a string specifying the given hypergraph in DOT Language. @type hgr: hypergraph @param hgr: Hypergraph. @type colored: boolean @param colored: Whether hyperedges should be colored. @rtype: string @return: String specifying the hypergraph in DOT Language. """ dotG = pydot.Dot() if not 'name' in dir(hgr): dotG.set_name('hypergraph') else: dotG.set_name(hgr.name) colortable = {} colorcount = 0 # Add all of the nodes first for node in hgr.nodes(): newNode = pydot.Node(str(node), hyper_node_type = 'hypernode') dotG.add_node(newNode) for hyperedge in hgr.hyperedges(): if (colored): colortable[hyperedge] = colors[colorcount % len(colors)] colorcount += 1 newNode = pydot.Node(str(hyperedge), hyper_node_type = 'hyperedge', \ color = str(colortable[hyperedge]), \ shape = 'point') else: newNode = pydot.Node(str(hyperedge), hyper_node_type = 'hyperedge') dotG.add_node(newNode) for link in hgr.links(hyperedge): newEdge = pydot.Edge(str(hyperedge), str(link)) dotG.add_edge(newEdge) return dotG.to_string()
def create_graph(self): g = pydot.Dot(graph_type='digraph') node_map = {} for h in self.network.topology.hosts: label = "<<B>%s</B><br/>%d %d<br/>%d>" % (h.name, h.receiving_cap, h.sending_cap, h.amp_factor) n = pydot.Node(h.name, label=label, style='filled', margin=-0.8, width=0.5, height=0.5, fontname=self.font_name, fontsize=self.node_fontsize) if type(h) is Server: if self.network.victims and h in self.network.victims: n.set_shape('doublecircle') else: n.set_shape('Mcircle') n.set_fillcolor(self.server_color) elif type(h) is Router: if self.network.victims and h in self.network.victims: n.set_shape('doubleoctagon') else: n.set_shape('octagon') n.set_fillcolor(self.server_color) else: if self.network.victims and h in self.network.victims: n.set_shape('doublecircle') else: n.set_shape('circle') if self.network.attackers and h in self.network.attackers: n.set_fillcolor(self.attacker_color) else: n.set_fillcolor(self.host_color) g.add_node(n) node_map[h] = n for l in self.network.topology.links: v1 = node_map[l.h1] v2 = node_map[l.h2] e = pydot.Edge(v1, v2, dir='none', label=str(l.capacity), color=self.link_color, fontcolor=self.link_color, fontname=self.font_name, fontsize=self.label_size) g.add_edge(e) if self.network.flows: f1 = sum([f.get(l.h1, l.h2) for f in self.network.flows]) f2 = sum([f.get(l.h2, l.h1) for f in self.network.flows]) if f1 > 0: g.add_edge(self.__create_link_flow(v1, v2, f1)) if f2 > 0: g.add_edge(self.__create_link_flow(v2, v1, f2)) return g
def main(gviz_path, layer_only=False): graphviz_setup(gviz_path) graph = pydot.Dot(graph_type='digraph', rankdir="TB") layer_children = {'CLASS': {'LABEL': {'STYLE': {}}, 'LEADER': {'STYLE': {}}, 'STYLE': {}, 'VALIDATION': {}}, 'CLUSTER': {}, 'COMPOSITE': {}, 'FEATURE': {'POINTS': {}}, 'GRID': {}, 'JOIN': {}, 'METADATA': {}, 'PROJECTION': {}, 'SCALETOKEN': {'VALUES': {}}, 'VALIDATION': {}} #pprint.pprint(layer_children) classes = { "MAP": {"LAYER": layer_children, 'LEGEND': {'LABEL': {}}, 'PROJECTION': {}, 'QUERYMAP': {}, 'REFERENCE': {}, 'SCALEBAR': {'LABEL': {}}, 'SYMBOL': {}, 'WEB': {'METADATA': {}, 'VALIDATION': {}}}} if layer_only: root = "LAYER" classes = classes["MAP"] fn = "layer_classes" else: fn = "map_classes" root, = classes.keys() node = pydot.Node(root, style="filled", fillcolor="#33a333", label=root, fontname=FONT, shape="polygon") graph.add_node(node) add_children(graph, root, classes[root]) save_file(graph, fn)
def render(self, filename, label='', labelloc='t', labeljust='c', rankdir="TB", ranksep=0.7, fontname='Arial', fontsize=24, use_urls=False, node_fixedsize='true', nodesep=0.1, node_width=0.85, node_height=0.85, node_fontsize=15, include_ids=False): import pydot # Graph graph_args = { "rankdir": rankdir, "ranksep": ranksep, "nodesep": nodesep, "fontname": fontname, "fontsize": fontsize, } if label: graph_args.update({ "labelloc": labelloc, "labeljust": labeljust, "label": label }) graph = pydot.Dot(**graph_args) # Node node_args = { "fontsize": node_fontsize, } if use_urls: node_seed_shape = 'rectangle' node_shape = 'oval' else: node_seed_shape = 'square' node_shape = 'circle' node_args.update({ "fixedsize": node_fixedsize, "width": node_width, "height": node_height, }) graph.set_node_defaults(**node_args) for page in self.pages: graph.add_node(pydot.Node(name=self._clean_page_name(page, include_id=include_ids), fontname=fontname, fontsize=node_fontsize, shape=node_seed_shape if page.is_seed else node_shape)) for link in page.links: graph.add_edge(pydot.Edge(self._clean_page_name(page, include_id=include_ids), self._clean_page_name(link, include_id=include_ids))) graph.write_png(filename)
def createConfigurationGraph(modules, selectedmods, moddb, filename): graph = pydot.Dot(graph_name="G", graph_type='digraph') nodes = dict() # add modules as nodes for mod in modules: tt = ", ".join(mod.provides) + " " if mod.name in selectedmods: fc = "#BEF781" else: fc = "white" if moddb.getConflictingModules(mod): nc = "#DF0101" else: nc = "black" node = pydot.Node(mod.name, tooltip=tt, style='filled', fillcolor=fc, color=nc, fontcolor=nc) # node = pydot.Node(mod.name) nodes[mod.name] = node graph.add_node(node) # add directed edges from modules to modules that satisfy at least one dependency for src in modules: dstmods = moddb.getSolutionCandidates(src) #print(str(src) + ' --> ' + str(dstmods)) # don't show modules that are not in 'modules' for dst in dstmods: if dst not in modules: continue tt = ", ".join(dstmods[dst]) + " " edge = pydot.Edge(src.name, dst.name, tooltip=tt) # edge = pydot.Edge(src.name, dst.name) graph.add_edge(edge) # add special directed edges for "modules" inclusion for src in modules: for dstname in src.modules: dst = moddb[dstname] edge = pydot.Edge(src.name, dst.name, color="green") graph.add_edge(edge) graph.write(filename)
def push_edge_stmt(str, loc, toks): tok_attrs = [a for a in toks if isinstance(a, P_AttrList)] attrs = {} for a in tok_attrs: attrs.update(a.attrs) e = [] if isinstance(toks[0][0], pydot.Graph): n_prev = pydot.frozendict(toks[0][0].obj_dict) else: n_prev = toks[0][0] + do_node_ports( toks[0] ) if isinstance(toks[2][0], ParseResults): n_next_list = [[n.get_name(),] for n in toks[2][0] ] for n_next in [n for n in n_next_list]: n_next_port = do_node_ports(n_next) e.append(pydot.Edge(n_prev, n_next[0]+n_next_port, **attrs)) elif isinstance(toks[2][0], pydot.Graph): e.append(pydot.Edge(n_prev, pydot.frozendict(toks[2][0].obj_dict), **attrs)) elif isinstance(toks[2][0], pydot.Node): node = toks[2][0] if node.get_port() is not None: name_port = node.get_name() + ":" + node.get_port() else: name_port = node.get_name() e.append(pydot.Edge(n_prev, name_port, **attrs)) elif isinstance(toks[2][0], type('')): for n_next in [n for n in tuple(toks)[2::2]]: if isinstance(n_next, P_AttrList) or not isinstance(n_next[0], type('')): continue n_next_port = do_node_ports( n_next ) e.append(pydot.Edge(n_prev, n_next[0]+n_next_port, **attrs)) n_prev = n_next[0]+n_next_port else: # UNEXPECTED EDGE TYPE pass return e
def model_to_dot(model, show_shapes=False, show_layer_names=True): dot = pydot.Dot() dot.set('rankdir', 'TB') dot.set('concentrate', True) dot.set_node_defaults(shape='record') if model.__class__.__name__ == 'Sequential': if not model.built: model.build() model = model.model layers = model.layers # first, populate the nodes of the graph for layer in layers: layer_id = str(id(layer)) if show_layer_names: label = str(layer.name) + ' (' + layer.__class__.__name__ + ')' else: label = layer.__class__.__name__ if show_shapes: # Build the label that will actually contain a table with the # input/output try: outputlabels = str(layer.output_shape) except: outputlabels = 'multiple' if hasattr(layer, 'input_shape'): inputlabels = str(layer.input_shape) elif hasattr(layer, 'input_shapes'): inputlabels = ', '.join( [str(ishape) for ishape in layer.input_shapes]) else: inputlabels = 'multiple' label = '%s\n|{input:|output:}|{{%s}|{%s}}' % (label, inputlabels, outputlabels) node = pydot.Node(layer_id, label=label) dot.add_node(node) # second, add the edges for layer in layers: layer_id = str(id(layer)) for i, node in enumerate(layer.inbound_nodes): node_key = layer.name + '_ib-' + str(i) if node_key in model.container_nodes: # add edges for inbound_layer in node.inbound_layers: inbound_layer_id = str(id(inbound_layer)) layer_id = str(id(layer)) dot.add_edge(pydot.Edge(inbound_layer_id, layer_id)) return dot
def generate(self): if not self.__schema: return None for database in self.__schema: for table in self.__schema[database]: graph = pydot.Dot(graph_type='digraph') main_node_name = "{}.{}".format(database, table['table']) graph.add_node( pydot.Node(main_node_name, style="filled", fillcolor="#CCCCCC") ) for field in table['fields']: if not field['metadata']: continue if 'reference' not in field['metadata']: continue related_table = "{}.{}".format( field['metadata']['reference']['database'], field['metadata']['reference']['table'] ) graph.add_node( pydot.Node(related_table, style="filled", fillcolor="#FFFFFF") ) # add relationship relationship = "{} -> {}".format( field['name'], field['metadata']['reference']['field'] ) graph.add_edge( pydot.Edge( main_node_name, related_table, label=relationship ) ) full_path = os.path.join( self.__output_path, 'graph_img' ) if not os.path.exists(full_path): os.makedirs(full_path) final_path = os.path.join( full_path, '{}.png'.format(main_node_name) ) graph.write_png(final_path) graph = None