Eval: Using nested graphs¶
To create this graph we need only to install the tierkreis package:
pip install tierkreis
Graph¶
We can run graphs from within other graphs by using Graph.eval.
Recall the fib_step graph that we wrote in the previous tutorial
from typing import NamedTuple
from tierkreis.builder import Graph
from tierkreis.builtins import iadd
from tierkreis.models import TKR
class FibData(NamedTuple):
a: TKR[int]
b: TKR[int]
builder = Graph(FibData, FibData)
sum = builder.task(iadd(builder.inputs.a, builder.inputs.b))
fib_step = builder.finish_with_outputs(FibData(builder.inputs.b, sum))
We create a graph fib4 that calls fib_step three times.
The graph will have no inputs and gives a single integer as output:
from tierkreis.models import EmptyModel
fib4 = Graph(EmptyModel, TKR[int])
The Graph.eval method takes a Workflow object as its first argument
and the appropriately typed input data as the second object.
second = fib4.eval(fib_step, FibData(a=fib4.const(0), b=fib4.const(1)))
We can iterate manually as follows:
third = fib4.eval(fib_step, second)
fourth = fib4.eval(fib_step, third)
workflow = fib4.finish_with_outputs(fourth.b)
In the next tutorial we will see how to iterate programmatically.
Execution¶
Since we still only use built-in functions, we execute the graph in the same way as before.
from uuid import UUID
from pathlib import Path
from tierkreis import run_graph
from tierkreis.storage import FileStorage, read_outputs
from tierkreis.executor import ShellExecutor
storage = FileStorage(UUID(int=99), name="Nested graphs using Eval")
executor = ShellExecutor(Path("."), workflow_dir=storage.workflow_dir)
storage.clean_graph_files()
run_graph(storage, executor, workflow, {})
print(read_outputs(workflow, storage))
3