Python pydot 模块,Edge() 实例源码

我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用pydot.Edge()

项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def draw(self, name, dname, draw_branches=True):
        from pydot import Dot, Edge
        g = Dot()
        g.set_node_defaults(color='lightgray', style='filled', shape='box',
                            fontname='Courier', fontsize='10')
        for node in sorted(self.nodes, key=lambda x: x.num):
            if draw_branches and node.type.is_cond:
                g.add_edge(Edge(str(node), str(node.true), color='green'))
                g.add_edge(Edge(str(node), str(node.false), color='red'))
            else:
                for suc in self.sucs(node):
                    g.add_edge(Edge(str(node), str(suc), color='blue'))
            for except_node in self.catch_edges.get(node, []):
                g.add_edge(Edge(str(node), str(except_node),
                                color='black', style='dashed'))

        g.write_png('%s/%s.png' % (dname, name))
项目:android_malware_detection    作者:congyuandong    | 项目源码 | 文件源码
def draw(self, name, dname, draw_branches=True):
        from pydot import Dot, Edge
        g = Dot()
        g.set_node_defaults(color='lightgray', style='filled', shape='box',
                            fontname='Courier', fontsize='10')
        for node in sorted(self.nodes, key=lambda x: x.num):
            if draw_branches and node.type.is_cond:
                g.add_edge(Edge(str(node), str(node.true), color='green'))
                g.add_edge(Edge(str(node), str(node.false), color='red'))
            else:
                for suc in self.sucs(node):
                    g.add_edge(Edge(str(node), str(suc), color='blue'))
            for except_node in self.catch_edges.get(node, []):
                g.add_edge(Edge(str(node), str(except_node),
                                color='black', style='dashed'))

        g.write_png('%s/%s.png' % (dname, name))
项目:SpaghettIDE    作者:Bleu-royal    | 项目源码 | 文件源码
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
项目:ObjRecPoseEst    作者:paroj    | 项目源码 | 文件源码
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)
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
def make_corenlp_tree(data_in_path, data_out_path, num_sentence):
    tree = ET.parse(data_in_path)
    root = tree.getroot()
    i = 0
    for dependencies in root.findall(".//dependencies"):
        if dependencies.get('type') != 'collapsed-dependencies':
            continue
        i += 1
        if i != num_sentence:
            continue
        graph = pydot.Dot()
        graph.set_type('digraph')
        for dep in dependencies.findall('dep'):
            from_edge = dep.find('governor').text + ' _ ' + dep.find('governor').get('idx')
            to_edge = dep.find('dependent').text + ' _ ' + dep.find('dependent').get('idx')
            if to_edge in ',':
                to_edge = r'\,'
            graph.add_edge(pydot.Edge(from_edge, to_edge))
        graph.write_jpeg(data_out_path)
        break
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def draw(self, name, dname, draw_branches=True):
        from pydot import Dot, Edge
        g = Dot()
        g.set_node_defaults(color='lightgray', style='filled', shape='box',
                            fontname='Courier', fontsize='10')
        for node in sorted(self.nodes, key=lambda x: x.num):
            if draw_branches and node.type.is_cond:
                g.add_edge(Edge(str(node), str(node.true), color='green'))
                g.add_edge(Edge(str(node), str(node.false), color='red'))
            else:
                for suc in self.sucs(node):
                    g.add_edge(Edge(str(node), str(suc), color='blue'))
            for except_node in self.catch_edges.get(node, []):
                g.add_edge(Edge(str(node), str(except_node),
                                color='black', style='dashed'))

        g.write_png('%s/%s.png' % (dname, name))
项目:DroidWatcher    作者:suemi994    | 项目源码 | 文件源码
def draw(self, name, dname, draw_branches=True):
        from pydot import Dot, Edge
        g = Dot()
        g.set_node_defaults(color='lightgray', style='filled', shape='box',
                            fontname='Courier', fontsize='10')
        for node in sorted(self.nodes, key=lambda x: x.num):
            if draw_branches and node.type.is_cond:
                g.add_edge(Edge(str(node), str(node.true), color='green'))
                g.add_edge(Edge(str(node), str(node.false), color='red'))
            else:
                for suc in self.sucs(node):
                    g.add_edge(Edge(str(node), str(suc), color='blue'))
            for except_node in self.catch_edges.get(node, []):
                g.add_edge(Edge(str(node), str(except_node),
                                color='black', style='dashed'))

        g.write_png('%s/%s.png' % (dname, name))
项目:guacml    作者:guacml    | 项目源码 | 文件源码
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
项目:pyOpenAireTextClassifier    作者:tyiannak    | 项目源码 | 文件源码
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)
项目:ExperimentPackage_PyTorch    作者:ICEORY    | 项目源码 | 文件源码
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])
项目:smtdos    作者:joelwanner    | 项目源码 | 文件源码
def __create_link_flow(self, h1, h2, f):
        e = pydot.Edge(h1, h2)
        e.set_fontname(self.font_name)
        e.set_fontsize(self.label_size)

        if f % 1 == 0:  # integer flow
            e.set_label(str(f))
        else:
            e.set_label("%.2f" % f)

        e.set_fontcolor(self.accent_color)
        e.set_color(self.accent_color)

        return e
项目:keras-recommendation    作者:sonyisme    | 项目源码 | 文件源码
def add_edge(self, f, t, graph):
        if f: graph.add_edge(pydot.Edge(f, t))
        return t
项目:mappyfile    作者:geographika    | 项目源码 | 文件源码
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))
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:100knock2017    作者:tmu-nlp    | 项目源码 | 文件源码
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]:
项目:mrt_tools    作者:KIT-MRT    | 项目源码 | 文件源码
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))
项目:python_gray_8_9_11_12    作者:3xp10it    | 项目源码 | 文件源码
def __init__ (self, src, dst):
        '''
        Class constructor.

        @type  src: Mixed
        @param src: Edge source
        @type  dst: Mixed
        @param dst: Edge destination
        '''

        # the unique id for any edge (provided that duplicates are not allowed) is the combination of the source and
        # the destination stored as a long long.
        self.id  = (src << 32) + dst
        self.src = src
        self.dst = dst

        # general graph attributes.
        self.color = 0x000000
        self.label = ""

        # gml relevant attributes.
        self.gml_arrow       = "none"
        self.gml_stipple     = 1
        self.gml_line_width  = 1.0


    ####################################################################################################################
项目:python_gray_8_9_11_12    作者:3xp10it    | 项目源码 | 文件源码
def render_edge_graphviz (self, graph):
        '''
        Render an edge 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 edge

        @rtype:  pydot.Edge()
        @return: Pydot object representing edge
        '''

        import pydot

        # no need to validate if nodes exist for src/dst. graphviz takes care of that for us transparently.

        dot_edge = pydot.Edge(self.src, self.dst)

        if self.label:
            dot_edge.label = self.label

        dot_edge.color = "#%06x" % self.color

        return dot_edge


    ####################################################################################################################
项目:lark    作者:erezsh    | 项目源码 | 文件源码
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)
项目:deep-coref    作者:clarkkev    | 项目源码 | 文件源码
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)
项目:deep-coref    作者:clarkkev    | 项目源码 | 文件源码
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)
项目:mcconf    作者:ManyThreads    | 项目源码 | 文件源码
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
项目:pydot3    作者:log0    | 项目源码 | 文件源码
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
项目:RecommendationSystem    作者:TURuibo    | 项目源码 | 文件源码
def add_edge(self, f, t, graph):
        if f: graph.add_edge(pydot.Edge(f, t))
        return t
项目:visual_turing_test-tutorial    作者:mateuszmalinowski    | 项目源码 | 文件源码
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)
项目:anormbookmarker    作者:jakeogh    | 项目源码 | 文件源码
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
项目:keras    作者:GeekLiB    | 项目源码 | 文件源码
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
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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()
项目:smtdos    作者:joelwanner    | 项目源码 | 文件源码
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
项目:MemoryAccessTracker-MAT    作者:NRauschmayr    | 项目源码 | 文件源码
def reduce_graph(self, threshold=0):

      if len(self.edges.keys()) == 0:
        print "Run first create_graph()"
        return False

      self.edges_reduced = {}
      self.graph = pydot.Dot(graph_type='digraph')

      # iterate through nodes and edges and filter by value
      for key in self.edges.keys():

        # check for most visited nodes
        if self.tree[key][1] > threshold * self.total:

          # iterate over edges and connect most visited nodes
          for parent_key in self.edges[key]:

            if parent_key != "Last" and self.tree[parent_key][1] > threshold * self.total:

              edge_label = ""

              for grandparent_key in self.edges[key][parent_key].keys():

                n = self.edges[key][parent_key][grandparent_key]

                if self.tree[parent_key][1] > threshold * self.total and self.edges[key][parent_key][grandparent_key][1] > threshold * self.total:
                  if key not in self.edges_reduced:
                    self.edges_reduced[key] = { parent_key: { grandparent_key: n } }
                  if parent_key not in self.edges_reduced[key]:
                    self.edges_reduced[key].update({ parent_key: { grandparent_key: n } })
                  if grandparent_key not in self.edges_reduced[key][parent_key]:
                     self.edges_reduced[key][parent_key].update({ grandparent_key: n })
                  else:
                     self.edges_reduced[key][parent_key][grandparent_key] = n
                  edge_counter = self.edges[key][parent_key][grandparent_key][1]
                  edge_label =  edge_label + "\n" + str(edge_counter) + " " + str(grandparent_key[0]) + "_" + str(grandparent_key[1]) + "_" + str(grandparent_key[2])

              if edge_label != "":
                new_edge =  pydot.Edge(self.tree[parent_key][0], self.tree[key][0])
                self.graph.add_node(self.tree[parent_key][0])
                self.graph.add_node(self.tree[key][0])
                self.graph.add_edge( new_edge )
                new_edge.set_label( edge_label )
项目:keras-customized    作者:ambrite    | 项目源码 | 文件源码
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
项目:frontera-docs-zh_CN    作者:xsren    | 项目源码 | 文件源码
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)
项目:mcconf    作者:ManyThreads    | 项目源码 | 文件源码
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)
项目:pydot3    作者:log0    | 项目源码 | 文件源码
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
项目:InnerOuterRNN    作者:Chemoinformatics    | 项目源码 | 文件源码
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
项目:documentr    作者:hive-tools    | 项目源码 | 文件源码
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