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