def halver(function, x_0, initial_step=None, epsilon=None, max_iterations=100): """ Halving the interval, evaluate *function* based on *param*. Use *initial_step*, *epsilon* precision and *max_iterations*. """ if initial_step is None: # Figure out the step based on x_0 units (take one in the given unit) step = q.Quantity(1, x_0.units) else: step = initial_step if epsilon is None: epsilon = q.Quantity(1e-4, x_0.units) direction = 1 i = 0 last_x = x_0 y_0 = function(x_0) # Remember the best x and y best = x_0, y_0 def turn(direction, step): """Turn to opposite direction and reduce the step by half.""" return -direction, step / 2.0 def move(x_0, direction, step): """Move to a *direction* by a *step*.""" return x_0 + direction * step x_0 = move(x_0, direction, step) while i < max_iterations: y_1 = function(x_0) if step < epsilon: break if y_1 >= y_0: # Worse, change direction and move to the half of the last # good x and the new x. direction, step = turn(direction, step) x_0 = (x_0 + last_x) / 2 else: # OK, move forward. if y_1 < best[1]: # The new y is better then the so far obtained one best = x_0, y_1 last_x = x_0 x_0 = move(x_0, direction, step) y_0 = y_1 i += 1 # Apply the best known value function(best[0]) return best[0]
def q_func(x): """We need to run the objective function in a separate thread and loop because we have been called from a running event loop and have things to do in an event loop and can't use the running one. """ coro = eval_func(q.Quantity(strip_func(x), x_0.units)) return run_in_loop_thread_blocking(coro)
async def _wrapper(instance): value = instance.uca.get_property(name) if unit: return q.Quantity(value, unit) return value
def _vectorize(scalar, coordinate): """ Return a vector with the *scalar* in the correct place given by the *coordinate* as x, y or z. """ vector = np.zeros(3, dtype=np.float) translate = dict(x=0, y=1, z=2) vector[translate[coordinate]] = scalar.magnitude return q.Quantity(vector, scalar.units)
def test_degree_conversion(self): from concert.devices.cameras.uca import Camera camera = Camera("mock") camera.degree_value = q.Quantity(5, q.celsius) self.assertEqual(camera.degree_value.magnitude, 5.0) val = camera.degree_value.magnitude camera.degree_value = camera.degree_value + 5 * q.delta_degC self.assertEqual(camera.degree_value.magnitude, val + 5)
def _get_weight(self): """ Get weight from the scales. The command does not return until a balanced position is found, thus it can time out. """ res = self._connection.execute("P") if "Err8.4" in res: self._set_state(ARRW60.ERROR) raise WeightError("More than maximum weight loaded") else: if self.state == ARRW60.ERROR or self.state == Device.NA: # Clear the error from before or set OK for the first time self._set_state(ARRW60.OK) # The returned string contains units res = q.Quantity(res).to(q.g) return float(res.magnitude) * q.counts
async def wrapper(eval_func, x_0, *args, **kwargs): def q_func(x): """We need to run the objective function in a separate thread and loop because we have been called from a running event loop and have things to do in an event loop and can't use the running one. """ coro = eval_func(q.Quantity(strip_func(x), x_0.units)) return run_in_loop_thread_blocking(coro) def to_run_in_exec(): """Run the optimization *function* in a pool so that we comply with the async nature of concert. """ return function(q_func, x_0.magnitude, *args, **kwargs) dim_less = await run_in_executor(to_run_in_exec) return q.Quantity(dim_less, x_0.units)
def wrapper(eval_func, x_0, *args, **kwargs): q_func = lambda x: eval_func(q.Quantity(strip_func(x), x_0.units)) dim_less = function(q_func, x_0.magnitude, *args, **kwargs) return q.Quantity(dim_less, x_0.units)