COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
test_rtcUFF.py
1 import numpy as np
2 import naga as ng
3 import os
4 from shesha.sutra_wrap import Rtc_UFF as Rtc
5 from shesha.supervisor.compassSupervisor import CompassSupervisor as Supervisor
6 from scipy.ndimage.measurements import center_of_mass
7 from shesha.util.utilities import load_config_from_file
8 
9 precision = 1e-2
10 
11 config = load_config_from_file(os.getenv("COMPASS_ROOT") +
12  "/shesha/tests/pytest/par/test_sh.py")
13 sup = Supervisor(config)
14 
15 sup.wfs.wfs.d_wfs[0].set_fakecam(True)
16 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
17 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
18 sup.next()
19 sup.rtc.open_loop(0)
20 sup.rtc.close_loop(0)
21 sup.rtc.do_control(0)
22 rtc = Rtc()
23 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
24  sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize, False, 0,
25  "cog")
26 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
27  sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
28  sup.config.p_controller0.delay, 0, "generic", idx_centro=np.zeros(1),
29  ncentro=1)
30 centro = rtc.d_centro[0]
31 control = rtc.d_control[0]
32 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
33 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
34 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
35 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
36 cmat = sup.rtc.get_command_matrix(0)
37 rtc.d_control[0].set_cmat(cmat)
38 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
39 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
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 = 5000
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  slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
250  offset = centro.offset
251  scale = centro.scale
252  vx = sup.config.p_wfs0._validsubsx
253  vy = sup.config.p_wfs0._validsubsy
254  npix = sup.config.p_wfs0.npix
255  for k in range(sup.config.p_wfs0._nvalid):
256  imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
257  np.float32) - threshold
258  imagette[np.where(imagette < 0)] = 0
259  tmp = center_of_mass(imagette)
260  slopes[k] = (tmp[0] - offset) * scale
261  slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
262  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
263  precision)
264 
265 
267  rtc.remove_centroider(0)
268  rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
269  sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
270  False, 0, "bpcog")
271 
272  centro = rtc.d_centro[-1]
273  bpix = 8
274  centro.set_nmax(8)
275  centro.set_npix(sup.config.p_wfs0.npix)
276  centro.load_validpos(xvalid, yvalid, xvalid.size)
277  centro.load_img(frame, frame.shape[0])
278  centro.calibrate_img()
279  rtc.do_centroids(0)
280  bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
281  bincube /= bincube.max()
282  slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
283  offset = centro.offset
284  scale = centro.scale
285  vx = sup.config.p_wfs0._validsubsx
286  vy = sup.config.p_wfs0._validsubsy
287  npix = sup.config.p_wfs0.npix
288  for k in range(sup.config.p_wfs0._nvalid):
289  imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
290  threshold = np.sort(imagette, axis=None)[-(bpix + 1)]
291  imagette -= threshold
292  imagette[np.where(imagette < 0)] = 0
293  tmp = center_of_mass(imagette)
294  slopes[k] = (tmp[0] - offset) * scale
295  slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
296  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
297  precision)
shesha.supervisor.compassSupervisor
Initialization and execution of a COMPASS supervisor.
Definition: compassSupervisor.py:1
test_rtcUFF.test_calibrate_img
def test_calibrate_img()
Definition: test_rtcUFF.py:122
test_rtcUFF.test_add_perturb
def test_add_perturb()
Definition: test_rtcUFF.py:179
test_rtcUFF.test_set_dark
def test_set_dark()
Definition: test_rtcUFF.py:114
test_rtcUFF.test_comp_voltage
def test_comp_voltage()
Definition: test_rtcUFF.py:209
test_rtcUFF.test_do_control_generic
def test_do_control_generic()
Definition: test_rtcUFF.py:141
test_rtcUFF.test_set_npix
def test_set_npix()
Definition: test_rtcUFF.py:90
test_rtcUFF.test_doCentroids_cog
def test_doCentroids_cog()
Definition: test_rtcUFF.py:128
test_rtcUFF.test_enable_perturb_voltage
def test_enable_perturb_voltage()
Definition: test_rtcUFF.py:195
test_rtcUFF.test_set_cmat
def test_set_cmat()
Definition: test_rtcUFF.py:102
shesha.sutra_wrap
Definition: sutra_wrap.py:1
test_rtcUFF.test_set_flat
def test_set_flat()
Definition: test_rtcUFF.py:118
test_rtcUFF.test_load_validposY
def test_load_validposY()
Definition: test_rtcUFF.py:98
test_rtcUFF.test_clipping
def test_clipping()
Definition: test_rtcUFF.py:155
shesha.util.utilities
Basic utilities function.
Definition: utilities.py:1
test_rtcUFF.test_initControl_nactu
def test_initControl_nactu()
Definition: test_rtcUFF.py:78
test_rtcUFF.test_initControl_delay
def test_initControl_delay()
Definition: test_rtcUFF.py:86
test_rtcUFF.test_initCentro_type
def test_initCentro_type()
Definition: test_rtcUFF.py:70
test_rtcUFF.test_reset_perturb_voltage
def test_reset_perturb_voltage()
Definition: test_rtcUFF.py:204
test_rtcUFF.test_set_gain
def test_set_gain()
Definition: test_rtcUFF.py:106
test_rtcUFF.relative_array_error
def relative_array_error(array1, array2)
Definition: test_rtcUFF.py:54
test_rtcUFF.test_set_comRange
def test_set_comRange()
Definition: test_rtcUFF.py:150
test_rtcUFF.test_initControl_nslope
def test_initControl_nslope()
Definition: test_rtcUFF.py:74
test_rtcUFF.test_remove_centroider
def test_remove_centroider()
Definition: test_rtcUFF.py:231
test_rtcUFF.test_doCentroids_bpcog
def test_doCentroids_bpcog()
Definition: test_rtcUFF.py:266
test_rtcUFF.test_add_perturb_voltage
def test_add_perturb_voltage()
Definition: test_rtcUFF.py:167
test_rtcUFF.test_doCentroids_tcog
def test_doCentroids_tcog()
Definition: test_rtcUFF.py:236
test_rtcUFF.test_initCentro_scale
def test_initCentro_scale()
Definition: test_rtcUFF.py:66
test_rtcUFF.test_initCentro_nvalid
def test_initCentro_nvalid()
Definition: test_rtcUFF.py:58
test_rtcUFF.test_initControl_type
def test_initControl_type()
Definition: test_rtcUFF.py:82
test_rtcUFF.test_initCentro_offset
def test_initCentro_offset()
Definition: test_rtcUFF.py:62
test_rtcUFF.test_remove_perturb_voltage
def test_remove_perturb_voltage()
Definition: test_rtcUFF.py:174
test_rtcUFF.test_load_validposX
def test_load_validposX()
Definition: test_rtcUFF.py:94
test_rtcUFF.test_disable_perturb_voltage
def test_disable_perturb_voltage()
Definition: test_rtcUFF.py:188
test_rtcUFF.test_load_img
def test_load_img()
Definition: test_rtcUFF.py:110