COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
widget_roket.py
1 import numpy as np
2 from glob import glob
3 import os
4 
5 import h5py
6 
7 from bokeh.plotting import figure
8 from bokeh.models import ColumnDataSource
9 from bokeh.models.widgets import Panel, DataTable, TableColumn, Tabs, Button, RadioButtonGroup, Select, DataTable, DateFormatter, TableColumn, PreText
10 from bokeh.layouts import layout, widgetbox
11 from bokeh.io import curdoc, output_file, show
12 
13 from guardians import drax
14 
15 
16 class Bokeh_roket:
17  """
18  Class that defines a bokeh layout and callback functions for ROKET
19  Usage: see bokeh_roket.py which is the executable
20  """
21 
22  def __init__(self):
23 
24  self.dataroot = os.getenv("DATA_GUARDIAN")
25  self.datapath = self.dataroot
26  self.files = [f.split('/')[-1] for f in glob(self.datapath + "roket_*.h5")]
27  if self.files == []:
28  self.files = ["No hdf5 files"]
29  self.f = None
30  self.Btt = None
31  self.P = None
32  self.cov = None
33  self.cor = None
34  self.url = "http://hippo6.obspm.fr/~fferreira/roket_display"
35  self.old = None
36 
37  # Widgets Elements
38  self.pretext = PreText(text=""" """, width=500, height=75)
39  self.button_load = Button(label="Load", button_type="success")
40  self.select_datapath = Select(
41  title="Datapath", value=self.dataroot,
42  options=[self.dataroot] + glob(self.dataroot + "*/"))
43  self.select_files = Select(title="File", value=self.files[0], options=self.files)
44  self.radioButton_basis = RadioButtonGroup(labels=["Actuators", "Btt"], active=1)
45 
46  self.colors = {
47  "filtered modes": "green",
48  "bandwidth": "orange",
49  "noise": "red",
50  "tomography": "purple",
51  "non linearity": "cyan",
52  "aliasing": "blue"
53  }
54  self.contributors = [c for c in self.colors.keys()]
55  self.source_breakdown = ColumnDataSource(
56  data=dict(n=[], a=[], b=[], t=[], nl=[], f=[], fm=[]))
57  self.source_cov = ColumnDataSource(
58  data=dict(Type=[], Noise=[], Trunc=[], Aliasing=[], FilteredModes=[],
59  Bandwidth=[], Tomography=[]))
60  self.source_cor = ColumnDataSource(
61  data=dict(Type=[], Noise=[], Trunc=[], Aliasing=[], FilteredModes=[],
62  Bandwidth=[], Tomography=[]))
63  self.source_params = ColumnDataSource(data=dict(Parameter=[], Value=[]))
64  columns = [
65  TableColumn(field="n", title="noise"),
66  TableColumn(field="a", title="aliasing"),
67  TableColumn(field="b", title="bandwidth"),
68  TableColumn(field="t", title="tomography"),
69  TableColumn(field="nl", title="non lin."),
70  TableColumn(field="f", title="fitting"),
71  TableColumn(field="fm", title="filt. modes")
72  ]
73 
74  self.table_breakdown = DataTable(source=self.source_breakdown, columns=columns,
75  width=400, height=75)
76  #self.update_breakdown()
77 
78  columns2 = [
79  TableColumn(field="Type", title="Cov."),
80  TableColumn(field="Noise", title="Noise"),
81  TableColumn(field="Trunc", title="Non lin."),
82  TableColumn(field="Aliasing", title="Alias."),
83  TableColumn(field="FilteredModes", title="Filt."),
84  TableColumn(field="Bandwidth", title="Band."),
85  TableColumn(field="Tomography", title="Tomo"),
86  ]
87  self.table_cov = DataTable(source=self.source_cov, columns=columns2, width=400,
88  height=200)
89  columns2[0] = TableColumn(field="Type", title="Cor.")
90  self.table_cor = DataTable(source=self.source_cor, columns=columns2, width=400,
91  height=250)
92  #self.update_covcor()
93 
94  tmp = [
95  TableColumn(field="Parameter", title="Parameter"),
96  TableColumn(field="Value", title="Value")
97  ]
98  self.table_params = DataTable(source=self.source_params, columns=tmp, width=600,
99  height=500)
100  #self.update_params()
102  for c in self.contributors:
103  self.source_variances[c] = ColumnDataSource(data=dict(x=[], y=[]))
104  self.p = figure(plot_height=600, plot_width=800, y_axis_type="log",
105  y_range=[1e-9, 1], title="Contibutors variance")
106  for c in self.contributors:
107  self.p.line(x="x", y="y", legend=c, color=self.colors[c],
108  muted_color=self.colors[c], muted_alpha=0.1,
109  source=self.source_variances[c])
110 
111  self.p.legend.click_policy = "mute"
112 
113  # Callback functions
114  self.select_datapath.on_change(
115  "value", lambda attr, old, new: self.update_files())
116  self.button_load.on_click(self.load_file)
117  self.radioButton_basis.on_change("active", lambda attr, old, new: self.update())
118 
119  # Layouts
120  self.control_box = widgetbox(self.select_datapath, self.select_files,
121  self.button_load, self.radioButton_basis)
122  self.tab = Panel(
123  child=layout([[
124  self.control_box, self.p,
125  widgetbox(self.pretext, self.table_breakdown, self.table_cov,
126  self.table_cor)
127  ], [self.table_params]]), title="ROKET")
128 
129  def update_files(self):
130  """
131  Update the select_files options following the current datapath
132  """
133  self.datapath = str(self.select_datapath.value)
134  self.files = self.files = [
135  f.split('/')[-1] for f in glob(self.datapath + "roket_*.h5")
136  ]
137  if self.files == []:
138  self.files = ["No hdf5 files"]
139  self.select_files.options = self.files
140  self.select_files.value = self.files[0]
141 
142  def load_file(self):
143  """
144  Load the selected file and update the display
145  """
146  self.button_load.button_type = "danger"
147  self.f = h5py.File(self.datapath + str(self.select_files.value), mode='r+')
148  self.Btt = self.f["Btt"][:]
149  self.P = self.f["P"][:] #/np.sqrt(self.IF.shape[0])
150  self.cov = self.f["cov"][:]
151  self.cor = self.f["cor"][:]
152 
153  self.update()
154  self.update_breakdown()
155  self.update_covcor()
156  self.update_params()
157  self.button_load.button_type = "success"
158 
159  print("DB loaded")
160 
161  def update_breakdown(self):
162  """
163  Update the values of the error breakdown tables
164  """
165  self.pretext.text = """ Updating error breakdown... Please wait"""
166 
167  breakdown = drax.get_breakdown(self.datapath + str(self.select_files.value))
168  self.source_breakdown.data = dict(
169  n=[int(np.round(breakdown["noise"]))
170  ], a=[int(np.round(breakdown["aliasing"]))
171  ], b=[int(np.round(breakdown["bandwidth"]))
172  ], t=[int(np.round(breakdown["tomography"]))
173  ], nl=[int(np.round(breakdown["non linearity"]))
174  ], f=[int(np.round(breakdown["fitting"]))],
175  fm=[int(np.round(breakdown["filtered modes"]))])
176  self.pretext.text = """ """
177 
178  def update_covcor(self):
179  """
180  Update tables of covariances and correlations
181  """
182  self.pretext.text = """ Updating cov cor tables... Please wait"""
183 
184  self.source_cov.data = dict(
185  Type=["Noise", "Trunc", "Alias.", "Filt.", "Band.",
186  "Tomo"], Noise=["%.2E" % v for v in self.cov[:, 0]
187  ], Trunc=["%.2E" % v for v in self.cov[:, 1]],
188  Aliasing=["%.2E" % v for v in self.cov[:, 2]
189  ], FilteredModes=["%.2E" % v for v in self.cov[:, 3]],
190  Bandwidth=["%.2E" % v for v in self.cov[:, 4]
191  ], Tomography=["%.2E" % v for v in self.cov[:, 5]])
192  self.source_cor.data = dict(
193  Type=["Noise", "Trunc", "Alias.", "Filt.", "Band.",
194  "Tomo"], Noise=["%.2f" % v for v in self.cor[:, 0]
195  ], Trunc=["%.2f" % v for v in self.cor[:, 1]],
196  Aliasing=["%.2f" % v for v in self.cor[:, 2]
197  ], FilteredModes=["%.2f" % v for v in self.cor[:, 3]],
198  Bandwidth=["%.2f" % v for v in self.cor[:, 4]
199  ], Tomography=["%.2f" % v for v in self.cor[:, 5]])
200 
201  self.pretext.text = """ """
202 
203  def update_params(self):
204  """
205  Update the simulation parameters table
206  """
207  self.pretext.text = """ Updating parameters table... Please wait"""
208  params = list(self.f.attrs.keys())
209  params.sort()
210  values = []
211  for k in params:
212  values.append(str(self.f.attrs[k]))
213  self.source_params.data = dict(Parameter=params, Value=values)
214  self.pretext.text = """ """
215 
216  def update(self):
217  """
218  Main callback function that update the bokeh display
219  """
220  tmp = self.button_load.button_type
221  self.button_load.button_type = "danger"
222  self.pretext.text = """ Updating plot... Please wait"""
223 
224  basis_active = self.radioButton_basis.active
225  xi = []
226  yi = []
227  coloris = []
228 
229  for c in self.contributors:
230  self.source_variances[c].data = dict(x=[], y=[], color=[], legend=[])
231  data = self.f[c][:]
232  self.p.xaxis.axis_label = "Actuators"
233  if (basis_active):
234  data = self.P.dot(data)
235  self.p.xaxis.axis_label = "Modes"
236  self.source_variances[c].data = dict(
237  x=np.arange(len(data)).tolist(), y=np.var(data, axis=1).tolist())
238 
239  self.pretext.text = """ """
240  self.button_load.button_type = tmp
widget_roket.Bokeh_roket.table_params
table_params
Definition: widget_roket.py:98
widget_roket.Bokeh_roket.P
P
Definition: widget_roket.py:31
widget_roket.Bokeh_roket.table_breakdown
table_breakdown
Definition: widget_roket.py:74
widget_roket.Bokeh_roket.source_breakdown
source_breakdown
Definition: widget_roket.py:55
widget_roket.Bokeh_roket.url
url
Definition: widget_roket.py:34
widget_roket.Bokeh_roket.datapath
datapath
Definition: widget_roket.py:25
widget_roket.Bokeh_roket.select_files
select_files
Definition: widget_roket.py:43
widget_roket.Bokeh_roket.Btt
Btt
Definition: widget_roket.py:30
widget_roket.Bokeh_roket
Definition: widget_roket.py:20
widget_roket.Bokeh_roket.update_files
def update_files(self)
Update the select_files options following the current datapath.
Definition: widget_roket.py:132
widget_roket.Bokeh_roket.f
f
Definition: widget_roket.py:29
widget_roket.Bokeh_roket.colors
colors
Definition: widget_roket.py:46
widget_roket.Bokeh_roket.tab
tab
Definition: widget_roket.py:122
widget_roket.Bokeh_roket.source_cor
source_cor
Definition: widget_roket.py:60
widget_roket.Bokeh_roket.source_params
source_params
Definition: widget_roket.py:63
widget_roket.Bokeh_roket.old
old
Definition: widget_roket.py:35
widget_roket.Bokeh_roket.p
p
Definition: widget_roket.py:104
widget_roket.Bokeh_roket.load_file
def load_file(self)
Load the selected file and update the display.
Definition: widget_roket.py:145
widget_roket.Bokeh_roket.update
def update(self)
Main callback function that update the bokeh display.
Definition: widget_roket.py:219
widget_roket.Bokeh_roket.cor
cor
Definition: widget_roket.py:33
widget_roket.Bokeh_roket.select_datapath
select_datapath
Definition: widget_roket.py:40
widget_roket.Bokeh_roket.source_cov
source_cov
Definition: widget_roket.py:57
widget_roket.Bokeh_roket.source_variances
source_variances
Definition: widget_roket.py:101
widget_roket.Bokeh_roket.update_breakdown
def update_breakdown(self)
Update the values of the error breakdown tables.
Definition: widget_roket.py:164
widget_roket.Bokeh_roket.files
files
Definition: widget_roket.py:26
widget_roket.Bokeh_roket.control_box
control_box
Definition: widget_roket.py:120
widget_roket.Bokeh_roket.dataroot
dataroot
Definition: widget_roket.py:24
widget_roket.Bokeh_roket.update_params
def update_params(self)
Update the simulation parameters table.
Definition: widget_roket.py:206
widget_roket.Bokeh_roket.table_cor
table_cor
Definition: widget_roket.py:90
widget_roket.Bokeh_roket.contributors
contributors
Definition: widget_roket.py:54
widget_roket.Bokeh_roket.button_load
button_load
Definition: widget_roket.py:39
widget_roket.Bokeh_roket.update_covcor
def update_covcor(self)
Update tables of covariances and correlations.
Definition: widget_roket.py:181
widget_roket.Bokeh_roket.table_cov
table_cov
Definition: widget_roket.py:87
widget_roket.Bokeh_roket.cov
cov
Definition: widget_roket.py:32
widget_roket.Bokeh_roket.__init__
def __init__(self)
Definition: widget_roket.py:22
widget_roket.Bokeh_roket.radioButton_basis
radioButton_basis
Definition: widget_roket.py:44
widget_roket.Bokeh_roket.pretext
pretext
Definition: widget_roket.py:38