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)
20 sup.wfs.wfs.d_wfs[0].set_fakecam(
True)
21 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
22 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
28 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
29 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
31 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
32 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
33 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
35 centro = rtc.d_centro[0]
36 control = rtc.d_control[0]
37 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
38 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
39 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
40 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
41 cmat = sup.rtc.get_command_matrix(0)
42 rtc.d_control[0].set_cmat(cmat)
43 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
44 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
45 rtc.d_centro[0].load_img(frame, frame.shape[0])
46 rtc.d_centro[0].calibrate_img()
49 slp = ng.array(rtc.d_control[0].d_centroids)
51 com = ng.array(rtc.d_control[0].d_com)
53 dark = np.random.random(frame.shape)
54 flat = np.random.random(frame.shape)
55 centro.set_dark(dark, frame.shape[0])
56 centro.set_flat(flat, frame.shape[0])
60 return np.abs((array1 - array2) / array2.max()).max()
64 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
68 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
72 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
76 assert (centro.type ==
"cog")
80 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
84 assert (control.nactu - sup.config.p_controller0.nactu < precision)
88 assert (control.type ==
"generic")
92 assert (control.delay - sup.config.p_controller0.delay < precision)
96 assert (centro.npix - sup.config.p_wfs0.npix < precision)
112 assert (control.gain - sup.config.p_controller0.gain < precision)
128 centro.calibrate_img()
129 imgCal = (frame.astype(np.float32) - dark) * flat
134 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
135 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
136 offset = centro.offset
138 for k
in range(sup.config.p_wfs0._nvalid):
139 tmp = center_of_mass(bincube[:, :, k])
140 slopes[k] = (tmp[0] - offset) * scale
141 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
147 slopes = ng.array(control.d_centroids).toarray()
149 cmat = ng.array(control.d_cmat).toarray()
150 commands = cmat.dot(slopes) * gain * (-1)
156 control.set_comRange(-1, 1)
157 assert (control.comRange == (-1, 1))
161 control.set_comRange(-1, 1)
162 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
163 control.set_com(C, C.size)
166 C_clipped[np.where(C > 1)] = 1
167 C_clipped[np.where(C < -1)] = -1
173 C = np.random.random(sup.config.p_controller0.nactu)
174 control.add_perturb_voltage(
"test", C, 1)
176 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
180 control.remove_perturb_voltage(
"test")
181 assert (control.d_perturb_map == {})
185 C = np.random.random(sup.config.p_controller0.nactu)
186 control.add_perturb_voltage(
"test", C, 1)
187 com = ng.array(control.d_com_clipped).toarray()
188 control.add_perturb()
194 control.disable_perturb_voltage(
"test")
195 com = ng.array(control.d_com).toarray()
196 control.add_perturb()
201 control.enable_perturb_voltage(
"test")
202 com = ng.array(control.d_com_clipped).toarray()
203 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
204 control.add_perturb()
210 control.reset_perturb_voltage()
211 assert (control.d_perturb_map == {})
217 control.set_comRange(volt_min, volt_max)
218 control.comp_voltage()
219 C = np.random.random(sup.config.p_controller0.nactu)
220 control.add_perturb_voltage(
"test", C, 1)
221 control.set_com(C, C.size)
222 com0 = ng.array(control.d_circularComs0).toarray()
223 com1 = ng.array(control.d_circularComs1).toarray()
224 control.comp_voltage()
225 delay = sup.config.p_controller0.delay
226 a = delay - int(delay)
228 commands = a * com0 + b * com1
229 comPertu = commands + C
230 comPertu[np.where(comPertu > volt_max)] = volt_max
231 comPertu[np.where(comPertu < volt_min)] = volt_min
232 val_max = control.val_max
233 commands = np.uint16((comPertu - volt_min) / (volt_max - volt_min) * val_max)
239 rtc.remove_centroider(0)
240 assert (rtc.d_centro == [])
244 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
245 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
248 centro = rtc.d_centro[-1]
250 centro.set_threshold(threshold)
251 centro.set_npix(sup.config.p_wfs0.npix)
252 centro.load_validpos(xvalid, yvalid, xvalid.size)
253 centro.load_img(frame, frame.shape[0])
254 centro.calibrate_img()
256 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
257 offset = centro.offset
259 vx = sup.config.p_wfs0._validsubsx
260 vy = sup.config.p_wfs0._validsubsy
261 npix = sup.config.p_wfs0.npix
262 for k
in range(sup.config.p_wfs0._nvalid):
263 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
264 np.float32) - threshold
265 imagette[np.where(imagette < 0)] = 0
266 tmp = center_of_mass(imagette)
267 slopes[k] = (tmp[0] - offset) * scale
268 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
274 rtc.remove_centroider(0)
275 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
276 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
279 centro = rtc.d_centro[-1]
282 centro.set_npix(sup.config.p_wfs0.npix)
283 centro.load_validpos(xvalid, yvalid, xvalid.size)
284 centro.load_img(frame, frame.shape[0])
285 centro.calibrate_img()
287 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
288 bincube /= bincube.max()
289 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
290 offset = centro.offset
292 vx = sup.config.p_wfs0._validsubsx
293 vy = sup.config.p_wfs0._validsubsy
294 npix = sup.config.p_wfs0.npix
295 for k
in range(sup.config.p_wfs0._nvalid):
296 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
297 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
298 imagette -= threshold
299 imagette[np.where(imagette < 0)] = 0
300 tmp = center_of_mass(imagette)
301 slopes[k] = (tmp[0] - offset) * scale
302 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale