def test_rank_models(): RE = RunEngine() # Create accurate fit motor = SynAxis(name='motor') det = SynSignal(name='centroid', func=lambda: 5 * motor.read()['motor']['value'] + 2) fit1 = LinearFit('centroid', 'motor', update_every=None, name='Accurate') RE(scan([det], motor, -1, 1, 50), fit1) # Create inaccurate fit det2 = SynSignal(name='centroid', func=lambda: 25 * motor.read()['motor']['value'] + 2) fit2 = LinearFit('centroid', 'motor', update_every=None, name='Inaccurate') RE(scan([det2], motor, -1, 1, 50), fit2) # Create inaccurate fit det3 = SynSignal(name='centroid', func=lambda: 12 * motor.read()['motor']['value'] + 2) fit3 = LinearFit('centroid', 'motor', update_every=None, name='Midly Inaccurate') RE(scan([det3], motor, -1, 1, 50), fit3) # Rank models ranking = rank_models([fit2, fit1, fit3], target=22, x=4) assert ranking[0] == fit1 assert ranking[1] == fit3 assert ranking[2] == fit2
def test_walk_to_pixel(RE, one_bounce_system): logger.debug("test_walk_to_pixel") _, mot, det = one_bounce_system ########################## # Test on simple devices # ########################## simple_motor = SynAxis(name='motor') simple_det = SynSignal(name='det', func=lambda: 5*simple_motor.read()['motor']['value'] + 2) # Naive step plan = run_wrapper(walk_to_pixel(simple_det, simple_motor, 200, 0, first_step=1e-6, tolerance=10, average=None, target_fields=['det', 'motor'], max_steps=3)) RE(plan) assert np.isclose(simple_det.read()['det']['value'], 200, atol=1) simple_motor.set(0.) # Gradient simple_motor = SynAxis(name='motor') simple_det = SynSignal(name='det', func=lambda: 5*simple_motor.read()['motor']['value'] + 2) plan = run_wrapper(walk_to_pixel(simple_det, simple_motor, 200, 0, gradient=1.6842e+06, tolerance=10, average=None, target_fields=['det', 'motor'], max_steps=3)) RE(plan) assert np.isclose(simple_det.read()['det']['value'], 200, atol=1) ########################## # Test on full model # ########################## # Naive step cent = 'detector_stats2_centroid_x' plan = run_wrapper(walk_to_pixel(det, mot, 200, 0, first_step=1e-6, tolerance=10, average=None, target_fields=[cent, 'sim_alpha'], max_steps=3)) RE(plan) assert np.isclose(det.read()[det.name + "_" + cent]['value'], 200, atol=1) mot.set(0.) # Gradient plan = run_wrapper(walk_to_pixel(det, mot, 200, 0, gradient=1.6842e+06, tolerance=10, average=None, target_fields=[cent, 'sim_alpha'], max_steps=3)) RE(plan) assert np.isclose(det.read()[det.name + "_" + cent]['value'], 200, atol=10)
def test_multi_fit(): RE = RunEngine() # Expected values of fit expected = {'x0': 5, 'x1': 4, 'x2': 3} m1 = SynAxis(name='m1') m2 = SynAxis(name='m2') det = SynSignal(name='centroid', func=lambda: 5 + 4 * m1.read()['m1']['value'] + 3 * m2. read()['m2']['value']) # Assemble fitting callback cb = MultiPitchFit('centroid', ('m1', 'm2'), update_every=None) RE(outer_product_scan([det], m1, -1, 1, 10, m2, -1, 1, 10, False), cb) # Check accuracy of fit logger.debug(cb.result.fit_report()) for k, v in expected.items(): assert np.allclose(cb.result.values[k], v, atol=1e-6) # Check we create an accurate estimate assert np.allclose(cb.eval(a0=5, a1=10), 55, atol=1e-5) assert np.allclose(cb.backsolve(55, a1=10)['a0'], 5, atol=1e-5) assert np.allclose(cb.backsolve(55, a0=5)['a1'], 10, atol=1e-5)
def test_linear_fit(): RE = RunEngine() # Expected values of fit expected = {'slope': 5, 'intercept': 2} motor = SynAxis(name='motor') det = SynSignal(name='centroid', func=lambda: 5 * motor.read()['motor']['value'] + 2) # Assemble fitting callback cb = LinearFit('centroid', 'motor', update_every=None) RE(scan([det], motor, -1, 1, 50), cb) # Check accuracy of fit for k, v in expected.items(): assert np.allclose(cb.result.values[k], v, atol=1e-6) # Check we create an accurate estimate assert np.allclose(cb.eval(x=10), 52, atol=1e-5) assert np.allclose(cb.eval(motor=10), 52, atol=1e-5) assert np.allclose(cb.backsolve(52)['x'], 10, atol=1e-5)