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)
19 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
20 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
False, 0,
22 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
23 sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
24 sup.config.p_controller0.delay, 0,
"generic", idx_centro=np.zeros(1),
26 centro = rtc.d_centro[0]
27 control = rtc.d_control[0]
28 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
29 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
30 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
31 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
32 cmat = sup.rtc.get_command_matrix(0)
33 rtc.d_control[0].set_cmat(cmat)
34 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
35 frame = sup.wfs.get_wfs_image(0)
37 rtc.d_centro[0].load_img(frame, frame.shape[0])
38 rtc.d_centro[0].calibrate_img()
41 slp = ng.array(rtc.d_control[0].d_centroids)
43 com = ng.array(rtc.d_control[0].d_com)
45 dark = np.random.random(frame.shape)
46 flat = np.random.random(frame.shape)
47 centro.set_dark(dark, frame.shape[0])
48 centro.set_flat(flat, frame.shape[0])
52 return np.abs((array1 - array2) / array2.max()).max()
56 assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
60 assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
64 assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
68 assert (centro.type ==
"cog")
72 assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
76 assert (control.nactu - sup.config.p_controller0.nactu < precision)
80 assert (control.type ==
"generic")
84 assert (control.delay - sup.config.p_controller0.delay < precision)
88 assert (centro.npix - sup.config.p_wfs0.npix < precision)
104 assert (control.gain - sup.config.p_controller0.gain < precision)
120 centro.calibrate_img()
121 imgCal = (frame - dark) * flat
126 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
127 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
128 offset = centro.offset
130 for k
in range(sup.config.p_wfs0._nvalid):
131 tmp = center_of_mass(bincube[:, :, k])
132 slopes[k] = (tmp[0] - offset) * scale
133 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
138 slopes = np.array(control.d_centroids)
140 cmat = np.array(control.d_cmat)
141 commands = cmat.dot(slopes) * gain * (-1)
146 control.set_comRange(-1, 1)
147 assert (control.comRange == (-1, 1))
151 control.set_comRange(-1, 1)
152 C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
153 control.set_com(C, C.size)
156 C_clipped[np.where(C > 1)] = 1
157 C_clipped[np.where(C < -1)] = -1
163 C = np.random.random(sup.config.p_controller0.nactu)
164 control.add_perturb_voltage(
"test", C, 1)
166 ng.array(control.d_perturb_map[
"test"][0]).toarray(), C) < precision)
170 control.remove_perturb_voltage(
"test")
171 assert (control.d_perturb_map == {})
175 C = np.random.random(sup.config.p_controller0.nactu)
176 control.add_perturb_voltage(
"test", C, 1)
177 com = ng.array(control.d_com_clipped).toarray()
178 control.add_perturb()
184 control.disable_perturb_voltage(
"test")
185 com = np.array(control.d_com)
186 control.add_perturb()
191 control.enable_perturb_voltage(
"test")
192 com = ng.array(control.d_com_clipped).toarray()
193 C = ng.array(control.d_perturb_map[
"test"][0]).toarray()
194 control.add_perturb()
200 control.reset_perturb_voltage()
201 assert (control.d_perturb_map == {})
207 control.set_comRange(volt_min, volt_max)
208 control.comp_voltage()
209 C = np.random.random(sup.config.p_controller0.nactu)
210 control.add_perturb_voltage(
"test", C, 1)
211 control.set_com(C, C.size)
212 com0 = ng.array(control.d_circularComs0).toarray()
213 com1 = ng.array(control.d_circularComs1).toarray()
214 control.comp_voltage()
215 delay = sup.config.p_controller0.delay
216 a = delay - int(delay)
218 commands = a * com0 + b * com1
219 comPertu = commands + C
220 comPertu[np.where(comPertu > volt_max)] = volt_max
221 comPertu[np.where(comPertu < volt_min)] = volt_min
227 rtc.remove_centroider(0)
228 assert (rtc.d_centro == [])
232 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
233 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
236 centro = rtc.d_centro[-1]
238 centro.set_threshold(threshold)
239 centro.set_npix(sup.config.p_wfs0.npix)
240 centro.load_validpos(xvalid, yvalid, xvalid.size)
241 centro.load_img(frame, frame.shape[0])
242 centro.calibrate_img()
244 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
245 bincube /= bincube.max()
246 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
247 offset = centro.offset
249 bincube = bincube - threshold
250 bincube[np.where(bincube < 0)] = 0
251 for k
in range(sup.config.p_wfs0._nvalid):
252 tmp = center_of_mass(bincube[:, :, k])
253 slopes[k] = (tmp[0] - offset) * scale
254 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
259 rtc.remove_centroider(0)
260 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
261 sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
264 centro = rtc.d_centro[-1]
267 centro.set_npix(sup.config.p_wfs0.npix)
268 centro.load_validpos(xvalid, yvalid, xvalid.size)
269 centro.load_img(frame, frame.shape[0])
270 centro.calibrate_img()
272 bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
273 bincube /= bincube.max()
274 slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
275 offset = centro.offset
277 for k
in range(sup.config.p_wfs0._nvalid):
278 imagette = bincube[:, :, k]
279 threshold = np.sort(imagette, axis=
None)[-(bpix + 1)]
280 imagette -= threshold
281 imagette[np.where(imagette < 0)] = 0
282 tmp = center_of_mass(imagette)
283 slopes[k] = (tmp[0] - offset) * scale
284 slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale