Exemple #1
0
def test_recover_funcs(function_factory: FunctionFactoryBase):
    f1 = function_factory.get('ConstFunc')(function_factory=function_factory,
                                           a=.5,
                                           name='f1')
    f2 = function_factory.get('LinearFunc')(function_factory=function_factory,
                                            m=.5,
                                            b=.2,
                                            name='f2')
    f3 = function_factory.get('ExponentialFunc')(
        function_factory=function_factory,
        A=.5,
        B=.2,
        tau1=1.3,
        tau2=1.5,
        name='f3')
    f4 = function_factory.get('CosFunc')(function_factory=function_factory,
                                         f=.5,
                                         A=3.4,
                                         th0=7.5,
                                         b=12.2,
                                         name='f4')

    function_factory.add_func(f1)
    function_factory.add_func(f2)
    function_factory.add_func(f3)
    function_factory.add_func(f4)

    funcs = function_factory.save_functions()
    assert len(funcs) == 4

    recovered_funcs, name_mapping = function_factory.recover_funcs(funcs)
    assert len(recovered_funcs) == 4
    assert len(name_mapping) == 4

    for f_name in ('f1', 'f2', 'f3', 'f4'):
        assert f_name in name_mapping
        assert name_mapping[f_name] != f_name
        assert f_name in function_factory.funcs_inst
        assert name_mapping[f_name] in function_factory.funcs_inst

        for name in function_factory.funcs_inst[f_name].get_gui_props():
            original_f = function_factory.funcs_inst[f_name]
            new_f = function_factory.funcs_inst[name_mapping[f_name]]

            if name == 'name':
                assert original_f.name != new_f.name
                assert new_f.name.startswith(original_f.name)
                continue

            assert getattr(original_f, name) == getattr(new_f, name)
Exemple #2
0
def test_recover_ref_funcs(function_factory: FunctionFactoryBase):
    from ceed.function import FuncGroup, CeedFuncRef
    f1 = function_factory.get('ConstFunc')(function_factory=function_factory,
                                           a=.5,
                                           name='f1',
                                           duration=1.2)
    f2 = function_factory.get('LinearFunc')(function_factory=function_factory,
                                            m=.5,
                                            b=.2,
                                            name='f2',
                                            duration=1.2)
    f3 = function_factory.get('ExponentialFunc')(
        function_factory=function_factory,
        A=.5,
        B=.2,
        tau1=1.3,
        tau2=1.5,
        name='f3',
        duration=1.2)
    f4 = function_factory.get('CosFunc')(function_factory=function_factory,
                                         f=.5,
                                         A=3.4,
                                         th0=7.5,
                                         b=12.2,
                                         name='f4',
                                         duration=1.2)
    g = FuncGroup(function_factory=function_factory, name='g')

    function_factory.add_func(f1)
    function_factory.add_func(f2)
    function_factory.add_func(f3)
    function_factory.add_func(f4)
    function_factory.add_func(g)

    g.add_func(function_factory.get_func_ref(name='f1'))
    g.add_func(function_factory.get_func_ref(name='f2'))
    g.add_func(function_factory.get_func_ref(name='f3'))
    g.add_func(function_factory.get_func_ref(name='f4'))

    funcs = function_factory.save_functions()
    assert len(funcs) == 5

    recovered_funcs, name_mapping = function_factory.recover_funcs(funcs)
    assert len(recovered_funcs) == 5
    assert len(name_mapping) == 5

    for f_name in ('f1', 'f2', 'f3', 'f4', 'g'):
        assert f_name in name_mapping
        assert name_mapping[f_name] != f_name
        assert f_name in function_factory.funcs_inst
        assert name_mapping[f_name] in function_factory.funcs_inst

        for name in function_factory.funcs_inst[f_name].get_gui_props():
            original_f = function_factory.funcs_inst[f_name]
            new_f = function_factory.funcs_inst[name_mapping[f_name]]

            if name == 'name':
                assert original_f.name != new_f.name
                assert new_f.name.startswith(original_f.name)
                continue

            assert getattr(original_f, name) == getattr(new_f, name)

    new_g: FuncGroup = function_factory.funcs_inst[name_mapping['g']]
    assert len(new_g.funcs) == 4

    func: CeedFuncRef
    for func, name in zip(new_g.funcs, ('f1', 'f2', 'f3', 'f4')):
        assert isinstance(func, CeedFuncRef)
        assert func.func is function_factory.funcs_inst[name_mapping[name]]