Пример #1
0
def test_nested_sequence_persistence1():
    # Test writing a nested pulse sequence to a ConfigObj.
    root = RootSequence()
    context = BaseContext()
    root.context = context
    root.external_vars = {'a': 1.5}

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10',
                   kind='Analogical', shape=SquareShape())
    seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10',
                                kind='Analogical', shape=SquareShape())])
    root.items.extend([pulse1, pulse2, pulse3, seq])

    pref = root.preferences_from_members()
    assert_items_equal(pref.keys(),
                       ['name', 'local_vars', 'time_constrained',
                        'enabled', 'item_class', 'sequence_duration',
                        'item_0', 'item_1', 'item_2', 'item_3',
                        'context', 'def_1', 'def_2', 'def_mode',
                        'external_vars'])
    assert_items_equal(pref['item_3'].keys(),
                       ['item_class', 'enabled', 'name', 'item_0',
                        'def_1', 'def_2', 'def_mode', 'local_vars',
                        'time_constrained'])
Пример #2
0
    def test_build_from_config2(self):
        # Test rebuilding a sequence including a template sequence.
        # Channel mapping of template_vars values are known.
        conf = {'template_id': 'test', 'name': 'Template',
                'template_vars': "{'b': '25'}"}
        seq = TemplateSequence.build_from_config(conf, self.dependecies)
        seq.context.channel_mapping = {'A': 'Ch1_L', 'B': 'Ch2_L',
                                       'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'}
        root = RootSequence()
        context = TestContext(sampling=0.5)
        root.context = context
        root.items = [seq]
        pref = root.preferences_from_members()

        new = RootSequence.build_from_config(pref, self.dependecies)
        assert_equal(new.items[0].index, 1)

        seq = new.items[0]
        assert_equal(seq.name, 'Template')
        assert_equal(seq.template_id, 'test')
        assert_equal(seq.template_vars, dict(b='25'))
        assert_equal(seq.local_vars, dict(a='1.5'))
        assert_equal(len(seq.items), 4)
        assert_equal(seq.items[3].index, 5)
        assert_equal(seq.docs, 'Basic user comment\nff')

        context = seq.context
        assert_equal(context.template, seq)
        assert_equal(context.logical_channels, ['A', 'B'])
        assert_equal(context.analogical_channels, ['Ch1', 'Ch2'])
        assert_equal(context.channel_mapping, {'A': 'Ch1_L', 'B': 'Ch2_L',
                                               'Ch1': 'Ch2_A', 'Ch2': 'Ch1_A'})
Пример #3
0
def test_sequence_indexing1():
    # Test adding, moving, deleting pulse in a sequence.
    root = RootSequence()
    root.time_constrained = True
    root.sequence_duration = '1.0'
    context = TestContext()
    root.context = context

    pulse1 = Pulse()
    pulse2 = Pulse()
    pulse3 = Pulse()

    root.items.append(pulse1)
    assert_equal(pulse1.index, 1)
    assert_is(pulse1.root, root)
    assert_items_equal(root.linkable_vars, ['sequence_end',
                                            '1_start', '1_stop', '1_duration'])

    root.items.append(pulse2)
    assert_equal(pulse1.index, 1)
    assert_equal(pulse2.index, 2)
    assert_is(pulse2.root, root)
    assert_items_equal(root.linkable_vars, ['sequence_end',
                                            '1_start', '1_stop', '1_duration',
                                            '2_start', '2_stop', '2_duration'])

    root.items.append(pulse3)
    assert_equal(pulse1.index, 1)
    assert_equal(pulse2.index, 2)
    assert_equal(pulse3.index, 3)
    assert_is(pulse3.root, root)
    assert_items_equal(root.linkable_vars, ['sequence_end',
                                            '1_start', '1_stop', '1_duration',
                                            '2_start', '2_stop', '2_duration',
                                            '3_start', '3_stop', '3_duration'])

    root.time_constrained = False
    root.items.remove(pulse2)
    assert_equal(pulse1.index, 1)
    assert_equal(pulse2.index, 0)
    assert_equal(pulse3.index, 2)
    assert_is(pulse2.root, None)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '2_start', '2_stop', '2_duration'])

    root.items.insert(1, pulse2)
    assert_equal(pulse1.index, 1)
    assert_equal(pulse2.index, 2)
    assert_equal(pulse3.index, 3)
    assert_is(pulse2.root, root)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '2_start', '2_stop', '2_duration',
                                            '3_start', '3_stop', '3_duration'])
Пример #4
0
def test_sequence_time_constaints_observation():
    # Test adding, moving, deleting pulse in a sequence.
    root = RootSequence()
    context = TestContext()
    root.context = context
    sequence = Sequence()
    root.items = [sequence]

    assert_equal(root.linkable_vars, [])

    sequence.time_constrained = True

    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration'])

    sequence.time_constrained = False

    assert_equal(root.linkable_vars, [])
