Пример #1
0
def igraph2image(igraph: networkx.DiGraph, fname_image: str, layout_engine="fdp"):
    """
    Creates an image file from *igraph* using :ref:`installation_graphviz` and the force directed layout engine *fdp*.
    To find out which file formats are supported call ``$ dot -T?``.

    **arguments**:
        * *igraph*: interaction graph
        * *fname_image*: name of image
        * *layout_engine*: one of "dot", "neato", "fdp", "sfdp", "circo", "twopi"

    **example**::

          >>> igraph2image(igraph, "mapk_igraph.pdf")
          >>> igraph2image(igraph, "mapk_igraph.jpg")
          >>> igraph2image(igraph, "mapk_igraph.svg")
    """

    digraph2image(igraph, fname_image, layout_engine=layout_engine)
Пример #2
0
def htg2image(htg: networkx.DiGraph,
              fname_image: str,
              layout_engine: str = "dot"):
    """
    Creates an image file from the *HTG*.

    **arguments**:
        * *HTG*: HTG
        * *fname_image*: name of output file
        * *layout_engine*: one of "dot", "neato", "fdp", "sfdp", "circo", "twopi"

    **example**::

          >>> htg2image(cgraph, "mapk_htg.pdf")
    """

    graph = htg.copy()
    convert_nodes_to_anonymous_strings(graph)
    digraph2image(graph, fname_image, layout_engine)
Пример #3
0
def condensationgraph2image(cgraph: networkx.DiGraph,
                            fname_image: str,
                            layout_engine: str = "dot"):
    """
    Creates an image file from the condensation graph.

    **arguments**:
        * *cgraph*: condensation graph
        * *fname_image*: name of output file
        * *layout_engine*: one of "dot", "neato", "fdp", "sfdp", "circo", "twopi"

    **example**::

          >>> condensationgraph2image(cgraph, "dot", "mapk_cgraph.pdf")
    """

    graph = cgraph.copy()
    convert_nodes_to_anonymous_strings(graph)
    digraph2image(graph, fname_image, layout_engine)
Пример #4
0
def stg2image(stg: networkx.DiGraph,
              fname_image: str,
              layout_engine: str = "fdp"):
    """
    Creates an image file from a state transition graph using :ref:`installation_graphviz` and the *layout_engine*.
    Use ``dot -T?`` to find out which output formats are supported on your installation.

    **arguments**:
        * *stg*: state transition graph
        * *fname_image*: name of output file
        * *layout_engine*: one of "dot", "neato", "fdp", "sfdp", "circo", "twopi"

    **example**::

          >>> stg2image(stg, "mapk_stg.pdf")
          >>> stg2image(stg, "mapk_stg.jpg", "neato")
          >>> stg2image(stg, "mapk_stg.svg", "dot")
    """

    digraph2image(digraph=stg,
                  fname_image=fname_image,
                  layout_engine=layout_engine)
Пример #5
0
def phenotype_diagram2image(diagram: networkx.DiGraph,
                            fname_image: Optional[str] = None):
    """
    Creates an image of the abstract commitment diagram.

    **arguments**:
        * *diagram*: a phenotype diagram
        * *fname_image*: name of the diagram image

    **returns**::
        * *styled_diagram*: the styled abstract commitment diagram

    **example**::

        >>> phenotype_diagram2image(primes, diagram, "diagram.pdf")
    """

    assert type(diagram) == type(networkx.DiGraph())

    primes = diagram.graph["primes"]
    size_total = size_state_space(primes)

    image = networkx.DiGraph()
    image.graph["node"] = {
        "shape": "rect",
        "style": "filled",
        "color": "none",
        "fillcolor": "lightgray"
    }

    image.graph["edge"] = {}
    labels = {}

    for node, data in diagram.nodes(data=True):
        labels[node] = {}
        image.add_node(node)
        head = divide_list_into_similar_length_lists(data["names"])
        head = [",".join(x) for x in head]
        labels[node]["head"] = "<br/>".join(head)

        if "fillcolor" in data:
            image.nodes[node]["fillcolor"] = data["fillcolor"]
        elif len(data["names"]) == 1:
            image.nodes[node]["fillcolor"] = "cornflowerblue"

        if "color" in data:
            image.nodes[node]["color"] = data["color"]

        if "penwidth" in data:
            image.nodes[node]["penwidth"] = data["penwidth"]

    for source, target, data in diagram.edges(data=True):
        image.add_edge(source, target)

    for x in diagram.nodes():
        perc = 100. * diagram.nodes[x]["initaccepting_size"] / size_total
        labels[x]["initaccepting_size"] = f"states: {perc2str(perc)}%"

    for x in diagram.nodes():
        image.nodes[x]['label'] = f"<{'<br/>'.join(labels[x].values())}>"

    ranks = {}
    for node, data in diagram.nodes(data=True):
        x = len(data["names"])
        if x not in ranks:
            ranks[x] = []

        ranks[x].append(node)

    ranks = sorted(ranks.items(), key=lambda x: x[0])

    for _, names in ranks:
        names = [f'"{x}"' for x in names]
        names = "; ".join(names)
        image.graph["{rank = same; %s;}" % names] = ""

    if fname_image:
        digraph2image(image, fname_image, layout_engine="dot")

    return image
def commitment_diagram2image(diagram: networkx.DiGraph,
                             fname_image: str,
                             style_inputs: bool = True,
                             style_edges: bool = False,
                             style_ranks: bool = True,
                             first_index: int = 1) -> networkx.DiGraph:
    """
    Creates the image file *fname_image* for the basin diagram given by *diagram*.
    The flag *StyleInputs* can be used to highlight which basins belong to which input combination.
    *style_edges* adds edge labels that indicate the size of the "border" (if *compute_border* was enabled in :ref:`commitment_compute_diagram`)
    and the size of the states of the source basin that can reach the target basin.

    **arguments**:
        * *diagram*: a commitment diagram
        * *fname_image*: file name of image
        * *style_inputs*: whether basins should be grouped by input combinations
        * *style_edges*: whether edges should be size of border / reachable states
        * *style_ranks*: style that places nodes with the same number of reachable attractors on the same rank (level)
        * *first_index*: first index of attractor names

    **returns**::
        * *styled_diagram*: the styled commitment diagram

    **example**::

        >>> attractors = compute_attractors(primes, update)
        >>> compute_phenotype_diagram(attractors)
        >>> commitment_diagram2image(diagram, "diagram.pdf")
    """

    primes = diagram.graph["primes"]
    size_total = pyboolnet.state_space.size_state_space(primes)
    size_per_input_combination = pyboolnet.state_space.size_state_space(
        primes, fixed_inputs=True)
    is_small_network = size_total <= 1024

    digraph = networkx.DiGraph()
    digraph.graph["node"] = {
        "shape": "rect",
        "style": "filled",
        "color": "none"
    }
    digraph.graph["edge"] = {}

    if style_inputs:
        digraph.graph["node"]["fillcolor"] = "grey95"
    else:
        digraph.graph["node"]["fillcolor"] = "lightgray"

    attractors = [x["attractors"] for _, x in diagram.nodes(data=True)]
    attractors = [x for x in attractors if len(x) == 1]
    attractors = set(subspace2str(primes, x[0]) for x in attractors)
    attractors = sorted(attractors)

    labels = {}
    # "labels" is used for node labels
    # keys:
    # head = reachable attractors
    # size = number of states in % (depends on StyleInputs)

    for node, data in diagram.nodes(data=True):
        labels[node] = {}
        digraph.add_node(node)

        if len(data["attractors"]) == 1:
            digraph.nodes[node]["fillcolor"] = "cornflowerblue"

            attr = subspace2str(primes, data["attractors"][0])
            index = attractors.index(attr) + first_index
            labels[node][
                "head"] = f'A{index} = <font face="Courier New">{attr}</font>'

        else:
            head = sorted(
                f"A{attractors.index(subspace2str(primes, x)) + first_index}"
                for x in data["attractors"])
            head = divide_list_into_similar_length_lists(head)
            head = [",".join(x) for x in head]
            labels[node]["head"] = "<br/>".join(head)

        if "fillcolor" in data:
            digraph.nodes[node]["fillcolor"] = data["fillcolor"]

    for source, target, data in diagram.edges(data=True):
        digraph.add_edge(source, target)

        if style_edges:
            edge_label = []

            #perc = 100.* data["EX_size"] / Diagram.nodes[source]["size"]
            #edge_label.append("EX: %s%%"%perc2str(perc))

            if "EF_size" in data:
                #perc = 100.* data["EF_size"] / Diagram.nodes[source]["size"]
                #edge_label.append("EF: %s%%"%perc2str(perc))

                if data["EF_size"] < diagram.nodes[source]["size"]:
                    digraph.adj[source][target]["color"] = "lightgray"

            #result.adj[source][target]["label"] = "<%s>"%("<br/>".join(edge_label))

    for x in diagram.nodes():
        if is_small_network:
            labels[x]["size"] = f"states: {diagram.nodes[x]['size']}"
        else:
            perc = 100. * diagram.nodes[x]["size"] / size_total
            labels[x]["size"] = f"states: {perc2str(perc)}%"

    subgraphs = []
    if style_inputs:
        for inputs in list_input_combinations(primes):
            if not inputs:
                continue

            nodes = [
                x for x in diagram.nodes() if dicts_are_consistent(
                    inputs, diagram.nodes[x]["attractors"][0])
            ]
            label = subspace2str(primes, inputs)
            subgraphs.append((nodes, {
                "label": f"inputs: {label}",
                "color": "none",
                "fillcolor": "lightgray"
            }))

            for x in nodes:
                perc = 100. * diagram.nodes[x][
                    "size"] / size_per_input_combination
                labels[x]["size"] = f"states: {perc2str(perc)}%"

        if subgraphs:
            digraph.graph["subgraphs"] = []
            add_style_subgraphs(digraph, subgraphs)

    for x in diagram.nodes():
        digraph.nodes[x]['label'] = f"<{'<br/>'.join(labels[x].values())}>"

    if style_ranks:
        if subgraphs:
            to_rank = digraph.graph["subgraphs"]
        else:
            to_rank = [digraph]

        for graph in to_rank:
            ranks = {}
            for node, data in diagram.nodes(data=True):
                if node not in graph:
                    continue

                size = len(data["attractors"])
                if size not in ranks:
                    ranks[size] = []

                ranks[size].append(node)

            ranks = list(ranks.items())
            ranks.sort(key=lambda x: x[0])

            for _, names in ranks:
                names = [f'"{x}"' for x in names]
                graph.graph[f"{{rank = same; {'; '.join(names)};}}"] = ""

    if fname_image:
        digraph2image(digraph, fname_image, layout_engine="dot")

    return digraph