Пример #1
0
def test_global_id():
    """Test that formatting a global id does raise.

    """
    shape = SquareShape()
    with pytest.raises(RuntimeError):
        shape.format_global_vars_id('r')
Пример #2
0
def test_global_id():
    """Test that formatting a global id does raise.

    """
    shape = SquareShape()
    with pytest.raises(RuntimeError):
        shape.format_global_vars_id("r")
Пример #3
0
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]
Пример #4
0
def test_eval_amplitude2():
    """Test evaluating the entries of a shape when some vars are missing.

    """
    shape = SquareShape(amplitude='1*{b}')
    root_vars = {'a': 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set('b')
Пример #5
0
def test_eval_amplitude_too_large():
    """Test handling a too large amplitude.

    """
    shape = SquareShape(amplitude="2*{a}")
    root_vars = {"a": 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert "0_shape_amplitude" in errors
Пример #6
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__'
Пример #7
0
def test_eval_amplitude_too_large():
    """Test handling a too large amplitude.

    """
    shape = SquareShape(amplitude='2*{a}')
    root_vars = {'a': 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert '0_shape_amplitude' in errors
Пример #8
0
def test_eval_amplitude3():
    """Test evaluating the entries of a shape when some entries are incorrect.

    """
    shape = SquareShape(amplitude='1*{a}+-')
    root_vars = {'a': 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert '0_shape_amplitude' in errors
Пример #9
0
def test_eval_square_amplitude1():
    """Test evaluating the amplitude of a square shape.

    """
    shape = SquareShape(amplitude="1*{a}")
    root_vars = {"a": 1.0}
    missing = set()
    errors = {}

    assert shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert errors == {}
    assert_array_equal(shape.compute(np.ones(1), "mus"), 1.0)
Пример #10
0
def test_eval_square_amplitude1():
    """Test evaluating the amplitude of a square shape.

    """
    shape = SquareShape(amplitude='1*{a}')
    root_vars = {'a': 1.0}
    missing = set()
    errors = {}

    assert shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert errors == {}
    assert_array_equal(shape.compute(np.ones(1), 'mus'), 1.0)
Пример #11
0
def test_eval_amplitude2():
    """Test evaluating the entries of an active modulation when some vars
    are missing.
    Issue on amplitude.

    """
    shape = SquareShape(amplitude="1*{b}")
    root_vars = {"a": 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set("b")
Пример #12
0
def test_eval_amplitude3():
    """Test evaluating the entries of an active modulation when some entries
    are incorrect.
    Issue on frequency.

    """
    shape = SquareShape(amplitude="1*{a}+-")
    root_vars = {"a": 1.0}
    missing = set()
    errors = {}

    assert not shape.eval_entries({}, root_vars, missing, errors)
    assert missing == set()
    assert "0_shape_amplitude" in errors
Пример #13
0
def test_eval_pulse16(pulse):
    """Test evaluating the entries of an analogical pulse.

    """
    pulse.index = 2
    pulse.def_1 = '1.0*2.0'
    pulse.def_2 = '5.0*{a}/{b} + {c}'

    pulse.shape = SquareShape(amplitude='0.5')
    pulse.kind = 'Analogical'

    pulse.modulation.frequency = '0.0'
    pulse.modulation.phase = '0.0'
    pulse.modulation.kind = 'cos'
    pulse.modulation.activated = True

    root_vars = {'a': 2.0, 'b': 5.0, 'c': 1.0}
    missing = set()
    errors = {}

    assert pulse.eval_entries(root_vars, root_vars, missing, errors)

    assert missing == set()
    assert errors == {}
    assert_array_equal(pulse.waveform, 0.5 * np.ones(1))
    assert pulse.shape.index == 2

    pulse.clean_cached_values()
    assert not pulse.modulation._cache
    assert not pulse.shape._cache
def test_sequence_compilation1(root):
    """Test compiling a flat sequence.

    """
    root.external_vars = OrderedDict({'a': 1.5})
    root.local_vars = OrderedDict({'b': '2*{a}'})

    pulse1 = Pulse(def_1='1.0',
                   def_2='{a}',
                   kind='Analogical',
                   shape=SquareShape(amplitude='0.5',
                                     _cache={'amplitude': 1.0}))
    pulse2 = Pulse(def_1='{a} + 1.0', def_2='3.0')
    pulse3 = Pulse(def_1='{2_stop} + 0.5', def_2='10 + {b}')
    add_children(root, (pulse1, pulse2, pulse3))

    res, missings, errors = root.evaluate_sequence()
    print(errors)
    pulses = root.items
    assert res
    assert len(pulses) == 3
    assert pulses[0].start == 1.0
    assert pulses[0].stop == 1.5
    assert pulses[0].duration == 0.5
    assert pulses[0].shape._cache['amplitude'] == 0.5
    assert pulses[1].start == 2.5
    assert pulses[1].stop == 3.0
    assert pulses[1].duration == 0.5
    assert pulses[2].start == 3.5
    assert pulses[2].stop == 13.0
    assert pulses[2].duration == 9.5
Пример #15
0
def test_traversing_pulse(pulse):
    """Test traversing a pulse.

    """
    assert list(pulse.traverse()) == [pulse]

    pulse.kind = 'Analogical'
    assert list(pulse.traverse()) == [pulse, pulse.modulation]

    pulse.shape = SquareShape()
    assert list(pulse.traverse()) == [pulse, pulse.modulation, pulse.shape]
Пример #16
0
def test_pulse_view3(windows, workbench, pulse):
    """Test showing a pulse with a shape at the start at the start.

    """
    import enaml
    from ecpy.testing.util import show_and_close_widget
    with enaml.imports():
        from ecpy_pulses.pulses.sequences.views.base_sequences_views\
            import RootSequenceView

    pulse.kind = 'Analogical'
    pulse.shape = SquareShape()
    root = pulse.root
    root.add_child_item(0, pulse)
    core = workbench.get_plugin('enaml.workbench.core')
    root_view = RootSequenceView(item=root, core=core)
    show_and_close_widget(root_view)
Пример #17
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.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
Пример #18
0
def test_analysing_shape_dependencies(workbench, shape_dep_collector):
    """Test analysing the dependencies of an shape.

    """
    dep = set()
    errors = dict()
    run = shape_dep_collector.analyse(workbench, SquareShape(), getattr, dep,
                                      errors)

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

    dep = set()
    run = shape_dep_collector.analyse(workbench, {'shape_id': '__dummy__'},
                                      getitem, dep, errors)
    assert not run
    assert not dep
    assert '__dummy__' in errors
Пример #19
0
def test_eval_pulse18(pulse):
    """Test evaluating the entries of an analogical pulse whose shape
    evaluation fails.

    """
    pulse.def_1 = '1.0*2.0'
    pulse.def_2 = '5.0*{a}/{b} + {c}'

    pulse.shape = SquareShape(amplitude='0.5*')
    pulse.kind = 'Analogical'

    pulse.modulation.frequency = '1.0'
    pulse.modulation.phase = '1.0'
    pulse.modulation.activated = True

    root_vars = {'a': 2.0, 'b': 5.0, 'c': 1.0}
    missing = set()
    errors = {}

    assert not pulse.eval_entries(root_vars, root_vars, missing, errors)

    assert missing == set()
    assert '0_shape_amplitude' in errors
Пример #20
0
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