Commit 115e259c authored by Nick Sauerwein's avatar Nick Sauerwein
Browse files

new gui - scipy optimizer used

parent 4a73267c
......@@ -160,14 +160,14 @@ class IO:
encrypt_pwd=True)
except:
print ('This computer does not support elog')
def save(self, data, name = None, elog = False, kind = 'config', comment = ''):
def save(self, data, name = None, elog = False, kind = 'config', comment = '', folder = ''):
data['comment'] = comment
if name == None:
name = kind+data['time']
np.save(self.path+name+'.npy',data)
np.save(self.path+folder+name+'.npy',data)
print (kind+' saved as '+name+'.npy')
if elog:
......
No preview for this file type
......@@ -100,7 +100,7 @@ class plot_config_tool:
layout_bt = widgets.Layout(width = '20px')
layout_text = widgets.Layout(width = 'auto')
if np.dtype(type(value)) == np.dtype('float64') or np.dtype(type(value)) == np.dtype('int64') or np.dtype(type(value)) == np.dtype('int32'):
plot_config[variable_name] = False
self.plot_config[variable_name] = False
self.cb = widgets.Checkbox(description=variable_name, layout = layout_text)
self.cb.observe(self.change_bool, names = 'value')
......@@ -121,97 +121,139 @@ class wrapper:
def measure(self, check):
self.measure_check = check['new']
def main(devices, io, parallel = False):
config = dm.get_config(devices)
config['comment'] = 'start of GUI'
#io.save(config, comment = 'start of GUI')
#main
display(widgets.HTML('<h1>LWFA GUI</h1>'))
display(widgets.HTML('<b>Save and Restore configurations</b>'))
bt_save = widgets.Button(description= 'Save config')
def save(bt):
comment = text_comment.value
class configio:
def __init__(self, devices, io, configset):
self.io = io
self.devices = devices
self.configset = configset
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b>Save and Restore configurations</b>')
self.bt_save = widgets.Button(description= 'Save config')
config = dm.get_config(self.devices)
config['comment'] = 'start of GUI'
self.bt_save.on_click(self.save)
self.ht_save = widgets.HTML('')
self.select_restore = widgets.Select(description='restore:', options = self.get_all())
self.bt_restore = widgets.Button(description='restore now!')
self.bt_restore.on_click(self.restore)
self.ht_restore = widgets.HTML('<b>selected config: '+config['comment']+'</b>')
self.bt_browse = widgets.Button(description='Browse configs')
self.bt_browse.on_click(self.find_config)
self.text_comment = widgets.Text(description= 'Comment:')
self.check_elog = widgets.Checkbox(value=False,
description='elog')
self.widget = widgets.VBox([heading,
widgets.HBox([self.select_restore,self.bt_restore,self.ht_restore]),
widgets.HBox([self.bt_save,self.text_comment,self.check_elog,self.ht_save])])
def show(self):
display(self.widget)
def save(self, bt):
comment = self.text_comment.value
if comment == '':
ht_save.value = '<b>Enter comment to save config!</b>'
self.ht_save.value = '<b>Enter comment to save config!</b>'
return
config = dm.get_config(devices)
io.save(config, comment = comment, elog = check_elog.value)
text_comment.value = ''
ht_save.value = '<b>Config saved!</b>'
select_restore.options = get_all()
bt_save.on_click(save)
ht_save = widgets.HTML('')
def restore(bt):
restore = select_restore.value
config = io.load(restore)
dm.set_config(devices,config)
refresh('')
ht_restore.value = '<b>restored config: '+config['comment']+'</b>'
select_restore.options = get_all()
def restore_view(inst):
config = dm.get_config(self.devices)
self.io.save(config, comment = comment, elog = self.check_elog.value)
self.text_comment.value = ''
self.ht_save.value = '<b>Config saved!</b>'
self.select_restore.options = self.get_all()
def restore(self, bt):
restore = self.select_restore.value
config = self.io.load(restore)
dm.set_config(self.devices,config)
self.configset.refresh()
self.ht_restore.value = '<b>restored config: '+config['comment']+'</b>'
self.select_restore.options = self.get_all()
def restore_view(self, inst):
restore = inst.new
try:
config = io.load(restore)
config = self.io.load(restore)
except:
ht_restore.value = '<b>Error: This config doesnot exist</b>'
self.ht_restore.value = '<b>Error: This config doesnot exist</b>'
return
ht_restore.value = '<b>selected config: '+config['comment']+'</b>'
self.ht_restore.value = '<b>selected config: '+config['comment']+'</b>'
def get_all():
configs = io.search(output = False)
def get_all(self):
configs = self.io.search(output = False)
c = {}
for i in range(len(configs)):
c[configs[i]['comment']] = i
return c
select_restore = widgets.Select(description='restore:', options = get_all())
bt_restore = widgets.Button(description='restore now!')
bt_restore.on_click(restore)
ht_restore = widgets.HTML('<b>selected config: '+config['comment']+'</b>')
display(widgets.HBox([select_restore,bt_restore, ht_restore]))
#browse configs
def find_config(bt):
config = io.load(gui = True)
dm.set_config(devices,config)
refresh('')
def find_config(self,bt):
config = self.io.load(gui = True)
dm.set_config(self.devices,config)
self.refresh('')
print ('restored config: '+config['comment'])
bt_browse = widgets.Button(description='Browse configs')
bt_browse.on_click(find_config)
#display(bt_browse)
text_comment = widgets.Text(description= 'Comment:')
check_elog = widgets.Checkbox(value=False,
description='elog')
display(widgets.HBox([bt_save,text_comment,check_elog,ht_save] ))
class configset:
def make_controller():
def __init__(self, devices, io):
self.devices = devices
self.io = io
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b><br>Interact with devices</b>')
self.bt_refresh = widgets.Button(description='Refresh configs')
self.bt_refresh.on_click(self.refresh)
self.tab, self.wps = self.make_controller()
self.widget = widgets.VBox([heading,
self.bt_refresh,
self.tab])
def show(self):
display(self.widget)
def make_controller(self):
#devices
vbs = []
names = {}
wps = []
meas_check = {}
for i in range(len(devices)):
config = devices[i].get_config()
for i in range(len(self.devices)):
config = self.devices[i].get_config()
wps += [wrapper(devices, i, io)]
wps += [wrapper(self.devices, i, self.io)]
wgs = []
for item in sorted(config.items()):
wgs += [interaction_tool(item[0],
item[1],
devices[i],
self.devices[i],
config).widget]
if hasattr(devices[i], 'measure'):
if hasattr(self.devices[i], 'measure'):
meas_check[i] = widgets.Checkbox(description='measure')
......@@ -219,34 +261,45 @@ def main(devices, io, parallel = False):
wgs += [meas_check[i]]
vbs += [widgets.VBox(wgs)]
names[i] = [type(devices[i]).__name__]
names[i] = [type(self.devices[i]).__name__]
tab = widgets.Tab(children = vbs, _titles= names)
return tab, wps
display(widgets.HTML('<b><br>Interact with devices</b>'))
bt_refresh = widgets.Button(description='Refresh configs')
global tab
global wps
tab, wps = make_controller()
def refresh(self, bt = 0):
self.tab.close()
tab, wps = self.make_controller()
self.widget.children = list(self.widget.children)[:-1] + [tab]
def find_devices2measure(self):
#load_devices to measure
devices2measure = []
for i in range(len(self.devices)):
if self.wps[i].measure_check:
devices2measure += [self.devices[i]]
return devices2measure
vb_config = widgets.VBox([bt_refresh, tab])
display(vb_config)
class plot_configurator:
def refresh(bt):
global tab
global wps
tab.close()
tab, wps = make_controller()
vb_config.children = [bt_refresh, tab]
bt_refresh.on_click(refresh)
def __init__(self, configset):
self.configset = configset
self.plot_config = {}
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b><br>Plot measurement configuration</b>')
self.bt_plot_config_init = widgets.Button(description='Measure to initialize')
self.bt_plot_config_init.on_click(self.initialize)
display(widgets.HTML('<b><br>Plot measurement configuration</b>'))
bt_plot_config_init = widgets.Button(description='Measure to initialize')
global plot_config
plot_config = {}
def plot_configurator(bt):
self.vb_plot_config = widgets.VBox([self.bt_plot_config_init])
self.widget = widgets.VBox([heading,
self.vb_plot_config])
def initialize(self, bt):
#devices
vbs = []
names = {}
......@@ -254,62 +307,89 @@ def main(devices, io, parallel = False):
plot_meas_check = {}
devices2measure = find_devices2measure()
devices2measure = self.configset.find_devices2measure()
measurement = dm.measure(devices2measure)
global plot_config
plot_config = {}
for i in range(len(devices2measure)):
device_name = type(devices2measure[i]).__name__
plot_config[device_name] = {}
self.plot_config[device_name] = {}
wgs = []
for item in sorted(measurement['measure'][device_name].items()):
wgs += [plot_config_tool(item[0],
item[1],
plot_config[device_name]).widget]
self.plot_config[device_name]).widget]
if hasattr(devices2measure[i], 'plot_measure'):
wgs += [plot_config_tool('plot_measure',
0,
plot_config[device_name]).widget]
self.plot_config[device_name]).widget]
vbs += [widgets.VBox(wgs)]
names[i] = [type(devices2measure[i]).__name__]
measure_tab = widgets.Tab(children = vbs, _titles= names)
vb_plot_config.children = [bt_plot_config_init, measure_tab]
self.vb_plot_config.children = [self.bt_plot_config_init, measure_tab]
class measureset:
def __init__(self, configset, plot_configurator, configio):
self.plot_configurator = plot_configurator
self.configio = configio
self.configset = configset
self.devices = configset.devices
self.io = configset.io
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b><br>Make measurements</b>')
self.loop_inttext = widgets.IntText(value = 10, description = 'loop')
self.output_check = widgets.Checkbox(description = 'output')
self.save_check = widgets.Checkbox(description = 'save')
bt_plot_config_init.on_click(plot_configurator)
vb_plot_config = widgets.VBox([bt_plot_config_init])
display(vb_plot_config)
display(widgets.HTML('<b><br>Make measurements</b>'))
loop_inttext = widgets.IntText(value = 10, description = 'loop')
output_check = widgets.Checkbox(description = 'output')
self.measure_bh = widgets.HBox([self.loop_inttext,
self.output_check,
self.save_check])
self.rate_text = widgets.FloatText(value = 5, description = 'rate')
self.save_folder = widgets.Text(description = 'save in ',value = self.io.path)
self.save_folder.on_submit(self.change_path)
self.save_folder_text = widgets.HTML('<b> current: '+self.io.path+'</b>')
self.meas_bt = widgets.Button(description = 'measure')
save_check = widgets.Checkbox(description = 'save')
self.clear_hist_bt = widgets.Button(description = 'clear history')
display(widgets.HBox([loop_inttext, output_check, save_check]))
rate_text = widgets.FloatText(value = 5, description = 'rate')
display(rate_text)
meas_bt = widgets.Button(description = 'measure')
clear_hist_bt = widgets.Button(description = 'clear history')
display(widgets.HBox([meas_bt, clear_hist_bt]))
def find_devices2measure():
#load_devices to measure
devices2measure = []
for i in range(len(devices)):
if wps[i].measure_check:
devices2measure += [devices[i]]
return devices2measure
def measure(bt, comment = '', name = None):
devices2measure = find_devices2measure()
self.meas_bt.on_click(self.measure)
self.clear_hist_bt.on_click(self.clear_hist)
self.widget = widgets.VBox([heading,
self.measure_bh,
self.rate_text,
self.save_folder,
widgets.HBox([self.meas_bt,
self.clear_hist_bt]) ])
def change_path(self, dum = None):
import os
if not os.path.isdir(self.save_folder.value):
os.mkdir(self.save_folder.value)
self.io.path = self.save_folder.value
self.save_folder_text.value = '<b> current: '+self.io.path+'</b>'
self.configio.select_restore.options = self.configio.get_all()
def show(self):
display(self.widget)
def measure(self, bt = None, comment = '', name = None):
devices2measure = self.configset.find_devices2measure()
parallel = False
#initialize parallelization
if parallel:
......@@ -323,8 +403,8 @@ def main(devices, io, parallel = False):
loop_measure(parallel = True, connec = self.connec)
#loop measure
def loop_measure(parallel = False, connec = None):
for i in range(loop_inttext.value):
measurements = []
for i in range(self.loop_inttext.value):
import IPython
IPython.display.clear_output(wait = True)
......@@ -333,37 +413,38 @@ def main(devices, io, parallel = False):
t = time.time()
#getting data from devices
measurement = dm.measure(devices, devices2measure = devices2measure)
measurement = dm.measure(self.devices, devices2measure = self.configset.find_devices2measure())
#save data
if save_check.value:
if self.save_check.value:
if name == None:
io.save(measurement,
self.io.save(measurement,
kind = 'measure', comment = comment)
else:
io.save(measurement,
self.io.save(measurement,
kind = 'measure', comment = comment, name = name+'_'+str(i))
if parallel:
connec.send(measurement)
elif output_check.value:
dm.plot_measure(measurement, devices2measure,plot_config, 0.01)
elif self.output_check.value:
dm.plot_measure(measurement,
self.configset.find_devices2measure(),
self.plot_configurator.plot_config, 0.01)
print ('maximal possible rate: ',np.round(1/(time.time() -t),1))
if (1/rate_text.value - (time.time() -t) < 0):
if (1/self.rate_text.value - (time.time() -t) < 0):
print ('set rate cannot be reached.')
else:
time.sleep(1/rate_text.value - (time.time() -t))
time.sleep(1/self.rate_text.value - (time.time() -t))
measurements += [measurement]
return measurements
if output_check.value and parallel:
global plot_config
if self.output_check.value and parallel:
conn1, conn2 = Pipe()
data_stream = DataStreamProcess(conn1)
data_stream.start()
......@@ -380,103 +461,433 @@ def main(devices, io, parallel = False):
plt.pause(0.001)
plt.draw_all(force = True)
else:
loop_measure()
return loop_measure()
def clear_hist(self, bt = None):
import Globals
Globals.plot_hist = {}
class parameter_chooser:
def __init__(self, title, devices):
self.title = title
self.devices = devices
device_dict = {}
for device in self.devices:
device_dict[type(device).__name__] = device
self.device_dict = device_dict
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b>'+self.title+'</b>')
self.select_device = widgets.Select(description = 'device: ',options = self.device_dict)
self.select_device.observe(self.on_value_change_device)
self.select_parameter = widgets.Select(description = 'parameter: ', options = {'no device selected': None})
self.select_parameter.observe(self.on_value_change_parameter)
self.current_value = widgets.HTML('<b> current value: no parameter choosen </b>')
self.minv = widgets.FloatText(description='minv:',value = 0)
self.maxv = widgets.FloatText(description='maxv:',value = 0)
self.nsteps = widgets.IntText(description='nsteps:', value = 1)
box_layout = widgets.Layout(display='flex',
flex_flow='column',
border='solid', width = '350px')
self.on_value_change_device()
self.widget = widgets.VBox([heading,
widgets.HBox([self.select_device]),
widgets.HBox([self.select_parameter]),
self.current_value,
self.minv,
self.maxv,
self.nsteps], layout = box_layout)
def info(self):
return self.select_parameter.value+'_'+str(self.minv.value)+'-'+str(self.maxv.value)+'_'+str(self.nsteps.value)+'+'
def on_value_change_device(self, dum= None):
device = self.select_device.value
scannable = []
config = dm.get_config([device])
for item in config[type(device).__name__].items():
if np.dtype(type(item[1])) == np.dtype('float64') or np.dtype(type(item[1])) == np.dtype(int):
scannable += [item[0]]
self.select_parameter.options = scannable
self.select_parameter.value = scannable[0]
def on_value_change_parameter(self, dum = None):
parameter = self.select_parameter.value
if parameter == None:
return
device = self.select_device.value
config = dm.get_config([device])
current_value = config[type(device).__name__][parameter]
self.current_value.value = '<b> current value: '+str(current_value)+' </b>'
self.minv.value = current_value
self.maxv.value = current_value
def get_linspace(self):
return np.linspace(self.minv.value, self.maxv.value, self.nsteps.value)
class scanning_tool:
def clear_hist(bt):
import Globals
Globals.plot_hist = {}
def __init__(self, measureset):
self.measureset = measureset
self.devices = measureset.devices
self.io = measureset.io
self.make_widget()
def make_widget(self):
heading = widgets.HTML('<b>Scanning Tool</b>')
self.dimensions_text = widgets.IntSlider(value=1,
min=0,
max=10,
step=1,
description='Number of Parameters:')
self.dimensions_text.observe(self.set_dimension)