def test_operation_links_multiple(self, sum_op, square_op, negative_op): # # (y) --> (n1) # my_func sum (n) --> (num) square # (x) --> (n2) # def my_func(x: int, y: int) -> (int, int): return y, x my_op = operation(my_func, output_names=("y", "x"))() workflow = Workflow() # workflow.add_operations(sum_op, square_op, negative_op, my_op) workflow.add_operations(my_op, sum_op, square_op, negative_op) link1 = (my_op, sum_op, "y", "n1") link2 = (my_op, sum_op, "x", "n2") link3 = (sum_op, square_op, "sum", "n") link4 = (square_op, negative_op, "square", "num") workflow.add_link(my_op, sum_op, "y", "n1") workflow.add_link(my_op, sum_op, "x", "n2") workflow.add_link(sum_op, square_op, "sum", "n") workflow.add_link(square_op, negative_op, "square", "num") # assert workflow.operation_links(my_op) == [link1, link2] # assert workflow.operation_links(sum_op) == [link3] # assert workflow.operation_links(square_op) == [link4] # assert workflow.operation_links(negative_op) == [] workflow._pretty_print() dask_graph, end_ids = workflow.as_dask_graph() # test execution results = workflow.execute_synchronous(x=3, y=5)
def test_end_node(self, double_and_triple_op, sum_op, square_op): workflow = Workflow() workflow.add_operations(double_and_triple_op, sum_op, square_op) workflow.add_link(sum_op, square_op, "sum", "n") workflow.add_link(square_op, double_and_triple_op, "square", "n") result = workflow.execute_synchronous(n1=1, n2=2) assert result == ({"double": 18, "triple": 27}, )
def test_execute_synchronous(self, sum_op, square_op, negative_op): workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) workflow.add_link(sum_op, square_op, "sum", "n") workflow.add_link(square_op, negative_op, "square", "num") results = workflow.execute_synchronous(n1=2, n2=5) assert results == ({"negative": -49}, )
def test_execute_synchronous_no_links_not_enough_kwargs( self, sum_op, square_op, negative_op): # TODO -- expect exception workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) results = workflow.execute_synchronous(n1=2, n2=5) assert results == ({"negative": -49}, )
def test_start_node(self, double_and_triple_op, sum_op, square_op): workflow = Workflow() workflow.add_operations(double_and_triple_op, sum_op, square_op) workflow.add_link(double_and_triple_op, sum_op, "double", "n1") workflow.add_link(double_and_triple_op, sum_op, "triple", "n2") workflow.add_link(sum_op, square_op, "sum", "n") result = workflow.execute_synchronous(n=1) assert result == ({"square": 25}, )
def test_mutliple_end_nodes(double_and_triple_op, sum_op, square_op): workflow = Workflow() workflow.add_operations(double_and_triple_op, sum_op, square_op) workflow.add_link(sum_op, double_and_triple_op, "sum", "n") workflow.add_link(sum_op, square_op, "sum", "n") result = workflow.execute_synchronous(n1=2, n2=3) assert len(result) == 2 assert {"double": 10, "triple": 15} in result assert {"square": 25} in result
def test_no_output_names(self): @operation def my_func(a1, a2): return a1, a2 op = my_func() w = Workflow() w.add_operations(op) result = w.execute_synchronous(a1=1, a2=2) assert result == ({"my_func": 1}, )
def test_one_output_name(self): @operation @output_names("return_val") def my_func(a1, a2): return a1, a2 op = my_func() workflow = Workflow() workflow.add_operations(op) result = workflow.execute_synchronous(a1=1, a2=2) assert result == ({"return_val": 1}, )
def test_execute_synchronous_no_links(self, sum_op, square_op, negative_op): workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) # n1, n2 -- inputs to sum_op; # n -- input to square_op # num -- input to negative_op results = workflow.execute_synchronous(n1=2, n2=5, n=10, num=50) assert len(results) == 3 assert {"sum": 7} in results assert {"square": 100} in results assert {"negative": -50} in results
def test_all_output_names(self): @operation @output_names('x', 'y') def my_func(a1, a2): return a1, a2 op = my_func() workflow = Workflow() workflow.add_operations(op) result = workflow.execute_synchronous(a1=1, a2=2) print(result) assert result == ({'x': 1, 'y': 2}, )
def test_two_copy_ops_to_one_op(self, double_and_triple_op, sum_op, square_op): # 1**2 + 2**2 => 5 workflow = Workflow() square_op.filled_values.update(n=1) square_op_2 = square_op.__class__() square_op_2.filled_values.update(n=2) square_op_2.output_names = ["square"] workflow.add_operations(sum_op, square_op, square_op_2) workflow.add_link(square_op, sum_op, "square", "n1") workflow.add_link(square_op_2, sum_op, "square", "n2") workflow._pretty_print() result = workflow.execute_synchronous() assert result == ({"sum": 5},)
def test_execute_all(self, qtbot, sum_op, square_op, negative_op): results = [{"negative": (1 + 2) ** 2 * -1}, {"negative": (3 + 4) ** 2 * -1}, {"negative": (5 + 6) ** 2 * -1}] def cb(*result): next_result = results.pop(0) assert result == next_result workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) workflow.add_link(sum_op, square_op, "sum", "n") workflow.add_link(square_op, negative_op, "square", "num") n1_values = [1, 3, 5] n2_values = [2, 4, 6] # TODO -- we are only getting one result, should get three (3 pairs of n1/n2). workflow.execute_all(callback_slot=cb, n1=n1_values, n2=n2_values).result()
def test_two_ops_to_one_op(self, negative_op, square_op, sum_op): workflow = Workflow() workflow.add_operations(negative_op, square_op, sum_op) workflow.add_link(negative_op, sum_op, "negative", "n1") workflow.add_link(square_op, sum_op, "square", "n2") print(workflow.get_inbound_links(sum_op)) #from dask import visualize #visualize(workflow.as_dask_graph()[0], filename="/home/ihumphrey/graph") graph = workflow.as_dask_graph()[0] print(graph) for k, op in graph.items(): print(k, op[0].node.name) from dask.threaded import get negative_op.filled_values.update(num=3) square_op.filled_values.update(n=4) print(get(graph, '0')) # WHY is this an issue? Complains that sum missing required 'n2'
def test_execute_all(self, sum_op, square_op, negative_op): workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) workflow.add_link(sum_op, square_op, "sum", "n") workflow.add_link(square_op, negative_op, "square", "num") n1_values = [1, 3, 5] n2_values = [2, 4, 6] # TODO -- we are only getting one result, should get three (3 pairs of n1/n2). results = list( workflow.execute_all(n1=n1_values, n2=n2_values).result()) assert results == [{ "negative": (1 + 2)**2 * -1 }, { "negative": (3 + 4)**2 * -1 }, { "negative": (5 + 6)**2 * -1 }]
def test_execute_no_links(self): @operation @output_names("doubled") def double_op(n): return n * 2 @operation @output_names("tripled") def triple_op(n): return n * 3 workflow = Workflow() workflow.add_operations(double_op, triple_op) results = workflow.execute(n=10).result() assert len(results) == 2 assert {"doubled": 20} in results assert {"tripled": 30} in results
def test_execute_synchronous_no_links_not_enough_kwargs( self, sum_op, square_op, negative_op): workflow = Workflow() workflow.add_operations(sum_op, square_op, negative_op) with pytest.raises(TypeError): results = workflow.execute_synchronous(n1=2, n2=5)