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