COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
test_rtc_cacao.py
1 import numpy as np
2 import naga as ng
3 import os, time
4 import pytest
5 
6 from shesha.sutra_wrap import Rtc_cacao_FFF as Rtc
7 from shesha.supervisor.compassSupervisor import CompassSupervisor as Supervisor
8 from scipy.ndimage.measurements import center_of_mass
9 from Octopus import CacaoInterface
10 from shesha.util.utilities import load_config_from_file
11 
12 precision = 1e-5
13 
14 config = load_config_from_file(os.getenv("COMPASS_ROOT") +
15  "/shesha/tests/pytest/par/test_sh.py")
16 sup = Supervisor(config)
17 sup.next()
18 sup.rtc.open_loop(0)
19 sup.rtc.close_loop(0)
20 sup.rtc.do_control(0)
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,
24  "cog")
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),
28  ncentro=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)
39 frame /= frame.max()
40 rtc.d_centro[0].load_img(frame, frame.shape[0])
41 rtc.d_centro[0].calibrate_img()
42 
43 rtc.do_centroids(0)
44 slp = ng.array(rtc.d_control[0].d_centroids)
45 rtc.do_control(0)
46 com = ng.array(rtc.d_control[0].d_com)
47 
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])
52 
53 
54 def relative_array_error(array1, array2):
55  return np.abs((array1 - array2) / array2.max()).max()
56 
57 
59  assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
60 
61 
63  assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
64 
65 
67  assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
68 
69 
71  assert (centro.type == "cog")
72 
73 
75  assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
76 
77 
79  assert (control.nactu - sup.config.p_controller0.nactu < precision)
80 
81 
83  assert (control.type == "generic")
84 
85 
87  assert (control.delay - sup.config.p_controller0.delay < precision)
88 
89 
91  assert (centro.npix - sup.config.p_wfs0.npix < precision)
92 
93 
95  assert (relative_array_error(np.array(centro.d_validx), xvalid) < precision)
96 
97 
99  assert (relative_array_error(np.array(centro.d_validy), yvalid) < precision)
100 
101 
103  assert (relative_array_error(ng.array(control.d_cmat).toarray(), cmat) < precision)
104 
105 
107  assert (control.gain - sup.config.p_controller0.gain < precision)
108 
109 
111  assert (relative_array_error(np.array(centro.d_img_raw), frame) < precision)
112 
113 
115  assert (relative_array_error(ng.array(centro.d_dark).toarray(), dark) < precision)
116 
117 
119  assert (relative_array_error(ng.array(centro.d_flat).toarray(), flat) < precision)
120 
121 
123  centro.calibrate_img()
124  imgCal = (frame - dark) * flat
125  assert (relative_array_error(ng.array(centro.d_img).toarray(), imgCal) < precision)
126 
127 
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
132  scale = centro.scale
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
137  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
138  precision)
139 
140 
142  slopes = ng.array(control.d_centroids).toarray()
143  gain = control.gain
144  cmat = ng.array(control.d_cmat).toarray()
145  commands = cmat.dot(slopes) * gain * (-1)
146  assert (relative_array_error(ng.array(control.d_com).toarray(), commands) <
147  precision)
148 
149 
151  control.set_comRange(-1, 1)
152  assert (control.comRange == (-1, 1))
153 
154 
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)
159  rtc.do_clipping(0)
160  C_clipped = C.copy()
161  C_clipped[np.where(C > 1)] = 1
162  C_clipped[np.where(C < -1)] = -1
163  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), C_clipped) <
164  precision)
165 
166 
168  C = np.random.random(sup.config.p_controller0.nactu)
169  control.add_perturb_voltage("test", C, 1)
170  assert (relative_array_error(
171  ng.array(control.d_perturb_map["test"][0]).toarray(), C) < precision)
172 
173 
175  control.remove_perturb_voltage("test")
176  assert (control.d_perturb_map == {})
177 
178 
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()
184  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), com + C) <
185  precision)
186 
187 
189  control.disable_perturb_voltage("test")
190  com = ng.array(control.d_com).toarray()
191  control.add_perturb()
192  assert (relative_array_error(ng.array(control.d_com).toarray(), com) < precision)
193 
194 
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()
200  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), com + C) <
201  precision)
202 
203 
205  control.reset_perturb_voltage()
206  assert (control.d_perturb_map == {})
207 
208 
210  volt_min = -1
211  volt_max = 1
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)
222  b = 1 - a
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
227  assert (relative_array_error(ng.array(control.d_voltage).toarray(), comPertu) <
228  precision)
229 
230 
232  rtc.remove_centroider(0)
233  assert (rtc.d_centro == [])
234 
235 
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,
239  False, 0, "tcog")
240 
241  centro = rtc.d_centro[-1]
242  threshold = 0.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()
248  rtc.do_centroids(0)
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
253  scale = centro.scale
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
260  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
261  precision)
262 
263 
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,
268  False, 0, "bpcog")
269 
270  centro = rtc.d_centro[-1]
271  bpix = 8
272  centro.set_nmax(8)
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()
277  rtc.do_centroids(0)
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
282  scale = centro.scale
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
291  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
292  precision)
293 
294 
296  centro = rtc.d_centro[-1]
297  rtc.publish()
298  interface = CacaoInterface.getInterface("compass_calPix")
299  tmp = np.array(interface)
300  assert (relative_array_error(np.array(centro.d_img), tmp) < precision)
301 
302 
303 @pytest.mark.skip(reason="intensities disabled")
305  centro = rtc.d_centro[-1]
306  rtc.publish()
307  it = CacaoInterface.getInterface("compass_loopData")
308  data = np.array(it)
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)
316  assert (relative_array_error(data, tmp) < precision)
test_rtc_cacao.test_set_npix
def test_set_npix()
Definition: test_rtc_cacao.py:90
shesha.supervisor.compassSupervisor
Initialization and execution of a COMPASS supervisor.
Definition: compassSupervisor.py:1
test_rtc_cacao.test_load_validposX
def test_load_validposX()
Definition: test_rtc_cacao.py:94
test_rtc_cacao.test_doCentroids_cog
def test_doCentroids_cog()
Definition: test_rtc_cacao.py:128
test_rtc_cacao.test_add_perturb
def test_add_perturb()
Definition: test_rtc_cacao.py:179
test_rtc_cacao.test_doCentroids_tcog
def test_doCentroids_tcog()
Definition: test_rtc_cacao.py:236
test_rtc_cacao.test_set_comRange
def test_set_comRange()
Definition: test_rtc_cacao.py:150
shesha.sutra_wrap
Definition: sutra_wrap.py:1
test_rtc_cacao.test_set_flat
def test_set_flat()
Definition: test_rtc_cacao.py:118
test_rtc_cacao.test_remove_centroider
def test_remove_centroider()
Definition: test_rtc_cacao.py:231
shesha.util.utilities
Basic utilities function.
Definition: utilities.py:1
test_rtc_cacao.test_initCentro_nvalid
def test_initCentro_nvalid()
Definition: test_rtc_cacao.py:58
test_rtc_cacao.test_set_gain
def test_set_gain()
Definition: test_rtc_cacao.py:106
test_rtc_cacao.test_initCentro_offset
def test_initCentro_offset()
Definition: test_rtc_cacao.py:62
test_rtc_cacao.test_comp_voltage
def test_comp_voltage()
Definition: test_rtc_cacao.py:209
test_rtc_cacao.test_initCentro_type
def test_initCentro_type()
Definition: test_rtc_cacao.py:70
test_rtc_cacao.test_clipping
def test_clipping()
Definition: test_rtc_cacao.py:155
test_rtc_cacao.test_calibrate_img
def test_calibrate_img()
Definition: test_rtc_cacao.py:122
test_rtc_cacao.test_remove_perturb_voltage
def test_remove_perturb_voltage()
Definition: test_rtc_cacao.py:174
test_rtc_cacao.test_do_control_generic
def test_do_control_generic()
Definition: test_rtc_cacao.py:141
test_rtc_cacao.test_set_cmat
def test_set_cmat()
Definition: test_rtc_cacao.py:102
test_rtc_cacao.test_initControl_nslope
def test_initControl_nslope()
Definition: test_rtc_cacao.py:74
test_rtc_cacao.test_load_img
def test_load_img()
Definition: test_rtc_cacao.py:110
test_rtc_cacao.test_initCentro_scale
def test_initCentro_scale()
Definition: test_rtc_cacao.py:66
test_rtc_cacao.test_publish_calPix
def test_publish_calPix()
Definition: test_rtc_cacao.py:295
test_rtc_cacao.test_doCentroids_bpcog
def test_doCentroids_bpcog()
Definition: test_rtc_cacao.py:264
test_rtc_cacao.test_disable_perturb_voltage
def test_disable_perturb_voltage()
Definition: test_rtc_cacao.py:188
test_rtc_cacao.test_initControl_delay
def test_initControl_delay()
Definition: test_rtc_cacao.py:86
test_rtc_cacao.test_add_perturb_voltage
def test_add_perturb_voltage()
Definition: test_rtc_cacao.py:167
test_rtc_cacao.test_publish_loopData
def test_publish_loopData()
Definition: test_rtc_cacao.py:304
test_rtc_cacao.test_set_dark
def test_set_dark()
Definition: test_rtc_cacao.py:114
test_rtc_cacao.test_load_validposY
def test_load_validposY()
Definition: test_rtc_cacao.py:98
test_rtc_cacao.test_reset_perturb_voltage
def test_reset_perturb_voltage()
Definition: test_rtc_cacao.py:204
test_rtc_cacao.test_initControl_type
def test_initControl_type()
Definition: test_rtc_cacao.py:82
test_rtc_cacao.relative_array_error
def relative_array_error(array1, array2)
Definition: test_rtc_cacao.py:54
test_rtc_cacao.test_enable_perturb_voltage
def test_enable_perturb_voltage()
Definition: test_rtc_cacao.py:195
test_rtc_cacao.test_initControl_nactu
def test_initControl_nactu()
Definition: test_rtc_cacao.py:78