Exemplo n.º 1
0
def test_threaded_evaluation():
    """Testing the threaded evaluation by asserting the result value

    Also testing the evaluation time.
    +---------------+          +---------------+
    |   AddNode1    |          |   AddNode2    |
    |---------------|          |---------------|
    o number1<1>    |     +--->o number1<2>    |
    o number2<1>    |     |    o number2<1>    |
    |        result o-----+    |        result o
    +---------------+     |    +---------------+
                          |    +---------------+
                          |    |   AddNode3    |
                          |    |---------------|
                          +--->o number1<2>    |
                               o number2<1>    |
                               |        result o
                               +---------------+
    """
    sleep_time = .2
    graph = Graph(name='threaded')

    @Node(outputs=['result'])
    def AddNode(number1, number2):
        time.sleep(sleep_time)
        return {'result': number1 + number2}

    n1 = AddNode(name='AddNode1', graph=graph, number1=1, number2=1)
    n2 = AddNode(name='AddNode2', graph=graph, number2=1)
    n3 = AddNode(name='AddNode3', graph=graph, number2=1)

    n1.outputs['result'] >> n2.inputs['number1']
    n1.outputs['result'] >> n3.inputs['number1']

    start = time.time()
    graph.evaluate(mode="threading", max_workers=2)
    end = time.time()

    runtime = end - start

    assert runtime < len(graph.nodes) * sleep_time
    assert n2.outputs['result'].value == 3
    assert n3.outputs['result'].value == 3
Exemplo n.º 2
0
def test_passing_evaluator(clear_default_graph, branching_graph):
    """Test that passing an evalutor to graph.evaluate works."""
    graph = Graph(name="test_passing_evaluator")

    @Node(outputs=["result"])
    def AddNode(number1, number2):
        return {"result": number1 + number2}

    n1 = AddNode(name="AddNode1", graph=graph, number1=1, number2=1)
    n2 = AddNode(name="AddNode2", graph=graph, number2=1)
    n3 = AddNode(name="AddNode3", graph=graph, number2=1)

    n1.outputs["result"] >> n2.inputs["number1"]
    n1.outputs["result"] >> n3.inputs["number1"]

    graph.evaluate(mode=None, evaluator=LinearEvaluator())

    assert n2.outputs["result"].value == 3
    assert n3.outputs["result"].value == 3
Exemplo n.º 3
0
def test_evaluation_matrix(clear_default_graph):
    """The nodes as a 2D grid."""
    @Node(outputs=["out", "out2"])
    def DummyNode(in1, in2):
        pass

    start = DummyNode(name="start")
    n11 = DummyNode(name="11")
    n12 = DummyNode(name="12")
    n21 = DummyNode(name="21")
    n31 = DummyNode(name="31")
    n32 = DummyNode(name="32")
    n33 = DummyNode(name="33")
    end = DummyNode(name="end")

    # Connect them
    start.outputs["out"] >> n11.inputs["in1"]
    start.outputs["out"] >> n21.inputs["in1"]
    start.outputs["out"] >> n31.inputs["in1"]["0"]

    n31.outputs["out"] >> n32.inputs["in1"]
    n32.outputs["out"] >> n33.inputs["in1"]

    n11.outputs["out"] >> n12.inputs["in1"]
    n33.outputs["out"] >> n12.inputs["in2"]

    n12.outputs["out"] >> end.inputs["in1"]
    n21.outputs["out2"]["0"] >> end.inputs["in2"]

    nodes = [start, n11, n12, n21, n31, n32, n33, end]
    graph = Graph(nodes=nodes)

    order = [[start], [n11, n21, n31], [n32], [n33], [n12], [end]]

    for i, row in enumerate(graph.evaluation_matrix):
        for node in row:
            assert node in order[i]
        assert len(row) == len(order[i])

    graph.evaluate()
Exemplo n.º 4
0
def test_evaluation_matrix(clear_default_graph):
    """The nodes as a 2D grid."""
    @Node(outputs=["out", "out2"])
    def DummyNode(in1, in2):
        pass

    start = DummyNode(name='start')
    n11 = DummyNode(name='11')
    n12 = DummyNode(name='12')
    n21 = DummyNode(name='21')
    n31 = DummyNode(name='31')
    n32 = DummyNode(name='32')
    n33 = DummyNode(name='33')
    end = DummyNode(name='end')

    # Connect them
    start.outputs['out'] >> n11.inputs['in1']
    start.outputs['out'] >> n21.inputs['in1']
    start.outputs['out'] >> n31.inputs['in1']['0']

    n31.outputs['out'] >> n32.inputs['in1']
    n32.outputs['out'] >> n33.inputs['in1']

    n11.outputs['out'] >> n12.inputs['in1']
    n33.outputs['out'] >> n12.inputs['in2']

    n12.outputs['out'] >> end.inputs['in1']
    n21.outputs['out2']['0'] >> end.inputs['in2']

    nodes = [start, n11, n12, n21, n31, n32, n33, end]
    graph = Graph(nodes=nodes)

    order = [[start], [n11, n21, n31], [n32], [n33], [n12], [end]]

    for i, row in enumerate(graph.evaluation_matrix):
        for node in row:
            assert node in order[i]

    graph.evaluate()
Exemplo n.º 5
0
def test_clear_plugs_after_use_if_not_data_persistent():
    """
    +--------------------+          +----------+          +-----------------+
    |         N1         |          |    N2    |          |         N3      |
    |--------------------|          |----------|          |-----------------|
    o in2<>              |          o in2<>    |          o in2<>           |
    o in_<{"data": ">    |     +--->o in_<>    |     +--->o in_<>           |
    |                out o-----+    |      out o-----+    |             out o
    +--------------------+          +----------+          +-----------------+
    """

    @Node(outputs=["out"])
    def NodeForTesting(in_, in2):
        return {"out": in_}

    graph = Graph()
    N1 = NodeForTesting(name="N1", graph=graph)
    N2 = NodeForTesting(name="N2", graph=graph)
    N3 = NodeForTesting(name="N3", graph=graph)

    N1.outputs["out"] >> N2.inputs["in_"]
    N2.outputs["out"] >> N3.inputs["in_"]

    data = {"data": "data"}

    N1.inputs["in_"].value = data

    graph.evaluate(data_persistence=False)

    assert N1.inputs["in_"].value == data
    assert N1.outputs["out"].value == None

    assert N2.inputs["in_"].value == None
    assert N2.outputs["out"].value == None

    assert N3.inputs["in_"].value == None
    assert N3.outputs["out"].value == data