Exemple #1
0
def test_conditional_sequence_compilation2(root):
    """Test compiling a conditional sequence whose condition evaluates to
    False.

    """
    root.external_vars = OrderedDict({'a': 1.5, 'include': False})

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = ConditionalSequence(condition='{include}')
    add_children(sequence1, [pulse2, sequence2, pulse4])

    add_children(root, [pulse1, sequence1, pulse5])

    res, missings, errors = root.evaluate_sequence()
    assert res
    pulses = root.simplify_sequence()
    assert len(pulses), 2
    assert pulses[0] is pulse1
    assert pulses[0].start, 1.0
    assert pulses[0].stop, 2.0
    assert pulses[0].duration, 1.0
    assert pulses[1] is pulse5
    assert pulses[1].start, 3.0
    assert pulses[1].stop, 3.5
    assert pulses[1].duration, 0.5
def test_sequence_compilation13(root):
    """Test compiling a nested sequence with wrong local vars definitions.

    """
    root.external_vars = OrderedDict({'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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars=OrderedDict({
        'b': '2**',
        'c': '{dummy}'
    }))
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 2
    assert 'b' in missings
    assert 'dummy' in missings
    assert '4_b' in errors
def test_sequence_compilation10(root):
    """Test compiling a nested sequence with circular reference in the deep
    one.

    """
    root.external_vars = OrderedDict({'a': 1.5})

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='{1_stop}', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 2
    assert '7_start' in missings
    assert '1_stop' in missings
    assert not errors
def test_sequence_compilation18(root):
    """Test compiling a nested fixed duration sequence.

    """
    root.external_vars = OrderedDict({'a': 1.5})
    root.time_constrained = True
    root.sequence_duration = '100'

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{4_start} + 0.5', def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5',
                             def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))
    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    print(errors)
    assert res

    root.sequence_duration = '1'
    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert 'root-stop' in errors
