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 sup = Supervisor(config)
16 sup.wfs.wfs.d_wfs[0].set_fakecam(
True)
17 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
18 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
24 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
25 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
27 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
28 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
29 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
31 centro = rtc.d_centro[0]
32 control = rtc.d_control[0]
33 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
34 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
35 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
36 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
37 cmat = sup.rtc.get_command_matrix(0)
38 rtc.d_control[0].set_cmat(cmat)
39 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
40 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
41 rtc.d_centro[0].load_img(frame, frame.shape[0])
42 rtc.d_centro[0].calibrate_img()
45 slp = ng.array(rtc.d_control[0].d_centroids)
47 com = ng.array(rtc.d_control[0].d_com)
49 dark = np.random.random(frame.shape)
50 flat = np.random.random(frame.shape)
51 centro.set_dark(dark, frame.shape[0])
52 centro.set_flat(flat, frame.shape[0])
56 return np.abs((array1 - array2) / array2.max()).max()
60 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
64 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
68 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
72 assert (centro.type ==
"cog")
76 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
80 assert (control.nactu - sup.config.p_controller0.nactu < precision)
84 assert (control.type ==
"generic")
88 assert (control.delay - sup.config.p_controller0.delay < precision)
92 assert (centro.npix - sup.config.p_wfs0.npix < precision)
108 assert (control.gain - sup.config.p_controller0.gain < precision)
124 centro.calibrate_img()
125 imgCal = (frame.astype(np.float32) - dark) * flat
130 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
131 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
132 offset = centro.offset
134 for k
in range(sup.config.p_wfs0._nvalid):
135 tmp = center_of_mass(bincube[:, :, k])
136 slopes[k] = (tmp[0] - offset) * scale
137 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
143 slopes = ng.array(control.d_centroids).toarray()
145 cmat = ng.array(control.d_cmat).toarray()
146 commands = cmat.dot(slopes) * gain * (-1)
152 control.set_comRange(-1, 1)
153 assert (control.comRange == (-1, 1))
157 control.set_comRange(-1, 1)
158 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
159 control.set_com(C, C.size)
162 C_clipped[np.where(C > 1)] = 1
163 C_clipped[np.where(C < -1)] = -1
169 C = np.random.random(sup.config.p_controller0.nactu)
170 control.add_perturb_voltage(
"test", C, 1)
172 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
176 control.remove_perturb_voltage(
"test")
177 assert (control.d_perturb_map == {})
181 C = np.random.random(sup.config.p_controller0.nactu)
182 control.add_perturb_voltage(
"test", C, 1)
183 com = ng.array(control.d_com_clipped).toarray()
184 control.add_perturb()
190 control.disable_perturb_voltage(
"test")
191 com = ng.array(control.d_com).toarray()
192 control.add_perturb()
197 control.enable_perturb_voltage(
"test")
198 com = ng.array(control.d_com_clipped).toarray()
199 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
200 control.add_perturb()
206 control.reset_perturb_voltage()
207 assert (control.d_perturb_map == {})
213 control.set_comRange(volt_min, volt_max)
214 control.comp_voltage()
215 C = np.random.random(sup.config.p_controller0.nactu)
216 control.add_perturb_voltage(
"test", C, 1)
217 control.set_com(C, C.size)
218 com0 = ng.array(control.d_circularComs0).toarray()
219 com1 = ng.array(control.d_circularComs1).toarray()
220 control.comp_voltage()
221 delay = sup.config.p_controller0.delay
222 a = delay - int(delay)
224 commands = a * com0 + b * com1
225 comPertu = commands + C
226 comPertu[np.where(comPertu > volt_max)] = volt_max
227 comPertu[np.where(comPertu < volt_min)] = volt_min
233 rtc.remove_centroider(0)
234 assert (rtc.d_centro == [])
238 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
239 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
242 centro = rtc.d_centro[-1]
244 centro.set_threshold(threshold)
245 centro.set_npix(sup.config.p_wfs0.npix)
246 centro.load_validpos(xvalid, yvalid, xvalid.size)
247 centro.load_img(frame, frame.shape[0])
248 centro.calibrate_img()
250 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
251 offset = centro.offset
253 vx = sup.config.p_wfs0._validsubsx
254 vy = sup.config.p_wfs0._validsubsy
255 npix = sup.config.p_wfs0.npix
256 for k
in range(sup.config.p_wfs0._nvalid):
257 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
258 np.float32) - threshold
259 imagette[np.where(imagette < 0)] = 0
260 tmp = center_of_mass(imagette)
261 slopes[k] = (tmp[0] - offset) * scale
262 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
268 rtc.remove_centroider(0)
269 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
270 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
273 centro = rtc.d_centro[-1]
276 centro.set_npix(sup.config.p_wfs0.npix)
277 centro.load_validpos(xvalid, yvalid, xvalid.size)
278 centro.load_img(frame, frame.shape[0])
279 centro.calibrate_img()
281 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
282 bincube /= bincube.max()
283 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
284 offset = centro.offset
286 vx = sup.config.p_wfs0._validsubsx
287 vy = sup.config.p_wfs0._validsubsy
288 npix = sup.config.p_wfs0.npix
289 for k
in range(sup.config.p_wfs0._nvalid):
290 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
291 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
292 imagette -= threshold
293 imagette[np.where(imagette < 0)] = 0
294 tmp = center_of_mass(imagette)
295 slopes[k] = (tmp[0] - offset) * scale
296 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale