COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
test_rtcUHU.py
1 import numpy as np
2 import naga as ng
3 import os
4 from shesha.sutra_wrap import Rtc_UFU 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 config.p_controller0.set_delay(0.0)
14 config.p_dms[0].unitpervolt = 500
15 config.p_dms[0].push4imat = 0.5
16 config.p_dms[1].unitpervolt = 500
17 config.p_dms[1].push4imat = 0.5
18 sup = Supervisor(config)
19 sup.wfs.wfs.d_wfs[0].set_fakecam(True)
20 sup.wfs.wfs.d_wfs[0].set_max_flux_per_pix(int(sup.config.p_wfs0._nphotons // 2))
21 sup.wfs.wfs.d_wfs[0].set_max_pix_value(2**16 - 1)
22 sup.next()
23 sup.rtc.open_loop(0)
24 sup.rtc.close_loop(0)
25 sup.rtc.do_control(0)
26 rtc = Rtc()
27 rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
28  sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize, False, 0,
29  "cog")
30 rtc.add_controller(sup.context, sup.config.p_wfs0._nvalid,
31  sup.config.p_wfs0._nvalid * 2, sup.config.p_controller0.nactu,
32  sup.config.p_controller0.delay, 0, "generic", idx_centro=np.zeros(1),
33  ncentro=1)
34 centro = rtc.d_centro[0]
35 control = rtc.d_control[0]
36 rtc.d_centro[0].set_npix(sup.config.p_wfs0.npix)
37 xvalid = np.array(sup.rtc.rtc.d_centro[0].d_validx)
38 yvalid = np.array(sup.rtc.rtc.d_centro[0].d_validy)
39 rtc.d_centro[0].load_validpos(xvalid, yvalid, xvalid.size)
40 cmat = sup.rtc.get_command_matrix(0)
41 rtc.d_control[0].set_cmat(cmat)
42 rtc.d_control[0].set_gain(sup.config.p_controller0.gain)
43 frame = np.array(sup.wfs.wfs.d_wfs[0].d_camimg)
44 rtc.d_centro[0].load_img(frame, frame.shape[0])
45 rtc.d_centro[0].calibrate_img()
46 
47 rtc.do_centroids(0)
48 slp = ng.array(rtc.d_control[0].d_centroids)
49 rtc.do_control(0)
50 com = ng.array(rtc.d_control[0].d_com)
51 
52 dark = np.random.random(frame.shape)
53 flat = np.random.random(frame.shape)
54 centro.set_dark(dark, frame.shape[0])
55 centro.set_flat(flat, frame.shape[0])
56 
57 
58 def relative_array_error(array1, array2):
59  return np.abs((array1 - array2) / array2.max()).max()
60 
61 
63  assert (centro.nvalid - sup.config.p_wfs0._nvalid < precision)
64 
65 
67  assert (centro.offset - (sup.config.p_wfs0.npix / 2 - 0.5) < precision)
68 
69 
71  assert (centro.scale - sup.config.p_wfs0.pixsize < precision)
72 
73 
75  assert (centro.type == "cog")
76 
77 
79  assert (control.nslope - sup.config.p_wfs0._nvalid * 2 < precision)
80 
81 
83  assert (control.nactu - sup.config.p_controller0.nactu < precision)
84 
85 
87  assert (control.type == "generic")
88 
89 
91  assert (control.delay - sup.config.p_controller0.delay < precision)
92 
93 
95  assert (centro.npix - sup.config.p_wfs0.npix < precision)
96 
97 
99  assert (relative_array_error(np.array(centro.d_validx), xvalid) < precision)
100 
101 
103  assert (relative_array_error(np.array(centro.d_validy), yvalid) < precision)
104 
105 
107  assert (relative_array_error(ng.array(control.d_cmat).toarray(), cmat) < precision)
108 
109 
111  assert (control.gain - sup.config.p_controller0.gain < precision)
112 
113 
115  assert (relative_array_error(np.array(centro.d_img_raw), frame) < precision)
116 
117 
119  assert (relative_array_error(ng.array(centro.d_dark).toarray(), dark) < precision)
120 
121 
123  assert (relative_array_error(ng.array(centro.d_flat).toarray(), flat) < precision)
124 
125 
127  centro.calibrate_img()
128  imgCal = (frame.astype(np.float32) - dark) * flat
129  assert (relative_array_error(ng.array(centro.d_img).toarray(), imgCal) < precision)
130 
131 
133  bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
134  slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
135  offset = centro.offset
136  scale = centro.scale
137  for k in range(sup.config.p_wfs0._nvalid):
138  tmp = center_of_mass(bincube[:, :, k])
139  slopes[k] = (tmp[0] - offset) * scale
140  slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
141  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
142  precision)
143 
144 
146  slopes = ng.array(control.d_centroids).toarray()
147  gain = control.gain
148  cmat = ng.array(control.d_cmat).toarray()
149  commands = cmat.dot(slopes) * gain * (-1)
150  assert (relative_array_error(ng.array(control.d_com).toarray(), commands) <
151  precision)
152 
153 
155  control.set_comRange(-1, 1)
156  assert (control.comRange == (-1, 1))
157 
158 
160  control.set_comRange(-1, 1)
161  C = (np.random.random(sup.config.p_controller0.nactu) - 0.5) * 4
162  control.set_com(C, C.size)
163  rtc.do_clipping(0)
164  C_clipped = C.copy()
165  C_clipped[np.where(C > 1)] = 1
166  C_clipped[np.where(C < -1)] = -1
167  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), C_clipped) <
168  precision)
169 
170 
172  C = np.random.random(sup.config.p_controller0.nactu)
173  control.add_perturb_voltage("test", C, 1)
174  assert (relative_array_error(
175  ng.array(control.d_perturb_map["test"][0]).toarray(), C) < precision)
176 
177 
179  control.remove_perturb_voltage("test")
180  assert (control.d_perturb_map == {})
181 
182 
184  C = np.random.random(sup.config.p_controller0.nactu)
185  control.add_perturb_voltage("test", C, 1)
186  com = ng.array(control.d_com_clipped).toarray()
187  control.add_perturb()
188  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), com + C) <
189  precision)
190 
191 
193  control.disable_perturb_voltage("test")
194  com = ng.array(control.d_com).toarray()
195  control.add_perturb()
196  assert (relative_array_error(ng.array(control.d_com).toarray(), com) < precision)
197 
198 
200  control.enable_perturb_voltage("test")
201  com = ng.array(control.d_com_clipped).toarray()
202  C = ng.array(control.d_perturb_map["test"][0]).toarray()
203  control.add_perturb()
204  assert (relative_array_error(ng.array(control.d_com_clipped).toarray(), com + C) <
205  precision)
206 
207 
209  control.reset_perturb_voltage()
210  assert (control.d_perturb_map == {})
211 
212 
214  volt_min = -1
215  volt_max = 1
216  control.set_comRange(volt_min, volt_max)
217  control.comp_voltage()
218  C = np.random.random(sup.config.p_controller0.nactu)
219  control.add_perturb_voltage("test", C, 1)
220  control.set_com(C, C.size)
221  com0 = ng.array(control.d_circularComs0).toarray()
222  com1 = ng.array(control.d_circularComs1).toarray()
223  control.comp_voltage()
224  delay = sup.config.p_controller0.delay
225  a = delay - int(delay)
226  b = 1 - a
227  commands = a * com0 + b * com1
228  comPertu = commands + C
229  comPertu[np.where(comPertu > volt_max)] = volt_max
230  comPertu[np.where(comPertu < volt_min)] = volt_min
231  val_max = control.val_max
232  commands = np.uint16((comPertu - volt_min) / (volt_max - volt_min) * val_max)
233  assert (relative_array_error(ng.array(control.d_voltage).toarray(), commands) <
234  precision)
235 
236 
238  rtc.remove_centroider(0)
239  assert (rtc.d_centro == [])
240 
241 
243  rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
244  sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
245  False, 0, "tcog")
246 
247  centro = rtc.d_centro[-1]
248  threshold = 500
249  centro.set_threshold(threshold)
250  centro.set_npix(sup.config.p_wfs0.npix)
251  centro.load_validpos(xvalid, yvalid, xvalid.size)
252  centro.load_img(frame, frame.shape[0])
253  centro.calibrate_img()
254  rtc.do_centroids(0)
255  slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
256  offset = centro.offset
257  scale = centro.scale
258  vx = sup.config.p_wfs0._validsubsx
259  vy = sup.config.p_wfs0._validsubsy
260  npix = sup.config.p_wfs0.npix
261  for k in range(sup.config.p_wfs0._nvalid):
262  imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(
263  np.float32) - threshold
264  imagette[np.where(imagette < 0)] = 0
265  tmp = center_of_mass(imagette)
266  slopes[k] = (tmp[0] - offset) * scale
267  slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
268  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
269  precision)
270 
271 
273  rtc.remove_centroider(0)
274  rtc.add_centroider(sup.context, sup.config.p_wfs0._nvalid,
275  sup.config.p_wfs0.npix / 2 - 0.5, sup.config.p_wfs0.pixsize,
276  False, 0, "bpcog")
277 
278  centro = rtc.d_centro[-1]
279  bpix = 8
280  centro.set_nmax(8)
281  centro.set_npix(sup.config.p_wfs0.npix)
282  centro.load_validpos(xvalid, yvalid, xvalid.size)
283  centro.load_img(frame, frame.shape[0])
284  centro.calibrate_img()
285  rtc.do_centroids(0)
286  bincube = np.array(sup.wfs.wfs.d_wfs[0].d_bincube)
287  bincube /= bincube.max()
288  slopes = np.zeros(sup.config.p_wfs0._nvalid * 2)
289  offset = centro.offset
290  scale = centro.scale
291  vx = sup.config.p_wfs0._validsubsx
292  vy = sup.config.p_wfs0._validsubsy
293  npix = sup.config.p_wfs0.npix
294  for k in range(sup.config.p_wfs0._nvalid):
295  imagette = frame[vx[k]:vx[k] + npix, vy[k]:vy[k] + npix].astype(np.float32)
296  threshold = np.sort(imagette, axis=None)[-(bpix + 1)]
297  imagette -= threshold
298  imagette[np.where(imagette < 0)] = 0
299  tmp = center_of_mass(imagette)
300  slopes[k] = (tmp[0] - offset) * scale
301  slopes[k + sup.config.p_wfs0._nvalid] = (tmp[1] - offset) * scale
302  assert (relative_array_error(ng.array(control.d_centroids).toarray(), slopes) <
303  precision)
shesha.supervisor.compassSupervisor
Initialization and execution of a COMPASS supervisor.
Definition: compassSupervisor.py:1
test_rtcUHU.test_remove_perturb_voltage
def test_remove_perturb_voltage()
Definition: test_rtcUHU.py:178
test_rtcUHU.test_remove_centroider
def test_remove_centroider()
Definition: test_rtcUHU.py:237
test_rtcUHU.test_disable_perturb_voltage
def test_disable_perturb_voltage()
Definition: test_rtcUHU.py:192
test_rtcUHU.test_set_flat
def test_set_flat()
Definition: test_rtcUHU.py:122
test_rtcUHU.test_add_perturb_voltage
def test_add_perturb_voltage()
Definition: test_rtcUHU.py:171
test_rtcUHU.test_clipping
def test_clipping()
Definition: test_rtcUHU.py:159
test_rtcUHU.test_set_comRange
def test_set_comRange()
Definition: test_rtcUHU.py:154
shesha.sutra_wrap
Definition: sutra_wrap.py:1
test_rtcUHU.test_load_img
def test_load_img()
Definition: test_rtcUHU.py:114
test_rtcUHU.test_set_gain
def test_set_gain()
Definition: test_rtcUHU.py:110
test_rtcUHU.test_initCentro_scale
def test_initCentro_scale()
Definition: test_rtcUHU.py:70
test_rtcUHU.test_load_validposY
def test_load_validposY()
Definition: test_rtcUHU.py:102
shesha.util.utilities
Basic utilities function.
Definition: utilities.py:1
test_rtcUHU.test_do_control_generic
def test_do_control_generic()
Definition: test_rtcUHU.py:145
test_rtcUHU.test_set_cmat
def test_set_cmat()
Definition: test_rtcUHU.py:106
test_rtcUHU.test_initControl_type
def test_initControl_type()
Definition: test_rtcUHU.py:86
test_rtcUHU.test_initControl_nslope
def test_initControl_nslope()
Definition: test_rtcUHU.py:78
test_rtcUHU.test_calibrate_img
def test_calibrate_img()
Definition: test_rtcUHU.py:126
test_rtcUHU.test_set_dark
def test_set_dark()
Definition: test_rtcUHU.py:118
test_rtcUHU.test_load_validposX
def test_load_validposX()
Definition: test_rtcUHU.py:98
test_rtcUHU.test_initControl_nactu
def test_initControl_nactu()
Definition: test_rtcUHU.py:82
test_rtcUHU.test_doCentroids_bpcog
def test_doCentroids_bpcog()
Definition: test_rtcUHU.py:272
test_rtcUHU.test_enable_perturb_voltage
def test_enable_perturb_voltage()
Definition: test_rtcUHU.py:199
test_rtcUHU.relative_array_error
def relative_array_error(array1, array2)
Definition: test_rtcUHU.py:58
test_rtcUHU.test_initControl_delay
def test_initControl_delay()
Definition: test_rtcUHU.py:90
test_rtcUHU.test_add_perturb
def test_add_perturb()
Definition: test_rtcUHU.py:183
test_rtcUHU.test_comp_voltage
def test_comp_voltage()
Definition: test_rtcUHU.py:213
test_rtcUHU.test_reset_perturb_voltage
def test_reset_perturb_voltage()
Definition: test_rtcUHU.py:208
test_rtcUHU.test_initCentro_nvalid
def test_initCentro_nvalid()
Definition: test_rtcUHU.py:62
test_rtcUHU.test_initCentro_type
def test_initCentro_type()
Definition: test_rtcUHU.py:74
test_rtcUHU.test_set_npix
def test_set_npix()
Definition: test_rtcUHU.py:94
test_rtcUHU.test_doCentroids_cog
def test_doCentroids_cog()
Definition: test_rtcUHU.py:132
test_rtcUHU.test_doCentroids_tcog
def test_doCentroids_tcog()
Definition: test_rtcUHU.py:242
test_rtcUHU.test_initCentro_offset
def test_initCentro_offset()
Definition: test_rtcUHU.py:66