Пример #5
0
def test_build_from_config():
    # Test building a pulse sequence.
    root = RootSequence()
    context = BaseContext()
    root.context = context
    root.external_vars = {'a': 1.5}

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10',
                   kind='Analogical', shape=SquareShape())
    seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10',
                                kind='Analogical', shape=SquareShape())])
    root.items.extend([pulse1, pulse2, pulse3, seq])

    pref = root.preferences_from_members()
    dependecies = {'pulses': {'Sequence': Sequence, 'Pulse': Pulse,
                              'shapes': {'SquareShape': SquareShape},
                              'contexts': {'BaseContext': BaseContext}}}

    aux = RootSequence.build_from_config(pref, dependecies)
    assert_equal(aux.external_vars, {'a': 1.5})
    assert_equal(len(aux.items), 4)
    assert_is_instance(aux.context, BaseContext)

    pulse1 = aux.items[0]
    assert_equal(pulse1.def_1, '1.0')
    assert_equal(pulse1.def_2, '{a}')

    pulse2 = aux.items[1]
    assert_equal(pulse2.def_1, '{a} + 1.0')
    assert_equal(pulse2.def_2, '3.0')

    pulse3 = aux.items[2]
    assert_equal(pulse3.def_1, '{2_stop} + 0.5')
    assert_equal(pulse3.def_2, '10')
    assert_equal(pulse3.kind, 'Analogical')
    assert_is_instance(pulse3.shape, SquareShape)

    seq = aux.items[3]
    assert_equal(len(seq.items), 1)
Пример #6
0
def test_walk_sequence():
    # Test walking a pulse sequence.
    root = RootSequence()
    context = BaseContext()
    root.context = context
    root.external_vars = {'a': 1.5}

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10',
                   kind='Analogical', shape=SquareShape())
    seq = Sequence(items=[Pulse(def_1='{2_stop} + 0.5', def_2='10',
                                kind='Analogical', shape=SquareShape())])
    root.items.extend([pulse1, pulse2, pulse3, seq])

    walk = root.walk(['item_class', 'shape_class'], {})

    flat = flatten_walk(walk, ['item_class', 'shape_class'])
    assert_in('item_class', flat)
    assert_equal(flat['item_class'],
                 set(['Pulse', 'RootSequence', 'Sequence']))
    assert_in('shape_class', flat)
    assert_equal(flat['shape_class'], set(['SquareShape']))
Пример #7
0
def create_template_sequence():
    root = RootSequence()
    context = TemplateContext(logical_channels=['A', 'B'],
                              analogical_channels=['Ch1', 'Ch2'],
                              channel_mapping={'A': '', 'B': '', 'Ch1': '',
                                               'Ch2': ''})
    root.context = context
    root.local_vars = {'a': '1.5'}

    pulse1 = Pulse(channel='A', def_1='1.0', def_2='{a}')
    pulse2 = Pulse(channel='B', def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(channel='Ch1', def_1='{2_stop} + 0.5', def_2='{b}',
                   kind='Analogical', shape=SquareShape())
    seq = Sequence(items=[Pulse(channel='Ch2',
                                def_1='{2_stop} + 0.5', def_2='{sequence_end}',
                                kind='Analogical', shape=SquareShape())])
    root.items.extend([pulse1, pulse2, seq,  pulse3])

    pref = root.preferences_from_members()
    pref['template_vars'] = repr(dict(b=''))
    del pref['item_class']
    del pref['external_vars']
    del pref['time_constrained']
    return pref
Пример #8
0
def test_sequence_indexing2():
    # Test adding, moving, deleting a sequence in a sequence.
    root = RootSequence()
    context = TestContext()
    root.context = context

    pulse1 = Pulse()
    pulse2 = Pulse()
    pulse3 = Pulse()
    pulse4 = Pulse()

    sequence1 = Sequence()
    sequence2 = Sequence()

    root.items.append(pulse1)
    root.items.append(sequence1)
    root.items.append(pulse2)

    assert_is(sequence1.parent, root)
    assert_is(sequence1.root, root)

    sequence1.items.append(sequence2)

    assert_is(sequence2.parent, sequence1)
    assert_is(sequence2.root, root)
    assert_equal(pulse1.index, 1)
    assert_equal(pulse2.index, 4)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '4_start', '4_stop', '4_duration'])

    pulse1.index = 200
    sequence2.items.append(pulse3)

    assert_is(pulse3.parent, sequence2)
    assert_is(pulse3.root, root)
    assert_equal(pulse2.index, 5)
    assert_equal(pulse3.index, 4)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '4_start', '4_stop', '4_duration',
                                            '5_start', '5_stop', '5_duration'])
    # Check that only the pulse below the modified sequence are updated.
    assert_equal(pulse1.index, 200)
    pulse1.index = 1

    sequence1.items.insert(0, pulse4)

    assert_equal(pulse4.index, 3)
    assert_equal(sequence2.index, 4)
    assert_equal(pulse3.index, 5)
    assert_equal(pulse2.index, 6)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '3_start', '3_stop', '3_duration',
                                            '5_start', '5_stop', '5_duration',
                                            '6_start', '6_stop', '6_duration'])

    sequence1.items = [pulse4]

    assert_is(sequence2.parent, None)
    assert_equal(sequence2.index, 0)
    assert_equal(pulse2.index, 4)
    assert_items_equal(root.linkable_vars, ['1_start', '1_stop', '1_duration',
                                            '3_start', '3_stop', '3_duration',
                                            '4_start', '4_stop', '4_duration'])

    sequence1.index = 200
    root2 = RootSequence()
    sequence2.root = root2
    sequence2.items = []

    # Check the observer was properly removed
    assert_equal(sequence1.index, 200)