6 from scipy.ndimage.measurements
import center_of_mass
11 config = load_config_from_file(os.getenv(
"COMPASS_ROOT") +
12 "/shesha/tests/pytest/par/test_sh.py")
13 config.p_controller0.set_delay(0.0)
14 config.p_dms[0].unitpervolt = 500
15 config.p_dms[0].push4imat = 0.5
16 config.p_dms[1].unitpervolt = 500
17 config.p_dms[1].push4imat = 0.5
18 sup = Supervisor(config)
19 sup.wfs.wfs.d_wfs[0].set_fakecam(
True)
20 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
21 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
27 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
28 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
30 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
31 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
32 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
34 centro = rtc.d_centro[0]
35 control = rtc.d_control[0]
36 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
37 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
38 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
39 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
40 cmat = sup.rtc.get_command_matrix(0)
41 rtc.d_control[0].set_cmat(cmat)
42 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
43 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
44 rtc.d_centro[0].load_img(frame, frame.shape[0])
45 rtc.d_centro[0].calibrate_img()
48 slp = ng.array(rtc.d_control[0].d_centroids)
50 com = ng.array(rtc.d_control[0].d_com)
52 dark = np.random.random(frame.shape)
53 flat = np.random.random(frame.shape)
54 centro.set_dark(dark, frame.shape[0])
55 centro.set_flat(flat, frame.shape[0])
59 return np.abs((array1 - array2) / array2.max()).max()
63 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
67 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
71 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
75 assert (centro.type ==
"cog")
79 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
83 assert (control.nactu - sup.config.p_controller0.nactu < precision)
87 assert (control.type ==
"generic")
91 assert (control.delay - sup.config.p_controller0.delay < precision)
95 assert (centro.npix - sup.config.p_wfs0.npix < precision)
111 assert (control.gain - sup.config.p_controller0.gain < precision)
127 centro.calibrate_img()
128 imgCal = (frame.astype(np.float32) - dark) * flat
133 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
134 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
135 offset = centro.offset
137 for k
in range(sup.config.p_wfs0._nvalid):
138 tmp = center_of_mass(bincube[:, :, k])
139 slopes[k] = (tmp[0] - offset) * scale
140 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
146 slopes = ng.array(control.d_centroids).toarray()
148 cmat = ng.array(control.d_cmat).toarray()
149 commands = cmat.dot(slopes) * gain * (-1)
155 control.set_comRange(-1, 1)
156 assert (control.comRange == (-1, 1))
160 control.set_comRange(-1, 1)
161 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
162 control.set_com(C, C.size)
165 C_clipped[np.where(C > 1)] = 1
166 C_clipped[np.where(C < -1)] = -1
172 C = np.random.random(sup.config.p_controller0.nactu)
173 control.add_perturb_voltage(
"test", C, 1)
175 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
179 control.remove_perturb_voltage(
"test")
180 assert (control.d_perturb_map == {})
184 C = np.random.random(sup.config.p_controller0.nactu)
185 control.add_perturb_voltage(
"test", C, 1)
186 com = ng.array(control.d_com_clipped).toarray()
187 control.add_perturb()
193 control.disable_perturb_voltage(
"test")
194 com = ng.array(control.d_com).toarray()
195 control.add_perturb()
200 control.enable_perturb_voltage(
"test")
201 com = ng.array(control.d_com_clipped).toarray()
202 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
203 control.add_perturb()
209 control.reset_perturb_voltage()
210 assert (control.d_perturb_map == {})
216 control.set_comRange(volt_min, volt_max)
217 control.comp_voltage()
218 C = np.random.random(sup.config.p_controller0.nactu)
219 control.add_perturb_voltage(
"test", C, 1)
220 control.set_com(C, C.size)
221 com0 = ng.array(control.d_circularComs0).toarray()
222 com1 = ng.array(control.d_circularComs1).toarray()
223 control.comp_voltage()
224 delay = sup.config.p_controller0.delay
225 a = delay - int(delay)
227 commands = a * com0 + b * com1
228 comPertu = commands + C
229 comPertu[np.where(comPertu > volt_max)] = volt_max
230 comPertu[np.where(comPertu < volt_min)] = volt_min
231 val_max = control.val_max
232 commands = np.uint16((comPertu - volt_min) / (volt_max - volt_min) * val_max)
238 rtc.remove_centroider(0)
239 assert (rtc.d_centro == [])
243 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
244 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
247 centro = rtc.d_centro[-1]
249 centro.set_threshold(threshold)
250 centro.set_npix(sup.config.p_wfs0.npix)
251 centro.load_validpos(xvalid, yvalid, xvalid.size)
252 centro.load_img(frame, frame.shape[0])
253 centro.calibrate_img()
255 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
256 offset = centro.offset
258 vx = sup.config.p_wfs0._validsubsx
259 vy = sup.config.p_wfs0._validsubsy
260 npix = sup.config.p_wfs0.npix
261 for k
in range(sup.config.p_wfs0._nvalid):
262 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
263 np.float32) - threshold
264 imagette[np.where(imagette < 0)] = 0
265 tmp = center_of_mass(imagette)
266 slopes[k] = (tmp[0] - offset) * scale
267 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
273 rtc.remove_centroider(0)
274 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
275 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
278 centro = rtc.d_centro[-1]
281 centro.set_npix(sup.config.p_wfs0.npix)
282 centro.load_validpos(xvalid, yvalid, xvalid.size)
283 centro.load_img(frame, frame.shape[0])
284 centro.calibrate_img()
286 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
287 bincube /= bincube.max()
288 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
289 offset = centro.offset
291 vx = sup.config.p_wfs0._validsubsx
292 vy = sup.config.p_wfs0._validsubsy
293 npix = sup.config.p_wfs0.npix
294 for k
in range(sup.config.p_wfs0._nvalid):
295 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
296 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
297 imagette -= threshold
298 imagette[np.where(imagette < 0)] = 0
299 tmp = center_of_mass(imagette)
300 slopes[k] = (tmp[0] - offset) * scale
301 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale