Exemple #1
0
def draw_graph(svg_file, V, A, show_labels=False, ignore=None, back=None,
               loss=None, graph_attrs=None, verbose=False):
    """Draw an arc-flow graph in .svg format."""
    from pygraphviz.agraph import AGraph
    if ignore is None:
        ignore = []
    if back is None:
        back = []
    if loss is None:
        loss = []
    elif not isinstance(loss, (tuple, list)):
        loss = [loss]
    g = AGraph(
        rankdir="LR", directed=True, bgcolor="white",
        ranksep="1.0", nodesep="0.10",
        strict=False
    )
    if graph_attrs is not None:
        for attr, value in graph_attrs.items():
            g.graph_attr[attr] = value
    g.node_attr["shape"] = "ellipse"
    g.node_attr["color"] = "black"
    g.node_attr["fontcolor"] = "black"
    g.node_attr["penwidth"] = "2.0"

    lbls = sorted(
        set(i for (u, v, i) in A if i not in loss),
        key=lambda lbl: (repr(type(lbl)), lbl)
    )

    colors = Colors.uniquecolors(len(lbls)+1, v=0.5, p=0.0)

    used = set()
    for (u, v, i) in A:
        if (u, v) in ignore:
            continue
        used.add(u)
        used.add(v)
        if (u, v) in back:
            u, v = v, u
            d = "back"
        else:
            d = "front"
        if i in loss:
            g.add_edge(u, v, color="black", style="dashed", penwidth=2, dir=d)
        else:
            lbl = str(i) if show_labels else ""
            color = colors[lbls.index(i) % len(colors)]
            g.add_edge(u, v, color=color, penwidth=2, label=lbl, dir=d)

    for v in V:
        if v not in used:
            g.add_node(v)

    g.draw(svg_file, format="svg", prog="dot")
    if verbose:
        print("SVG file '{0}' generated!".format(svg_file))
Exemple #2
0
    def draw(svg_file, V, A, multigraph=True, showlabel=False, ignore=None, loss=None):
        if loss == None:
            loss = [i for (u,v,i) in A if type(i)!=int]
            loss.append(max([i for (u,v,i) in A if type(i)==int]+[-1]))
        try:
            from pygraphviz.agraph import AGraph
        except:
            print "Failed to load pygraphviz!"
            return
        g = AGraph(rankdir="LR", directed=True, bgcolor="white", text="black",
                   font_color="white", ranksep="1.0", nodesep="0.10", strict=not multigraph)
        #g.node_attr["shape"] = "point"
        g.node_attr["shape"] = "circle"
        g.node_attr["color"] = "black"
        g.node_attr["fontcolor"] = "black"
        g.node_attr["fontstyle"] = "bold"
        g.node_attr["penwidth"] = "2.0"

        lbls = list(set(i for (u,v,i) in A))
        lbls.sort()
        M = len(lbls)

        if multigraph:
            colors = uniquecolors(2*M)
            shuffle(colors)

            for (u,v,i) in A:
                if ignore != None and (u,v) in ignore: continue
                assert u != v
                if i in loss:
                    g.add_edge(u,v,color="black", penwidth=4)
                else:
                    lbl = str(i) if showlabel else ""
                    g.add_edge(u,v,color=colors[lbls.index(i)%len(colors)], penwidth="%d" % 4, label=lbl)

        else:
            colors = uniquecolors(M+1)
            shuffle(colors)

            links = {}
            for (u, v, i) in A:
                if (u,v) not in links:
                    links[u,v] = []
                links[u,v].append(i)

            for (ind, (u, v)) in enumerate(links):
                if ignore != None and (u,v) in ignore: continue
                assert u != v
                if M in links[u,v] or any(type(e) != int for e in links[u,v]):
                    g.add_edge(u,v,color="black", penwidth=4)
                if len(links[u,v]) != 1:
                    lbl = ",".join(map(str,links[u,v,])) if showlabel else ""
                    g.add_edge(u,v,color=colors[ind%len(colors)], penwidth="%d" % 4, label=lbl)

        g.draw(svg_file, format="svg", prog="dot")
        print "SVG file '%s' generated!" % svg_file
Exemple #3
0
def AGraphComputerSetMultiDiGraph(spsg: nx.MultiDiGraph,
                                  cf: Callable) -> AGraph:
    A = nx.nx_agraph.to_agraph(spsg)
    A = AGraph(directed=True)
    A.node_attr["style"] = "filled"
    A.node_attr["shape"] = "rectangle"
    A.node_attr["fixedsize"] = "false"
    A.node_attr["fontcolor"] = "black"

    for node in spsg.nodes:
        A.add_node(node_2_string(node))
    edges = spsg.edges(data=True)
    for edge in edges:
        s, t, data_dict = edge
        computer_set = data_dict["computers"]
        ss, st = tuple(map(node_2_string, (s, t)))
        A.add_edge(ss, st)
        Ae = A.get_edge(ss, st)
        Ae.attr["label"] = "\n".join([c.__name__ for c in computer_set])
    return A
Exemple #4
0
    def draw(svg_file, V, A, multigraph=True, showlabel=False, ignore=None):
        try:
            from pygraphviz.agraph import AGraph
        except:
            print "Failed to load pygraphviz!"
            return
        g = AGraph(rankdir="LR",
                   directed=True,
                   bgcolor="white",
                   text="black",
                   font_color="white",
                   ranksep="1.0",
                   nodesep="0.10",
                   strict=not multigraph)
        #g.node_attr["shape"] = "point"
        g.node_attr["shape"] = "circle"
        g.node_attr["color"] = "black"
        g.node_attr["fontcolor"] = "black"
        g.node_attr["fontstyle"] = "bold"
        g.node_attr["penwidth"] = "2.0"

        M = max(i for (u, v, i) in A if type(i) == int)

        if multigraph:
            colors = uniquecolors(2 * M)
            shuffle(colors)

            for (u, v, i) in A:
                if ignore != None and (u, v) in ignore: continue
                assert u != v
                if i == M or type(i) != int:
                    g.add_edge(u, v, color="black", penwidth=4)
                else:
                    lbl = str(i) if showlabel else ""
                    g.add_edge(u,
                               v,
                               color=colors[i % len(colors)],
                               penwidth="%d" % 4,
                               label=lbl)

        else:
            colors = uniquecolors(M + 1)
            shuffle(colors)

            links = {}
            for (u, v, i) in A:
                if (u, v) not in links:
                    links[u, v] = []
                links[u, v].append(i)

            for (ind, (u, v)) in enumerate(links):
                if ignore != None and (u, v) in ignore: continue
                assert u != v
                if M in links[u, v] or any(
                        type(e) != int for e in links[u, v]):
                    g.add_edge(u, v, color="black", penwidth=4)
                if len(links[u, v]) != 1:
                    lbl = ",".join(map(str, links[u,
                                                  v, ])) if showlabel else ""
                    g.add_edge(u,
                               v,
                               color=colors[ind % len(colors)],
                               penwidth="%d" % 4,
                               label=lbl)

        g.draw(svg_file, format="svg", prog="dot")
        print "SVG file '%s' generated!" % svg_file
Exemple #5
0
    def draw(svg_file, V, A, multigraph=True, showlabel=False, ignore=None, loss=None, verbose=None):
        """Draws arc-flow graphs in .svg format."""
        if loss is None:
            loss = [i for (u, v, i) in A if not isinstance(i, int)]
            loss.append(max([i for (u, v, i) in A if isinstance(i, int)] + [-1]))
        try:
            from pygraphviz.agraph import AGraph
        except:
            raise Exception("Failed to load pygraphviz!")
        g = AGraph(
            rankdir="LR",
            directed=True,
            bgcolor="white",
            text="black",
            font_color="white",
            ranksep="1.0",
            nodesep="0.10",
            strict=not multigraph,
        )
        # g.node_attr["shape"] = "point"
        g.node_attr["shape"] = "circle"
        g.node_attr["color"] = "black"
        g.node_attr["fontcolor"] = "black"
        g.node_attr["fontstyle"] = "bold"
        g.node_attr["penwidth"] = "2.0"

        lbls = list(set(i for (u, v, i) in A))
        lbls.sort()
        M = len(lbls)

        if multigraph:
            colors = Colors.uniquecolors(2 * M)
            random.shuffle(colors)

            for (u, v, i) in A:
                if ignore is not None and (u, v) in ignore:
                    continue
                assert u != v
                if i in loss:
                    g.add_edge(u, v, color="black", penwidth=4)
                else:
                    lbl = str(i) if showlabel else ""
                    g.add_edge(u, v, color=colors[lbls.index(i) % len(colors)], penwidth="{0}".format(4), label=lbl)

        else:
            colors = Colors.uniquecolors(M + 1)
            random.shuffle(colors)

            links = {}
            for (u, v, i) in A:
                if (u, v) not in links:
                    links[u, v] = []
                links[u, v].append(i)

            for (ind, (u, v)) in enumerate(links):
                if ignore is not None and (u, v) in ignore:
                    continue
                assert u != v
                if M in links[u, v] or any(not isinstance(e, int) for e in links[u, v]):
                    g.add_edge(u, v, color="black", penwidth=4)
                if len(links[u, v]) != 1:
                    lbl = ",".join(map(str, links[u, v])) if showlabel else ""
                    g.add_edge(u, v, color=colors[ind % len(colors)], penwidth="{0}".format(4), label=lbl)

        g.draw(svg_file, format="svg", prog="dot")
        print "SVG file '{0}' generated!".format(svg_file)
Exemple #6
0
def draw_graph(svg_file,
               V,
               A,
               show_labels=False,
               ignore=None,
               back=None,
               loss=None,
               graph_attrs=None,
               verbose=False):
    """Draw an arc-flow graph in .svg format."""
    from pygraphviz.agraph import AGraph
    if ignore is None:
        ignore = []
    if back is None:
        back = []
    if loss is None:
        loss = []
    elif not isinstance(loss, (tuple, list)):
        loss = [loss]
    g = AGraph(rankdir="LR",
               directed=True,
               bgcolor="white",
               ranksep="1.0",
               nodesep="0.10",
               strict=False)
    if graph_attrs is not None:
        for attr, value in graph_attrs.items():
            g.graph_attr[attr] = value
    g.node_attr["shape"] = "ellipse"
    g.node_attr["color"] = "black"
    g.node_attr["fontcolor"] = "black"
    g.node_attr["penwidth"] = "2.0"

    lbls = sorted(set(i for (u, v, i) in A if i not in loss),
                  key=lambda lbl: (repr(type(lbl)), lbl))

    colors = Colors.uniquecolors(len(lbls) + 1, v=0.5, p=0.0)

    used = set()
    for (u, v, i) in A:
        if (u, v) in ignore:
            continue
        used.add(u)
        used.add(v)
        if (u, v) in back:
            u, v = v, u
            d = "back"
        else:
            d = "front"
        if i in loss:
            g.add_edge(u, v, color="black", style="dashed", penwidth=2, dir=d)
        else:
            lbl = str(i) if show_labels else ""
            color = colors[lbls.index(i) % len(colors)]
            g.add_edge(u, v, color=color, penwidth=2, label=lbl, dir=d)

    for v in V:
        if v not in used:
            g.add_node(v)

    g.draw(svg_file, format="svg", prog="dot")
    if verbose:
        print("SVG file '{0}' generated!".format(svg_file))