Skip to content

Commit

Permalink
Enable optimization arguments by focusing
Browse files Browse the repository at this point in the history
  • Loading branch information
tfarago committed Nov 7, 2013
1 parent 5ec30fc commit 5024098
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions concert/processes.py
Expand Up @@ -108,21 +108,22 @@ def dscan(parameter_list, n_intervals, handler):
return ascan(parameter_list, n_intervals, handler, initial_values)


def focus(camera, motor, measure=np.std, optimization_consumer=None,
frame_consumer=None):
def focus(camera, motor, measure=np.std, opt_kwargs=None,
optimization_consumer=None, frame_consumer=None):
"""
Focus *camera* by moving *motor*. *measure* is a callable that computes a
scalar that has to be maximized from an image taken with *camera*.
*opt_kwargs* are keyword arguments sent to the optimization algorithm.
*optimization_consumer* is fed with (x, y) values from the optimization and
*frame_consumer* is fed with the incoming frames.
This function is returning a future encapsulating the focusing event. Note,
that the camera is stopped from recording as soon as the optimal position
is found.
"""
# we should guess this from motor limits
opts = {'initial_step': 10 * q.mm,
'epsilon': 5e-3 * q.mm}
if opt_kwargs is None:
opt_kwargs = {'initial_step': 0.5 * q.mm,
'epsilon': 1e-2 * q.mm}

def get_measure():
frame = camera.grab()
Expand All @@ -132,7 +133,7 @@ def get_measure():

camera.start_recording()
f = optimize_parameter(motor['position'], get_measure, motor.position,
halver, alg_kwargs=opts,
halver, alg_kwargs=opt_kwargs,
consumer=optimization_consumer)
f.add_done_callback(lambda unused: camera.stop_recording())
return f
Expand Down
2 changes: 1 addition & 1 deletion concert/tests/integration/test_processes.py
Expand Up @@ -36,7 +36,7 @@ def _trigger_real(self):
def test_focusing():
motor = Motor(hard_limits=(MIN_POSITION.magnitude,
MAX_POSITION.magnitude))
motor.position = 85. * q.mm
motor.position = 40. * q.mm
camera = BlurringCamera(motor)
focus(camera, motor).wait()
assert_almost_equal(motor.position, FOCUS_POSITION, 1e-2)

0 comments on commit 5024098

Please sign in to comment.