Example #1
0
def test_func_ref(function_factory: FunctionFactoryBase):
    const_cls = function_factory.get('ConstFunc')
    f = const_cls(function_factory=function_factory,
                  duration=4,
                  a=.7,
                  name='f')
    f2 = const_cls(function_factory=function_factory,
                   duration=5,
                   a=.9,
                   name='f')

    function_factory.add_func(f)

    ref1 = function_factory.get_func_ref(name='f')
    ref2 = function_factory.get_func_ref(func=f2)

    assert ref1.func is f
    assert ref2.func is f2
    assert f.has_ref
    assert f2.has_ref
    assert f in function_factory._ref_funcs
    assert f2 in function_factory._ref_funcs

    function_factory.return_func_ref(ref1)
    assert ref2.func is f2
    assert not f.has_ref
    assert f2.has_ref
    assert f not in function_factory._ref_funcs
    assert f2 in function_factory._ref_funcs

    function_factory.return_func_ref(ref2)
    assert not f.has_ref
    assert not f2.has_ref
    assert f not in function_factory._ref_funcs
    assert f2 not in function_factory._ref_funcs
Example #2
0
def test_copy_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)

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

    for func in (f1, f2, f3, f4):
        func_copy = copy.deepcopy(func)
        assert func is not func_copy
        assert isinstance(func_copy, func.__class__)

        for name in func.get_gui_props():
            if name == 'name':
                continue

            assert getattr(func, name) == getattr(func_copy, name)

    func_copy = copy.deepcopy(g)
    assert len(func_copy.funcs) == 4
    for new_f, original_f in zip(func_copy.funcs, g.funcs):
        assert new_f is not original_f
        assert isinstance(new_f, CeedFuncRef)
        assert isinstance(original_f, CeedFuncRef)
        assert new_f.func is original_f.func
Example #3
0
def test_can_other_func_be_added_ref(function_factory: FunctionFactoryBase):
    factory = function_factory
    const_cls = factory.get('ConstFunc')

    g1 = FuncGroup(function_factory=factory)

    g2 = FuncGroup(function_factory=factory)
    ref_g2 = function_factory.get_func_ref(func=g2)
    g1.add_func(ref_g2)

    f = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f)

    f2 = const_cls(function_factory=factory, a=.25, duration=2)
    g1.add_func(f2)

    g3 = FuncGroup(function_factory=factory)
    g1.add_func(g3)
    f4 = const_cls(function_factory=factory, a=.8, duration=2)
    g3.add_func(f4)

    assert g1.can_other_func_be_added(g2)
    assert g1.can_other_func_be_added(ref_g2)
    assert g1.can_other_func_be_added(g3)
    assert not g1.can_other_func_be_added(g1)

    assert not g2.can_other_func_be_added(g1)
    assert g2.can_other_func_be_added(g3)
    assert not g2.can_other_func_be_added(g2)
    assert not g2.can_other_func_be_added(ref_g2)

    assert not g3.can_other_func_be_added(g1)
    assert g3.can_other_func_be_added(g2)
    assert g3.can_other_func_be_added(ref_g2)
    assert not g3.can_other_func_be_added(g3)
Example #4
0
def test_call_funcs_ref(function_factory: FunctionFactoryBase):
    factory = function_factory
    const_cls = factory.get('ConstFunc')

    g1 = FuncGroup(function_factory=factory)

    g2 = FuncGroup(function_factory=factory)
    ref_g2 = function_factory.get_func_ref(func=g2)
    g1.add_func(ref_g2)

    f = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f)

    f2 = const_cls(function_factory=factory, a=.25, duration=2)
    g1.add_func(f2)

    with pytest.raises(TypeError):
        g1.init_func(0)

    with pytest.raises(TypeError):
        ref_g2.init_func(0)

    with pytest.raises(TypeError):
        ref_g2(0)

    with pytest.raises(TypeError):
        g1(0)
Example #5
0
def test_expand_ref_funcs(function_factory: FunctionFactoryBase):
    from ceed.function import FuncGroup, CeedFuncRef
    factory = function_factory
    const_cls = factory.get('ConstFunc')

    g1 = FuncGroup(function_factory=factory)

    g2 = FuncGroup(function_factory=factory)
    ref_g2 = function_factory.get_func_ref(func=g2)
    g1.add_func(ref_g2)

    f = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f)
    f1 = const_cls(function_factory=factory, a=.1, duration=2)
    g2.add_func(f1)

    f2 = const_cls(function_factory=factory, a=.25, duration=2)
    g1.add_func(f2)
    f3 = const_cls(function_factory=factory, a=.75, duration=2)
    ref_f3 = function_factory.get_func_ref(func=f3)
    g1.add_func(ref_f3)

    g3 = FuncGroup(function_factory=factory)
    g1.add_func(g3)
    f4 = const_cls(function_factory=factory, a=.8, duration=2)
    ref_f4 = function_factory.get_func_ref(func=f4)
    g3.add_func(ref_f4)
    f5 = const_cls(function_factory=factory, a=.2, duration=2)
    g3.add_func(f5)

    assert list(g1.get_funcs(step_into_ref=False)) == \
        [g1, ref_g2, f2, ref_f3, g3, ref_f4, f5]
    assert list(g1.get_funcs(step_into_ref=True)) == \
        [g1, g2, f, f1, f2, f3, g3, f4, f5]

    g1_copy = g1.copy_expand_ref()
    # the copy shouldn't have any refs
    assert len(list(g1_copy.get_funcs(step_into_ref=False))) == \
        len(list(g1.get_funcs(step_into_ref=True)))

    for original_f, new_f in zip(g1.get_funcs(step_into_ref=True),
                                 g1_copy.get_funcs(step_into_ref=False)):
        for name in original_f.get_gui_props():
            if name == 'name':
                continue

            assert getattr(original_f, name) == getattr(new_f, name)
Example #6
0
def test_group_remove_func(function_factory: FunctionFactoryBase):
    factory = function_factory
    const_cls = factory.get('ConstFunc')

    g1 = FuncGroup(function_factory=factory)

    g2 = FuncGroup(function_factory=factory)
    ref_g2 = function_factory.get_func_ref(func=g2)
    g1.add_func(ref_g2)
    f = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f)
    f1 = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f1)

    f2 = const_cls(function_factory=factory, a=.25, duration=2)
    g1.add_func(f2)

    assert list(g1.get_funcs(step_into_ref=False)) == [g1, ref_g2, f2]
    assert g1.duration == 6
    assert g1.duration_min == 6
    assert g1.duration_min_total == 6

    g1.remove_func(f2)
    assert list(g1.get_funcs(step_into_ref=False)) == [g1, ref_g2]
    assert g1.duration == 4
    assert g1.duration_min == 4
    assert g1.duration_min_total == 4

    g2.remove_func(f)
    assert list(g1.get_funcs(step_into_ref=False)) == [g1, ref_g2]
    assert g1.duration == 2
    assert g1.duration_min == 2
    assert g1.duration_min_total == 2

    g1.remove_func(ref_g2)
    assert list(g1.get_funcs(step_into_ref=False)) == [
        g1,
    ]
    assert g1.duration == 0
    assert g1.duration_min == 0
    assert g1.duration_min_total == 0
Example #7
0
def test_clear_funcs_with_ref(function_factory: FunctionFactoryBase):
    const_cls = function_factory.get('ConstFunc')
    f = const_cls(function_factory=function_factory,
                  duration=4,
                  a=.7,
                  name='f')
    f2 = const_cls(function_factory=function_factory,
                   duration=5,
                   a=.9,
                   name='f2')

    function_factory.add_func(f)
    function_factory.add_func(f2)

    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert function_factory.funcs_inst['f2'] is f2
    assert f2 in function_factory.funcs_user

    ref = function_factory.get_func_ref(name='f')
    function_factory.clear_added_funcs()

    # f should not have been removed, but f2 was removed
    assert ref.func is f
    assert f.has_ref
    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert 'f2' not in function_factory.funcs_inst
    assert f2 not in function_factory.funcs_user

    function_factory.clear_added_funcs(force=True)

    assert ref.func is f
    assert f.has_ref
    assert 'f' not in function_factory.funcs_inst
    assert f not in function_factory.funcs_user

    function_factory.return_func_ref(ref)
    assert not f.has_ref
Example #8
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]]
Example #9
0
def test_remove_func_with_ref(function_factory: FunctionFactoryBase):
    const_cls = function_factory.get('ConstFunc')
    f = const_cls(function_factory=function_factory,
                  duration=4,
                  a=.7,
                  name='f')
    f2 = const_cls(function_factory=function_factory,
                   duration=5,
                   a=.9,
                   name='f2')
    f3 = const_cls(function_factory=function_factory,
                   duration=5,
                   a=.9,
                   name='f3')

    function_factory.add_func(f)
    function_factory.add_func(f2)
    function_factory.add_func(f3)

    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert function_factory.funcs_inst['f2'] is f2
    assert f2 in function_factory.funcs_user
    assert function_factory.funcs_inst['f3'] is f3
    assert f3 in function_factory.funcs_user

    ref = function_factory.get_func_ref(name='f')
    ref3 = function_factory.get_func_ref(name='f3')
    assert not function_factory.remove_func(f)

    assert ref.func is f
    assert f.has_ref
    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert function_factory.funcs_inst['f2'] is f2
    assert f2 in function_factory.funcs_user
    assert function_factory.funcs_inst['f3'] is f3
    assert f3 in function_factory.funcs_user

    assert function_factory.remove_func(f2)

    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert 'f2' not in function_factory.funcs_inst
    assert f2 not in function_factory.funcs_user
    assert function_factory.funcs_inst['f3'] is f3
    assert f3 in function_factory.funcs_user

    assert not function_factory.remove_func(f3)

    assert ref3.func is f3
    assert f3.has_ref
    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert function_factory.funcs_inst['f3'] is f3
    assert f3 in function_factory.funcs_user

    assert function_factory.remove_func(f3, force=True)

    assert ref3.func is f3
    assert f3.has_ref
    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user
    assert 'f3' not in function_factory.funcs_inst
    assert f3 not in function_factory.funcs_user

    assert not function_factory.remove_func(f)

    assert ref.func is f
    assert f.has_ref
    assert function_factory.funcs_inst['f'] is f
    assert f in function_factory.funcs_user

    function_factory.return_func_ref(ref)
    assert not f.has_ref

    assert function_factory.remove_func(f)

    assert 'f' not in function_factory.funcs_inst
    assert f not in function_factory.funcs_user

    function_factory.return_func_ref(ref3)
    assert not f3.has_ref
Example #10
0
def test_replace_ref_func_with_source_funcs(
        function_factory: FunctionFactoryBase):
    from ceed.function import FuncGroup, CeedFuncRef
    factory = function_factory
    const_cls = factory.get('ConstFunc')

    g1 = FuncGroup(function_factory=factory, name='g1')

    g2 = FuncGroup(function_factory=factory, name='g2')
    ref_g2 = function_factory.get_func_ref(func=g2)
    g1.add_func(ref_g2)

    f = const_cls(function_factory=factory, a=.9, duration=2)
    g2.add_func(f)

    f1 = const_cls(function_factory=factory, a=.1, duration=2, name='f1')
    function_factory.add_func(f1)
    ref_f1 = function_factory.get_func_ref(func=f1)
    g2.add_func(ref_f1)

    f2 = const_cls(function_factory=factory, a=.25, duration=2)
    g1.add_func(f2)

    f3 = const_cls(function_factory=factory, a=.75, duration=2, name='f3')
    function_factory.add_func(f3)
    ref_f3 = function_factory.get_func_ref(func=f3)
    g1.add_func(ref_f3)

    with pytest.raises(ValueError):
        g1.replace_ref_func_with_source(f2)

    with pytest.raises(ValueError):
        g1.replace_ref_func_with_source(ref_f1)

    f3_new, i = g1.replace_ref_func_with_source(ref_f3)

    assert i == 2
    assert ref_f3 not in g1.funcs
    assert f3 not in g1.funcs
    assert not isinstance(f3_new, CeedFuncRef)
    assert isinstance(f3_new, f3.__class__)
    assert g1.funcs[i] is f3_new

    for name in f3.get_gui_props():
        if name == 'name':
            continue
        assert getattr(f3, name) == getattr(f3_new, name)

    g2_new: FuncGroup
    g2_new, i = g1.replace_ref_func_with_source(ref_g2)

    assert i == 0
    assert ref_g2 not in g1.funcs
    assert g2 not in g1.funcs
    assert not isinstance(g2_new, CeedFuncRef)
    assert isinstance(g2_new, FuncGroup)
    assert g1.funcs[i] is g2_new

    assert len(g2_new.funcs) == 2
    assert g2_new.funcs[0] is not g2.funcs[0]
    assert g2_new.funcs[1] is not g2.funcs[1]
    assert isinstance(g2_new.funcs[0], f.__class__)
    assert isinstance(g2_new.funcs[1], ref_f1.__class__)
    assert isinstance(g2_new.funcs[1], CeedFuncRef)

    for name in f.get_gui_props():
        if name == 'name':
            continue
        assert getattr(f, name) == getattr(g2_new.funcs[0], name)
    assert g2_new.funcs[1].func is f1