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