Beispiel #1
0
def main(example_file: str,
         array_format: str = FORMAT_NUMPY,
         verbose: bool = False):
    """
    Main entry point for execution engine.

    Args:
        example_file: The MDF file to execute.
        array_format: The format of arrays to use. Allowed values: 'numpy' or 'tensorflow'.
        verbose: Whether to print output to standard out during execution.

    """

    from modeci_mdf.utils import load_mdf, print_summary

    mod_graph = load_mdf(example_file).graphs[0]

    if verbose:
        print("Loaded Graph:")
        print_summary(mod_graph)

        print("------------------")
    eg = EvaluableGraph(mod_graph, verbose)
    eg.evaluate(array_format=array_format)

    return eg
Beispiel #2
0
def main():
    mod = Model(id="abc_conditions")
    mod_graph = Graph(id="abc_conditions_example")
    mod.graphs.append(mod_graph)

    input_node = Node(id="input0")
    input_node.parameters.append(Parameter(id="input_level", value=0.0))
    input_node.parameters.append(Parameter(id="count_0", value="count_0 + 1"))
    op1 = OutputPort(id="out_port")
    op1.value = "input_level"
    input_node.output_ports.append(op1)
    mod_graph.nodes.append(input_node)

    def create_simple_node(graph, id_, sender=None):
        n = Node(id=id_)
        graph.nodes.append(n)

        ip1 = InputPort(id="input_port1", shape="(1,)")
        n.input_ports.append(ip1)

        n.output_ports.append(OutputPort(id="output_1", value=ip1.id))

        if sender is not None:
            simple_connect(sender, n, graph)

        return n

    a = create_simple_node(mod_graph, "A", input_node)
    a.parameters.append(Parameter(id="count_A", value="count_A + 1"))

    b = create_simple_node(mod_graph, "B", a)
    b.parameters.append(Parameter(id="count_B", value="count_B + 1"))

    c = create_simple_node(mod_graph, "C", a)

    c.parameters.append(Parameter(id="count_C", value="count_C+ 1"))

    cond_i = Condition(type="BeforeNCalls", dependencies=input_node.id, n=1)
    cond_a = Condition(type="Always")
    cond_b = Condition(type="EveryNCalls", dependencies=a.id, n=2)
    cond_c = Condition(type="EveryNCalls", dependencies=a.id, n=3)
    cond_term = Condition(
        type="And",
        dependencies=[
            Condition(type="AfterNCalls", dependencies=c.id, n=2),
            Condition(type="JustRan", dependencies=a.id),
        ],
    )

    mod_graph.conditions = ConditionSet(
        node_specific={
            input_node.id: cond_i,
            a.id: cond_a,
            b.id: cond_b,
            c.id: cond_c
        },
        termination={"environment_state_update": cond_term},
    )

    mod.to_json_file(
        os.path.join(os.path.dirname(__file__), "%s.json" % mod.id))
    mod.to_yaml_file(
        os.path.join(os.path.dirname(__file__), "%s.yaml" % mod.id))

    print_summary(mod_graph)

    import sys

    if "-run" in sys.argv:
        verbose = True
        # verbose = False
        from modeci_mdf.execution_engine import EvaluableGraph
        from modelspec.utils import FORMAT_NUMPY, FORMAT_TENSORFLOW

        format = FORMAT_TENSORFLOW if "-tf" in sys.argv else FORMAT_NUMPY
        eg = EvaluableGraph(mod_graph, verbose=verbose)
        eg.evaluate(array_format=format)

    if "-graph" in sys.argv:
        mod.to_graph_image(
            engine="dot",
            output_format="png",
            view_on_render=False,
            level=3,
            filename_root="abc_conditions",
            only_warn_on_fail=
            True,  # Makes sure test of this doesn't fail on Windows on GitHub Actions
        )
Beispiel #3
0
    from modeci_mdf.utils import load_mdf, print_summary

    verbose = True

    if len(sys.argv) < 3:
        print(
            "Usage:\n\n  python graphviz.py MDF_JSON_FILE level [%s]\n\n" %
            NO_VIEW +
            "where level = 1, 2 or 3. Include %s to supress viewing generated graph on render\n"
            % NO_VIEW)
        exit()

    example = sys.argv[1]
    view = NO_VIEW not in sys.argv

    model = load_mdf(example)

    mod_graph = model.graphs[0]

    print("------------------")
    print("Loaded Graph:")
    print_summary(mod_graph)

    print("------------------")
    # nmllite_file = example.replace('.json','.nmllite.json')
    mdf_to_graphviz(mod_graph,
                    engine=engines["d"],
                    view_on_render=view,
                    level=int(sys.argv[2]))
Beispiel #4
0
def main():
    mod = Model(id="ABCD")
    mod_graph = Graph(id="abcd_example")
    mod.graphs.append(mod_graph)

    input_node = Node(id="input0", metadata={"color": ".8 .8 .8"})
    input_node.parameters.append(Parameter(id="input_level", value=0.0))
    op1 = OutputPort(id="out_port")
    op1.value = "input_level"
    input_node.output_ports.append(op1)
    mod_graph.nodes.append(input_node)

    print(input_node)
    print(input_node.output_ports)

    # a = create_example_node('A', mod_graph)
    a = Node(id="A", metadata={"color": ".8 0 0"})
    mod_graph.nodes.append(a)
    ip1 = InputPort(id="input_port1")
    a.input_ports.append(ip1)

    a.parameters.append(Parameter(id="slope", value=abcd.A_slope))
    a.parameters.append(Parameter(id="intercept", value=abcd.A_intercept))

    f1 = Parameter(
        id="linear_func",
        function="linear",
        args={
            "variable0": ip1.id,
            "slope": "slope",
            "intercept": "intercept"
        },
    )
    a.parameters.append(f1)
    a.output_ports.append(OutputPort(id="output_1", value="linear_func"))

    e1 = simple_connect(input_node, a, mod_graph)

    b = Node(id="B", metadata={"color": "0 .8 0"})
    mod_graph.nodes.append(b)
    ip1 = InputPort(id="input_port1")
    b.input_ports.append(ip1)

    b.parameters.append(Parameter(id="gain", value=abcd.B_gain))
    b.parameters.append(Parameter(id="bias", value=abcd.B_bias))
    b.parameters.append(Parameter(id="offset", value=abcd.B_offset))

    f1 = Parameter(
        id="logistic_func",
        function="logistic",
        args={
            "variable0": ip1.id,
            "gain": "gain",
            "bias": "bias",
            "offset": "offset"
        },
    )
    b.parameters.append(f1)
    b.output_ports.append(OutputPort(id="output_1", value="logistic_func"))

    simple_connect(a, b, mod_graph)

    c = Node(id="C", metadata={"color": "0 0 .8"})
    mod_graph.nodes.append(c)
    ip1 = InputPort(id="input_port1", shape="(1,)")
    c.input_ports.append(ip1)

    c.parameters.append(Parameter(id="scale", value=abcd.C_scale))
    c.parameters.append(Parameter(id="rate", value=abcd.C_rate))
    c.parameters.append(Parameter(id="bias", value=abcd.C_bias))
    c.parameters.append(Parameter(id="offset", value=abcd.C_offset))

    f1 = Parameter(
        id="exponential_func",
        function="exponential",
        args={
            "variable0": ip1.id,
            "scale": "scale",
            "rate": "rate",
            "bias": "bias",
            "offset": "offset",
        },
    )
    c.parameters.append(f1)
    c.output_ports.append(OutputPort(id="output_1", value="exponential_func"))

    simple_connect(b, c, mod_graph)

    d = Node(id="D", metadata={"color": ".8 0 .8"})
    mod_graph.nodes.append(d)

    ip1 = InputPort(id="input_port1", shape="(1,)")
    d.input_ports.append(ip1)
    d.parameters.append(Parameter(id="scale", value=abcd.D_scale))

    f1 = Parameter(id="sin_func",
                   function="sin",
                   args={
                       "variable0": ip1.id,
                       "scale": "scale"
                   })
    d.parameters.append(f1)
    d.output_ports.append(OutputPort(id="output_1", value="sin_func"))

    simple_connect(c, d, mod_graph)

    print(mod)

    print("------------------")
    # print(mod.to_json())
    new_file = mod.to_json_file("%s.json" % mod.id)
    new_file = mod.to_yaml_file("%s.yaml" % mod.id)

    print_summary(mod_graph)

    import sys

    if "-run" in sys.argv:
        verbose = True
        # verbose = False
        from modeci_mdf.execution_engine import EvaluableGraph
        from modelspec.utils import FORMAT_NUMPY, FORMAT_TENSORFLOW

        format = FORMAT_TENSORFLOW if "-tf" in sys.argv else FORMAT_NUMPY
        eg = EvaluableGraph(mod_graph, verbose=verbose)
        eg.evaluate(array_format=format)

    if "-graph" in sys.argv:
        mod.to_graph_image(
            engine="dot",
            output_format="png",
            view_on_render=False,
            level=1,
            filename_root="abcd",
            only_warn_on_fail=
            True,  # Makes sure test of this doesn't fail on Windows on GitHub Actions
        )
        mod.to_graph_image(
            engine="dot",
            output_format="png",
            view_on_render=False,
            level=3,
            filename_root="abcd_3",
            only_warn_on_fail=
            True,  # Makes sure test of this doesn't fail on Windows on GitHub Actions
        )