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_dms[0].push4imat = 0.5
14 config.p_dms[1].unitpervolt = 500
15 config.p_dms[1].push4imat = 0.5
16 config.p_dms[0].unitpervolt = 500
17 sup = Supervisor(config)
18 sup.wfs.wfs.d_wfs[0].set_fakecam(
True)
19 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
20 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
26 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
27 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
29 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
30 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
31 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
33 centro = rtc.d_centro[0]
34 control = rtc.d_control[0]
35 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
36 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
37 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
38 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
39 cmat = sup.rtc.get_command_matrix(0)
40 rtc.d_control[0].set_cmat(cmat)
41 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
42 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
43 rtc.d_centro[0].load_img(frame, frame.shape[0])
44 rtc.d_centro[0].calibrate_img()
47 slp = ng.array(rtc.d_control[0].d_centroids)
49 com = ng.array(rtc.d_control[0].d_com)
51 dark = np.random.random(frame.shape)
52 flat = np.random.random(frame.shape)
53 centro.set_dark(dark, frame.shape[0])
54 centro.set_flat(flat, frame.shape[0])
58 return np.abs((array1 - array2) / array2.max()).max()
62 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
66 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
70 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
74 assert (centro.type ==
"cog")
78 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
82 assert (control.nactu - sup.config.p_controller0.nactu < precision)
86 assert (control.type ==
"generic")
90 assert (control.delay - sup.config.p_controller0.delay < precision)
94 assert (centro.npix - sup.config.p_wfs0.npix < precision)
110 assert (control.gain - sup.config.p_controller0.gain < precision)
126 centro.calibrate_img()
127 imgCal = (frame.astype(np.float32) - dark) * flat
132 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
133 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
134 offset = centro.offset
136 for k
in range(sup.config.p_wfs0._nvalid):
137 tmp = center_of_mass(bincube[:, :, k])
138 slopes[k] = (tmp[0] - offset) * scale
139 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
145 slopes = ng.array(control.d_centroids).toarray()
147 cmat = ng.array(control.d_cmat).toarray()
148 commands = cmat.dot(slopes) * gain * (-1)
154 control.set_comRange(-1, 1)
155 assert (control.comRange == (-1, 1))
159 control.set_comRange(-1, 1)
160 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
161 control.set_com(C, C.size)
164 C_clipped[np.where(C > 1)] = 1
165 C_clipped[np.where(C < -1)] = -1
171 C = np.random.random(sup.config.p_controller0.nactu)
172 control.add_perturb_voltage(
"test", C, 1)
174 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
178 control.remove_perturb_voltage(
"test")
179 assert (control.d_perturb_map == {})
183 C = np.random.random(sup.config.p_controller0.nactu)
184 control.add_perturb_voltage(
"test", C, 1)
185 com = ng.array(control.d_com_clipped).toarray()
186 control.add_perturb()
192 control.disable_perturb_voltage(
"test")
193 com = ng.array(control.d_com).toarray()
194 control.add_perturb()
199 control.enable_perturb_voltage(
"test")
200 com = ng.array(control.d_com_clipped).toarray()
201 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
202 control.add_perturb()
208 control.reset_perturb_voltage()
209 assert (control.d_perturb_map == {})
215 control.set_comRange(volt_min, volt_max)
216 control.comp_voltage()
217 C = np.random.random(sup.config.p_controller0.nactu)
218 control.add_perturb_voltage(
"test", C, 1)
219 control.set_com(C, C.size)
220 com0 = ng.array(control.d_circularComs0).toarray()
221 com1 = ng.array(control.d_circularComs1).toarray()
222 control.comp_voltage()
223 delay = sup.config.p_controller0.delay
224 a = delay - int(delay)
226 commands = a * com0 + b * com1
227 comPertu = commands + C
228 comPertu[np.where(comPertu > volt_max)] = volt_max
229 comPertu[np.where(comPertu < volt_min)] = volt_min
230 val_max = control.val_max
231 commands = np.uint16((comPertu - volt_min) / (volt_max - volt_min) * val_max)
237 rtc.remove_centroider(0)
238 assert (rtc.d_centro == [])
242 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
243 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
246 centro = rtc.d_centro[-1]
248 centro.set_threshold(threshold)
249 centro.set_npix(sup.config.p_wfs0.npix)
250 centro.load_validpos(xvalid, yvalid, xvalid.size)
251 centro.load_img(frame, frame.shape[0])
252 centro.calibrate_img()
254 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
255 offset = centro.offset
257 vx = sup.config.p_wfs0._validsubsx
258 vy = sup.config.p_wfs0._validsubsy
259 npix = sup.config.p_wfs0.npix
260 for k
in range(sup.config.p_wfs0._nvalid):
261 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
262 np.float32) - threshold
263 imagette[np.where(imagette < 0)] = 0
264 tmp = center_of_mass(imagette)
265 slopes[k] = (tmp[0] - offset) * scale
266 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
272 rtc.remove_centroider(0)
273 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
274 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
277 centro = rtc.d_centro[-1]
280 centro.set_npix(sup.config.p_wfs0.npix)
281 centro.load_validpos(xvalid, yvalid, xvalid.size)
282 centro.load_img(frame, frame.shape[0])
283 centro.calibrate_img()
285 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
286 bincube /= bincube.max()
287 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
288 offset = centro.offset
290 vx = sup.config.p_wfs0._validsubsx
291 vy = sup.config.p_wfs0._validsubsy
292 npix = sup.config.p_wfs0.npix
293 for k
in range(sup.config.p_wfs0._nvalid):
294 imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
295 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
296 imagette -= threshold
297 imagette[np.where(imagette < 0)] = 0
298 tmp = center_of_mass(imagette)
299 slopes[k] = (tmp[0] - offset) * scale
300 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale