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