COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
widget_groot.py
1 import numpy as np
2 from glob import glob
3 import os
4 import datetime
5 
6 import h5py
7 import matplotlib as mpl
8 
9 from bokeh.plotting import figure
10 from bokeh.models import ColumnDataSource, Range1d
11 from bokeh.models.widgets import Panel, Toggle, TextInput, Slider, CheckboxButtonGroup, DataTable, TableColumn, Tabs, Button, RadioButtonGroup, Select, DataTable, DateFormatter, TableColumn, PreText
12 from bokeh.layouts import layout, widgetbox
13 from bokeh.io import curdoc, output_file, show
14 
15 from guardians import gamora, groot, drax
16 
17 
19 
20  def __init__(self):
21 
22  self.dataroot = os.getenv("DATA_GUARDIAN")
23  self.datapath = self.dataroot
24  self.files = [f.split('/')[-1] for f in glob(self.datapath + "roket_*.h5")]
25  if self.files == []:
26  self.files = ["No hdf5 files"]
27  self.f = None
28  self.Btt = None
29  self.P = None
30  self.nactus = None
31  self.nmodes = None
32  self.nslopes = None
33 
34  self.url = "http://" + os.uname()[1] + ".obspm.fr/~" + os.getlogin(
35  ) + "/roket_display"
36  self.old = None
37  self.psf_compass = None
38  self.psf_roket = None
39  self.psf_groot = None
40  self.covmat_groot = None
41  self.covmat_roket = None
42 
43  # Widgets Elements
44  self.pretext = PreText(text=""" """, width=500, height=75)
45  self.SRcompass = TextInput(value=" ", title="SR compass:")
46  self.SRroket = TextInput(value=" ", title="SR roket:")
47  self.SRgroot = TextInput(value=" ", title="SR groot:")
48 
49  self.button_covmat = Button(label="Covmat", button_type="success")
50  self.button_psf = Button(label="PSF !", button_type="success")
51  self.toggle_fit = Toggle(label="Fitting", button_type="primary")
52 
53  self.select_datapath = Select(
54  title="Datapath", value=self.dataroot,
55  options=[self.dataroot] + glob(self.dataroot + "*/"))
56  self.select_files = Select(title="File", value=self.files[0], options=self.files)
57 
58  self.contributors = ["noise", "bandwidth & tomography", "aliasing"]
59  self.checkboxButtonGroup_contributors = CheckboxButtonGroup(
60  labels=self.contributors, active=[])
61  self.radioButton_basis = RadioButtonGroup(labels=["Actus", "Btt", "Slopes"],
62  active=0)
63 
64  self.xdr = Range1d(start=0, end=1024)
65  self.ydr = Range1d(start=1024, end=0)
66  self.xdr2 = Range1d(start=0, end=1024)
67  self.ydr2 = Range1d(start=1024, end=0)
68  self.image_roket = figure(x_range=self.xdr, y_range=self.ydr,
69  x_axis_location="above", title="PSF ROKET")
70  self.image_groot = figure(x_range=self.image_roket.x_range,
71  y_range=self.image_roket.y_range,
72  x_axis_location="above", title="PSF GROOT")
73  self.im_covmat_roket = figure(x_range=self.xdr2, y_range=self.ydr2,
74  x_axis_location="above", title="Covmat ROKET")
75  self.im_covmat_groot = figure(x_range=self.im_covmat_roket.x_range,
76  y_range=self.im_covmat_roket.y_range,
77  x_axis_location="above", title="Covmat GROOT")
78  self.plot_psf_cuts = figure(plot_height=600, plot_width=800, y_range=[1e-9, 1],
79  x_range=self.image_roket.x_range, y_axis_type="log")
80  self.source_psf_roket = ColumnDataSource(data=dict(x=[], y=[]))
81  self.source_psf_groot = ColumnDataSource(data=dict(x=[], y=[]))
82  self.source_psf_compass = ColumnDataSource(data=dict(x=[], y=[]))
83  self.source_covmat_roket = ColumnDataSource(data=dict(x=[], y=[]))
84  self.source_covmat_groot = ColumnDataSource(data=dict(x=[], y=[]))
85 
86  self.image_roket.image_url(url=[], x=0, y=0, w=1024, h=1024)
87  self.image_groot.image_url(url=[], x=0, y=0, w=1024, h=1024)
88  self.im_covmat_roket.image_url(url=[], x=0, y=0, w=1024, h=1024)
89  self.im_covmat_groot.image_url(url=[], x=0, y=0, w=1024, h=1024)
90  self.plot_psf_cuts.line(x="x", y="y", legend="ROKET", color="blue",
91  muted_alpha=0.1, source=self.source_psf_roket)
92  self.plot_psf_cuts.line(x="x", y="y", legend="COMPASS", color="red",
93  muted_alpha=0.1, source=self.source_psf_compass)
94  self.plot_psf_cuts.line(x="x", y="y", legend="GROOT", color="green",
95  muted_alpha=0.1, source=self.source_psf_groot)
96  self.plot_psf_cuts.legend.click_policy = "mute"
97 
98  # Callback functions
99  self.select_datapath.on_change(
100  "value", lambda attr, old, new: self.update_files())
101  self.select_files.on_change("value", lambda attr, old, new: self.update())
102  self.button_psf.on_click(self.comp_psf)
103  self.button_covmat.on_click(self.comp_covmats)
104  self.update()
105 
106  #layouts
107  self.control_box = widgetbox(self.select_datapath, self.select_files,
109  self.radioButton_basis, self.button_covmat,
110  self.button_psf, self.toggle_fit, self.SRcompass,
111  self.SRroket, self.SRgroot, self.pretext)
112  self.tab = Panel(
113  child=layout([[
115  ], [self.image_roket, self.image_groot], [self.plot_psf_cuts]]),
116  title="GROOT")
117 
118  def update(self):
119  """
120  Update the attributes based on the new selected filename
121  """
122  if os.path.exists(self.datapath + str(self.select_files.value)):
123  self.f = h5py.File(self.datapath + str(self.select_files.value), mode='r+')
124  self.psf_compass = self.f["psf"][:]
125  self.SRcompass.value = "%.2f" % (self.psf_compass.max())
126  self.psf_groot = None
127  self.psf_roket = None
128  self.covmat_groot = None
129  self.covmat_roket = None
130  self.Btt = self.f["Btt"][:]
131  self.P = self.f["P"][:]
132  self.nactus = self.P.shape[1]
133  self.nmodes = self.P.shape[0]
134  self.nslopes = self.f["R"][:].shape[1]
135 
136  def update_files(self):
137  """
138  Update the select_files options following the current datapath
139  """
140  self.datapath = str(self.select_datapath.value)
141  self.files = self.files = [
142  f.split('/')[-1] for f in glob(self.datapath + "roket_*.h5")
143  ]
144  if self.files == []:
145  self.files = ["No hdf5 files"]
146  self.select_files.options = self.files
147  self.select_files.value = self.files[0]
148 
149  def update_psf(self):
150  """
151  Update the PSF display
152  """
153  if self.psf_roket is not None:
154  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
155  self.old = "/home/" + os.getlogin(
156  ) + "/public_html/roket_display" + time + ".png"
157  mpl.image.imsave(self.old, np.log10(np.abs(self.psf_roket)))
158  self.image_roket.image_url(
159  url=dict(value=self.url + time + ".png"), x=0, y=0,
160  w=self.psf_roket.shape[0], h=self.psf_roket.shape[0])
161  self.image_roket.x_range.update(start=0, end=self.psf_roket.shape[0])
162  self.image_roket.y_range.update(start=self.psf_roket.shape[0], end=0)
163  self.SRroket.value = "%.2f" % (self.psf_roket.max())
164 
165  if self.psf_groot is not None:
166  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
167  self.old = "/home/" + os.getlogin(
168  ) + "/public_html/roket_display" + time + ".png"
169  mpl.image.imsave(self.old, np.log10(np.abs(self.psf_groot)))
170  self.image_groot.image_url(
171  url=dict(value=self.url + time + ".png"), x=0, y=0,
172  w=self.psf_groot.shape[0], h=self.psf_groot.shape[0])
173  self.SRgroot.value = "%.2f" % (self.psf_groot.max())
174 
175  self.update_cuts()
176 
177  def update_covmats(self):
178  """
179  Update the covmats
180  """
181  if self.covmat_roket is not None:
182  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
183  self.old = "/home/" + os.getlogin(
184  ) + "/public_html/roket_display" + time + ".png"
185  mpl.image.imsave(self.old, self.covmat_roket)
186  self.im_covmat_roket.image_url(
187  url=dict(value=self.url + time + ".png"), x=0, y=0,
188  w=self.covmat_roket.shape[0], h=self.covmat_roket.shape[0])
189  self.im_covmat_roket.x_range.update(start=0, end=self.covmat_roket.shape[0])
190  self.im_covmat_roket.y_range.update(start=self.covmat_roket.shape[0], end=0)
191 
192  if self.covmat_groot is not None:
193  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
194  self.old = "/home/" + os.getlogin(
195  ) + "/public_html/roket_display" + time + ".png"
196  mpl.image.imsave(self.old, self.covmat_groot)
197  self.im_covmat_groot.image_url(
198  url=dict(value=self.url + time + ".png"), x=0, y=0,
199  w=self.covmat_groot.shape[0], h=self.covmat_groot.shape[0])
200 
201  def update_cuts(self):
202  """
203  Update the PSF cuts
204  """
205  if self.psf_roket is not None:
206  x = np.arange(self.psf_roket.shape[0])
207  self.source_psf_roket.data = dict(
208  x=x, y=self.psf_roket[:, self.psf_roket.shape[0] // 2])
209  if self.psf_groot is not None:
210  x = np.arange(self.psf_groot.shape[0])
211  self.source_psf_groot.data = dict(
212  x=x, y=self.psf_groot[:, self.psf_groot.shape[0] // 2])
213  if self.psf_compass is not None:
214  x = np.arange(self.psf_compass.shape[0])
215  self.source_psf_compass.data = dict(
216  x=x, y=self.psf_compass[:, self.psf_compass.shape[0] // 2])
217 
218  def comp_covmats(self):
219  """
220  Compute the covmats using GROOT model and display it
221  """
222  self.pretext.text = """ Computing covmats... Please wait"""
223  self.button_covmat.button_type = "danger"
224  contrib = [
225  self.contributors[c]
226  for c in self.checkboxButtonGroup_contributors.active
227  ]
228  if contrib == []:
229  contrib = self.contributors
230  if "bandwidth & tomography" in contrib:
231  contrib.remove("bandwidth & tomography")
232  contrib.append("bandwidth")
233  contrib.append("tomography")
234  modal = self.radioButton_basis.active
235  if modal == 1:
236  self.covmat_groot = np.zeros((self.nmodes, self.nmodes))
237  elif modal == 2:
238  self.covmat_groot = np.zeros((self.nslopes, self.nslopes))
239  else:
240  self.covmat_groot = np.zeros((self.nactus, self.nactus))
241 
242  if modal != 2:
243  if "noise" in contrib:
244  self.covmat_groot += groot.compute_Cn_cpu(
245  self.datapath + str(self.select_files.value), modal=modal)
246  if "aliasing" in contrib:
247  self.covmat_groot += groot.compute_Ca_cpu(
248  self.datapath + str(self.select_files.value), modal=modal)
249  if "tomography" in contrib or "bandwidth" in contrib:
250  self.covmat_groot += groot.compute_Cerr(
251  self.datapath + str(self.select_files.value), modal=modal)
252 
253  err = drax.get_err_contributors(self.datapath + str(self.select_files.value),
254  contrib)
255  self.covmat_roket = err.dot(err.T) / err.shape[1]
256  if modal:
257  self.covmat_roket = self.P.dot(self.covmat_roket).dot(self.P.T)
258  else:
259  if "aliasing" in contrib:
260  self.covmat_groot, self.covmat_roket = groot.compute_Calias(
261  self.datapath + str(self.select_files.value))
262 
263  self.update_covmats()
264 
265  self.pretext.text = """ """
266  self.button_covmat.button_type = "success"
267 
268  def comp_psf(self):
269  """
270  Compute the PSF from the covmats
271  """
272  self.pretext.text = """ Computing PSFs... Please wait"""
273  self.button_psf.button_type = "danger"
274 
275  fit = self.toggle_fit.active
276  if self.covmat_groot.shape[0] != self.nmodes:
277  self.covmat_groot = self.P.dot(self.covmat_groot).dot(self.P.T)
278  self.covmat_roket = self.P.dot(self.covmat_roket).dot(self.P.T)
279 
280  otftel, otf2, self.psf_groot, _ = gamora.psf_rec_Vii(
281  self.datapath + str(self.select_files.value),
282  cov=self.covmat_groot.astype(np.float32), fitting=False)
283  if fit:
284  otffit, _ = groot.compute_OTF_fitting(
285  self.datapath + str(self.select_files.value), otftel)
286  self.psf_groot = gamora.add_fitting_to_psf(
287  self.datapath + str(self.select_files.value), otf2 * otftel, otffit)
288 
289  _, _, self.psf_roket, _ = gamora.psf_rec_Vii(
290  self.datapath + str(self.select_files.value),
291  cov=self.covmat_roket.astype(np.float32), fitting=fit)
292 
293  self.update_psf()
294  self.pretext.text = """ """
295  self.button_psf.button_type = "success"
widget_groot.Bokeh_groot.dataroot
dataroot
Definition: widget_groot.py:22
widget_groot.Bokeh_groot.covmat_groot
covmat_groot
Definition: widget_groot.py:40
widget_groot.Bokeh_groot.im_covmat_roket
im_covmat_roket
Definition: widget_groot.py:73
widget_groot.Bokeh_groot.ydr
ydr
Definition: widget_groot.py:65
widget_groot.Bokeh_groot.comp_covmats
def comp_covmats(self)
Compute the covmats using GROOT model and display it.
Definition: widget_groot.py:221
widget_groot.Bokeh_groot.toggle_fit
toggle_fit
Definition: widget_groot.py:51
widget_groot.Bokeh_groot.control_box
control_box
Definition: widget_groot.py:107
widget_groot.Bokeh_groot.contributors
contributors
Definition: widget_groot.py:58
widget_groot.Bokeh_groot.nactus
nactus
Definition: widget_groot.py:30
widget_groot.Bokeh_groot.f
f
Definition: widget_groot.py:27
widget_groot.Bokeh_groot.source_psf_compass
source_psf_compass
Definition: widget_groot.py:82
widget_groot.Bokeh_groot.update_cuts
def update_cuts(self)
Update the PSF cuts.
Definition: widget_groot.py:204
widget_groot.Bokeh_groot.select_datapath
select_datapath
Definition: widget_groot.py:53
widget_groot.Bokeh_groot.update_psf
def update_psf(self)
Update the PSF display.
Definition: widget_groot.py:152
widget_groot.Bokeh_groot.SRroket
SRroket
Definition: widget_groot.py:46
widget_groot.Bokeh_groot.source_psf_groot
source_psf_groot
Definition: widget_groot.py:81
widget_groot.Bokeh_groot
Definition: widget_groot.py:18
widget_groot.Bokeh_groot.plot_psf_cuts
plot_psf_cuts
Definition: widget_groot.py:78
widget_groot.Bokeh_groot.P
P
Definition: widget_groot.py:29
widget_groot.Bokeh_groot.Btt
Btt
Definition: widget_groot.py:28
widget_groot.Bokeh_groot.comp_psf
def comp_psf(self)
Compute the PSF from the covmats.
Definition: widget_groot.py:271
widget_groot.Bokeh_groot.psf_roket
psf_roket
Definition: widget_groot.py:38
widget_groot.Bokeh_groot.button_covmat
button_covmat
Definition: widget_groot.py:49
widget_groot.Bokeh_groot.select_files
select_files
Definition: widget_groot.py:56
widget_groot.Bokeh_groot.psf_compass
psf_compass
Definition: widget_groot.py:37
widget_groot.Bokeh_groot.update_covmats
def update_covmats(self)
Update the covmats.
Definition: widget_groot.py:180
widget_groot.Bokeh_groot.radioButton_basis
radioButton_basis
Definition: widget_groot.py:61
widget_groot.Bokeh_groot.files
files
Definition: widget_groot.py:24
widget_groot.Bokeh_groot.tab
tab
Definition: widget_groot.py:112
widget_groot.Bokeh_groot.source_psf_roket
source_psf_roket
Definition: widget_groot.py:80
widget_groot.Bokeh_groot.im_covmat_groot
im_covmat_groot
Definition: widget_groot.py:75
widget_groot.Bokeh_groot.__init__
def __init__(self)
Definition: widget_groot.py:20
widget_groot.Bokeh_groot.button_psf
button_psf
Definition: widget_groot.py:50
widget_groot.Bokeh_groot.nslopes
nslopes
Definition: widget_groot.py:32
widget_groot.Bokeh_groot.update
def update(self)
Update the attributes based on the new selected filename.
Definition: widget_groot.py:121
widget_groot.Bokeh_groot.image_roket
image_roket
Definition: widget_groot.py:68
widget_groot.Bokeh_groot.datapath
datapath
Definition: widget_groot.py:23
widget_groot.Bokeh_groot.old
old
Definition: widget_groot.py:36
widget_groot.Bokeh_groot.xdr
xdr
Definition: widget_groot.py:64
widget_groot.Bokeh_groot.nmodes
nmodes
Definition: widget_groot.py:31
widget_groot.Bokeh_groot.SRcompass
SRcompass
Definition: widget_groot.py:45
widget_groot.Bokeh_groot.url
url
Definition: widget_groot.py:34
widget_groot.Bokeh_groot.source_covmat_groot
source_covmat_groot
Definition: widget_groot.py:84
widget_groot.Bokeh_groot.pretext
pretext
Definition: widget_groot.py:44
widget_groot.Bokeh_groot.xdr2
xdr2
Definition: widget_groot.py:66
widget_groot.Bokeh_groot.source_covmat_roket
source_covmat_roket
Definition: widget_groot.py:83
widget_groot.Bokeh_groot.SRgroot
SRgroot
Definition: widget_groot.py:47
widget_groot.Bokeh_groot.update_files
def update_files(self)
Update the select_files options following the current datapath.
Definition: widget_groot.py:139
widget_groot.Bokeh_groot.checkboxButtonGroup_contributors
checkboxButtonGroup_contributors
Definition: widget_groot.py:59
widget_groot.Bokeh_groot.psf_groot
psf_groot
Definition: widget_groot.py:39
widget_groot.Bokeh_groot.ydr2
ydr2
Definition: widget_groot.py:67
widget_groot.Bokeh_groot.image_groot
image_groot
Definition: widget_groot.py:70
widget_groot.Bokeh_groot.covmat_roket
covmat_roket
Definition: widget_groot.py:41