def test_sequence_compilation17(root):
    """Test compiling a nested sequence with internal fixed length but
    incoherent pulse stop.

    """
    root.external_vars = OrderedDict({'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='{4_start} + 0.5', def_2='{4_start}+{4_duration}+0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5',
                             def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert not missings
    assert 'test-stop' in errors
def test_sequence_compilation17(root):
    """Test compiling a nested sequence with internal fixed length but
    incoherent pulse stop.

    """
    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='{4_start} + 0.5',
                   def_2='{4_start}+{4_duration}+0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5', def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert not missings
    assert 'test-stop' in errors
def test_sequence_compilation18(root):
    """Test compiling a nested fixed duration sequence.

    """
    root.external_vars = {'a': 1.5}
    root.time_constrained = True
    root.sequence_duration = '100'

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{4_start} + 0.5',
                   def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5', def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))
    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    print(errors)
    assert res

    root.sequence_duration = '1'
    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert 'root-stop' in errors
def test_conditional_sequence_compilation2(root):
    """Test compiling a conditional sequence whose condition evaluates to
    False.

    """
    root.external_vars = {'a': 1.5, 'include': False}

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = ConditionalSequence(condition='{include}')
    add_children(sequence1, [pulse2, sequence2, pulse4])

    add_children(root, [pulse1, sequence1, pulse5])

    res, missings, errors = root.evaluate_sequence()
    assert res
    pulses = root.simplify_sequence()
    assert len(pulses), 2
    assert pulses[0] is pulse1
    assert pulses[0].start, 1.0
    assert pulses[0].stop, 2.0
    assert pulses[0].duration, 1.0
    assert pulses[1] is pulse5
    assert pulses[1].start, 3.0
    assert pulses[1].stop, 3.5
    assert pulses[1].duration, 0.5
def test_sequence_compilation13(root):
    """Test compiling a nested sequence with wrong local vars definitions.

    """
    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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars={'b': '2**', 'c': '{dummy}'})
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 2
    assert 'b' in missings
    assert 'dummy' in missings
    assert '4_b' in errors
def test_get_accessible_vars():
    """Test getting the accessible vars of a sequence.

    """
    root = RootSequence(external_vars=OrderedDict({'a': 1}),
                        local_vars=OrderedDict({'a2': '5'}),
                        time_constrained=True)
    context = TestContext()
    root.context = context
    sequence1 = BaseSequence(local_vars=OrderedDict({'b': '1'}))
    sequence2 = BaseSequence(local_vars=OrderedDict({'c': '2'}))
    pulse = Pulse()

    sequence2.add_child_item(0, pulse)
    sequence1.add_child_item(0, sequence2)
    root.add_child_item(0, sequence1)

    variables = [
        'sequence_end', 'a', 'a2', '3_start', '3_stop', '3_duration', 'b', 'c'
    ]
    lv = sequence2.get_accessible_vars()
    for v in variables:
        assert v in lv

    variables.pop()
    lv = sequence1.get_accessible_vars()
    for v in variables:
        assert v in lv

    variables.pop()
    lv = root.get_accessible_vars()
    for v in variables:
        assert v in lv
def test_sequence_compilation10(root):
    """Test compiling a nested sequence with circular reference in the deep
    one.

    """
    root.external_vars = {'a': 1.5}

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='{1_stop}', def_2='0.5',
                   def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 2
    assert '7_start' in missings
    assert '1_stop' in missings
    assert not errors
def test_update_preferences():
    """Test that update preferences does update the items too.

    """
    sequence = BaseSequence()
    sequence.add_child_item(0, Pulse())
    sequence.update_members_from_preferences({'time_constrained': 'True',
                                              'item_0': {'def_1': '10'}})
    assert sequence.time_constrained
    assert sequence.items[0].def_1 == '10'
Exemple #13
0
def template_sequence(pulses_plugin):
    """Create a template sequence and make sure the plugin pick it up.

    """
    from ecpy_pulses.pulses.pulse import Pulse
    from ecpy_pulses.pulses.sequences.base_sequences import (RootSequence,
                                                             BaseSequence)
    from ecpy_pulses.pulses.shapes.square_shape import SquareShape
    from ecpy_pulses.pulses.contexts.template_context import TemplateContext

    root = RootSequence()
    context = TemplateContext(logical_channels=['A', 'B'],
                              analogical_channels=['Ch1', 'Ch2'],
                              channel_mapping={
                                  'A': '',
                                  'B': '',
                                  'Ch1': '',
                                  'Ch2': ''
                              })
    root.context = context
    root.local_vars = OrderedDict({'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 = BaseSequence()
    seq.add_child_item(
        0,
        Pulse(channel='Ch2',
              def_1='{2_stop} + 0.5',
              def_2='{sequence_end}',
              kind='Analogical',
              shape=SquareShape()))
    for i in [pulse1, pulse2, seq, pulse3][::-1]:
        root.add_child_item(0, i)

    pref = root.preferences_from_members()
    pref['template_vars'] = repr(dict(b=''))
    del pref['item_id']
    del pref['external_vars']
    del pref['time_constrained']

    temp_path = os.path.join(pulses_plugin.templates_folders[0],
                             '__dummy__.temp_pulse.ini')
    save_sequence_prefs(temp_path, pref, 'dummy doc')

    pulses_plugin._refresh_known_template_sequences()

    return '__dummy__'
def test_get_accessible_vars():
    """Test getting the accessible vars of a sequence.

    """
    root = RootSequence(external_vars={'a': 1}, local_vars={'a2': '5'},
                        time_constrained=True)
    context = TestContext()
    root.context = context
    sequence1 = BaseSequence(local_vars={'b': '1'})
    sequence2 = BaseSequence(local_vars={'c': '2'})
    pulse = Pulse()

    sequence2.add_child_item(0, pulse)
    sequence1.add_child_item(0, sequence2)
    root.add_child_item(0, sequence1)

    variables = ['sequence_end', 'a', 'a2', '3_start', '3_stop',
                 '3_duration', 'b', 'c']
    lv = sequence2.get_accessible_vars()
    for v in variables:
        assert v in lv

    variables.pop()
    lv = sequence1.get_accessible_vars()
    for v in variables:
        assert v in lv

    variables.pop()
    lv = root.get_accessible_vars()
    for v in variables:
        assert v in lv
def test_analysing_item_dependencies(workbench, item_dep_collector):
    """Test analysing the dependencies of an item.

    """
    dep = set()
    errors = dict()
    run = item_dep_collector.analyse(workbench, BaseSequence(), getattr, dep,
                                     errors)

    assert not run
    assert 'ecpy_pulses.BaseSequence' in dep
    assert not errors

    dep = set()
    errors = dict()
    run = item_dep_collector.analyse(workbench, Pulse(), getattr, dep, errors)

    assert not run
    assert 'ecpy_pulses.Pulse' in dep
    assert not errors

    dep = set()
    run = item_dep_collector.analyse(workbench, {'item_id': '__dummy__'},
                                     getitem, dep, errors)
    assert not run
    assert not dep
    assert '__dummy__' in errors
def test_traverse_sequence():
    """Test traversing a pulse sequence.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    root.external_vars = OrderedDict({'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())
    pulse4 = Pulse(def_1='{2_stop} + 0.5',
                   def_2='10',
                   kind='Analogical',
                   shape=SquareShape())
    seq = BaseSequence()
    add_children(root, [pulse1, pulse2, pulse3, seq])
    add_children(seq, [pulse4])

    items = root.traverse()
    assert len(list(items)) == 11

    assert list(
        root.traverse(0)) == [root, pulse1, pulse2, pulse3, seq, context]
Exemple #17
0
def test_compiling_a_sequence_not_compiling2(workspace, root, monkeypatch,
                                             process_and_sleep, windows):
    """Test compiling a sequence that can be evaluated but not compiled.

    """
    def __raise(*args, **kwargs):
        return False, {}, {'test': False}

    from ecpy_pulses.testing.context import TestContext
    monkeypatch.setattr(TestContext, 'compile_and_transfer_sequence', __raise)
    workbench = workspace.workbench
    ui = workbench.get_plugin('enaml.workbench.ui')
    ui.show_window()
    process_and_sleep()

    root.external_vars = OrderedDict({'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='{4_start} + 0.5', def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5',
                             def_2='6')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    workspace.state.sequence = root

    dial = CompileDialog(workspace=workspace)
    dial.show()
    process_and_sleep()
    comp_widget = dial.central_widget().widgets()[0]

    comp_widget.widgets()[-1].clicked = True
    process_and_sleep()

    assert comp_widget.elapsed_time
    assert comp_widget.errors
    assert comp_widget.widgets()[-2].background == parse_color('red')
Exemple #18
0
def test_compiling_a_sequence_not_compiling2(workspace, root, monkeypatch,
                                             process_and_sleep, windows):
    """Test compiling a sequence that can be evaluated but not compiled.

    """
    def __raise(*args, **kwargs):
        return False, {}, {'test': False}
    from ecpy_pulses.testing.context import TestContext
    monkeypatch.setattr(TestContext, 'compile_and_transfer_sequence',
                        __raise)
    workbench = workspace.workbench
    ui = workbench.get_plugin('enaml.workbench.ui')
    ui.show_window()
    process_and_sleep()

    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='{4_start} + 0.5',
                   def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5', def_2='6')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    workspace.state.sequence = root

    dial = CompileDialog(workspace=workspace)
    dial.show()
    process_and_sleep()
    comp_widget = dial.central_widget().widgets()[0]

    comp_widget.widgets()[-1].clicked = True
    process_and_sleep()

    assert comp_widget.elapsed_time
    assert comp_widget.errors
    assert comp_widget.widgets()[-2].background == parse_color('red')
def test_sequence_compilation15(root):
    """Test compiling a nested sequence with internal fixed length.

    """
    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='{4_start} + 0.5',
                   def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5', def_2='6')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    pulses = root.simplify_sequence()
    assert res
    assert len(pulses) == 5
    assert pulses[0] is pulse1
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[1] is pulse2
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2] is pulse3
    assert pulses[2].start == 4
    assert pulses[2].stop == 5.5
    assert pulses[2].duration == 1.5
    assert pulses[3] is pulse4
    assert pulses[3].start == 2.0
    assert pulses[3].stop == 2.5
    assert pulses[3].duration == 0.5
    assert pulses[4] is pulse5
    assert pulses[4].start == 3.0
    assert pulses[4].stop == 3.5
    assert pulses[4].duration == 0.5
Exemple #20
0
def test_collect_dependencies(workbench):
    """Test collecting build dependencies.

    """
    from ecpy_pulses.pulses.sequences.base_sequences import RootSequence
    root = RootSequence(context=TestContext())

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='{1_stop}', def_2='0.5',
                   def_mode='Start/Duration')
    pulse5.shape = SquareShape(amplitude='0.5')
    pulse5.kind = 'Analogical'

    pulse5.modulation.frequency = '1.0**'
    pulse5.modulation.phase = '1.0'
    pulse5.modulation.activated = True

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    sequence1.add_child_item(0, pulse2)
    sequence1.add_child_item(1, sequence2)
    sequence1.add_child_item(2, pulse4)

    root.add_child_item(0, pulse1)
    root.add_child_item(1, sequence1)
    root.add_child_item(2, pulse5)

    core = workbench.get_plugin(u'enaml.workbench.core')
    com = 'ecpy.app.dependencies.analyse'
    dep = core.invoke_command(com, {'obj': root, 'dependencies': 'build'})
    assert not dep.errors

    com = 'ecpy.app.dependencies.collect'
    dep = core.invoke_command(com, {'kind': 'build',
                                    'dependencies': dep.dependencies})

    assert not dep.errors
    assert 'ecpy.pulses.items' in dep.dependencies
    assert 'ecpy.pulses.contexts' in dep.dependencies
    assert 'ecpy.pulses.shapes' in dep.dependencies
    assert 'ecpy.pulses.modulation' in dep.dependencies
def test_sequence_compilation15(root):
    """Test compiling a nested sequence with internal fixed length.

    """
    root.external_vars = OrderedDict({'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='{4_start} + 0.5', def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5',
                             def_2='6')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    pulses = root.simplify_sequence()
    assert res
    assert len(pulses) == 5
    assert pulses[0] is pulse1
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[1] is pulse2
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2] is pulse3
    assert pulses[2].start == 4
    assert pulses[2].stop == 5.5
    assert pulses[2].duration == 1.5
    assert pulses[3] is pulse4
    assert pulses[3].start == 2.0
    assert pulses[3].stop == 2.5
    assert pulses[3].duration == 0.5
    assert pulses[4] is pulse5
    assert pulses[4].start == 3.0
    assert pulses[4].stop == 3.5
    assert pulses[4].duration == 0.5
Exemple #22
0
def test_base_sequence_refresh(windows):
    """Test the refresh function of the base sequence view.

    """
    view = BaseSequenceView(item=BaseSequence())
    show_widget(view)
    view.hide()
    view.refresh()
    assert view.visible
Exemple #23
0
def test_abstract_refresh(windows):
    """Test the refresh function of the abstract view.

    """
    view = AbstractSequenceView(item=BaseSequence())
    show_widget(view)
    view.hide()
    view.refresh()
    assert view.visible
def test_sequence_compilation12(root):
    """Test compiling a nested sequence using local vars.

    """
    root.external_vars = OrderedDict({'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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars=OrderedDict({'b': '2**2'}))
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    print(missings, errors)
    pulses = root.simplify_sequence()
    assert res
    assert len(pulses) == 5
    assert pulses[0] is pulse1
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[1] is pulse2
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2] is pulse3
    assert pulses[2].start == 3.5
    assert pulses[2].stop == 4
    assert pulses[2].duration == 0.5
    assert pulses[3] is pulse4
    assert pulses[3].start == 2.0
    assert pulses[3].stop == 2.5
    assert pulses[3].duration == 0.5
    assert pulses[4] is pulse5
    assert pulses[4].start == 3.0
    assert pulses[4].stop == 3.5
    assert pulses[4].duration == 0.5
def test_sequence_compilation7(root):
    """Test compiling a nested sequence with a disabled item

    """
    root.external_vars = OrderedDict({'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='{3_stop} + 0.5', def_2='10.0')
    pulse3bis = Pulse(def_1='{3_stop} + 0.5', def_2='10.0', enabled=False)
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    add_children(sequence2, (pulse3, pulse3bis))
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    pulses = root.simplify_sequence()
    assert res
    assert len(pulses) == 5
    assert pulses[0] is pulse1
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[1] is pulse2
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2] is pulse3
    assert pulses[2].start == 3.5
    assert pulses[2].stop == 10.0
    assert pulses[2].duration == 6.5
    assert pulses[3] is pulse4
    assert pulses[3].start == 2.0
    assert pulses[3].stop == 2.5
    assert pulses[3].duration == 0.5
    assert pulses[4] is pulse5
    assert pulses[4].start == 3.0
    assert pulses[4].stop == 3.5
    assert pulses[4].duration == 0.5
def test_sequence_compilation12(root):
    """Test compiling a nested sequence using local vars.

    """
    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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars={'b': '2**2'})
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    print(missings, errors)
    pulses = root.simplify_sequence()
    assert res
    assert len(pulses) == 5
    assert pulses[0] is pulse1
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[1] is pulse2
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2] is pulse3
    assert pulses[2].start == 3.5
    assert pulses[2].stop == 4
    assert pulses[2].duration == 0.5
    assert pulses[3] is pulse4
    assert pulses[3].start == 2.0
    assert pulses[3].stop == 2.5
    assert pulses[3].duration == 0.5
    assert pulses[4] is pulse5
    assert pulses[4].start == 3.0
    assert pulses[4].stop == 3.5
    assert pulses[4].duration == 0.5
Exemple #27
0
def test_compiling_a_sequence_not_compiling(workspace, process_and_sleep, root,
                                            windows):
    """Test compiling that cannot compile as the sequence does not evaluate.

    """
    workbench = workspace.workbench
    ui = workbench.get_plugin('enaml.workbench.ui')
    ui.show_window()
    process_and_sleep()

    root.external_vars = {'a': 1.5}

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a + {b} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{4_start} - 0.5',
                   def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5', def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    workspace.state.sequence = root

    dial = CompileDialog(workspace=workspace)
    dial.show()
    process_and_sleep()
    comp_widget = dial.central_widget().widgets()[0]

    comp_widget.widgets()[-1].clicked = True
    process_and_sleep()

    assert comp_widget.elapsed_time
    assert comp_widget.errors
    assert comp_widget.widgets()[-2].background == parse_color('red')
Exemple #28
0
def test_compiling_a_sequence_not_compiling(workspace, process_and_sleep, root,
                                            windows):
    """Test compiling that cannot compile as the sequence does not evaluate.

    """
    workbench = workspace.workbench
    ui = workbench.get_plugin('enaml.workbench.ui')
    ui.show_window()
    process_and_sleep()

    root.external_vars = OrderedDict({'a': 1.5})

    pulse1 = Pulse(def_1='1.0', def_2='{a}')
    pulse2 = Pulse(def_1='{a + {b} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{4_start} - 0.5', def_2='{4_start}+{4_duration}-0.5')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence(time_constrained=True,
                             def_1='{3_stop} + 0.5',
                             def_2='6',
                             name='test')
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    workspace.state.sequence = root

    dial = CompileDialog(workspace=workspace)
    dial.show()
    process_and_sleep()
    comp_widget = dial.central_widget().widgets()[0]

    comp_widget.widgets()[-1].clicked = True
    process_and_sleep()

    assert comp_widget.elapsed_time
    assert comp_widget.errors
    assert comp_widget.widgets()[-2].background == parse_color('red')
def test_root_handling():
    """Test updating of item when a sequence get/lose the root.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    sequence1 = BaseSequence()
    sequence2 = BaseSequence()
    pulse = Pulse()

    sequence2.add_child_item(0, pulse)
    sequence1.add_child_item(0, sequence2)
    root.add_child_item(0, sequence1)

    assert pulse.root is root and sequence2.root is root
    assert sequence2.has_observers('_last_index')

    root.remove_child_item(0)
    assert pulse.root is None and sequence2.root is None
    assert not sequence2.has_observers('_last_index')
Exemple #30
0
def template_sequence(pulses_plugin):
    """Create a template sequence and make sure the plugin pick it up.

    """
    from ecpy_pulses.pulses.pulse import Pulse
    from ecpy_pulses.pulses.sequences.base_sequences import RootSequence, BaseSequence
    from ecpy_pulses.pulses.shapes.square_shape import SquareShape
    from ecpy_pulses.pulses.contexts.template_context import TemplateContext

    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 = BaseSequence()
    seq.add_child_item(
        0, Pulse(channel="Ch2", def_1="{2_stop} + 0.5", def_2="{sequence_end}", kind="Analogical", shape=SquareShape())
    )
    for i in [pulse1, pulse2, seq, pulse3][::-1]:
        root.add_child_item(0, i)

    pref = root.preferences_from_members()
    pref["template_vars"] = repr(dict(b=""))
    del pref["item_id"]
    del pref["external_vars"]
    del pref["time_constrained"]

    temp_path = os.path.join(pulses_plugin.templates_folders[0], "__dummy__.temp_pulse.ini")
    save_sequence_prefs(temp_path, pref, "dummy doc")

    pulses_plugin._refresh_known_template_sequences()

    return "__dummy__"
Exemple #31
0
def test_conditional_sequence_compilation3(root):
    """Test compiling a conditional sequence with a wrong condition.

    """
    root.external_vars = OrderedDict({'a': 1.5, 'include': False})

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = ConditionalSequence(condition='{include}*/')
    add_children(sequence1, [pulse2, sequence2, pulse4])

    add_children(root, [pulse1, sequence1, pulse5])

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert '2_condition' in errors
def test_conditional_sequence_compilation3(root):
    """Test compiling a conditional sequence with a wrong condition.

    """
    root.external_vars = {'a': 1.5, 'include': False}

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='0.5', def_mode='Start/Duration')

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = ConditionalSequence(condition='{include}*/')
    add_children(sequence1, [pulse2, sequence2, pulse4])

    add_children(root, [pulse1, sequence1, pulse5])

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert '2_condition' in errors
def test_sequence_time_constaints_observation():
    """Test observation of time constraint by the root.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    sequence = BaseSequence()
    root.add_child_item(0, sequence)

    assert root.global_vars == []

    sequence.time_constrained = True

    assert (sorted(root.global_vars) ==
            sorted(['1_start', '1_stop', '1_duration']))

    sequence.time_constrained = False

    assert root.global_vars == []

    root.time_constrained = True
    assert root.linkable_vars
def test_sequence_time_constaints_observation():
    """Test observation of time constraint by the root.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    sequence = BaseSequence()
    root.add_child_item(0, sequence)

    assert root.global_vars == []

    sequence.time_constrained = True

    assert (sorted(root.global_vars) == sorted(
        ['1_start', '1_stop', '1_duration']))

    sequence.time_constrained = False

    assert root.global_vars == []

    root.time_constrained = True
    assert root.linkable_vars
def test_sequence_compilation14(root):
    """Test the locality of local vars.

    """
    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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='{b}', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars={'b': '2**2'})
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 1
    assert 'b' in missings
    assert not errors
def test_sequence_compilation14(root):
    """Test the locality of local vars.

    """
    root.external_vars = OrderedDict({'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='{3_stop} + 0.5', def_2='{b}')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='3.0', def_2='{b}', def_mode='Start/Duration')

    sequence2 = BaseSequence(local_vars=OrderedDict({'b': '2**2'}))
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    add_children(sequence1, (pulse2, sequence2, pulse4))

    add_children(root, (pulse1, sequence1, pulse5))

    res, missings, errors = root.evaluate_sequence()
    assert not res
    assert len(missings) == 1
    assert 'b' in missings
    assert not errors
Exemple #37
0
def test_root_sequence_view(windows, workbench):
    """Test the root sequence view.

    """
    core = workbench.get_plugin('enaml.workbench.core')
    view = RootSequenceView(item=RootSequence(), core=core)

    show_widget(view)
    seq = BaseSequence()
    view.item.add_child_item(0, seq)
    assert seq in view._cache
    view.item.remove_child_item(0)
    assert seq not in view._cache

    but = view.widgets()[-1].pages()[1].page_widget().widgets()[0]
    with handle_dialog('reject'):
        but.clicked = True
def test_root_handling():
    """Test updating of item when a sequence get/lose the root.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    sequence1 = BaseSequence()
    sequence2 = BaseSequence()
    pulse = Pulse()

    sequence2.add_child_item(0, pulse)
    sequence1.add_child_item(0, sequence2)
    root.add_child_item(0, sequence1)

    assert pulse.root is root and sequence2.root is root
    assert sequence2.has_observers('_last_index')

    root.remove_child_item(0)
    assert pulse.root is None and sequence2.root is None
    assert not sequence2.has_observers('_last_index')
def test_update_preferences():
    """Test that update preferences does update the items too.

    """
    sequence = BaseSequence()
    sequence.add_child_item(0, Pulse())
    sequence.update_members_from_preferences({
        'time_constrained': 'True',
        'item_0': {
            'def_1': '10'
        }
    })
    assert sequence.time_constrained
    assert sequence.items[0].def_1 == '10'
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 = BaseSequence()
    sequence2 = BaseSequence()

    root.add_child_item(0, pulse1)
    root.add_child_item(1, sequence1)
    root.add_child_item(2, pulse2)

    assert sequence1.parent is root
    assert sequence1.root is root

    sequence1.add_child_item(0, sequence2)

    assert sequence2.parent is sequence1
    assert sequence2.root is root
    assert pulse1.index == 1
    assert pulse2.index == 4
    assert (sorted(root.get_accessible_vars()) ==
            sorted(['1_start', '1_stop', '1_duration',
                    '4_start', '4_stop', '4_duration']))

    pulse1.index = 200
    sequence2.add_child_item(0, pulse3)

    assert pulse3.parent is sequence2
    assert pulse3.root is root
    assert pulse2.index == 5
    assert pulse3.index == 4
    assert (sorted(root.get_accessible_vars()) ==
            sorted(['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 pulse1.index == 200
    pulse1.index = 0

    sequence1.add_child_item(0, pulse4)

    assert pulse4.index == 3
    assert sequence2.index == 4
    assert pulse3.index == 5
    assert pulse2.index == 6
    assert (sorted(root.get_accessible_vars()) ==
            sorted(['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.remove_child_item(1)

    assert sequence2.parent is None
    assert sequence2.index == 0
    assert pulse2.index == 4
    assert (sorted(root.get_accessible_vars()) ==
            sorted(['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
    while True:
        sequence2.remove_child_item(0)
        if not sequence2.items:
            break

    # Check the observer was properly removed
    assert sequence1.index == 200
def test_collect_dependencies(workbench):
    """Test collecting build dependencies.

    """
    from ecpy_pulses.pulses.sequences.base_sequences import RootSequence
    root = RootSequence(context=TestContext())

    pulse1 = Pulse(def_1='1.0', def_2='{7_start} - 1.0')
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='{6_start} + 1.0')
    pulse3 = Pulse(def_1='{3_stop} + 0.5', def_2='10.0')
    pulse4 = Pulse(def_1='2.0', def_2='0.5', def_mode='Start/Duration')
    pulse5 = Pulse(def_1='{1_stop}', def_2='0.5', def_mode='Start/Duration')
    pulse5.shape = SquareShape(amplitude='0.5')
    pulse5.kind = 'Analogical'

    pulse5.modulation.frequency = '1.0**'
    pulse5.modulation.phase = '1.0'
    pulse5.modulation.activated = True

    sequence2 = BaseSequence()
    sequence2.add_child_item(0, pulse3)
    sequence1 = BaseSequence()
    sequence1.add_child_item(0, pulse2)
    sequence1.add_child_item(1, sequence2)
    sequence1.add_child_item(2, pulse4)

    root.add_child_item(0, pulse1)
    root.add_child_item(1, sequence1)
    root.add_child_item(2, pulse5)

    core = workbench.get_plugin(u'enaml.workbench.core')
    com = 'ecpy.app.dependencies.analyse'
    dep = core.invoke_command(com, {'obj': root, 'dependencies': 'build'})
    assert not dep.errors

    com = 'ecpy.app.dependencies.collect'
    dep = core.invoke_command(com, {
        'kind': 'build',
        'dependencies': dep.dependencies
    })

    assert not dep.errors
    assert 'ecpy.pulses.item' in dep.dependencies
    assert 'ecpy.pulses.context' in dep.dependencies
    assert 'ecpy.pulses.shape' in dep.dependencies
    assert 'ecpy.pulses.modulation' in dep.dependencies
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 = BaseSequence()
    sequence2 = BaseSequence()

    root.add_child_item(0, pulse1)
    root.add_child_item(1, sequence1)
    root.add_child_item(2, pulse2)

    assert sequence1.parent is root
    assert sequence1.root is root

    sequence1.add_child_item(0, sequence2)

    assert sequence2.parent is sequence1
    assert sequence2.root is root
    assert pulse1.index == 1
    assert pulse2.index == 4
    assert (sorted(root.get_accessible_vars()) == sorted(
        ['1_start', '1_stop', '1_duration', '4_start', '4_stop',
         '4_duration']))

    pulse1.index = 200
    sequence2.add_child_item(0, pulse3)

    assert pulse3.parent is sequence2
    assert pulse3.root is root
    assert pulse2.index == 5
    assert pulse3.index == 4
    assert (sorted(root.get_accessible_vars()) == sorted([
        '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 pulse1.index == 200
    pulse1.index = 0

    sequence1.add_child_item(0, pulse4)

    assert pulse4.index == 3
    assert sequence2.index == 4
    assert pulse3.index == 5
    assert pulse2.index == 6
    assert (sorted(root.get_accessible_vars()) == sorted([
        '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.remove_child_item(1)

    assert sequence2.parent is None
    assert sequence2.index == 0
    assert pulse2.index == 4
    assert (sorted(root.get_accessible_vars()) == sorted([
        '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
    while True:
        sequence2.remove_child_item(0)
        if not sequence2.items:
            break

    # Check the observer was properly removed
    assert sequence1.index == 200
def test_build_from_config():
    """Test building a pulse sequence.

    """
    root = RootSequence()
    context = TestContext()
    root.context = context
    root.external_vars = OrderedDict({'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())
    pulse4 = Pulse(def_1='{2_stop} + 0.5',
                   def_2='10',
                   kind='Analogical',
                   shape=SquareShape())
    seq = BaseSequence()
    add_children(root, [pulse1, pulse2, pulse3, seq])
    add_children(seq, [pulse4])

    pref = root.preferences_from_members()
    dependecies = {
        'ecpy.pulses.item': {
            'ecpy_pulses.BaseSequence': BaseSequence,
            'ecpy_pulses.Pulse': Pulse
        },
        'ecpy.pulses.shape': {
            'ecpy_pulses.SquareShape': SquareShape
        },
        'ecpy.pulses.context': {
            'ecpy_pulses.TestContext': TestContext
        }
    }

    aux = RootSequence.build_from_config(pref, dependecies)
    assert aux.external_vars == {'a': 1.5}
    assert len(aux.items) == 4
    assert isinstance(aux.context, TestContext)

    pulse1 = aux.items[0]
    assert pulse1.parent
    assert pulse1.def_1 == '1.0'
    assert pulse1.def_2 == '{a}'

    pulse2 = aux.items[1]
    assert pulse2.parent
    assert pulse2.def_1 == '{a} + 1.0'
    assert pulse2.def_2 == '3.0'

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

    seq = aux.items[3]
    assert seq.parent
    assert len(seq.items) == 1