Exemple #1
0
    def draw(self):
        s = self.s
        graph = self.graph
        sp = s.sparsity()
        row = sp.row()

        deps = getDeps(s)

        f = s.which_function()

        for k, d in enumerate(deps):
            graph.add_edge(
                pydot.Edge(str(d.__hash__()),
                           "funinput" + str(s.__hash__()) + ":f%d" % k,
                           rankdir="LR"))

        graph = pydot.Cluster(str(s.__hash__()),
                              rank='max',
                              label='Function:\n %s' % f.name())
        self.graph.add_subgraph(graph)

        s = (" %d inputs: |" % f.n_in()) + " | ".join("<f%d> %d" % (i, i)
                                                      for i in range(f.n_in()))
        graph.add_node(
            pydot.Node("funinput" + str(self.s.__hash__()),
                       label=s,
                       shape='Mrecord'))

        s = (" %d outputs: |" % f.n_out()) + " | ".join(
            "<f%d> %d" % (i, i) for i in range(f.n_out()))
        graph.add_node(
            pydot.Node(str(self.s.__hash__()), label=s, shape='Mrecord'))
Exemple #2
0
 def drawSparsity(self,s,id=None,depid=None,graph=None,nzlabels=None):
   if id is None:
     id = str(s.__hash__())
   if depid is None:
     depid = str(s.dep(0).__hash__())
   if graph is None:
     graph = self.graph
   sp = s.sparsity()
   deps = getDeps(s)
   if nzlabels is None:
     nzlabels = list(map(str,list(range(sp.nnz()))))
   nzlabelcounter = 0
   if s.nnz()==s.numel():
     graph.add_node(pydot.Node(id,label="%d x %d" % (s.size1(),s.size2()),shape='rectangle',color=self.sparsitycol,style="filled"))
   else:
     label = '<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">'
     label+="<TR><TD COLSPAN='%d'><font color='#666666'>%s</font></TD></TR>" % (s.size2(), s.dim())
     for i in range(s.size1()):
       label+="<TR>"
       for j in range(s.size2()):
         k = sp.get_nz(i,j)
         if k==-1:
           label+="<TD>.</TD>"
         else:
           label+="<TD PORT='f%d' BGCOLOR='%s'>%s</TD>" % (k,self.sparsitycol,nzlabels[nzlabelcounter])
           nzlabelcounter +=1
       label+="</TR>"
     label+="</TABLE>>"
     graph.add_node(pydot.Node(id,label=label,shape='plaintext'))
   graph.add_edge(pydot.Edge(depid,id))
Exemple #3
0
    def draw(self):
        k = self.s
        graph = self.graph
        dep = getDeps(k)

        show_sp = not (all([d.sparsity() == k.sparsity() for d in dep]))

        if show_sp:
            op = "op"
            self.drawSparsity(k, depid=op + str(k.__hash__()))
        else:
            op = ""

        if len(dep) > 1:
            # Non-commutative operators are represented by 'record' shapes.
            # The dependencies have different 'ports' where arrows should arrive.
            s = print_operator(self.s,
                               ["| <f%d> | " % i for i in range(len(dep))])
            if s.startswith("(|") and s.endswith("|)"):
                s = s[2:-2]

            graph.add_node(
                pydot.Node(op + str(k.__hash__()), label=s, shape='Mrecord'))
            for i, n in enumerate(dep):
                graph.add_edge(
                    pydot.Edge(str(n.__hash__()),
                               op + str(k.__hash__()) + ":f%d" % i))
        else:
            s = print_operator(k, ["."])
            self.graph.add_node(
                pydot.Node(op + str(k.__hash__()), label=s, shape='oval'))
            for i, n in enumerate(dep):
                self.graph.add_edge(
                    pydot.Edge(str(n.__hash__()), op + str(k.__hash__())))
Exemple #4
0
 def draw(self):
     s = self.s
     graph = self.graph
     sp = s.sparsity()
     row = sp.row()
     M = s.getMatrixValue()
     col = "#009900"
     if s.nnz() == s.numel() and s.nnz() == 1:
         graph.add_node(
             pydot.Node(str(self.s.__hash__()) + ":f0",
                        label=str(M[0, 0]),
                        shape='rectangle',
                        color=col))
     else:
         # The Matrix grid is represented by a html table with 'ports'
         label = '<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" COLOR="%s">' % col
         label += "<TR><TD COLSPAN='%d'><font color='#666666'>%s</font></TD></TR>" % (
             s.size2(), s.dim())
         if not "max_numel" in self.kwargs or s.numel(
         ) < self.kwargs["max_numel"]:
             for i in range(s.size1()):
                 label += "<TR>"
                 for j in range(s.size2()):
                     k = sp.get_nz(i, j)
                     if k == -1:
                         label += "<TD>.</TD>"
                     else:
                         label += "<TD PORT='f%d' BGCOLOR='#eeeeee'> %s </TD>" % (
                             k, M[i, j])
                 label += "</TR>"
         label += "</TABLE>>"
         graph.add_node(
             pydot.Node(str(self.s.__hash__()),
                        label=label,
                        shape='plaintext'))
Exemple #5
0
    def draw(self):
        k = self.s
        graph = self.graph
        dep = getDeps(k)
        if not (k.is_commutative()):
            # Non-commutative operators are represented by 'record' shapes.
            # The dependencies have different 'ports' where arrows should arrive.
            if len(dep) == 2:
                s = print_operator(self.s, ["| <f0> | ", " | <f1> |"])
            else:
                s = print_operator(self.s, ["| <f0> | "])
            if s.startswith("(|") and s.endswith("|)"):
                s = s[2:-2]

            graph.add_node(
                pydot.Node(str(k.__hash__()), label=s, shape='Mrecord'))
            for i, n in enumerate(dep):
                graph.add_edge(
                    pydot.Edge(str(n.__hash__()),
                               str(k.__hash__()) + ":f%d" % i))
        else:
            # Commutative operators can be represented more compactly as 'oval' shapes.
            s = print_operator(k, [".", "."])
            if s.startswith("(.") and s.endswith(".)"):
                s = s[2:-2]
            if s.startswith("(") and s.endswith(")"):
                s = s[1:-1]
            self.graph.add_node(
                pydot.Node(str(k.__hash__()), label=s, shape='oval'))
            for i, n in enumerate(dep):
                self.graph.add_edge(
                    pydot.Edge(str(n.__hash__()), str(k.__hash__())))
Exemple #6
0
    def draw(self):
        s = self.s
        graph = self.graph
        n = getDeps(s)[0]

        show_sp = not (s.nnz() == s.numel() and s.nnz() == 1)

        if show_sp:
            op = "op"
            self.drawSparsity(s, depid=op + str(s.__hash__()))
        else:
            op = ""

        sp = s.sparsity()
        row = sp.row()
        M = s.mapping()
        col = "#333333"
        if s.nnz() == s.numel() and s.nnz() == 1:
            graph.add_node(
                pydot.Node(op + str(s.__hash__()) + ":f0",
                           label="[%s]" % str(M[0, 0]),
                           shape='rectangle',
                           style="filled",
                           fillcolor='#eeeeff'))
        else:
            # The Matrix grid is represented by a html table with 'ports'
            label = '<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" COLOR="%s">' % col
            label += "<TR><TD COLSPAN='%d' PORT='entry'>getNonzeros</TD></TR>" % (
                s.size2())
            if not "max_nnz" in self.kwargs or s.nnz(
            ) < self.kwargs["max_nnz"]:
                for i in range(s.size1()):
                    label += "<TR>"
                    for j in range(s.size2()):
                        k = sp.get_nz(i, j)
                        if k == -1:
                            label += "<TD>.</TD>"
                        else:
                            label += "<TD PORT='f%d' BGCOLOR='#eeeeff'> %s </TD>" % (
                                k, M[i, j])
                    label += "</TR>"
            label += "</TABLE>>"
            graph.add_node(
                pydot.Node(op + str(s.__hash__()),
                           label=label,
                           shape='plaintext'))
        self.graph.add_edge(
            pydot.Edge(str(n.__hash__()), op + str(s.__hash__())))
Exemple #7
0
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()
Exemple #8
0
 def draw(self):
   k = self.s
   graph = self.graph
   dep = getDeps(k)
   s = print_operator(k,[".", "."])
   self.graph.add_node(pydot.Node(str(k.__hash__()),label=s,shape='oval'))
   self.graph.add_edge(pydot.Edge(str(dep[0].__hash__()),str(k.__hash__())))
Exemple #9
0
  def draw(self):
    k = self.s
    graph = self.graph
    dep = getDeps(k)

    self.graph.add_node(pydot.Node(str(k.__hash__()),label="densify(.)",shape='oval'))
    self.graph.add_edge(pydot.Edge(str(dep[0].__hash__()),str(k.__hash__())))
Exemple #10
0
 def create_node(self, node):
     "Create a graphviz node from the miniast node"
     label = '"%s"' % self.format_node(node, want_type_info=False)
     self.counter += 1
     pydot_node = pydot.Node(str(self.counter), label=label)
     self.add_node(pydot_node)
     return pydot_node
Exemple #11
0
    def draw(self):
        s = self.s
        graph = self.graph
        sp = s.sparsity()
        row = sp.row()

        # The Matrix grid is represented by a html table with 'ports'
        label = '<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">'
        for i in range(s.size1()):
            label += "<TR>"
            for j in range(s.size2()):
                k = sp.get_nz(i, j)
                if k == -1:
                    label += "<TD>.</TD>"
                else:
                    sx = s.nz[k]
                    if self.shouldEmbed(sx):
                        label += "<TD BGCOLOR='#eeeeee'>%s</TD>" % str(sx)
                    else:
                        self.graph.add_edge(
                            pydot.Edge(str(sx.__hash__()),
                                       "%s:f%d" % (str(self.s.__hash__()), k)))
                        label += "<TD PORT='f%d' BGCOLOR='#eeeeee'> <font color='#666666'>(%d,%d|%d)</font> </TD>" % (
                            k, i, j, k)
            label += "</TR>"
        label += "</TABLE>>"
        graph.add_node(
            pydot.Node(str(self.s.__hash__()), label=label, shape='plaintext'))
Exemple #12
0
 def addElement(self, ID, label, style):
     Node = PD.Node(ID)
     Node.set_shape(style['shape'])
     Node.set_color(style['color'])
     #Node.set_fontsize(FONTSIZE)
     Node.set_label(label)
     self.G.add_node(Node)
     return Node
Exemple #13
0
    def draw(self):
        k = self.s
        graph = self.graph
        dep = getDeps(k)

        show_sp = True

        if k.is_unary() and dep[0].sparsity() == k.sparsity():
            show_sp = False
        if k.is_binary() and dep[0].sparsity() == k.sparsity(
        ) and dep[1].sparsity() == k.sparsity():
            show_sp = False

        if show_sp:
            op = "op"
            self.drawSparsity(k, depid=op + str(k.__hash__()))
        else:
            op = ""

        if not (k.is_commutative()):
            # Non-commutative operators are represented by 'record' shapes.
            # The dependencies have different 'ports' where arrows should arrive.
            s = print_operator(self.s, ["| <f0> | ", " | <f1> |"])
            if s.startswith("(|") and s.endswith("|)"):
                s = s[2:-2]

            graph.add_node(
                pydot.Node(op + str(k.__hash__()), label=s, shape='Mrecord'))
            for i, n in enumerate(dep):
                graph.add_edge(
                    pydot.Edge(str(n.__hash__()),
                               op + str(k.__hash__()) + ":f%d" % i))
        else:
            # Commutative operators can be represented more compactly as 'oval' shapes.
            s = print_operator(k, [".", "."])
            if s.startswith("(.") and s.endswith(".)"):
                s = s[2:-2]
            if s.startswith("(") and s.endswith(")"):
                s = s[1:-1]
            self.graph.add_node(
                pydot.Node(op + str(k.__hash__()), label=s, shape='oval'))
            for i, n in enumerate(dep):
                self.graph.add_edge(
                    pydot.Edge(str(n.__hash__()), op + str(k.__hash__())))
Exemple #14
0
 def draw(self):
   if len(self.invdep[self.s]) == 1:
     master = list(self.invdep[self.s])[0]
     if hasattr(self.artists[master],'shouldEmbed'):
       if self.artists[master].shouldEmbed(self.s):
         return
   style = "solid" # Symbolic nodes are represented box'es
   if self.s.is_constant():
     style = "bold" # Constants are represented by bold box'es
   self.graph.add_node(pydot.Node(str(self.s.__hash__()),label=str(self.s),shape="box",style=style))
Exemple #15
0
  def draw(self):
    k = self.s
    graph = self.graph
    dep = getDeps(k)

    show_sp = True

    s = "<f0> ? | <f1> true"

    graph.add_node(pydot.Node(str(k.__hash__()),label=s,shape='Mrecord'))
    for i,n in enumerate(dep):
      graph.add_edge(pydot.Edge(str(n.__hash__()),str(k.__hash__())+":f%d" % i))
Exemple #16
0
  def draw(self):
    k = self.s
    graph = self.graph
    dep = getDeps(k)

    # Non-commutative operators are represented by 'record' shapes.
    # The dependencies have different 'ports' where arrows should arrive.
    s = "mul(| <f0> | , | <f1> | )"

    graph.add_node(pydot.Node(str(k.__hash__()),label=s,shape='Mrecord'))
    for i,n in enumerate(dep):
      graph.add_edge(pydot.Edge(str(n.__hash__()),str(k.__hash__())+":f%d" % i))
Exemple #17
0
    def visit_FunctionNode(self, node):
        "Create a graphviz graph"
        self.graph = pydot.Dot(self.name, graph_type='digraph')

        pydot_function = self.create_node(node)
        pydot_body = self.visit(node.body)

        # Create artificial arguments for brevity
        pydot_args = pydot.Node("Arguments (omitted)")
        self.add_node(pydot_args)

        self.add_edge(pydot_function, pydot_body)
        self.add_edge(pydot_function, pydot_args)

        return self.graph
Exemple #18
0
    def draw(self):
        s = self.s
        graph = self.graph
        entry = getDeps(s)[0]
        target = getDeps(s)[1]

        show_sp = not (all([d.sparsity() == s.sparsity() for d in getDeps(s)]))

        if show_sp:
            op = "op"
            self.drawSparsity(s, depid=op + str(s.__hash__()))
        else:
            op = ""

        sp = target.sparsity()
        row = sp.row()
        M = list(s.mapping())
        Mk = 0
        col = "#333333"

        # The Matrix grid is represented by a html table with 'ports'
        label = '<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" COLOR="%s">' % col
        label += "<TR><TD COLSPAN='%d' PORT='entry'>addNonzeros</TD></TR>" % (
            s.size2())
        for i in range(s.size1()):
            label += "<TR>"
            for j in range(s.size2()):
                k = sp.get_nz(i, j)
                if k == -1 or Mk >= len(M) or k != M[Mk]:
                    label += "<TD>.</TD>"
                    if Mk < len(M) - 1 and M[Mk] == -1 and k != -1: Mk += 1
                else:
                    label += "<TD PORT='f%d' BGCOLOR='#eeeeff'> %s </TD>" % (
                        Mk, Mk)
                    Mk += 1
            label += "</TR>"
        label += "</TABLE>>"
        graph.add_node(
            pydot.Node(op + str(s.__hash__()), label=label, shape='plaintext'))
        self.graph.add_edge(
            pydot.Edge(str(entry.__hash__()),
                       op + str(s.__hash__()) + ':entry'))
        self.graph.add_edge(
            pydot.Edge(str(target.__hash__()), op + str(s.__hash__())))
Exemple #19
0
def write(G, weighted=False):
    """
    Return a string specifying the given graph in Dot language.
    
    @type  G: graph
    @param G: Graph.

    @type  weighted: boolean
    @param weighted: Whether edges should be labelled with their weight.
    
    @rtype:  string
    @return: String specifying the graph in Dot Language.
    """
    dotG = pydot.Dot()

    if not 'name' in dir(G):
        dotG.set_name('graphname')
    else:
        dotG.set_name(G.name)

    if (isinstance(G, graph)):
        dotG.set_type('graph')
        directed = False
    elif (isinstance(G, digraph)):
        dotG.set_type('digraph')
        directed = True
    elif (isinstance(G, hypergraph)):
        return write_hypergraph(G)
    else:
        raise InvalidGraphType("Expected graph or digraph, got %s" % repr(G))

    for node in G.nodes():
        attr_list = {}
        for attr in G.node_attributes(node):
            attr_list[str(attr[0])] = str(attr[1])

        newNode = pydot.Node(str(node), **attr_list)

        dotG.add_node(newNode)

    # Pydot doesn't work properly with the get_edge, so we use
    #  our own set to keep track of what's been added or not.
    seen_edges = set([])
    for edge_from, edge_to in G.edges():
        if (str(edge_from) + "-" + str(edge_to)) in seen_edges:
            continue

        if (not directed) and (str(edge_to) + "-" +
                               str(edge_from)) in seen_edges:
            continue

        attr_list = {}
        for attr in G.edge_attributes((edge_from, edge_to)):
            attr_list[str(attr[0])] = str(attr[1])

        if str(G.edge_label((edge_from, edge_to))):
            attr_list['label'] = str(G.edge_label((edge_from, edge_to)))

        elif weighted:
            attr_list['label'] = str(G.edge_weight((edge_from, edge_to)))

        if weighted:
            attr_list['weight'] = str(G.edge_weight((edge_from, edge_to)))

        newEdge = pydot.Edge(str(edge_from), str(edge_to), **attr_list)

        dotG.add_edge(newEdge)

        seen_edges.add(str(edge_from) + "-" + str(edge_to))

    return dotG.to_string()