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