Пример #1
0
def test_tune_centroid(RE, hw):
    det = hw.det
    motor = hw.motor
    scan1 = bp.tune_centroid([det], 'det', motor, 0, 5, 0.1, 10, snake=True)
    scan2 = bp.tune_centroid([det], 'det', motor, 0, 5, 0.01, 10, snake=True)
    scan3 = bp.tune_centroid([det], 'det', motor, 0, 5, 0.1, 10, snake=False)
    scan4 = bp.tune_centroid([det], 'det', motor, 5, 0, 0.1, 10, snake=False)

    actual_traj = []
    col = collector('motor', actual_traj)
    counter1 = CallbackCounter()
    counter2 = CallbackCounter()

    RE(scan1, {'event': [col, counter1]})
    RE(scan2, {'event': counter2})
    #assert counter1.value > counter2.value
    assert actual_traj[0] == 0

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan3, {'event': col})
    monotonic_increasing = np.all(np.diff(actual_traj) > 0)
    assert not monotonic_increasing

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan4, {'event': col})
    monotonic_decreasing = np.all(np.diff(actual_traj) < 0)
    assert not monotonic_decreasing

    with pytest.raises(ValueError):  # min step < 0
        scan5 = bp.tune_centroid([det], 'det', motor, 5, 0, -0.1, 10, snake=False)
        RE(scan5)
Пример #2
0
def test_adaptive_ascan(RE, hw):
    scan1 = bp.adaptive_scan([hw.det], 'det', hw.motor, 0, 5, 0.1, 1, 0.1, True)
    scan2 = bp.adaptive_scan([hw.det], 'det', hw.motor, 0, 5, 0.1, 1, 0.2, True)
    scan3 = bp.adaptive_scan([hw.det], 'det', hw.motor, 0, 5, 0.1, 1, 0.1, False)
    scan4 = bp.adaptive_scan([hw.det], 'det', hw.motor, 5, 0, 0.1, 1, 0.1, False)

    actual_traj = []
    col = collector('motor', actual_traj)
    counter1 = CallbackCounter()
    counter2 = CallbackCounter()

    RE(scan1, {'event': [col, counter1]})
    RE(scan2, {'event': counter2})
    assert counter1.value > counter2.value
    assert actual_traj[0] == 0

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan3, {'event': col})
    monotonic_increasing = np.all(np.diff(actual_traj) > 0)
    assert monotonic_increasing

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan4, {'event': col})
    monotonic_decreasing = np.all(np.diff(actual_traj) < 0)
    assert monotonic_decreasing

    with pytest.raises(ValueError):  # min step < 0
        scan5 = bp.adaptive_scan([hw.det], 'det', hw.motor,
                                 5, 0, -0.1, 1.0, 0.1, False)
        RE(scan5)
Пример #3
0
def test_all():
    c = CallbackCounter()
    RE(stepscan(det, motor), subs={'all': c})
    assert c.value == 10 + 1 + 2  # events, descriptor, start and stop

    c = CallbackCounter()
    token = RE.subscribe('all', c)
    RE(stepscan(det, motor))
    RE.unsubscribe(token)
    assert c.value == 10 + 1 + 2
Пример #4
0
def test_all(RE, hw):
    c = CallbackCounter()
    RE(stepscan(hw.det, hw.motor), {'all': c})
    assert c.value == 10 + 1 + 2  # events, descriptor, start and stop

    c = CallbackCounter()
    token = RE.subscribe(c)
    RE(stepscan(hw.det, hw.motor))
    RE.unsubscribe(token)
    assert c.value == 10 + 1 + 2
Пример #5
0
def test_average_stream(RE, hw):
    # Create callback chain
    avg = AverageStream(10)
    c = CallbackCounter()
    d = DocCollector()
    avg.subscribe(c)
    avg.subscribe(d.insert)
    # Run a basic plan
    RE(stepscan(hw.det, hw.motor), {'all': avg})
    assert c.value == 1 + 1 + 2  # events, descriptor, start and stop
    # See that we made sensible descriptor
    start_uid = d.start[0]['uid']
    assert start_uid in d.descriptor
    desc_uid = d.descriptor[start_uid][0]['uid']
    assert desc_uid in d.event
    evt = d.event[desc_uid][0]
    assert evt['seq_num'] == 1
    assert all([
        key in d.descriptor[start_uid][0]['data_keys']
        for key in evt['data'].keys()
    ])
    # See that we returned the correct average
    assert evt['data']['motor'] == -0.5  # mean of range(-5, 5)
    assert evt['data']['motor_setpoint'] == -0.5  # mean of range(-5, 5)
    assert start_uid in d.stop
    assert d.stop[start_uid]['num_events'] == {'primary': 1}
Пример #6
0
def test_adaptive_ascan():
    scan1 = AdaptiveAbsScan([det], 'det', motor, 0, 5, 0.1, 1, 0.1, True)
    scan2 = AdaptiveAbsScan([det], 'det', motor, 0, 5, 0.1, 1, 0.2, True)
    scan3 = AdaptiveAbsScan([det], 'det', motor, 0, 5, 0.1, 1, 0.1, False)

    actual_traj = []
    col = collector('motor', actual_traj)
    counter1 = CallbackCounter()
    counter2 = CallbackCounter()

    RE(scan1, subs={'event': [col, counter1]})
    RE(scan2, subs={'event': counter2})
    assert_greater(counter1.value, counter2.value)
    assert_equal(actual_traj[0], 0)

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan3, {'event': col})
    monotonic_increasing = np.all(np.diff(actual_traj) > 0)
    assert_true(monotonic_increasing)
Пример #7
0
def test_adaptive_dscan():
    scan1 = AdaptiveDeltaScanPlan([det], 'det', motor, 0, 5, 0.1, 1, 0.1, True)
    scan2 = AdaptiveDeltaScanPlan([det], 'det', motor, 0, 5, 0.1, 1, 0.2, True)
    scan3 = AdaptiveDeltaScanPlan([det], 'det', motor, 0, 5, 0.1, 1, 0.1,
                                  False)

    actual_traj = []
    col = collector('motor', actual_traj)
    counter1 = CallbackCounter()
    counter2 = CallbackCounter()

    motor.set(1)
    RE(scan1, subs={'event': [col, counter1]})
    RE(scan2, subs={'event': counter2})
    assert counter1.value > counter2.value
    assert actual_traj[0] == 1

    actual_traj = []
    col = collector('motor', actual_traj)
    RE(scan3, {'event': col})
    monotonic_increasing = np.all(np.diff(actual_traj) > 0)
    assert monotonic_increasing
Пример #8
0
def test_subscribe_msg():
    assert RE.state == 'idle'
    c = CallbackCounter()

    def counting_stepscan(det, motor):
        yield Msg('subscribe', None, 'start', c)
        yield from stepscan(det, motor)

    RE(counting_stepscan(det, motor))  # should advance c
    assert c.value == 1
    RE(counting_stepscan(det, motor))  # should advance c
    assert c.value == 2
    RE(stepscan(det, motor))  # should not
    assert c.value == 2
Пример #9
0
def test_measure_average(RE, hw):
    logger.debug("test_measure_average")

    # Pseudo-plan to measure average and check values
    def measure_plan(detectors):
        yield from open_run()
        ret = yield from measure_average(detectors, num=250)
        assert ret['motor'] == 0.0
        assert ret['motor_setpoint'] == 0.0
        assert np.isclose(ret['noisy_det'], 1.0, atol=0.01)
        yield from close_run()

    # Execute plan
    cnt = CallbackCounter()
    RE(measure_plan([hw.motor, hw.noisy_det]), {'event': [cnt]})
    # Check that we saw the right number of events
    assert cnt.value == 250
Пример #10
0
def test_straight_through_stream(RE, hw):
    # Just a stream that sinks the events it receives
    ss = NegativeStream()
    # Create callback chain
    c = CallbackCounter()
    d = DocCollector()
    ss.subscribe(c)
    ss.subscribe(d.insert)
    # Run a basic plan
    RE(stepscan(hw.det, hw.motor), {'all': ss})
    # Check that our metadata is there
    assert c.value == 10 + 1 + 2  # events, descriptor, start and stop
    assert d.start[0]['stream_level'] == 'boring'
    desc = d.descriptor[d.start[0]['uid']][0]
    events = d.event[desc['uid']]
    print(desc)
    print([evt['data'] for evt in events])
    assert all([
        evt['data'][key] <= 0 for evt in events for key in evt['data'].keys()
    ])
    assert all([key in desc['data_keys'] for key in events[0]['data'].keys()])