38 x = (-1.0)**(np.arange(width) - width//2)
39 y = (-1.0)**(np.arange(height) - height//2)
42 probe[15:19, 6:10] = np.outer(x, y)
43 probe[15:19, 6:10] = np.outer(y, x)
44 if direction ==
'right':
55 x = (-1.0)**(np.arange(width) - width//2)
56 y = (-1.0)**(np.arange(height) - height//2)
59 probe[8:12, 15:19] = np.outer(y, x)
60 probe[8:12, 15:19] = np.outer(x, y)
61 probe = np.sign(probe)
74def wait_for_state(client, indi_property, value, wait_time=1, tolerance=None):
76 while not (client[indi_property] == value):
80 while not abs(client[indi_property] - value) < tolerance:
84def calibrate_indi_device(client, device_name, device_property, delta_pertubation, camera, measurement_function, num_stack=50, do_wait=False):
85 client.get_properties(
'{:s}'.format(device_name))
87 property_current =
'{:s}.{:s}.current'.format(device_name, device_property)
88 property_target =
'{:s}.{:s}.target'.format(device_name, device_property)
89 current_position = client[property_current]
90 print(
"Probe to {:f} +- {:f}".format(current_position, delta_pertubation))
93 client[property_target] = current_position + s * delta_pertubation
95 wait_for_state(client, property_current, current_position + s * delta_pertubation, tolerance=0.1 * delta_pertubation)
101 im = camera.grab_stack(num_stack)
103 measurement = measurement_function(im)
104 slope += s * measurement / (2 * delta_pertubation)
106 client[property_target] = current_position
108 wait_for_state(client, property_current, current_position, tolerance=0.1 * delta_pertubation)
143def align_pupil_mask(camera, reference_image, client, indi_targets, reconstruction_matrix, num_steps=None, shift_offset = np.array([0,0]), options = {
'num_stack' : 4}, do_pad=
False):
145 xcorr_class =
XCorrShift(reference_image, 1001, 101, filter_size=1)
147 cmd = np.array([0,0])
148 current_positions = [client[
'{:s}.current'.format(indi_targets[i])]
for i
in [0, 1]]
150 if num_steps
is not None:
151 for k
in range(num_steps):
152 im = camera.grab_stack(options[
'num_stack'])
153 shift = xcorr_class.measure(im) - shift_offset
156 err = reconstruction_matrix.dot(shift)
157 cmd = cmd - 0.25 * err
159 client[
'{:s}.target'.format(indi_targets[0])] = current_positions[0] + cmd[0]
160 client[
'{:s}.target'.format(indi_targets[1])] = current_positions[1] + cmd[1]
165 im = camera.grab_stack(options[
'num_stack'])
166 if im.shape[0] != 1024
and do_pad:
167 roi_state = camera._get_roi_state
168 roi_h = roi_state[
'roi_region_h']
169 roi_w = roi_state[
'roi_region_w']
171 w_center = (1024 - roi_w // 2)
172 h_center = (1024 - roi_h // 2)
174 im_padded = np.zeros(1024, 1024)
175 im_padded = im[h_center:h_center + roi_h//2, w_center:w_center + roi_w//2]
176 im = im_padded.copy()
178 shift = xcorr_class.measure(im) - shift_offset
181 err = reconstruction_matrix.dot(shift)
182 cmd = cmd - 0.25 * err
184 client[
'{:s}.target'.format(indi_targets[0])] = current_positions[0] + cmd[0]
185 client[
'{:s}.target'.format(indi_targets[1])] = current_positions[1] + cmd[1]
188 if abs(shift[0]) <= 0.11
and abs(shift[1]) <0.11:
align_pupil_mask(camera, reference_image, client, indi_targets, reconstruction_matrix, num_steps=None, shift_offset=np.array([0, 0]), options={ 'num_stack' :4}, do_pad=False)
calibrate_indi_device(client, device_name, device_property, delta_pertubation, camera, measurement_function, num_stack=50, do_wait=False)