Пример #1
0
def test_workflow(sum_fn, return_one_fn, two_output_fn):
    """Test the Workflow class"""
    p = simplepipe.Workflow()
    data_in = {'a': 1, 'b': 2}
    data_out = {'a': 1, 'b': 2, 'c': 3}
    p.add_task(sum_fn, inputs=['a', 'b'], outputs=['c'])
    assert(p(data_in) == data_out)

    # Test composition of workflows
    p2 = simplepipe.Workflow()
    p2.add_task(p)
    p2.add_task(return_one_fn, inputs=[], outputs=['d'])
    data_out2 = {'a': 1, 'b': 2, 'c': 3, 'd': 1}
    assert(p2(data_in) == data_out2)

    # Test protection against mutation
    def bad_mutator_fn(workspace):
        workspace['a'] = 'just_messing_with_a'
        return {'e': 'foobar'}

    wf3 = simplepipe.Workflow()
    wf3.add_task(task=bad_mutator_fn)
    assert wf3({'a': 1, 'b': 2}) == {'a': 1, 'b': 2, 'e': 'foobar'}
Пример #2
0
def test_hooks(sum_fn):
    """Test hooks in workflow"""
    def after_sum_1(data):
        data['c'] = 10

    def after_sum_2(data):
        data['e'] = 100

    p = simplepipe.Workflow()
    data_in = {'a': 1, 'b': 2}
    data_out = {'a': 1, 'b': 2, 'c': 3}
    p.add_task(sum_fn, inputs=['a', 'b'], outputs=['c']) \
     .add_hook_point('after_sum') \
     .add_task(lambda c: 2*c, inputs=['c'], outputs=['d'])

    p.add_hook('after_sum', after_sum_1)
    p.add_hook('after_sum', after_sum_2)
    output = p(data_in)

    assert(output == {'a': 1, 'b': 2, 'c': 10, 'd': 20, 'e': 100})
Пример #3
0
    module.__dict__.update({'__builtin__':{}})
    exec(code_string, module.__dict__)
    return getattr(module,function_name)

QCPICodeGen = sp.Workflow([
    # Create module for holding compiled code
    sp.Task(ft.partial(create_module), inputs='problem_data', outputs=('code_module')),

    # sp.Task(make_functions, inputs=('problem_data', 'code_module'), outputs=('code_module','compute_control_fn')),
    # Load equation template files and generate code
    sp.Task(ft.partial(load_eqn_template,
                template_file='deriv_func.py.mu'),
            inputs='problem_data',
            outputs='deriv_func_code'),
    sp.Task(ft.partial(load_eqn_template,
                template_file='bc_func.py.mu'),
            inputs='problem_data',
            outputs='bc_func_code'),

    # Compile generated code
    sp.Task(ft.partial(compile_code_py, function_name='deriv_func'),
            inputs=['deriv_func_code', 'code_module'],
            outputs='deriv_func_fn'),
    sp.Task(ft.partial(compile_code_py, function_name='bc_func'),
            inputs=['bc_func_code', 'code_module'],
            outputs='bc_func_fn'),
], description='Generates and compiles the required BVP functions from problem data')


pert_eom_bck = None
@ft.lru_cache(maxsize=16)
PythonCodeGen = sp.Workflow(
    [
        # Create module for holding compiled code
        sp.Task(ft.partial(create_module),
                inputs='problem_data',
                outputs=('code_module')),

        # Load equation template files and generate code
        sp.Task(ft.partial(load_eqn_template,
                           template_file=beluga.root() +
                           '/optimlib/templates/brysonho/deriv_func.py.mu'),
                inputs='problem_data',
                outputs='deriv_func_code'),
        sp.Task(ft.partial(load_eqn_template,
                           template_file=beluga.root() +
                           '/optimlib/templates/brysonho/bc_func.py.mu'),
                inputs='problem_data',
                outputs='bc_func_code'),
        sp.Task(
            ft.partial(load_eqn_template,
                       template_file=beluga.root() +
                       '/optimlib/templates/brysonho/compute_control.py.mu'),
            inputs='problem_data',
            outputs='compute_control_code'),

        # Compile generated code
        sp.Task(ft.partial(compile_code_py, function_name='deriv_func'),
                inputs=['deriv_func_code', 'code_module'],
                outputs='deriv_func_fn'),
        sp.Task(ft.partial(compile_code_py, function_name='bc_func'),
                inputs=['bc_func_code', 'code_module'],
                outputs='bc_func_fn'),
        sp.Task(ft.partial(compile_code_py, function_name='compute_control'),
                inputs=['compute_control_code', 'code_module'],
                outputs='compute_control_fn'),

        # sp.Task(make_bvp, inputs='*', outputs=['bvp'])
    ],
    description=
    'Generates and compiles the required BVP functions from problem data')