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 sup = Supervisor(config)
15 sup.wfs.wfs.d_wfs[0].set_fakecam(
True)
16 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
17 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
23 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
24 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
26 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
27 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
28 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
30 centro = rtc.d_centro[0]
31 control = rtc.d_control[0]
32 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
33 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
34 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
35 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
36 cmat = sup.rtc.get_command_matrix(0)
37 rtc.d_control[0].set_cmat(cmat)
38 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
39 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
40 rtc.d_centro[0].load_img(frame, frame.shape[0])
41 rtc.d_centro[0].calibrate_img()
44 slp = ng.array(rtc.d_control[0].d_centroids)
46 com = ng.array(rtc.d_control[0].d_com)
48 dark = np.random.random(frame.shape)
49 flat = np.random.random(frame.shape)
50 centro.set_dark(dark, frame.shape[0])
51 centro.set_flat(flat, frame.shape[0])
55 return np.abs((array1 - array2) / array2.max()).max()
59 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
63 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
67 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
71 assert (centro.type ==
"cog")
75 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
79 assert (control.nactu - sup.config.p_controller0.nactu < precision)
83 assert (control.type ==
"generic")
87 assert (control.delay - sup.config.p_controller0.delay < precision)
91 assert (centro.npix - sup.config.p_wfs0.npix < precision)
107 assert (control.gain - sup.config.p_controller0.gain < precision)
123 centro.calibrate_img()
124 imgCal = (frame - dark) * flat
129 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
130 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
131 offset = centro.offset
133 for k
in range(sup.config.p_wfs0._nvalid):
134 tmp = center_of_mass(bincube[:, :, k])
135 slopes[k] = (tmp[0] - offset) * scale
136 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
142 slopes = ng.array(control.d_centroids).toarray()
144 cmat = ng.array(control.d_cmat).toarray()
145 commands = cmat.dot(slopes) * gain * (-1)
151 control.set_comRange(-1, 1)
152 assert (control.comRange == (-1, 1))
156 control.set_comRange(-1, 1)
157 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
158 control.set_com(C, C.size)
161 C_clipped[np.where(C > 1)] = 1
162 C_clipped[np.where(C < -1)] = -1
168 C = np.random.random(sup.config.p_controller0.nactu)
169 control.add_perturb_voltage(
"test", C, 1)
171 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
175 control.remove_perturb_voltage(
"test")
176 assert (control.d_perturb_map == {})
180 C = np.random.random(sup.config.p_controller0.nactu)
181 control.add_perturb_voltage(
"test", C, 1)
182 com = ng.array(control.d_com_clipped).toarray()
183 control.add_perturb()
189 control.disable_perturb_voltage(
"test")
190 com = ng.array(control.d_com).toarray()
191 control.add_perturb()
196 control.enable_perturb_voltage(
"test")
197 com = ng.array(control.d_com_clipped).toarray()
198 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
199 control.add_perturb()
205 control.reset_perturb_voltage()
206 assert (control.d_perturb_map == {})
212 control.set_comRange(volt_min, volt_max)
213 control.comp_voltage()
214 C = np.random.random(sup.config.p_controller0.nactu)
215 control.add_perturb_voltage(
"test", C, 1)
216 control.set_com(C, C.size)
217 com0 = ng.array(control.d_circularComs0).toarray()
218 com1 = ng.array(control.d_circularComs1).toarray()
219 control.comp_voltage()
220 delay = sup.config.p_controller0.delay
221 a = delay - int(delay)
223 commands = a * com0 + b * com1
224 comPertu = commands + C
225 comPertu[np.where(comPertu > volt_max)] = volt_max
226 comPertu[np.where(comPertu < volt_min)] = volt_min
232 rtc.remove_centroider(0)
233 assert (rtc.d_centro == [])
237 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
238 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
241 centro = rtc.d_centro[-1]
243 centro.set_threshold(threshold)
244 centro.set_npix(sup.config.p_wfs0.npix)
245 centro.load_validpos(xvalid, yvalid, xvalid.size)
246 centro.load_img(frame, frame.shape[0])
247 centro.calibrate_img()
249 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
250 offset = centro.offset
252 vx = sup.config.p_wfs0._validsubsx
253 vy = sup.config.p_wfs0._validsubsy
254 npix = sup.config.p_wfs0.npix
255 for k
in range(sup.config.p_wfs0._nvalid):
256 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
257 np.float32) - threshold
258 imagette[np.where(imagette < 0)] = 0
259 tmp = center_of_mass(imagette)
260 slopes[k] = (tmp[0] - offset) * scale
261 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
267 rtc.remove_centroider(0)
268 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
269 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
272 centro = rtc.d_centro[-1]
275 centro.set_npix(sup.config.p_wfs0.npix)
276 centro.load_validpos(xvalid, yvalid, xvalid.size)
277 centro.load_img(frame, frame.shape[0])
278 centro.calibrate_img()
280 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
281 bincube /= bincube.max()
282 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
283 offset = centro.offset
285 vx = sup.config.p_wfs0._validsubsx
286 vy = sup.config.p_wfs0._validsubsy
287 npix = sup.config.p_wfs0.npix
288 for k
in range(sup.config.p_wfs0._nvalid):
289 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
290 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
291 imagette -= threshold
292 imagette[np.where(imagette < 0)] = 0
293 tmp = center_of_mass(imagette)
294 slopes[k] = (tmp[0] - offset) * scale
295 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale