Esempio n. 1
0
def simple_nodes():
    return [
        Node.from_list(
            "Second",
            "sixth",
            "fifth",
            "fourth",
            "third",
            "second",
            "initial",
        )
    ]
Esempio n. 2
0
def simple_nodes():
    """Fixture to supply a simple, linear set of nodes."""
    return [
        Node.from_list(
            "Second",
            "sixth",
            "fifth",
            "fourth",
            "third",
            "second",
            "initial",
        )
    ]
Esempio n. 3
0
    def get_nodes(self):
        terminal_nodes = list(set(self.terminal_nodes()))
        terminal: List[Node]= list(map(lambda t: Node(t), terminal_nodes))

        last: List[str] = [] 
        new_nodes = terminal
        next: List[str] = sorted(list(map(lambda n: n.item, new_nodes)))

        while (next != last):
            last = next
            nn = flatten_array(list(map(lambda n: self.step_node(n), new_nodes)))
            next = sorted(list(map(lambda n: n.item, nn)))
            if next != last:
                new_nodes = nn

        return new_nodes
Esempio n. 4
0
def branched_nodes():
    third = Node.from_list("third", "second", "initial")
    tip = Node("Merge branch 'side'",
               parents=[
                   Node("Second",
                        parents=[
                            Node("sixth",
                                 parents=[
                                     Node("fifth",
                                          parents=[
                                              Node("fourth", parents=[third]),
                                          ]),
                                 ]),
                        ]),
                   Node("side-2", parents=[
                       Node("side-1", parents=[third]),
                   ]),
               ])
    return [tip]
Esempio n. 5
0
def branched_nodes():
    """Fixture to supply a simple set of nodes with one feature branch."""
    third = Node.from_list("third", "second", "initial")
    tip = Node("Merge branch 'side'",
               parents=[
                   Node("Second",
                        parents=[
                            Node("sixth",
                                 parents=[
                                     Node("fifth",
                                          parents=[
                                              Node("fourth", parents=[third]),
                                          ]),
                                 ]),
                        ]),
                   Node("side-2", parents=[
                       Node("side-1", parents=[third]),
                   ]),
               ])
    return [tip]
Esempio n. 6
0
def tangled_nodes():
    """Fixture to supply a complicated set of nodes with multiple branches."""
    second = Node.from_list("second", "initial")
    third = Node("third", parents=[second])
    fifth = Node("fifth", parents=[
        Node("fourth", parents=[third]),
    ])
    side1 = Node("side-1", parents=[third])
    tangle = Node(
        "Merge tag 'tangle'",
        parents=[
            Node("Merge branch 'side' (early part) into tangle",
                 parents=[
                     Node("Merge branch 'master' (early part) into tangle",
                          parents=[
                              Node("tangle-a", parents=[second]),
                              fifth,
                          ]),
                     side1,
                 ]),
            Node("Merge branch 'side'",
                 parents=[
                     Node("side-2", parents=[side1]),
                     Node("Second", parents=[
                         Node("sixth", parents=[fifth]),
                     ])
                 ]),
        ])
    tip = Node("Merge tag 'reach'",
               parents=[
                   Node("Merge tags 'octopus-a' and 'octopus-b'",
                        parents=[
                            Node("seventh", parents=[tangle]),
                            Node("octopus-b", parents=[tangle]),
                            Node("octopus-a", parents=[tangle]),
                        ]),
                   Node("reach", parents=[tangle]),
               ])
    return [tip]
Esempio n. 7
0
def main():
    """Run a simple demo of the package's functionality."""
    # start-after
    from asciidag.graph import Graph
    from asciidag.node import Node

    graph = Graph()

    root = Node('root')
    grandpa = Node('grandpa', parents=[root])
    tips = [
        Node('child', parents=[
            Node('mom', parents=[
                Node('grandma', parents=[
                    Node('greatgrandma', parents=[]),
                ]),
                grandpa,
            ]),
            Node('dad', parents=[
                Node('bill', parents=[
                    Node('martin'),
                    Node('james'),
                    Node('paul'),
                    Node('jon'),
                ])]),
            Node('stepdad', parents=[grandpa]),
        ]),
        Node('foo', [Node('bar')]),
    ]

    graph.show_nodes(tips)
Esempio n. 8
0
def get_ascii_graph(result: Union[Array, DictOfNamedArrays],
                    use_color: bool = True) -> str:
    """Return a string representing the computation of *result*
    using the `asciidag <https://pypi.org/project/asciidag/>`_ package.

    :arg result: Outputs of the computation (cf.
        :func:`pytato.generate_loopy`).
    :arg use_color: Colorized output
    """
    outputs: DictOfNamedArrays = normalize_outputs(result)
    del result

    mapper = ArrayToDotNodeInfoMapper()
    for elem in outputs._data.values():
        mapper(elem)

    nodes = mapper.nodes

    input_arrays: List[Array] = []
    internal_arrays: List[ArrayOrNames] = []
    array_to_id: Dict[ArrayOrNames, str] = {}

    id_gen = UniqueNameGenerator()
    for array in nodes:
        array_to_id[array] = id_gen("array")
        if isinstance(array, InputArgumentBase):
            input_arrays.append(array)
        else:
            internal_arrays.append(array)

    # Since 'asciidag' prints the DAG from top to bottom (ie, with the inputs
    # at the bottom), we need to invert our representation of it, that is, the
    # 'parents' constructor argument to Node() actually means 'children'.
    from asciidag.node import Node  # type: ignore[import]
    asciidag_nodes: Dict[ArrayOrNames, Node] = {}

    from collections import defaultdict
    asciidag_edges: Dict[ArrayOrNames, List[ArrayOrNames]] = defaultdict(list)

    # Reverse edge directions
    for array in internal_arrays:
        for _, v in nodes[array].edges.items():
            asciidag_edges[v].append(array)

    # Add the internal arrays in reversed order
    for array in internal_arrays[::-1]:
        ary_edges = [asciidag_nodes[v] for v in asciidag_edges[array]]

        if array == internal_arrays[-1]:
            ary_edges.append(Node("Outputs"))

        asciidag_nodes[array] = Node(f"{nodes[array].title}",
                                     parents=ary_edges)

    # Add the input arrays last since they have no predecessors
    for array in input_arrays:
        ary_edges = [asciidag_nodes[v] for v in asciidag_edges[array]]
        asciidag_nodes[array] = Node(f"{nodes[array].title}",
                                     parents=ary_edges)

    input_node = Node("Inputs",
                      parents=[asciidag_nodes[v] for v in input_arrays])

    from asciidag.graph import Graph  # type: ignore[import]
    from io import StringIO

    f = StringIO()
    graph = Graph(fh=f, use_color=use_color)

    graph.show_nodes([input_node])

    # Get the graph and remove trailing whitespace
    res = "\n".join([s.rstrip() for s in f.getvalue().split("\n")])

    return res
Esempio n. 9
0
def main():
    graph = Graph()

    root = Node('root')
    grandpa = Node('grandpa', parents=[root])
    tips = [
        Node('child',
             parents=[
                 Node('mom',
                      parents=[
                          Node('grandma',
                               parents=[
                                   Node('greatgrandma', parents=[]),
                               ]),
                          grandpa,
                      ]),
                 Node('dad',
                      parents=[
                          Node('bill',
                               parents=[
                                   Node('martin'),
                                   Node('james'),
                                   Node('paul'),
                                   Node('jon'),
                               ])
                      ]),
                 Node('stepdad', parents=[grandpa]),
             ]),
        Node('foo', [Node('bar')]),
    ]

    graph.show_nodes(tips)
Esempio n. 10
0
 def step_node(self, node: Node) -> List[Node]:
     node_names = self.step(node.item, True)
     if node_names != [node.item]:
         return list(map(lambda n: Node(n, parents=[node]), node_names))
     else:
         return [node]