8 from scipy.ndimage.measurements
import center_of_mass
9 from Octopus
import CacaoInterface
14 config = load_config_from_file(os.getenv(
"COMPASS_ROOT") +
15 "/shesha/tests/pytest/par/test_sh.py")
16 sup = Supervisor(config)
21 rtc = Rtc(
"compass_calPix",
"compass_loopData")
22 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
23 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
25 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
26 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
27 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
29 centro = rtc.d_centro[0]
30 control = rtc.d_control[0]
31 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
32 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
33 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
34 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
35 cmat = sup.rtc.get_command_matrix(0)
36 rtc.d_control[0].set_cmat(cmat)
37 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
38 frame = sup.wfs.get_wfs_image(0)
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 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
250 bincube /= bincube.max()
251 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
252 offset = centro.offset
254 bincube = bincube - threshold
255 bincube[np.where(bincube < 0)] = 0
256 for k
in range(sup.config.p_wfs0._nvalid):
257 tmp = center_of_mass(bincube[:, :, k])
258 slopes[k] = (tmp[0] - offset) * scale
259 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
265 rtc.remove_centroider(0)
266 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
267 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
270 centro = rtc.d_centro[-1]
273 centro.set_npix(sup.config.p_wfs0.npix)
274 centro.load_validpos(xvalid, yvalid, xvalid.size)
275 centro.load_img(frame, frame.shape[0])
276 centro.calibrate_img()
278 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
279 bincube /= bincube.max()
280 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
281 offset = centro.offset
283 for k
in range(sup.config.p_wfs0._nvalid):
284 imagette = bincube[:, :, k]
285 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
286 imagette -= threshold
287 imagette[np.where(imagette < 0)] = 0
288 tmp = center_of_mass(imagette)
289 slopes[k] = (tmp[0] - offset) * scale
290 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
296 centro = rtc.d_centro[-1]
298 interface = CacaoInterface.getInterface(
"compass_calPix")
299 tmp = np.array(interface)
303 @pytest.mark.skip(reason=
"intensities disabled")
305 centro = rtc.d_centro[-1]
307 it = CacaoInterface.getInterface(
"compass_loopData")
309 nvalid = sup.config.p_wfs0._nvalid
310 nslopes = sup.config.p_controller0.nslope
311 nactus = sup.config.p_controller0.nactu
312 tmp = np.zeros(nvalid + nslopes + nactus)
313 tmp[:nvalid] = np.array(centro.d_intensities)
314 tmp[nvalid:nvalid + nslopes] = np.array(control.d_centroids)
315 tmp[nvalid + nslopes:] = np.array(control.d_com)