Ejemplo n.º 1
0
Archivo: lg.py Proyecto: n-st/bird-lg
 def add_node(_as, **kwargs):
     if _as not in nodes:
         kwargs["label"] = '<<TABLE CELLBORDER="0" BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD ALIGN="CENTER">' + escape(kwargs.get("label", get_as_name(_as))).replace("\r","<BR/>") + "</TD></TR></TABLE>>"
         nodes[_as] = pydot.Node(_as, style="filled", fontsize="10", **kwargs)
         graph.add_node(nodes[_as])
     return nodes[_as]
Ejemplo n.º 2
0
def parser(lines):
    global token,description,cluster_index,stmt_sequence_index
    stmt_sequence_index=0
    cluster_index=0
    [token, description] = lines[0].split(",")
    graph = pydot.Dot(graph_type='digraph', rankdir="TB")
    def match(text):

        global token, description
        if text == "number" or text == "identifier":
            if description.lower() == text:
                del lines[0]
                if len(lines)>0:
                    [token, description] = lines[0].split(",")
                return True
            else:
                return False
        else:
            if token == text:
                del lines[0]
                if len(lines) > 0:
                    [token, description] = lines[0].split(",")
                return True
            else:
                return False

    def repeat_stmt():
        global token, description
        global repeat_index
        if token == "repeat":
            match("repeat")
            stmt_sequence_repeat = stmt_sequence()
            match("until")
            exp_node = exp()
            repeat_node = Node("repeat" + str(repeat_index))
            repeat_index += 1
            stmt_sequence_repeat.parent = repeat_node
            exp_node.parent = repeat_node
            return repeat_node

    def assign_stmt():
        global token, description
        global assign_index, id_index
        if description.lower() == "identifier":
            assign_node = Node("assign" + str(assign_index))
            assign_index += 1
            id_node = Node("id" + str(id_index) + "\n(" + token + ")", parent=assign_node)
            id_index += 1
            match("identifier")
            match(":=")
            exp_node = exp()
            exp_node.parent = assign_node
            return assign_node

    def term():
        global token, description
        global op_index
        temp = factor()

        while token == "*":
            newtemp = Node("op" + str(op_index) + "\n" + token)
            op_index += 1
            match(token)
            temp.parent = newtemp
            factor().parent = newtemp
            temp = newtemp
        return temp

    def factor():
        global token, description
        global const_index, id_index
        if token == "(":
            match("(")
            temp=exp()
            match(")")
            return temp
        elif description.lower() == "number":
            temp = Node("Const" + str(const_index) + "\n(" + token + ")")
            const_index += 1
            match("number")
            return temp

        elif description.lower() == "identifier":
            temp = Node("Id" + str(id_index) + "\n(" + token + ")")
            id_index += 1
            match("identifier")
            return temp
        else:
            return

    def write_stmt():
        global token, description, write_index
        match("write")
        temp = Node("Write" + str(write_index))
        write_index += 1
        exp().parent = temp
        return temp

    def read_stmt():
        global token, description, read_index, id_index
        match("read")
        temp = Node("Read" + str(read_index))
        read_index += 1
        newtemp = Node("Id" + str(id_index) + "\n(" + token + ")", parent=temp)
        id_index += 1
        match("identifier")
        return temp

    def stmt_sequence():
        global token, description, stmt_sequence_index
        temp = Node("stmt_sequence" + str(stmt_sequence_index) + "\n")
        stmt_sequence_index += 1
        statement().parent = temp

        while token == ";":
            match(token)
            statement().parent = temp

        return temp

    def simple_exp():
        global token, description, op_index
        temp = term()

        while (token == "+" or token == "-"):
            newtemp = Node("op" + str(op_index) + "\n" + token)
            op_index += 1
            match(token)
            temp.parent = newtemp
            term().parent = newtemp
            temp = newtemp
        return temp

    def exp():
        global token, description, op_index
        temp = simple_exp()

        while (token == "<" or token == ">" or token == "=" or token == "!="):
            newtemp = Node("op" + str(op_index) + "\n" + token)
            op_index += 1
            match(token)
            temp.parent = newtemp
            simple_exp().parent = newtemp
            temp = newtemp
        return temp

    def if_stmt():
        global token, description, if_index
        temp = Node("if" + str(if_index))
        if_index += 1
        match("if")
        match("(")
        exp().parent = temp
        match(")")
        match("then")
        stmt_sequence().parent = temp
        if token == "else":
            match("else")
            stmt_sequence().parent = temp
        match("end")
        return temp

    def statement():
        global token, description
        if token == "if":
            return if_stmt()
        elif token == "repeat":
            return repeat_stmt()
        elif description.lower() == "identifier":
            return assign_stmt()
        elif token == "read":
            return read_stmt()
        elif token == "write":
            return write_stmt()

    def draw_edges(root):
        if "stmt_sequence" in root.name:
            edge = pydot.Edge(root.parent.name, root.children[0].name)
            graph.add_edge(edge)
            for x in range(0, len(root.children) - 1):
                edge = pydot.Edge(root.children[x].name, root.children[x + 1].name, dir='none')
                graph.add_edge(edge)
            for i in root.children:
                draw_edges(i)
        else:
            for i in root.children:
                if "stmt_sequence" not in i.name:
                    edge = pydot.Edge(root.name, i.name)
                    graph.add_edge(edge)
                draw_edges(i)
        return

    start_node = Node("Start")
    stmt_sequence().parent = start_node
    cluster1 = pydot.Subgraph("level" + str(cluster_index), rank="same")
    cluster_index += 1
    # cluster1.add_node(pydot.Node(node.name))

    # subgraphs=[[node.name for node in children] for children in LevelOrderGroupIter(start_node)]
    subgraphs = []
    for children in LevelOrderGroupIter(start_node):
        childrenss = []
        for node in children:
            if "stmt_sequence" in node.name:
                for inner_child in node.children:
                    childrenss.append(inner_child.name)
            else:
                if len(subgraphs) == 0:
                    childrenss.append(node.name)
                elif node.name not in subgraphs[-1]:
                    childrenss.append(node.name)
        if len(childrenss) > 0:
            subgraphs.append(childrenss)

    for sub in subgraphs:
        for i in sub:
            if "stmt_sequence" in i:
                continue
            cluster1.add_node(pydot.Node(i))
        graph.add_subgraph(cluster1)
        cluster1 = pydot.Subgraph("level" + str(cluster_index), rank="same")
        cluster_index += 1
    draw_edges(start_node)
    graph.write('p2.txt')
    cmd = ["dot", 'p2.txt', "-T", 'png', "-o", 'Parser.png']
    call(cmd)
    return
Ejemplo n.º 3
0
def main():
    techs = {}

    with open(PATH + COSTS) as costs:
        for line in costs:
            x = line.split(',')
            name, cost = x[0], int(x[11])
            if name.startswith('CAM1'):  # Automatic stuff
                continue
            if cost > 450 * 32:
                cost = 450 * 32  # A runtime cap, according to Zarel
            techs[name] = Tech(name, cost)

    from pprint import pprint
    #pprint(techs)

    with open(PATH + PREREQS) as prereqs:
        for line in prereqs:
            name, prereq, junk = line.split(',')
            if name == prereq:  # This tells the game the tech is automatic
                continue
            if prereq.startswith('CAM1'):  # Automatic stuff
                continue
            techs[name].prereqs.add(techs[prereq])
            techs[prereq].dependents.add(techs[name])

    with open(PATH + NAMES) as names:
        for line in names:
            m = re.match(r'^([a-zA-Z0-9-]+)\s+_\("(.*)"\)\s*$', line)
            if m is not None:
                name, label = m.groups()
                if name in techs:
                    techs[name].label = label

    #print techs['R-Vehicle-Body01'].cumcost
    #for tech in techs.itervalues():
    #    print tech, list(tech.prereqs), tech.cumcost

    #sortedtechs = sorted(techs.itervalues(), key=lambda tech: tech.cumcost)
    #for tech in sortedtechs:
    #    print tech.cumcost, tech, tech.prereqs

    graph = pydot.Dot()
    graph.set_ranksep('1.0')
    graph.set_rank('min')
    graph.set_rankdir('LR')
    graph.set_concentrate('true')
    #graph.set_size('60,60')

    automatic = pydot.Cluster()
    automatic.set_label("Automatic")
    automatic.set_fontcolor(
        "darkgrey")  # Lighter than "grey".  Don't ask me why.
    graph.add_subgraph(automatic)

    # Pick out the ones we want
    #chosen_techs = techs.values()
    #import random
    #random.shuffle(techvalues)
    chosen_techs = set(tech for tech in techs.values()
                       if tech.prereqs)  # if tech.color == 'red')
    # Recursively grab their dependencies
    dirty = set(chosen_techs)
    while dirty:
        x = set(dirty)
        dirty.clear()
        chosen_techs |= x
        for tech in x:
            dirty.update(tech.prereqs - chosen_techs)

    #print "Retaliation:", techs['R-Vehicle-Body03'].cumcost
    #print "Turbo-Charged Engine:", techs['R-Vehicle-Engine04'].cumcost
    #print "Gas Turbine Engine:", techs['R-Vehicle-Engine07'].cumcost

    chosen_techs = sorted(chosen_techs,
                          key=lambda tech: (tech.cumcost, tech.label))

    # Add nodes
    for tech in chosen_techs:
        node = pydot.Node(tech.name)
        node.set_label(tech.label)
        #node.set_label(tech.name)
        #node.set_label(r'%s\n%s' % (tech.label, tech.name))
        node.set_shape('box')
        node.set_style('filled,setlinewidth(3.0)')
        node.set_color('black')
        node.set_fillcolor('lightgrey')
        node.set_fontsize('24')

        if tech.color is not None:
            node.set_color(tech.color)

        if tech.fillcolor is not None:
            node.set_fillcolor(tech.fillcolor)

        if tech.name in [
                'R-Sys-Engineering01', 'R-Sys-Sensor-Turret01', 'R-Wpn-MG1Mk1'
        ]:
            #node.set_URL(r"//wzguide.co.cc/new/r/sensorturret")
            automatic.add_node(node)
        else:
            graph.add_node(node)

    # Add edges
    for tech in chosen_techs:
        for prereq in tech.prereqs:
            edge = pydot.Edge(prereq.name, tech.name)
            edge.set_tailport('e')
            edge.set_headport('w')

            #if prereq.name in ['R-Struc-Research-Upgrade07', 'R-Sys-Sensor-Upgrade01']:
            #    edge.set_style('dashed')
            #elif prereq.name in ['R-Defense-HardcreteWall', 'R-Struc-Research-Upgrade04']:
            #    edge.set_style('dotted')
            style = prereq.edgestyle
            if prereq.color is None:
                edge.set_color('black')
            else:
                edge.set_color(prereq.color)
            edge.set_style(style)

            #edge.set_weight(str(20.0 / (len(tech.prereqs) + len(prereq.dependents))))
            weight = 8.0 / 1.2**(len(prereq.dependents) +
                                 len(tech.prereqs) / 4)
            if tech.color != prereq.color:
                weight /= 2
            if tech.cumcost:
                weight /= (tech.cumcost / 10000)
            edge.set_weight(str(weight))

            graph.add_edge(edge)
    graph.write_svg('warzoneresearch.svg')
Ejemplo n.º 4
0
 def createCommentNode(self, word):
     return pydot.Node(word, style="filled", fillcolor="#976856")
Ejemplo n.º 5
0
 def createDirectionNode(self):
     arr = [w for (w, T) in self.taggedRecipe]
     directionWhole = " ".join(arr)
     return pydot.Node(directionWhole, style="filled", fillcolor="#ffffff")
Ejemplo n.º 6
0
    def layout(self, type_):

        if pygraphviz:
            G = pygraphviz.AGraph(strict=False, directed=True)
            if type_ == 'dot LR':
                G.graph_attr['rankdir'] = 'LR'
            type_ = type_.split()[0]
            G.graph_attr['ranksep'] = '0.125'
        elif pydot:
            G = pydot.Dot('graphname', graph_type='digraph')
            if type_ == 'dot LR':
                G.set_layout('dot')
                G.set('rankdir', 'LR')
            else:
                G.set_layout(type_)
            G.set('ranksep', '0.125')
        for from_, to in self.link.values():
            if pygraphviz:
                G.add_edge(
                    (from_, from_.gnx),
                    (to, to.gnx),
                )
            elif pydot:
                G.add_edge(pydot.Edge(from_.gnx, to.gnx))

        for i in self.nodeItem:
            if pygraphviz:
                G.add_node((i, i.gnx))
            elif pydot:
                gnode = pydot.Node(i.gnx)
                # rect = self.nodeItem[i].boundingRect()
                G.add_node(gnode)
                for child in i.children:
                    key = (i, child)
                    if key not in self.hierarchyLinkItem or child not in self.nodeItem:
                        continue
                    G.add_edge(pydot.Edge(i.gnx, child.gnx))
        if pygraphviz:
            G.layout(prog=type_)
        elif pydot:
            tempName = tempfile.NamedTemporaryFile(dir=tempfile.gettempdir(),
                                                   delete=False)
            G.write_dot(tempName.name)
            G = pydot.graph_from_dot_file(tempName.name)

        for i in self.nodeItem:
            if pygraphviz:
                gn = G.get_node((i, i.gnx))
                x, y = map(float, gn.attr['pos'].split(','))
                i.u['_bklnk']['x'] = x
                i.u['_bklnk']['y'] = -y
                self.nodeItem[i].setPos(x, -y)
                self.nodeItem[i].do_update()
            elif pydot:
                lst = G.get_node(''.join(['"', i.gnx, '"']))
                if lst:
                    x, y = map(float, lst[0].get_pos().strip('"').split(','))
                    i.u['_bklnk']['x'] = x
                    i.u['_bklnk']['y'] = -y
                    self.nodeItem[i].setPos(x, -y)
                    self.nodeItem[i].do_update()
        if pydot:
            x, y, width, height = map(float, G.get_bb().strip('"').split(','))
            self.ui.canvasView.setSceneRect(
                self.ui.canvas.sceneRect().adjusted(x, y, width, height))
        self.do_update(adjust=False)
        self.center_graph()
Ejemplo n.º 7
0
 def createIngredientNode(self, word):
     return pydot.Node(word, style="filled", fillcolor="green")
Ejemplo n.º 8
0
def bfs(initial_state, mode="bfs"):
    graph = pydot.Dot(
        graph_type='digraph',
        label="Missionaries and Cannibals (BFS) - Aakash Giree(20) ",
        fontsize="30",
        color="red",
        fontcolor="black",
        fontname='Impact',
        fillcolor="black")
    start_node = Node(initial_state, None, None, 0)
    if start_node.goal_test():
        return start_node.find_solution()

    q = Queue()
    q.put(start_node)
    explored = []
    killed = []
    print("The starting node is \ndepth=%d" % start_node.depth)
    print(str(start_node.state))
    while not (q.empty()):
        node = q.get()
        print("\nThe node selected to expand is\ndepth=" + str(node.depth) +
              "\n" + str(node.state) + "\n")
        explored.append(node.state)
        graph.add_node(node.graph_node)
        if node.parent:
            diff = np.subtract(node.parent.state, node.state)
            if node.parent.state[2] == 0:
                diff[0], diff[1] = -diff[0], -diff[1]
            graph.add_edge(
                pydot.Edge(node.parent.graph_node,
                           node.graph_node,
                           label=str(diff)))
        children = node.generate_child()
        if not node.is_killed():
            print("The children nodes of this node are", end="")
            for child in children:
                if child.state not in explored:
                    print("\ndepth=%d" % child.depth)
                    print(str(child.state))
                    if child.goal_test():
                        print("Which is the goal state\n")
                        graph.add_node(child.graph_node)
                        diff = np.subtract(node.parent.state, node.state)
                        if node.parent.state[2] == 0:
                            diff[0], diff[1] = -diff[0], -diff[1]

                        graph.add_edge(
                            pydot.Edge(child.parent.graph_node,
                                       child.graph_node,
                                       label=str(diff)))

                        # colour all leaves blue
                        leafs = {n.get_name(): True for n in graph.get_nodes()}
                        for e in graph.get_edge_list():
                            leafs[e.get_source()] = False
                        for leaf in leafs:
                            if leafs[leaf] and str(leaf) not in killed and str(
                                    leaf) != "\"[0, 0, 0]\"":
                                node = pydot.Node(leaf,
                                                  style="filled",
                                                  fillcolor="blue")
                                graph.add_node(node)

                        draw_legend(graph)
                        graph.write_png('BFS_search_tree.png')

                        return child.find_solution()
                    if child.is_valid():
                        q.put(child)
                        explored.append(child.state)

        else:
            print("This node is killed")
            killed.append("\"" + str(node.state) + "\"")

    return
Ejemplo n.º 9
0
    f = get_follow_sets()
    pprint.pprint(f, indent=4, depth=4)
    start = g["productions"][0][0]
    start_prod = pick_production(start, l)
    derivation.extend(start_prod[1].split())

    print('-----------------------')
    print('Cuvantul testat este:')
    print(" ".join(str(x) for x in string_input))
    print('-----------------------')

    print('Afisam derivarea')
    print('-----------------------')

    graph = pydot.Dot(graph_type='digraph', nodesep='2.5')
    node_from = pydot.Node(str(uuid.uuid1()), label=str(start))
    graph.add_node(node_from)
    graph_nodes.append(node_from)
    visited_nodes.append(node_from.get_name())
    for term in derivation:
        if is_terminal(term):  # coloram special daca e terminal
            node_to = pydot.Node(str(term),
                                 label=str(term),
                                 shape='box',
                                 style='filled',
                                 fillcolor='#ffff00')
        else:
            node_to = pydot.Node(str(term), label=str(term), shape='box')
        node_to = add_node_if_existing(graph, node_to, is_terminal(term))
        # graph.add_node(node_to)
        # visited_nodes.append(node_to.get_name())
Ejemplo n.º 10
0
    def buildNode(self, dimName, objtName):
        b = Borg()
        objtUrl = dimName + '#' + str(objtName)
        if (dimName == 'persona'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shapefile=b.staticDir + '/assets/modelActor.png',
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl,
                           peripheries='0'))
        elif (dimName == 'tag'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='note',
                           style='filled',
                           margin=0,
                           pencolor='black',
                           color='yellow',
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'attacker'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shapefile=b.staticDir + '/assets/modelAttacker.png',
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl,
                           peripheries='0'))
        elif (dimName == 'asset'):
            assetObjt = self.dbProxy.dimensionObject(objtName, 'asset')
            borderColour = 'black'
            if (assetObjt.critical()):
                borderColour = 'red'
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='record',
                           color=borderColour,
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl,
                           width='0',
                           height='0',
                           style='filled',
                           pencolor='black',
                           fillcolor='white',
                           label=arrayToAssetSecurityPropertiesTable(
                               assetObjt.securityProperties(
                                   self.theEnvironmentName), objtName)))
        elif (dimName == 'threat'):
            thrObjt = self.dbProxy.dimensionObject(objtName, 'threat')
            thrLhood = thrObjt.likelihood(
                self.theEnvironmentName,
                self.theEnvironmentObject.duplicateProperty(),
                self.theEnvironmentObject.overridingEnvironment())
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='record',
                           style='filled',
                           margin=0,
                           color='black',
                           fillcolor=threatLikelihoodColourCode(thrLhood),
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl,
                           label=arrayToThreatSecurityPropertiesTable(
                               thrObjt.securityProperties(
                                   self.theEnvironmentName), objtName)))
        elif (dimName == 'vulnerability'):
            vulObjt = self.dbProxy.dimensionObject(objtName, 'vulnerability')
            vulSev = vulObjt.severity(
                self.theEnvironmentName,
                self.theEnvironmentObject.duplicateProperty(),
                self.theEnvironmentObject.overridingEnvironment())
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='record',
                           style='filled',
                           margin=0,
                           colorscheme='orrd4',
                           color='black',
                           fillcolor=vulnerabilitySeverityColourCode(vulSev),
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'risk'):
            riskObjt = self.dbProxy.dimensionObject(objtName, 'risk')
            riskScores = self.dbProxy.riskScore(riskObjt.threat(),
                                                riskObjt.vulnerability(),
                                                self.theEnvironmentName,
                                                objtName)
            highestScore = 0
            for riskScore in riskScores:
                currentScore = riskScore[2]
                if (currentScore > highestScore):
                    highestScore = currentScore
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='diamond',
                           style='filled',
                           margin=0,
                           color='black',
                           fillcolor=threatColourCode(highestScore),
                           fontcolor=riskTextColourCode(highestScore),
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'response'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='note',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'countermeasure'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='component',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'component'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='component',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'requirement'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='circle',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'goal'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='parallelogram',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'obstacle'):
            obsId = self.dbProxy.getDimensionId(objtName, 'obstacle')
            envId = self.dbProxy.getDimensionId(self.theEnvironmentName,
                                                'environment')
            obsProb, obsRationale = self.dbProxy.obstacleProbability(
                obsId, envId)
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='polygon',
                           margin=0,
                           skew='-0.4',
                           style='filled',
                           pencolor='black',
                           colorscheme='ylorrd9',
                           fillcolor=obstacleColourCode(obsProb),
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           fontcolor=probabilityTextColourCode(obsProb),
                           URL=objtUrl))
        elif (dimName == 'role'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shapefile=b.staticDir + '/assets/modelRole.png',
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl,
                           peripheries='0'))
        elif (dimName == 'responsibility'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='doubleoctagon',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'environment'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='doublecircle',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'domainproperty'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='house',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'inconsistency'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='polygon',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           URL=objtUrl))
        elif (dimName == 'task'):
            taskScore = self.dbProxy.taskUsabilityScore(
                objtName, self.theEnvironmentName)
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='ellipse',
                           margin=0,
                           style='filled',
                           color=usabilityColourCode(taskScore),
                           pencolor='black',
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           fontcolor=usabilityTextColourCode(taskScore),
                           URL=objtUrl))

        elif (dimName == 'misusecase'):
            self.theGraph.add_node(
                pydot.Node(objtName,
                           shape='ellipse',
                           margin=0,
                           fontname=self.fontName,
                           fontsize=self.fontSize,
                           style='filled',
                           color='black',
                           fontcolor='white',
                           URL=objtUrl))
        else:
            raise UnknownNodeType(dimName)
Ejemplo n.º 11
0
def plot_model(model,
               filename='model.png',
               show_shapes=True,
               rankdir='TB',
               dpi=96,
               **kwargs):
    """
    :param model:
    :param filename:
    :return:
    """
    """ Setup params """
    graph = pydot.Dot(graph_type='digraph', strict=True)
    graph.set('rankdir', rankdir)
    graph.set('concentrate', True)
    graph.set('dpi', dpi)
    graph.set_node_defaults(shape='record')
    """ First, let's initialize our graph """
    #graph = pydot.Dot(graph_type='digraph', strict = True)
    #graph.set_nodesep(2)
    #graph.set_fontpath(tp.__fonts_dir__)
    #graph.set_fontname(tp.__layers_css__['globals']['font_tag']['name'])
    """ Now let's define each node (layer) """
    nodes = {}
    inbounds = {}
    node_layers = {}
    in_shapes = {}
    for ly in model.layers:
        """ Get layer params """
        __name__ = ly.output.name
        """ Create node """
        nodes[__name__] = generate_node(ly, name=__name__)
        node_layers[__name__] = ly
        """ Get inbounds for this layer """
        if __name__ not in inbounds:
            inbounds[__name__] = []
        if __name__ not in in_shapes:
            in_shapes[__name__] = []

        _input_ = ly.input if isinstance(ly.input, list) else [ly.input]
        inbounds[__name__] += [
            ln.name for ln in _input_ if ln.name != __name__
        ]
        in_shapes[__name__] += [f'(?,{",".join([str(lln) for lln in ln.shape.as_list()[1:]])})' \
                                for ln in _input_ if ln.name != __name__]
        """ If inbound not in nodes, make it now (it means it's an input) """
        for iln, ln in enumerate(inbounds[__name__]):
            if ln not in nodes:
                """ create node """
                nodes[ln] = pydot.Node(ln)
                node_layers[ln] = ly.input[iln]

            if ln not in inbounds:
                inbounds[ln] = []
    """ Now we can easily identify the inputs/outputs of the model by looking at inbounds """
    input_nodes = [
        node_name for node_name in inbounds if len(inbounds[node_name]) == 0
    ]
    output_nodes = [
        node_layers[ib].output.name for ib in list(inbounds.keys())
        if ib not in np.hstack(list(inbounds.values()))
    ]
    print(in_shapes)
    """ Add nodes and edges to graph """
    for node_name in inbounds:
        """ If this is an input_node, recompile using right style """
        if node_name in input_nodes:
            style = copy.deepcopy(tp.__layers_css__['layers']['InputLayer'])
            style['tag'] = copy.deepcopy(
                eval(style['tag'])(node_layers[node_name]))
            graph.del_node(nodes[node_name])
            nodes[node_name] = generate_node(None, name=node_name, style=style)

        node = nodes[node_name]
        node.set_fontname(tp.__layers_css__['globals']['font_tag']['name'])
        graph.add_node(node)
        print(f'[INFO] - Adding layer {node_name} to graph.')

        for ib, ish in zip(inbounds[node_name], in_shapes[node_name]):
            kww = {'label': ish} if show_shapes else {}
            edge = pydot.Edge(nodes[ib], node, **kww)
            graph.add_edge(edge)
            edge.set_fontname(tp.__layers_css__['globals']['font_tag']['name'])
        """ If this is an output node, add extra block """
        if node_name in output_nodes:
            style = copy.deepcopy(tp.__layers_css__['layers']['OutputLayer'])
            style['tag'] = copy.deepcopy(
                eval(style['tag'])(node_layers[node_name]))
            graph.del_node('out_' + node_name)
            out_node = generate_node(None,
                                     name='out_' + node_name,
                                     style=style)
            graph.add_node(out_node)
            edge = pydot.Edge(node, out_node)
            graph.add_edge(edge)

    graph.write_png(filename)
    graph.write_svg(filename.replace('png', 'svg'))

    # Return the image as a Jupyter Image object, to be displayed in-line.
    # Note that we cannot easily detect whether the code is running in a
    # notebook, and thus we always return the Image if Jupyter is available.
    try:
        from IPython import display
        return display.Image(filename=filename)
    except ImportError:
        pass
Ejemplo n.º 12
0
"""
44. 係り受け木の可視化
与えられた文の係り受け木を有向グラフとして可視化せよ.可視化には,Graphviz等を用いるとよい.
"""
from itertools import islice

import pydot

from knock41 import cabocha_into_chunks
from knock43 import ChunkNormalized

if __name__ == "__main__":

    def input_k():
        return int(input("Enter a number (0: exit) -> "))

    for k in iter(input_k, 0):
        for chunks in islice(cabocha_into_chunks(), k - 1, k):
            chunks = {k: ChunkNormalized(v) for k, v in chunks.items()}
            G = pydot.Dot(graph_type="digraph")
            for i, c in chunks.items():
                if c.norm:
                    color = "red" if c.has_pos("名詞") else "black"
                    G.add_node(pydot.Node(i, label=c.norm, color=color))
            for i, c in chunks.items():
                if c.dst == -1:
                    continue
                if c.norm and c.dst in chunks and chunks[c.dst].norm:
                    G.add_edge(pydot.Edge(i, c.dst))
            G.write_png(f"./out44_{k}.png")
Ejemplo n.º 13
0
def dot_tree(
        root: behaviour.Behaviour,
        visibility_level: common.VisibilityLevel=common.VisibilityLevel.DETAIL,
        collapse_decorators: bool=False,
        with_qualified_names: bool=False):
    """
    Paint your tree on a pydot graph.

    .. seealso:: :py:func:`render_dot_tree`.

    Args:
        root (:class:`~py_trees.behaviour.Behaviour`): the root of a tree, or subtree
        visibility_level (:class`~py_trees.common.VisibilityLevel`): collapse subtrees at or under this level
        collapse_decorators (:obj:`bool`, optional): only show the decorator (not the child), defaults to False
        with_qualified_names: (:obj:`bool`, optional): print the class information for each behaviour in each node, defaults to False

    Returns:
        pydot.Dot: graph

    Examples:

        .. code-block:: python

            # convert the pydot graph to a string object
            print("{}".format(py_trees.display.dot_graph(root).to_string()))
    """
    def get_node_attributes(node):
        blackbox_font_colours = {common.BlackBoxLevel.DETAIL: "dodgerblue",
                                 common.BlackBoxLevel.COMPONENT: "lawngreen",
                                 common.BlackBoxLevel.BIG_PICTURE: "white"
                                 }
        if isinstance(node, composites.Chooser):
            attributes = ('doubleoctagon', 'cyan', 'black')  # octagon
        elif isinstance(node, composites.Selector):
            attributes = ('octagon', 'cyan', 'black')  # octagon
        elif isinstance(node, composites.Sequence):
            attributes = ('box', 'orange', 'black')
        elif isinstance(node, composites.Parallel):
            attributes = ('parallelogram', 'gold', 'black')
        elif isinstance(node, decorators.Decorator):
            attributes = ('ellipse', 'ghostwhite', 'black')
        else:
            attributes = ('ellipse', 'gray', 'black')
        if node.blackbox_level != common.BlackBoxLevel.NOT_A_BLACKBOX:
            attributes = (attributes[0], 'gray20', blackbox_font_colours[node.blackbox_level])
        return attributes

    def get_node_label(node_name, behaviour):
        """
        This extracts a more detailed string (when applicable) to append to
        that which will be used for the node name.
        """
        node_label = node_name
        if behaviour.verbose_info_string():
            node_label += "\n{}".format(behaviour.verbose_info_string())
        if with_qualified_names:
            node_label += "\n({})".format(utilities.get_fully_qualified_name(behaviour))
        return node_label

    fontsize = 9
    graph = pydot.Dot(graph_type='digraph')
    graph.set_name("pastafarianism")  # consider making this unique to the tree sometime, e.g. based on the root name
    # fonts: helvetica, times-bold, arial (times-roman is the default, but this helps some viewers, like kgraphviewer)
    graph.set_graph_defaults(fontname='times-roman')  # splines='curved' is buggy on 16.04, but would be nice to have
    graph.set_node_defaults(fontname='times-roman')
    graph.set_edge_defaults(fontname='times-roman')
    (node_shape, node_colour, node_font_colour) = get_node_attributes(root)
    node_root = pydot.Node(
        root.name,
        label=get_node_label(root.name, root),
        shape=node_shape,
        style="filled",
        fillcolor=node_colour,
        fontsize=fontsize,
        fontcolor=node_font_colour)
    graph.add_node(node_root)
    names = [root.name]

    def add_edges(root, root_dot_name, visibility_level, collapse_decorators):
        if isinstance(root, decorators.Decorator) and collapse_decorators:
            return
        if visibility_level < root.blackbox_level:
            for c in root.children:
                (node_shape, node_colour, node_font_colour) = get_node_attributes(c)
                node_name = c.name
                while node_name in names:
                    node_name += "*"
                names.append(node_name)
                # Node attributes can be found on page 5 of
                #    https://graphviz.gitlab.io/_pages/pdf/dot.1.pdf
                # Attributes that may be useful: tooltip, xlabel
                node = pydot.Node(
                    node_name,
                    label=get_node_label(node_name, c),
                    shape=node_shape,
                    style="filled",
                    fillcolor=node_colour,
                    fontsize=fontsize,
                    fontcolor=node_font_colour,
                )
                graph.add_node(node)
                edge = pydot.Edge(root_dot_name, node_name)
                graph.add_edge(edge)
                if c.children != []:
                    add_edges(c, node_name, visibility_level, collapse_decorators)

    add_edges(root, root.name, visibility_level, collapse_decorators)
    return graph
Ejemplo n.º 14
0
    lines = [line.strip() for line in f.readlines()]
    V = set()
    edges = []
    for line in lines:
        data = line.split(' ')
        orig = line[0]
        dest = line[1]
        V.add(orig)
        V.add(dest)
        edges.append((orig, dest))
    return V, edges


parser = argparse.ArgumentParser()
parser.add_argument('-f', help='graph filename')
args = parser.parse_args()

fn = args.f
V, edges = parse_zoo(fn)

graph = pydot.Dot()
graph.set_type('digraph')

for v in range(V):
    graph.add_node(pydot.Node(v))

for u, v in edges:
    graph.add_edge(pydot.Edge(u, v))

graph.write_pdf(fn + '.pdf')
Ejemplo n.º 15
0
import graphviz
import pydot
from IPython.display import Image, display
#
G = pydot.Dot(graph_type="digraph")
#
# node = pydot.Node(str(spinf[0]['samples']),style="filled",fillcolor="orange")
node = pydot.Node('Demo_node')
#
G.add_node(node)
# #
im = Image(G.create())
# #
display(im)
# #
#
# from graphviz import Graph
#
# e = Graph('ER', filename='er.gv', engine='neato')
#
# e.attr('node', shape='box')
# e.node('course')
# e.node('institute')
# e.node('student')
#
# e.attr('node', shape='ellipse')
# e.node('name0', label='name')
# e.node('name1', label='name')
# e.node('name2', label='name')
# e.node('code')
# e.node('grade')
Ejemplo n.º 16
0
def parse(prod):
    global start_pos
    global l
    global syntax_error
    global derivation
    global graph
    global visited_nodes

    rhsterms = get_rhs_of_prod(prod).split()
    lhsterm = get_lhs_of_prod(prod)
    start_pos = 0
    for term in rhsterms:
        if is_terminal(term):
            if term == "eps":  # daca e eps, facem backtrack prin return
                # l = match_eps()
                # prod = pick_production(lhsterm, l)
                # parse(word, prod)
                return
            else:
                l, error = match(
                    term)  # daca e terminal, facem match si mergem mai departe
                if error:
                    return
                l = lookahead()
        else:  # term is nonterminal , daca e neterminal, alegem urmatoarea productie
            prod = pick_production(term, l)
            # derivation = rhsterms
            prod_terms = prod[1].split()
            # derivation.extend(rhsterms)
            # derivation.insert(0, prod_terms)
            if "eps" in prod_terms:  # cand afisam derivarea, daca avem X->eps, dispare X din derivare
                t = prod[0]
                derivation.remove(t)
            else:
                derivation = derivation[:look_position - 1] + prod_terms + derivation[
                    look_position:]  # inlocuim in derivare neterminalul cu termenii productiei
            print(" ".join(str(x) for x in derivation))

            starting_node = get_node_by_label(graph, prod[0])
            if isinstance(starting_node, list):
                starting_node = search_and_get_first_unvisited_node(
                    graph, prod[0])
                if isinstance(starting_node, list):
                    starting_node = starting_node[0]

            visited_nodes.append(starting_node.get_name())
            for p_term in prod_terms:
                if is_terminal(p_term):  # terminalele le coloram
                    node_t = pydot.Node(str(p_term),
                                        label=str(p_term),
                                        shape='box',
                                        style='filled',
                                        fillcolor='#ffff00')
                else:
                    node_t = pydot.Node(str(p_term),
                                        label=str(p_term),
                                        shape='box')
                node_t = add_node_if_existing(graph, node_t,
                                              is_terminal(p_term))
                graph.add_edge(pydot.Edge(starting_node, node_t))

            if prod is None:  # daca nu exista nici o productie corespunzatoare, eroare
                syntax_error = True
                return
            parse(prod)  # continuam recursia cu productia urmatoare
Ejemplo n.º 17
0
def make_graph(pdus: List[dict], filename_prefix: str) -> None:
    """
    Generate a dot and SVG file for a graph of events in the room based on the
    topological ordering by querying a homeserver.
    """
    pdu_map = {}
    node_map = {}

    origins = set()
    colors = {"red", "green", "blue", "yellow", "purple"}

    for pdu in pdus:
        origins.add(pdu.get("origin"))

    color_map = {color: color for color in colors if color in origins}
    colors -= set(color_map.values())

    color_map[None] = "black"

    for o in origins:
        if o in color_map:
            continue
        try:
            c = colors.pop()
            color_map[o] = c
        except Exception:
            print("Run out of colours!")
            color_map[o] = "black"

    graph = pydot.Dot(graph_name="Test")

    for pdu in pdus:
        name = make_name(pdu.get("pdu_id"), pdu.get("origin"))
        pdu_map[name] = pdu

        t = datetime.datetime.fromtimestamp(
            float(pdu["ts"]) / 1000).strftime("%Y-%m-%d %H:%M:%S,%f")

        label = ("<"
                 "<b>%(name)s </b><br/>"
                 "Type: <b>%(type)s </b><br/>"
                 "State key: <b>%(state_key)s </b><br/>"
                 "Content: <b>%(content)s </b><br/>"
                 "Time: <b>%(time)s </b><br/>"
                 "Depth: <b>%(depth)s </b><br/>"
                 ">") % {
                     "name": name,
                     "type": pdu.get("pdu_type"),
                     "state_key": pdu.get("state_key"),
                     "content": cgi.escape(json.dumps(pdu.get("content")),
                                           quote=True),
                     "time": t,
                     "depth": pdu.get("depth"),
                 }

        node = pydot.Node(name=name,
                          label=label,
                          color=color_map[pdu.get("origin")])
        node_map[name] = node
        graph.add_node(node)

    for pdu in pdus:
        start_name = make_name(pdu.get("pdu_id"), pdu.get("origin"))
        for i, o in pdu.get("prev_pdus", []):
            end_name = make_name(i, o)

            if end_name not in node_map:
                print("%s not in nodes" % end_name)
                continue

            edge = pydot.Edge(node_map[start_name], node_map[end_name])
            graph.add_edge(edge)

        # Add prev_state edges, if they exist
        if pdu.get("prev_state_id") and pdu.get("prev_state_origin"):
            prev_state_name = make_name(pdu.get("prev_state_id"),
                                        pdu.get("prev_state_origin"))

            if prev_state_name in node_map:
                state_edge = pydot.Edge(node_map[start_name],
                                        node_map[prev_state_name],
                                        style="dotted")
                graph.add_edge(state_edge)

    graph.write("%s.dot" % filename_prefix, format="raw", prog="dot")
    #    graph.write_png("%s.png" % filename_prefix, prog='dot')
    graph.write_svg("%s.svg" % filename_prefix, prog="dot")
Ejemplo n.º 18
0
 def add_graph_node(graph, nodes, name, label=None, color='black'):
     if name not in nodes:
         node = pydot.Node(name, color=color)
         nodes[name] = node
         graph.add_node(node)
Ejemplo n.º 19
0
 def createActionNode(self, word):
     return pydot.Node(word, style="filled", fillcolor="red")
Ejemplo n.º 20
0
    def plot_apply(app, d):
        if d == 0:
            return
        if app in my_list:
            return
        astr = apply_name(app) + '_' + str(len(my_list.keys()))
        if len(astr) > max_label_size:
            astr = astr[:max_label_size - 3] + '...'
        my_list[app] = astr

        use_color = None
        for opName, color in colorCodes.items():
            if opName in app.op.__class__.__name__:
                use_color = color

        if use_color is None:
            g.add_node(pd.Node(astr, shape='box'))
        elif high_contrast:
            g.add_node(pd.Node(astr, style='filled', fillcolor=use_color,
                               shape='box'))
        else:
            g.add_node(pd.Nonde(astr, color=use_color, shape='box'))

        for i, nd in enumerate(app.inputs):
            if nd not in my_list:
                varastr = var_name(nd) + '_' + str(len(my_list.keys()))
                if len(varastr) > max_label_size:
                    varastr = varastr[:max_label_size - 3] + '...'
                my_list[nd] = varastr
                if nd.owner is not None:
                    g.add_node(pd.Node(varastr))
                elif high_contrast:
                    g.add_node(pd.Node(varastr, style='filled',
                                       fillcolor='green'))
                else:
                    g.add_node(pd.Node(varastr, color='green'))
            else:
                varastr = my_list[nd]
            label = None
            if len(app.inputs) > 1:
                label = str(i)
            g.add_edge(pd.Edge(varastr, astr, label=label))

        for i, nd in enumerate(app.outputs):
            if nd not in my_list:
                varastr = var_name(nd) + '_' + str(len(my_list.keys()))
                if len(varastr) > max_label_size:
                    varastr = varastr[:max_label_size - 3] + '...'
                my_list[nd] = varastr
                color = None
                if nd in vars:
                    color = 'blue'
                elif nd in orphanes:
                    color = 'gray'
                if color is None:
                    g.add_node(pd.Node(varastr))
                elif high_contrast:
                    g.add_node(pd.Node(varastr, style='filled',
                                       fillcolor=color))
                else:
                    g.add_node(pd.Node(varastr, color=color))
            else:
                varastr = my_list[nd]
            label = None
            if len(app.outputs) > 1:
                label = str(i)
            g.add_edge(pd.Edge(astr, varastr, label=label))
        for nd in app.inputs:
            if nd.owner:
                plot_apply(nd.owner, d - 1)
Ejemplo n.º 21
0
 def createToolNode(self, word):
     return pydot.Node(word, style="filled", fillcolor="#0000ff")
Ejemplo n.º 22
0
def pydotprint(fct, outfile=None,
               compact=True, format='png', with_ids=False,
               high_contrast=True, cond_highlight=None, colorCodes=None,
               max_label_size=70, scan_graphs=False,
               var_with_name_simple=False,
               print_output_file=True,
               assert_nb_all_strings=-1,
               return_image=False,
               ):
    """Print to a file the graph of a compiled theano function's ops. Supports
    all pydot output formats, including png and svg.

    :param fct: a compiled Theano function, a Variable, an Apply or
                a list of Variable.
    :param outfile: the output file where to put the graph.
    :param compact: if True, will remove intermediate var that don't have name.
    :param format: the file format of the output.
    :param with_ids: Print the toposort index of the node in the node name.
                     and an index number in the variable ellipse.
    :param high_contrast: if true, the color that describes the respective
            node is filled with its corresponding color, instead of coloring
            the border
    :param colorCodes: dictionary with names of ops as keys and colors as
            values
    :param cond_highlight: Highlights a lazy if by sorrounding each of the 3
                possible categories of ops with a border. The categories
                are: ops that are on the left branch, ops that are on the
                right branch, ops that are on both branches
                As an alternative you can provide the node that represents
                the lazy if
    :param scan_graphs: if true it will plot the inner graph of each scan op
                in files with the same name as the name given for the main
                file to which the name of the scan op is concatenated and
                the index in the toposort of the scan.
                This index can be printed with the option with_ids.
    :param var_with_name_simple: If true and a variable have a name,
                we will print only the variable name.
                Otherwise, we concatenate the type to the var name.
    :param assert_nb_all_strings: Used for tests. If non-negative, assert that
                the number of unique string nodes in the dot graph is equal to
                this number. This is used in tests to verify that dot won't
                merge Theano nodes.
    :param return_image: If True, it will create the image and return it.
        Useful to display the image in ipython notebook.

        .. code-block:: python

            import theano
            v = theano.tensor.vector()
            from IPython.display import SVG
            SVG(theano.printing.pydotprint(v*2, return_image=True,
                                           format='svg'))

    In the graph, ellipses are Apply Nodes (the execution of an op)
    and boxes are variables.  If variables have names they are used as
    text (if multiple vars have the same name, they will be merged in
    the graph).  Otherwise, if the variable is constant, we print its
    value and finally we print the type + a unique number to prevent
    multiple vars from being merged.  We print the op of the apply in
    the Apply box with a number that represents the toposort order of
    application of those Apply.  If an Apply has more than 1 input, we
    label each edge between an input and the Apply node with the
    input's index.

    Green boxes are inputs variables to the graph,
    blue boxes are outputs variables of the graph,
    grey boxes are variables that are not outputs and are not used,
    red ellipses are transfers from/to the gpu (ops with names GpuFromHost,
    HostFromGpu).

    For edges, they are black by default. If a node returns a view
    of an input, we put the corresponding input edge in blue. If it
    returns a destroyed input, we put the corresponding edge in red.

    .. note::

        Since October 20th, 2014, this print the inner function of all
        scan separately after the top level debugprint output.

    """
    if colorCodes is None:
        colorCodes = default_colorCodes

    if outfile is None:
        outfile = os.path.join(config.compiledir, 'theano.pydotprint.' +
                               config.device + '.' + format)

    if isinstance(fct, Function):
        mode = fct.maker.mode
        profile = getattr(fct, "profile", None)
        if (not isinstance(mode, ProfileMode)
                or fct not in mode.profile_stats):
                mode = None
        outputs = fct.maker.fgraph.outputs
        topo = fct.maker.fgraph.toposort()
    elif isinstance(fct, gof.FunctionGraph):
        mode = None
        profile = None
        outputs = fct.outputs
        topo = fct.toposort()
    else:
        if isinstance(fct, gof.Variable):
            fct = [fct]
        elif isinstance(fct, gof.Apply):
            fct = fct.outputs
        assert isinstance(fct, (list, tuple))
        assert all(isinstance(v, gof.Variable) for v in fct)
        fct = gof.FunctionGraph(inputs=gof.graph.inputs(fct),
                                outputs=fct)
        mode = None
        profile = None
        outputs = fct.outputs
        topo = fct.toposort()
    if not pydot_imported:
        raise RuntimeError("Failed to import pydot. You must install pydot"
                           " for `pydotprint` to work.")
        return

    g = pd.Dot()

    if cond_highlight is not None:
        c1 = pd.Cluster('Left')
        c2 = pd.Cluster('Right')
        c3 = pd.Cluster('Middle')
        cond = None
        for node in topo:
            if (node.op.__class__.__name__ == 'IfElse'
                    and node.op.name == cond_highlight):
                cond = node
        if cond is None:
            _logger.warn("pydotprint: cond_highlight is set but there is no"
                         " IfElse node in the graph")
            cond_highlight = None

    if cond_highlight is not None:
        def recursive_pass(x, ls):
            if not x.owner:
                return ls
            else:
                ls += [x.owner]
                for inp in x.inputs:
                    ls += recursive_pass(inp, ls)
                return ls

        left = set(recursive_pass(cond.inputs[1], []))
        right = set(recursive_pass(cond.inputs[2], []))
        middle = left.intersection(right)
        left = left.difference(middle)
        right = right.difference(middle)
        middle = list(middle)
        left = list(left)
        right = list(right)

    var_str = {}
    all_strings = set()

    def var_name(var):
        if var in var_str:
            return var_str[var]

        if var.name is not None:
            if var_with_name_simple:
                varstr = var.name
            else:
                varstr = 'name=' + var.name + " " + str(var.type)
        elif isinstance(var, gof.Constant):
            dstr = 'val=' + str(numpy.asarray(var.data))
            if '\n' in dstr:
                dstr = dstr[:dstr.index('\n')]
            varstr = '%s %s' % (dstr, str(var.type))
        elif (var in input_update
              and input_update[var].variable.name is not None):
            if var_with_name_simple:
                varstr = input_update[var].variable.name + " UPDATE"
            else:
                varstr = (input_update[var].variable.name + " UPDATE "
                          + str(var.type))
        else:
            # a var id is needed as otherwise var with the same type will be
            # merged in the graph.
            varstr = str(var.type)
        if (varstr in all_strings) or with_ids:
            idx = ' id=' + str(len(var_str))
            if len(varstr) + len(idx) > max_label_size:
                varstr = varstr[:max_label_size - 3 - len(idx)] + idx + '...'
            else:
                varstr = varstr + idx
        elif len(varstr) > max_label_size:
            varstr = varstr[:max_label_size - 3] + '...'
            idx = 1
            while varstr in all_strings:
                idx += 1
                suffix = ' id=' + str(idx)
                varstr = (varstr[:max_label_size - 3 - len(suffix)] +
                          '...' +
                          suffix)
        var_str[var] = varstr
        all_strings.add(varstr)

        return varstr
    apply_name_cache = {}

    def apply_name(node):
        if node in apply_name_cache:
            return apply_name_cache[node]
        prof_str = ''
        if mode:
            time = mode.profile_stats[fct].apply_time.get(node, 0)
            # second, % total time in profiler, %fct time in profiler
            if mode.local_time == 0:
                pt = 0
            else:
                pt = time * 100 / mode.local_time
            if mode.profile_stats[fct].fct_callcount == 0:
                pf = 0
            else:
                pf = time * 100 / mode.profile_stats[fct].fct_call_time
            prof_str = '   (%.3fs,%.3f%%,%.3f%%)' % (time, pt, pf)
        elif profile:
            time = profile.apply_time.get(node, 0)
            # second, %fct time in profiler
            if profile.fct_callcount == 0:
                pf = 0
            else:
                pf = time * 100 / profile.fct_call_time
            prof_str = '   (%.3fs,%.3f%%)' % (time, pf)
        applystr = str(node.op).replace(':', '_')
        applystr += prof_str
        if (applystr in all_strings) or with_ids:
            idx = ' id=' + str(topo.index(node))
            if len(applystr) + len(idx) > max_label_size:
                applystr = (applystr[:max_label_size - 3 - len(idx)] + idx
                            + '...')
            else:
                applystr = applystr + idx
        elif len(applystr) > max_label_size:
            applystr = applystr[:max_label_size - 3] + '...'
            idx = 1
            while applystr in all_strings:
                idx += 1
                suffix = ' id=' + str(idx)
                applystr = (applystr[:max_label_size - 3 - len(suffix)] +
                            '...' +
                            suffix)

        all_strings.add(applystr)
        apply_name_cache[node] = applystr
        return applystr

    # Update the inputs that have an update function
    input_update = {}
    # Here outputs can be the original list, as we should not change
    # it, we must copy it.
    outputs = list(outputs)
    if isinstance(fct, Function):
        for i in reversed(fct.maker.expanded_inputs):
            if i.update is not None:
                input_update[outputs.pop()] = i

    apply_shape = 'ellipse'
    var_shape = 'box'
    for node_idx, node in enumerate(topo):
        astr = apply_name(node)

        use_color = None
        for opName, color in colorCodes.items():
            if opName in node.op.__class__.__name__:
                use_color = color

        if use_color is None:
            nw_node = pd.Node(astr, shape=apply_shape)
        elif high_contrast:
            nw_node = pd.Node(astr, style='filled', fillcolor=use_color,
                              shape=apply_shape)
        else:
            nw_node = pd.Node(astr, color=use_color, shape=apply_shape)
        g.add_node(nw_node)
        if cond_highlight:
            if node in middle:
                c3.add_node(nw_node)
            elif node in left:
                c1.add_node(nw_node)
            elif node in right:
                c2.add_node(nw_node)

        for id, var in enumerate(node.inputs):
            varstr = var_name(var)
            label = str(var.type)
            if len(node.inputs) > 1:
                label = str(id) + ' ' + label
            if len(label) > max_label_size:
                label = label[:max_label_size - 3] + '...'
            param = {}
            if hasattr(node.op, 'view_map') and id in reduce(
                    list.__add__, node.op.view_map.values(), []):
                    param['color'] = 'blue'
            elif hasattr(node.op, 'destroy_map') and id in reduce(
                    list.__add__, node.op.destroy_map.values(), []):
                        param['color'] = 'red'
            if var.owner is None:
                if high_contrast:
                    g.add_node(pd.Node(varstr,
                                       style='filled',
                                       fillcolor='green',
                                       shape=var_shape))
                else:
                    g.add_node(pd.Node(varstr, color='green', shape=var_shape))
                g.add_edge(pd.Edge(varstr, astr, label=label, **param))
            elif var.name or not compact:
                g.add_edge(pd.Edge(varstr, astr, label=label, **param))
            else:
                # no name, so we don't make a var ellipse
                g.add_edge(pd.Edge(apply_name(var.owner), astr,
                           label=label, **param))

        for id, var in enumerate(node.outputs):
            varstr = var_name(var)
            out = var in outputs
            label = str(var.type)
            if len(node.outputs) > 1:
                label = str(id) + ' ' + label
            if len(label) > max_label_size:
                label = label[:max_label_size - 3] + '...'
            if out:
                g.add_edge(pd.Edge(astr, varstr, label=label))
                if high_contrast:
                    g.add_node(pd.Node(varstr, style='filled',
                                       fillcolor='blue', shape=var_shape))
                else:
                    g.add_node(pd.Node(varstr, color='blue', shape=var_shape))
            elif len(var.clients) == 0:
                g.add_edge(pd.Edge(astr, varstr, label=label))
                if high_contrast:
                    g.add_node(pd.Node(varstr, style='filled',
                                       fillcolor='grey', shape=var_shape))
                else:
                    g.add_node(pd.Node(varstr, color='grey', shape=var_shape))
            elif var.name or not compact:
                g.add_edge(pd.Edge(astr, varstr, label=label))
#            else:
            # don't add egde here as it is already added from the inputs.

    if cond_highlight:
        g.add_subgraph(c1)
        g.add_subgraph(c2)
        g.add_subgraph(c3)

    if not outfile.endswith('.' + format):
        outfile += '.' + format

    if assert_nb_all_strings != -1:
        assert len(all_strings) == assert_nb_all_strings, len(all_strings)

    if scan_graphs:
        scan_ops = [(idx, x) for idx, x in enumerate(topo)
                    if isinstance(x.op, theano.scan_module.scan_op.Scan)]
        path, fn = os.path.split(outfile)
        basename = '.'.join(fn.split('.')[:-1])
        # Safe way of doing things .. a file name may contain multiple .
        ext = fn[len(basename):]

        for idx, scan_op in scan_ops:
            # is there a chance that name is not defined?
            if hasattr(scan_op.op, 'name'):
                new_name = basename + '_' + scan_op.op.name + '_' + str(idx)
            else:
                new_name = basename + '_' + str(idx)
            new_name = os.path.join(path, new_name + ext)
            pydotprint(scan_op.op.fn, new_name, compact, format, with_ids,
                       high_contrast, cond_highlight, colorCodes,
                       max_label_size, scan_graphs)

    if return_image:
        return g.create(prog='dot', format=format)
    else:
        g.write(outfile, prog='dot', format=format)
        if print_output_file:
            print 'The output file is available at', outfile
Ejemplo n.º 23
0
 def createProbableIngreNode(self, words):
     return pydot.Node(words, style="filled", fillcolor="#ffff66")
Ejemplo n.º 24
0
def get_pydot_graph(caffe_net, rankdir, label_edges=True):
    """Create a data structure which represents the `caffe_net`.

    Parameters
    ----------
    caffe_net : object
    rankdir : {'LR', 'TB', 'BT'}
        Direction of graph layout.
    label_edges : boolean, optional
        Label the edges (default is True).

    Returns
    -------
    pydot graph object
    """
    pydot_graph = pydot.Dot(caffe_net.name,
                            graph_type='digraph',
                            rankdir=rankdir)
    pydot_nodes = {}
    pydot_edges = []
    for layer in caffe_net.layer:
        node_label = get_layer_label(layer, rankdir)
        node_name = "%s_%s" % (layer.name, layer.type)
        if (len(layer.bottom) == 1 and len(layer.top) == 1
                and layer.bottom[0] == layer.top[0]):
            # We have an in-place neuron layer.
            pydot_nodes[node_name] = pydot.Node(node_label,
                                                **NEURON_LAYER_STYLE)
        else:
            layer_style = LAYER_STYLE_DEFAULT
            layer_style['fillcolor'] = choose_color_by_layertype(layer.type)
            pydot_nodes[node_name] = pydot.Node(node_label, **layer_style)
        for bottom_blob in layer.bottom:
            pydot_nodes[bottom_blob + '_blob'] = pydot.Node(
                '%s' % bottom_blob, **BLOB_STYLE)
            edge_label = '""'
            pydot_edges.append({
                'src': bottom_blob + '_blob',
                'dst': node_name,
                'label': edge_label
            })
        for top_blob in layer.top:
            pydot_nodes[top_blob + '_blob'] = pydot.Node('%s' % (top_blob))
            if label_edges:
                edge_label = get_edge_label(layer)
            else:
                edge_label = '""'
            pydot_edges.append({
                'src': node_name,
                'dst': top_blob + '_blob',
                'label': edge_label
            })
    # Now, add the nodes and edges to the graph.
    for node in list(pydot_nodes.values()):
        pydot_graph.add_node(node)
    for edge in pydot_edges:
        pydot_graph.add_edge(
            pydot.Edge(pydot_nodes[edge['src']],
                       pydot_nodes[edge['dst']],
                       label=edge['label']))
    return pydot_graph
Ejemplo n.º 25
0
 def createIngreWholeNode(self):
     arr = [w for (w, T) in self.taggedIngredient]
     ingWhole = " ".join(arr)
     return pydot.Node(ingWhole, style="filled", fillcolor="#ccf5ff")
Ejemplo n.º 26
0
def graph_get(cr, graph, wkf_ids, nested, workitem, witm_trans,
              processed_subflows):
    import pydot
    cr.execute(
        'select * from wkf_activity where wkf_id in (' +
        ','.join(['%s'] * len(wkf_ids)) + ')', wkf_ids)
    nodes = cr.dictfetchall()
    activities = {}
    actfrom = {}
    actto = {}
    for n in nodes:
        activities[n['id']] = n
        if n['subflow_id'] and nested and n[
                'subflow_id'] not in processed_subflows:
            processed_subflows.add(
                n['subflow_id']
            )  # don't create multiple times the same cluster.
            cr.execute('select * from wkf where id=%s', (n['subflow_id'], ))
            wkfinfo = cr.dictfetchone()
            graph2 = pydot.Cluster('subflow' + str(n['subflow_id']),
                                   fontsize='12',
                                   label="\"Subflow: %s\\nOSV: %s\"" %
                                   (n['name'], wkfinfo['osv']))
            (s1, s2) = graph_get(cr, graph2, [n['subflow_id']], True, workitem,
                                 witm_trans, processed_subflows)
            graph.add_subgraph(graph2)
            actfrom[n['id']] = s2
            actto[n['id']] = s1
        else:
            args = {}
            if n['flow_start'] or n['flow_stop']:
                args['style'] = 'filled'
                args['color'] = 'lightgrey'
            args['label'] = n['name']
            workitems = ''
            if n['id'] in workitem:
                workitems = '\\nx ' + str(workitem[n['id']])
                args['label'] += workitems
                args['color'] = "red"
                args['style'] = 'filled'
            if n['subflow_id']:
                args['shape'] = 'box'
                if nested and n['subflow_id'] in processed_subflows:
                    cr.execute('select * from wkf where id=%s',
                               (n['subflow_id'], ))
                    wkfinfo = cr.dictfetchone()
                    args['label'] = \
                        '\"Subflow: %s\\nOSV: %s\\n(already expanded)%s\"' % \
                        (n['name'], wkfinfo['osv'], workitems)
                    args['color'] = 'green'
                    args['style'] = 'filled'
            graph.add_node(pydot.Node(n['id'], **args))
            actfrom[n['id']] = (n['id'], {})
            actto[n['id']] = (n['id'], {})
            node_ids = tuple(map(itemgetter('id'), nodes))
    cr.execute(
        'select * from wkf_transition where act_from IN %s ORDER BY sequence,id',
        (node_ids, ))
    transitions = cr.dictfetchall()
    for t in transitions:
        if not t['act_to'] in activities:
            continue
        args = {
            'label':
            str(t['condition']).replace(' or ',
                                        '\\nor ').replace(' and ', '\\nand ')
        }
        if t['signal']:
            args['label'] += '\\n' + str(t['signal'])
            args['style'] = 'bold'

        if activities[t['act_from']]['split_mode'] == 'AND':
            args['arrowtail'] = 'box'
        elif str(activities[t['act_from']]['split_mode']) == 'OR ':
            args['arrowtail'] = 'inv'

        if activities[t['act_to']]['join_mode'] == 'AND':
            args['arrowhead'] = 'crow'
        if t['id'] in witm_trans:
            args['color'] = 'red'

        activity_from = actfrom[t['act_from']][1].get(
            t['signal'], actfrom[t['act_from']][0])
        activity_to = actto[t['act_to']][1].get(t['signal'],
                                                actto[t['act_to']][0])
        graph.add_edge(
            pydot.Edge(str(activity_from),
                       str(activity_to),
                       fontsize='10',
                       **args))

    cr.execute(
        'select * from wkf_activity where flow_start=True and wkf_id in (' +
        ','.join(['%s'] * len(wkf_ids)) + ')', wkf_ids)
    start = cr.fetchone()[0]
    cr.execute(
        "select 'subflow.'||name,id from wkf_activity where flow_stop=True and wkf_id in ("
        + ','.join(['%s'] * len(wkf_ids)) + ')', wkf_ids)
    stop = cr.fetchall()
    if stop:
        stop = (stop[0][1], dict(stop))
    else:
        stop = ("stop", {})
    return (start, {}), stop
Ejemplo n.º 27
0
 def _make_graph(url, parent_node):
     for child_url in tree[url]:
         child_node = pydot.Node('"' + child_url.decode('utf-8') + '"')
         graph.add_node(child_node)
         graph.add_edge(pydot.Edge(parent_node, child_node))
         _make_graph(child_url, child_node)
Ejemplo n.º 28
0
    def add_to_graph(self, graph, draw_callbacks=False, prefix=""):
        """
        Adds the FSM(s) to a pydot graph
        """
        fsms = pydot.Cluster("FSMs",
                             label="FSMs",
                             fontname="helvetica",
                             color="gray",
                             fontcolor="gray")
        graph.add_subgraph(fsms)
        fsm_nodes = {}
        for name, fsm in self.fsms.iteritems():

            fsm_graph = pydot.Cluster(name,
                                      label=name,
                                      fontname="helvetica",
                                      color="gray",
                                      fontcolor="gray")

            fsms.add_subgraph(fsm_graph)

            label = pydot.Node(prefix + "_fsm_" + name,
                               label=name,
                               style="invis")
            fsm_graph.add_node(label)

            fsm_nodes[name] = label
            node_mapping = {}
            for state in fsm.states():

                if state != "none":
                    state_node = pydot.Node(name=prefix + state,
                                            shape="ellipse",
                                            label=state)
                    fsm_graph.add_node(state_node)
                    node_mapping[state] = state_node
                    fsm_nodes[(name, state)] = state_node

            for ev in fsm.events():
                src = ev["src"]
                dst = ev["dst"]
                name = ev["name"]

                if "before" in ev and draw_callbacks:
                    before = '<font color="orange">%s</font>' % ev["before"]
                else:
                    before = ""

                if "after" in ev and draw_callbacks:
                    after = '<font color="orange">%s</font><br/>' % ev["after"]
                else:
                    after = ""

                label = "<%s  <b>%s</b> %s>" % (before, name, after)

                transition_edge = pydot.Edge(node_mapping[src],
                                             node_mapping[dst],
                                             label=label)

                fsm_graph.add_edge(transition_edge)
            if draw_callbacks:
                for name, callback in fsm.state_callbacks.iteritems():
                    if "enter" in callback:
                        node = pydot.Node(name=prefix + "_enter_" + name,
                                          shape="circle",
                                          style="invis")
                        fsm_graph.add_node(node)
                        enter_edge = pydot.Edge(node,
                                                node_mapping[name],
                                                label=callback["enter"],
                                                style="dotted",
                                                fontcolor="blue")
                        fsm_graph.add_edge(enter_edge)
                    if "exit" in callback:
                        node = pydot.Node(name=prefix + "_exit_" + name,
                                          shape="circle",
                                          style="invis")
                        fsm_graph.add_node(node)
                        exit_edge = pydot.Edge(node_mapping[name],
                                               node,
                                               label=callback["exit"],
                                               style="dotted",
                                               fontcolor="blue")
                        fsm_graph.add_edge(exit_edge)
        return fsm_nodes
Ejemplo n.º 29
0
    def get_beam_dot(self, dictionary=None, n_best=None):
        """Creates pydot graph representation of the beam.

        :param outputs: self.outputs from the beam
        :param dictionary: tok 2 word dict to save words in the tree nodes
        :returns: pydot graph
        """
        try:
            import pydot
        except ImportError:
            print("Please install pydot package to dump beam visualization")

        graph = pydot.Dot(graph_type='digraph')
        outputs = [i.tolist() for i in self.outputs]
        bookkeep = [i.tolist() for i in self.bookkeep]
        all_scores = [i.tolist() for i in self.all_scores]
        if n_best is None:
            n_best = int(self.beam_size / 2)

        # get top nbest hyp
        top_hyp_idx_n_best = []
        n_best_colors = [
            'aquamarine', 'chocolate1', 'deepskyblue', 'green2', 'tan'
        ]
        sorted_finished = self.get_rescored_finished(n_best=n_best)
        for hyptail in sorted_finished:
            # do not include EOS since it has rescored score not from original
            # self.all_scores, we color EOS with black
            top_hyp_idx_n_best.append(self.get_hyp_from_finished(hyptail))

        # create nodes
        for tstep, lis in enumerate(outputs):
            for hypid, token in enumerate(lis):
                if tstep == 0:
                    hypid = 0  # collapse all __NULL__ nodes
                node_tail = self.HypothesisTail(timestep=tstep,
                                                hypid=hypid,
                                                score=all_scores[tstep][hypid],
                                                tokenid=token)
                color = 'white'
                rank = None
                for i, hypseq in enumerate(top_hyp_idx_n_best):
                    if node_tail in hypseq:
                        if n_best <= 5:  # color nodes only if <=5
                            color = n_best_colors[i]
                        rank = i
                        break
                label = ("<{}".format(
                    dictionary.
                    vec2txt([token]) if dictionary is not None else token) +
                         " : " + "{:.{prec}f}>".format(
                             all_scores[tstep][hypid], prec=3))

                graph.add_node(
                    pydot.Node(
                        node_tail.__repr__(),
                        label=label,
                        fillcolor=color,
                        style='filled',
                        xlabel='{}'.format(rank) if rank is not None else ''))

        # create edges
        for revtstep, lis in reversed(list(enumerate(bookkeep))):
            for i, prev_id in enumerate(lis):
                from_node = graph.get_node('"{}"'.format(
                    self.HypothesisTail(
                        timestep=revtstep,
                        hypid=prev_id,
                        score=all_scores[revtstep][prev_id],
                        tokenid=outputs[revtstep][prev_id]).__repr__()))[0]
                to_node = graph.get_node('"{}"'.format(
                    self.HypothesisTail(timestep=revtstep + 1,
                                        hypid=i,
                                        score=all_scores[revtstep + 1][i],
                                        tokenid=outputs[revtstep +
                                                        1][i]).__repr__()))[0]
                newedge = pydot.Edge(from_node.get_name(), to_node.get_name())
                graph.add_edge(newedge)

        return graph
Ejemplo n.º 30
0
Archivo: gk.py Proyecto: i2mint/meshed
    def plot(self, filename=None, show=False):
        """
        Plot the graph.

        params:
        :param str filename:
            Write the output to a png, pdf, or graphviz dot file. The extension
            controls the output format.

        :param boolean show:
            If this is set to True, use matplotlib to show the graph diagram
            (Default: False)

        :returns:
            An instance of the pydot graph

        """
        from contextlib import suppress

        with suppress(ModuleNotFoundError, ImportError):
            import pydot
            import matplotlib.pyplot as plt
            import matplotlib.image as mpimg

            assert self.graph is not None

            def get_node_name(a):
                if isinstance(a, DataPlaceholderNode):
                    return a
                return a.name

            g = pydot.Dot(graph_type='digraph')

            # draw nodes
            for nx_node in gr.nodes(self.graph):
                if isinstance(nx_node, DataPlaceholderNode):
                    node = pydot.Node(name=nx_node, shape='rect')
                else:
                    node = pydot.Node(name=nx_node.name, shape='circle')
                g.add_node(node)

            # draw edges
            for src, dst in gr.edges(self.graph):
                src_name = get_node_name(src)
                dst_name = get_node_name(dst)
                edge = pydot.Edge(src=src_name, dst=dst_name)
                g.add_edge(edge)

            # save plot
            if filename:
                basename, ext = os.path.splitext(filename)
                with open(filename, 'w') as fh:
                    if ext.lower() == '.png':
                        fh.write(g.create_png())
                    elif ext.lower() == '.dot':
                        fh.write(g.to_string())
                    elif ext.lower() in ['.jpg', '.jpeg']:
                        fh.write(g.create_jpeg())
                    elif ext.lower() == '.pdf':
                        fh.write(g.create_pdf())
                    elif ext.lower() == '.svg':
                        fh.write(g.create_svg())
                    else:
                        raise Exception(
                            'Unknown file format for saving graph: %s' % ext)

            # display graph via matplotlib
            if show:
                png = g.create_png()
                sio = StringIO(png)
                img = mpimg.imread(sio)
                plt.imshow(img, aspect='equal')
                plt.show()

            return g