COMPASS  5.0.0
End-to-end AO simulation tool using GPU acceleration
widget_gamora.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, 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
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 
28  self.f = None
29  self.Btt = None
30  self.P = None
31 
32  self.url = "http://" + os.uname()[1] + ".obspm.fr/~" + os.getlogin(
33  ) + "/roket_display"
34  self.old = None
35  self.psf_compass = None
36  self.psf_Vii = None
37 
38  # Widgets Elements
39  self.pretext = PreText(text=""" """, width=500, height=75)
40  self.SRcompass = TextInput(value=" ", title="SR compass:")
41  self.SRVii = TextInput(value=" ", title="SR Vii:")
42 
43  self.button_psf = Button(label="PSF !", button_type="success")
44  self.button_roll = Button(label="Roll", button_type="primary")
45 
46  self.select_datapath = Select(
47  title="Datapath", value=self.dataroot,
48  options=[self.dataroot] + glob(self.dataroot + "*/"))
49  self.select_files = Select(title="File", value=self.files[0], options=self.files)
50 
51  self.xdr = Range1d(start=0, end=1024)
52  self.ydr = Range1d(start=1024, end=0)
53  self.image_compass = figure(x_range=self.xdr, y_range=self.ydr,
54  x_axis_location="above", title="PSF COMPASS")
55  self.image_Vii = figure(x_range=self.image_compass.x_range,
56  y_range=self.image_compass.y_range,
57  x_axis_location="above", title="PSF ROKET")
58  self.plot_psf_cuts = figure(plot_height=600, plot_width=800, y_range=[1e-9, 1],
59  x_range=self.image_compass.x_range,
60  y_axis_type="log")
61  self.source_psf_compass = ColumnDataSource(data=dict(x=[], y=[]))
62  self.source_psf_Vii = ColumnDataSource(data=dict(x=[], y=[]))
63 
64  self.image_compass.image_url(url=[], x=0, y=0, w=1024, h=1024)
65  self.image_Vii.image_url(url=[], x=0, y=0, w=1024, h=1024)
66  self.plot_psf_cuts.line(x="x", y="y", legend="COMPASS", color="red",
67  muted_alpha=0.1, source=self.source_psf_compass)
68  self.plot_psf_cuts.line(x="x", y="y", legend="Vii", color="blue",
69  muted_alpha=0.1, source=self.source_psf_Vii)
70  self.plot_psf_cuts.legend.click_policy = "mute"
71 
72  # Callback functions
73  self.select_datapath.on_change(
74  "value", lambda attr, old, new: self.update_files())
75  self.select_files.on_change("value", lambda attr, old, new: self.update())
76  self.button_psf.on_click(self.comp_psf)
77  self.button_roll.on_click(self.roll_psf)
78 
79  self.update()
80 
81  #layouts
82  self.control_box = widgetbox(self.select_datapath, self.select_files,
83  self.button_psf, self.button_roll, self.SRcompass,
84  self.SRVii, self.pretext)
85  self.tab = Panel(
86  child=layout([[self.control_box, self.image_compass, self.image_Vii],
87  [self.plot_psf_cuts]]), title="GAMORA")
88 
89  def update(self):
90  """
91  Update the attributes based on the new selected filename
92  """
93  if os.path.exists(self.datapath + str(self.select_files.value)):
94  self.f = h5py.File(self.datapath + str(self.select_files.value), mode='r+')
95  self.psf_compass = self.f["psf"][:]
96  self.psf_Vii = None
97  self.Btt = self.f["Btt"][:]
98  self.P = self.f["P"][:]
99 
100  def update_files(self):
101  """
102  Update the select_files options following the current datapath
103  """
104  self.datapath = str(self.select_datapath.value)
105  self.files = self.files = [
106  f.split('/')[-1] for f in glob(self.datapath + "roket_*.h5")
107  ]
108  if self.files == []:
109  self.files = ["No hdf5 files"]
110 
111  self.select_files.options = self.files
112  self.select_files.value = self.files[0]
113 
114  def update_psf(self):
115  """
116  Update the PSF by ensquaring them
117  """
118 
119  psfc = self.psf_compass
120  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
121  self.old = "/home/" + os.getlogin() + "/public_html/roket_display" + time + ".png"
122  mpl.image.imsave(self.old, np.log10(np.abs(psfc)))
123  self.image_compass.image_url(
124  url=dict(value=self.url + time + ".png"), x=0, y=0, w=psfc.shape[0],
125  h=psfc.shape[0])
126  self.image_compass.x_range.update(start=0, end=psfc.shape[0])
127  self.image_compass.y_range.update(start=psfc.shape[0], end=0)
128 
129  self.SRcompass.value = "%.2f" % (self.psf_compass.max())
130 
131  if self.psf_Vii is not None:
132 
133  psfv = self.psf_Vii
134  time = str(datetime.datetime.now().strftime('%Y-%m-%d_%H_%M_%f'))
135  self.old = "/home/" + os.getlogin(
136  ) + "/public_html/roket_display" + time + ".png"
137  mpl.image.imsave(self.old, np.log10(np.abs(psfv)))
138  self.image_Vii.image_url(
139  url=dict(value=self.url + time + ".png"), x=0, y=0, w=psfc.shape[0],
140  h=psfc.shape[0])
141  self.SRVii.value = "%.2f" % (self.psf_Vii.max())
142 
143  self.update_cuts()
144 
145  def update_cuts(self):
146  """
147  Update the PSF cuts
148  """
149  x = np.arange(self.psf_compass.shape[0])
150  self.source_psf_compass.data = dict(
151  x=x, y=self.psf_compass[:, self.psf_compass.shape[0] // 2])
152  if self.psf_Vii is not None:
153  self.source_psf_Vii.data = dict(
154  x=x, y=self.psf_Vii[:, self.psf_Vii.shape[0] // 2])
155 
156  def comp_psf(self):
157  """
158  Compute the PSF using the Vii functions and display it
159  """
160  self.pretext.text = """ Computing PSF using Vii... Please wait"""
161  self.button_psf.button_type = "danger"
162  _, _, self.psf_Vii, _ = gamora.psf_rec_Vii(self.datapath +
163  str(self.select_files.value))
164  self.psf_compass = self.f["psf"][:]
165  self.update_psf()
166  self.pretext.text = """ """
167  self.button_psf.button_type = "success"
168 
169  def roll_psf(self):
170  """
171  Roll the COMPASS PSF (for retro-compatibility with old ROKET files)
172  """
173  self.psf_compass = np.fft.fftshift(self.psf_compass)
174  self.update_psf()
widget_gamora.Bokeh_gamora.Btt
Btt
Definition: widget_gamora.py:29
widget_gamora.Bokeh_gamora.SRVii
SRVii
Definition: widget_gamora.py:41
widget_gamora.Bokeh_gamora.psf_compass
psf_compass
Definition: widget_gamora.py:35
widget_gamora.Bokeh_gamora
Definition: widget_gamora.py:18
widget_gamora.Bokeh_gamora.plot_psf_cuts
plot_psf_cuts
Definition: widget_gamora.py:58
widget_gamora.Bokeh_gamora.xdr
xdr
Definition: widget_gamora.py:51
widget_gamora.Bokeh_gamora.update_cuts
def update_cuts(self)
Update the PSF cuts.
Definition: widget_gamora.py:148
widget_gamora.Bokeh_gamora.ydr
ydr
Definition: widget_gamora.py:52
widget_gamora.Bokeh_gamora.old
old
Definition: widget_gamora.py:34
widget_gamora.Bokeh_gamora.dataroot
dataroot
Definition: widget_gamora.py:22
widget_gamora.Bokeh_gamora.P
P
Definition: widget_gamora.py:30
widget_gamora.Bokeh_gamora.f
f
Definition: widget_gamora.py:28
widget_gamora.Bokeh_gamora.source_psf_compass
source_psf_compass
Definition: widget_gamora.py:61
widget_gamora.Bokeh_gamora.button_roll
button_roll
Definition: widget_gamora.py:44
widget_gamora.Bokeh_gamora.image_compass
image_compass
Definition: widget_gamora.py:53
widget_gamora.Bokeh_gamora.__init__
def __init__(self)
Definition: widget_gamora.py:20
widget_gamora.Bokeh_gamora.button_psf
button_psf
Definition: widget_gamora.py:43
widget_gamora.Bokeh_gamora.control_box
control_box
Definition: widget_gamora.py:82
widget_gamora.Bokeh_gamora.select_datapath
select_datapath
Definition: widget_gamora.py:46
widget_gamora.Bokeh_gamora.psf_Vii
psf_Vii
Definition: widget_gamora.py:36
widget_gamora.Bokeh_gamora.update_files
def update_files(self)
Update the select_files options following the current datapath.
Definition: widget_gamora.py:103
widget_gamora.Bokeh_gamora.roll_psf
def roll_psf(self)
Roll the COMPASS PSF (for retro-compatibility with old ROKET files)
Definition: widget_gamora.py:172
widget_gamora.Bokeh_gamora.url
url
Definition: widget_gamora.py:32
widget_gamora.Bokeh_gamora.SRcompass
SRcompass
Definition: widget_gamora.py:40
widget_gamora.Bokeh_gamora.pretext
pretext
Definition: widget_gamora.py:39
widget_gamora.Bokeh_gamora.select_files
select_files
Definition: widget_gamora.py:49
widget_gamora.Bokeh_gamora.datapath
datapath
Definition: widget_gamora.py:23
widget_gamora.Bokeh_gamora.files
files
Definition: widget_gamora.py:24
widget_gamora.Bokeh_gamora.image_Vii
image_Vii
Definition: widget_gamora.py:55
widget_gamora.Bokeh_gamora.comp_psf
def comp_psf(self)
Compute the PSF using the Vii functions and display it.
Definition: widget_gamora.py:159
widget_gamora.Bokeh_gamora.source_psf_Vii
source_psf_Vii
Definition: widget_gamora.py:62
widget_gamora.Bokeh_gamora.update_psf
def update_psf(self)
Update the PSF by ensquaring them.
Definition: widget_gamora.py:117
widget_gamora.Bokeh_gamora.update
def update(self)
Update the attributes based on the new selected filename.
Definition: widget_gamora.py:92
widget_gamora.Bokeh_gamora.tab
tab
Definition: widget_gamora.py:85