Exemplo n.º 1
0
 def test_maximum_out_of_soft_limits_left(self):
     feedback = DummyGradientMeasure(self.motor['position'], 20 * q.mm)
     optimize_parameter(self.motor["position"],
                        lambda: -feedback(),
                        self.motor.position,
                        optimization.halver,
                        alg_kwargs=self.halver_kwargs).wait()
     assert_almost_equal(self.motor.position, 25 * q.mm)
Exemplo n.º 2
0
 def run_optimization(self, initial_position=1 * q.mm):
     optimize_parameter(
         self.motor["position"],
         lambda: -self.feedback(),
         initial_position,
         optimization.halver,
         alg_kwargs=self.halver_kwargs,
     ).join()
Exemplo n.º 3
0
 def test_maximum_out_of_soft_limits_left(self):
     feedback = DummyGradientMeasure(self.motor["position"], 20 * q.mm)
     optimize_parameter(
         self.motor["position"],
         lambda: -feedback(),
         self.motor.position,
         optimization.halver,
         alg_kwargs=self.halver_kwargs,
     ).join()
     assert_almost_equal(self.motor.position, 25 * q.mm)
Exemplo n.º 4
0
def focus(camera,
          motor,
          measure=np.std,
          opt_kwargs=None,
          plot_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.
    *plot_consumer* is fed with 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.
    """
    if opt_kwargs is None:
        opt_kwargs = {'initial_step': 0.5 * q.mm, 'epsilon': 1e-2 * q.mm}

    def get_measure():
        camera.trigger()
        frame = camera.grab()
        if frame_consumer:
            frame_consumer.send(frame)
        return -measure(frame)

    @coroutine
    def filter_optimization():
        """
        Filter the optimization's (x, y) subresults to only the y part,
        otherwise the the plot update is not lucid.
        """
        while True:
            tup = yield
            if plot_consumer:
                plot_consumer.send(tup[1])

    camera.trigger_mode = camera.trigger_modes.SOFTWARE
    camera.start_recording()
    f = optimize_parameter(motor['position'],
                           get_measure,
                           motor.position,
                           halver,
                           alg_kwargs=opt_kwargs,
                           consumer=filter_optimization())
    f.add_done_callback(lambda unused: camera.stop_recording())
    return f
Exemplo n.º 5
0
def focus(camera, motor, measure=np.std, opt_kwargs=None,
          plot_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.
    *plot_consumer* is fed with 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.
    """
    if opt_kwargs is None:
        opt_kwargs = {'initial_step': 0.5 * q.mm,
                      'epsilon': 1e-2 * q.mm}

    def get_measure():
        camera.trigger()
        frame = camera.grab()
        if frame_consumer:
            frame_consumer.send(frame)
        return - measure(frame)

    @coroutine
    def filter_optimization():
        """
        Filter the optimization's (x, y) subresults to only the y part,
        otherwise the the plot update is not lucid.
        """
        while True:
            tup = yield
            if plot_consumer:
                plot_consumer.send(tup[1])

    camera.trigger_source = camera.trigger_sources.SOFTWARE
    camera.start_recording()
    f = optimize_parameter(motor['position'], get_measure, motor.position,
                           halver, alg_kwargs=opt_kwargs,
                           consumer=filter_optimization())
    f.add_done_callback(lambda unused: camera.stop_recording())
    return f
Exemplo n.º 6
0
def focus(camera, motor, measure=np.std):
    """
    Focus *camera* by moving *motor*. *measure* is a callable that computes a
    scalar that has to be maximized from an image taken with *camera*.

    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}

    def get_measure():
        frame = camera.grab()
        return - measure(frame)

    camera.start_recording()
    f = optimize_parameter(motor['position'], get_measure, motor.position,
                           halver, alg_kwargs=opts)
    f.add_done_callback(lambda unused: camera.stop_recording())
    return f
Exemplo n.º 7
0
 def run_optimization(self, initial_position=1 * q.mm):
     optimize_parameter(self.motor["position"],
                        lambda: -self.feedback(),
                        initial_position,
                        optimization.halver,
                        alg_kwargs=self.halver_kwargs).wait()
Exemplo n.º 8
0
 def optimize(self, algorithm):
     optimize_parameter(self.motor["position"], self.feedback,
                        self.motor.position, algorithm).join()
Exemplo n.º 9
0
 def optimize(self, algorithm):
     optimize_parameter(self.motor["position"], self.feedback,
                        self.motor.position, algorithm).wait()