forked from SchusterLab/slab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
executable file
·523 lines (448 loc) · 20.7 KB
/
gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/usr/bin/python2.7
"""
gui.py
usage:
- Create a UI in Qt Designer, and with pyuic convert the .ui to .py
- Use the 'SlabWindow' template as a base to enable config_file menu items
(! not yet fully functional !)
- Create a class inheriting from SlabWindow and Ui_MainWindow (as provided by
the designer file)
- In the class' __init__ method
- run self.setupSlabWindow()
- register any plots (or actually any UI components to be
dynamically updated) from the UI with the plot manager, i.e.
self.plots["myplot"] = self.myplot
- For connecting parameter widgets there are three options
- For "standard" widgets (i.e. SpinBox, Checkbox, ButtonGroup, LineEdit),
register a widget with a name using self.register_param(widget, "widgetName")
- Automatically register parameter widgets by modifying the call to setupSlabWindow
self.setupSlabWindow(autoparam=True)
This will locate widgets named with the convention param_widgetName, and
automatically call register_param(widget, "widgetName")
- For non-standard widgets find the appropriate signal to call self.set_param from, i.e
self.connect(self.myparamSpinBox, SIGNAL("valueChanged(int)"),
lambda i: self.set_param("myparam", i))
- Subclass DataThread to add run_script(). In the DataThread
namespace, you will find self.instruments (an InstrumentManager),
self.params (a dictionary of parameters), and self.plots (a PlotManager)
- For any scripts/functions which you wish to execute in a non-blocking
fashion, add a method to the DataThread subclass that executes this
script using the provided resources
- register the methods with start and stop buttons, i.e. after
providing the "myscript" method:
self.register_script("myscript", self.my_go_button, self.my_abort_button)
- after registering all methods, call self.start_thread()
A more complete (functional) example is given at the end of the file
"""
#important to import ipython stuff before any other gui things or everything breaks
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.manager import QtKernelManager
from IPython.lib import guisupport
from slab.instruments import InstrumentManager
from .dataanalysis import get_next_filename
#from slab.widgets import SweepDialog
from PyQt4.Qt import *
try:
from PyQt4.QtCore import QString
except ImportError:
# QString is not defined in newer versions
QString = str
from PyQt4.QtGui import QFileDialog
from PyQt4.QtCore import QTimer
from PyQt4 import uic
import numpy as np
import inspect, time
DEBUG = False
def loadui(fname):
return uic.loadUiType(fname)[0]
class IPythonWidget(RichIPythonWidget):
def __init__(self, parent=None, **kwargs):
super(self.__class__, self).__init__(parent)
self.app = app = guisupport.get_app_qt4()
self.kernel_manager = QtKernelManager()
self.kernel_manager.start_kernel()
self.kernel_manager.kernel.gui = 'qt4'
self.kernel_client=self.kernel_manager.client()
self.kernel_client.start_channels()
self.confirm_restart=False
class Bunch(object):
def __init__(self, adict):
try: self.__dict__.update(adict)
except: self.__dict__.update(adict.cache)
class ManagerProxy(QObject):
def __init__(self, emit_name):
QObject.__init__(self)
self.emit_name = emit_name
def __getitem__(self, item_name):
class IProxy(object):
def __getattr__(self2, method_name):
return lambda *args, **kwargs: self.emit(SIGNAL(self.emit_name),
item_name, method_name, args, kwargs)
return IProxy()
class DictProxy(QObject):
def __init__(self, emit_name):
QObject.__init__(self)
self.cache = {}
self.emit_name = emit_name
def set_nosignal(self, name, value):
self.cache[name] = value
def __setitem__(self, name, value):
self.set_nosignal(name, value)
self.emit(SIGNAL(self.emit_name+"Changed"), name, value)
def update(self):
self.emit(SIGNAL(self.emit_name+"Get"))
def update_cache(self, db):
self.cache = db
def __getitem__(self, name):
return self.cache[name]
def __contains__(self, key):
return key in self.cache
def keys(self): #Added by DS not sure if this is right
return list(self.cache.keys())
class DataThread(QObject):
def __init__(self, config_file=None):
QObject.__init__(self)
self.config_file = config_file
self.instruments = InstrumentManager(self.config_file)
self.params = DictProxy("threadParameter")
self.connect(self.params, SIGNAL("threadParameterChanged"),
lambda n,v: self.emit(SIGNAL("threadParameterChanged"), n, v))
self.plots = ManagerProxy("plotMethodCalled")
self.gui = ManagerProxy("guiMethodCalled")
self._aborted = False
def run_on_instrument_manager(inst_name, method_name, args, kwargs):
try: getattr(self.instrument_manager[inst_name], method_name)(*args, **kwargs)
except Exception as e: self.emit("instrumentMethodFailed", (str(e),))
def set_param(self, name, value):
self.params.set_nosignal(name, value)
def save_experimental_settings(self):
filename = str(QFileDialog.getSaveFileName())
self.instruments.save_settings(filename)
def save_all_settings(self):
filename = str(QFileDialog.getSaveFileName())
# save_settings does not currently (11/13) process params meaningfully
self.instruments.save_settings(filename, params=self.params)
def load_settings(self):
filename = QFileDialog.getOpenFileName(filter="*.cfg")
self.instruments.load_config_file(filename)
def list_instruments(self):
for key, inst in self.instruments.items():
self.msg(key + inst.name)
def get_local_params(self):
self.emit(SIGNAL("localParams"), self.params)
def msg(self, *args):
self.emit(SIGNAL("msg"), *args)
def status(self, s):
self.emit(SIGNAL("status"), s)
def progress(self, val, tot):
self.emit(SIGNAL("progress"), (val, tot))
def abort(self):
self._aborted = True
def aborted(self):
if self._aborted:
self._aborted = False
return True
else:
return False
def run_sweep(self, whichStack):
self.msg("Sweeping...")
params = Bunch(self.params)
if whichStack[params.sweep1] == 0:
prange = list(range(params.sweep1_start_int,
params.sweep1_stop_int, params.sweep1_step_int))
else:
prange = np.arange(params.sweep1_start_float,
params.sweep1_stop_float, params.sweep1_step_float)
self.msg(prange)
if params.sweep2_enabled:
raise NotImplementedError
actions = params.actions.splitlines()
for p in prange:
self.msg(params.sweep1 + "=" + str(p))
for action in actions:
self.params[params.sweep1] = p
try:
if self.params["abort_sweep"]:
self.params["abort_sweep"] = False
return
except: pass
getattr(self, action)()
self.msg("Done")
def run_data_thread(self, method, *args):
if DEBUG:
print(method, "running")
self.msg(method)
self.msg(args)
getattr(self, method)(*args)
self.emit(SIGNAL(method + "done"))
class SlabWindow(QMainWindow):
def __init__(self, DataThreadC=DataThread, config_file=None):
QMainWindow.__init__(self)
self.instruments = ManagerProxy("instrumentMethodCalled")
self.plots = {}
self.gui = {}
self.params = DictProxy("parameter")
self.param_widgets = []
self.registered_actions = []
if config_file is not None:
self.data_thread_obj = DataThreadC(config_file=config_file)
else:
self.data_thread_obj = DataThreadC()
self.connect(self.instruments, SIGNAL("instrumentMethodCalled"),
self.data_thread_obj.run_on_instrument_manager)
self.connect(self.data_thread_obj, SIGNAL("instrumentMethodFailed"),
self.msg)
self.connect(self.data_thread_obj.plots, SIGNAL("plotMethodCalled"),
self.run_on_plot_manager)
self.connect(self.data_thread_obj.gui, SIGNAL("guiMethodCalled"),
self.run_on_gui)
self.connect(self.params, SIGNAL("parameterChanged"),
self.data_thread_obj.set_param)
self.connect(self.params, SIGNAL("parameterChanged"),
self.write_param_widget)
self.connect(self.data_thread_obj, SIGNAL("threadParameterChanged"),
self.write_param_widget)
self.connect(self.params, SIGNAL("parameterGet"),
self.data_thread_obj.get_local_params)
self.connect(self.data_thread_obj, SIGNAL("localParams"),
self.params.update_cache)
self.connect(self.data_thread_obj, SIGNAL("msg"), self.msg)
self.connect(self.data_thread_obj, SIGNAL("status"), self.status)
self.connect(self.data_thread_obj, SIGNAL("progress"), self.progress)
def setupSlabWindow(self, autoparam=False, prefix="param_"):
"Connect Ui components provided by the SlabWindow designer template"
self.setupUi(self)
self._data_thread = QThread()
self.data_thread_obj.moveToThread(self._data_thread)
self.connect(self, SIGNAL("lastWindowClosed()"), self._data_thread.exit)
try:
self.actionExperimental_Settings.triggered.connect(
self.data_thread_obj.save_experimental_settings)
except Exception as e:
print("Could not connect menu actions", e)
try:
self.actionExperimental_and_Instrument_Settings.triggered.connect(
self.data_thread_obj.save_all_settings)
except Exception as e:
print("Could not connect menu actions", e)
try:
self.actionLoad.triggered.connect(
self.data_thread_obj.load_settings)
except Exception as e:
print("Could not connect menu actions", e)
if autoparam:
self.autoparam(self, prefix)
def auto_register_gui(self, container=None, prefix=""):
if container is None:
container = self
for wname, widget in list(self_dict(container, prefix).items()):
if not inspect.isroutine(widget):
self.gui[wname] = widget
def autoparam(self, container, prefix="param_"):
for wname, widget in list(self_dict(container, prefix).items()):
if isinstance(widget, QWidget) or isinstance(widget, QAction):
if DEBUG: print("attached parameter", wname)
self.register_param(widget, wname)
def run_on_plot_manager(self, plot_name, method_name, args, kwargs):
getattr(self.plots[plot_name], method_name)(*args, **kwargs)
def run_on_gui(self, plot_name, method_name, args, kwargs):
getattr(self.gui[plot_name], method_name)(*args, **kwargs)
def set_param(self, name, value):
if isinstance(value, QString):
value = str(value)
self.params[name] = value
def start_thread(self):
self.connect(self, SIGNAL("RunOnDataThread"), self.data_thread_obj.run_data_thread)
self.read_param_widgets()
self._data_thread.start()
def start_script(self, method, *args):
self.emit(SIGNAL("RunOnDataThread"), method, *args)
launcher_widget_tools = {
QPushButton : "released",
QDialog: "accepted",
QAction: "triggered",
QListWidget: "itemDoubleClicked",
QTreeWidget: "itemDoubleClicked",
QLineEdit: "returnPressed"
}
def register_script(self, method, launch_widget, abort_widget=None):
"""
Connect a launcher widget to a method in the data thread, causing that method
to run when the widget is activated.
@param method: A string sharing the name of a method belonging to your data thread
@param go_widget: A widget in this window, which must be an instance of either
- QPushButton
- QDialog
- QAction
- QListWidget
- QTreeWidget
In the case that the widget is a List/Tree Widget, the method will be invoked
when an item is double-clicked, and the method will receive as
it's first (and only) method the List/TreeWidgetItem clicked.
"""
self.registered_actions.append(method)
for WidgetC, sig_name in self.launcher_widget_tools.items():
if isinstance(launch_widget, WidgetC):
getattr(launch_widget, sig_name).connect(
lambda *args:
self.start_script(method, *args))
print("attached method", method)
break
else:
print("Did not attach", method, "could not identify widget", launch_widget)
if abort_widget is not None:
for WidgetC, sig_name in self.launcher_widget_tools.items():
if isinstance(abort_widget, WidgetC):
getattr(abort_widget, sig_name).connect(self.data_thread_obj.abort, Qt.DirectConnection)
widget_tools = {
QSpinBox : {"read" : "value",
"write" : "setValue",
"change_signal" : "valueChanged"},
QDoubleSpinBox : {"read" : "value",
"write" : "setValue",
"change_signal" : "valueChanged"},
QTextEdit : {"read" : "toPlainText",
"write" : None,
"change_signal" :
lambda w, f: w.textChanged.connect(lambda: f(w.document().toPlainText()))},
QLineEdit : {"read" : "text",
"write" : "setText",
"change_signal" : "textEdited"},
QButtonGroup : {"read" : "checkedId",
"write" : "setId",
"change_signal" : "buttonClicked"},
QCheckBox : {"read" : "isChecked",
"write" : "setChecked",
"change_signal" : "stateChanged"},
QComboBox : {"read" : "currentText",
"write" :
lambda w, v: w.setCurrentIndex(w.findText(v)),
"change_signal":
lambda w, f: w.currentIndexChanged[str].connect(f)},
QAction : {"read" : None,
"write" : None,
"change_signal":
lambda w, f: w.triggered.connect(lambda: f(True))},
QTreeWidget : {"read" : "currentItem",
"write" : "setCurrentItem",
"change_signal":
lambda w, f:
w.currentItemChanged.connect(lambda cur, prev: f(cur))}
}
def read_param_widgets(self):
for widget, name in self.param_widgets:
for cls, clstools in list(self.widget_tools.items()):
if isinstance(widget, cls):
if clstools["read"] is not None:
if hasattr(clstools["read"], "__call__"):
self.set_param(clstools["read"](widget))
else:
self.set_param(name, getattr(widget, clstools["read"])())
break
def write_param_widget(self, name, value):
res = lookup(self.param_widgets, name, 1)
if res is not None:
widget, name = res
for cls, clstools in list(self.widget_tools.items()):
if isinstance(widget, cls):
if clstools["write"] is not None:
if hasattr(clstools["write"], "__call__"):
clstools["write"](widget, value)
else:
getattr(widget, clstools["write"])(value)
break
def register_param(self, widget, name):
self.param_widgets.append((widget, name))
set_fn = lambda i: self.set_param(name, i)
for cls, clstools in list(self.widget_tools.items()):
if isinstance(widget, cls):
if hasattr(clstools["change_signal"], "__call__"):
clstools["change_signal"](widget, set_fn)
else:
getattr(widget, clstools["change_signal"]).connect(set_fn)
break
else:
print("Error: Could Not match parameter", name, "with class")
def msg(self, *args):
t = time.strftime("%d %H:%M:%S")
message = t + " :: " + " ".join(map(str, args))
try:
self.message_box.append(message)
except Exception as e:
if DEBUG:
print("no msg box:", e)
print(message_)
def status(self, message):
try:
self.statusbar.showMessage(message)
except Exception as e:
if DEBUG:
print("no status bar:", e)
print(message)
def progress(self, pair):
val, tot = pair
self.progressBar.setMaximum(tot)
self.progressBar.setValue(val)
# def save_time_series(self, *xs):
# datapath = self.params["datapath"]
# fname = os.path.join(datapath,
# get_next_filename(datapath, self.params["prefix"]))
# f = open(fname, 'w')
# csv.writer(f).writerows(zip(*xs))
# f.close()
# if DEBUG: print "saved", fname
#
# def add_instrument(self, iname):
# pass
def add_sweep_dialog(self):
self.sweep_dialog = sd = SweepDialog()
try: self.actionStart_Sweep.triggered.connect(sd.exec_)
except AttributeError: print("No actionStart_Sweep to connect")
self.whichStack = {}
for w, name in self.param_widgets:
if isinstance(w, QSpinBox) or isinstance(w, QDoubleSpinBox):
print("sweep enabled on", name)
sd.param_sweep1.addItem(name)
sd.param_sweep2.addItem(name)
if isinstance(w, QSpinBox):
self.whichStack[name] = 0
else:
self.whichStack[name] = 1
for name in self.registered_actions:
sd.actions_comboBox.addItem(name)
sd.addActionButton.clicked.connect(
lambda: sd.param_actions.append(
sd.actions_comboBox.currentText()))
sd.clearActionsButton.clicked.connect(sd.param_actions.clear)
sd.param_sweep1.currentIndexChanged[str].connect(
lambda name:
sd.sweep1_stackedWidget.setCurrentIndex(self.whichStack[str(name)]))
sd.param_sweep2.currentIndexChanged[str].connect(
lambda name:
sd.sweep2_stackedWidget.setCurrentIndex(self.whichStack[str(name)]))
sd.accepted.connect(lambda: self.emit(SIGNAL("run_sweep"), self.whichStack))
self.data_thread_obj.connect(self, SIGNAL("run_sweep"), self.data_thread_obj.run_sweep)
self.autoparam(self.sweep_dialog)
def compile_ui(prefix):
uifilename = prefix + ".ui"
pyfile = open(prefix + "_ui.py", 'w')
uic.compileUi(uifilename, pyfile)
pyfile.close()
def self_dict(obj, prefix="param_"):
res = {}
for name in dir(obj):
if name.startswith(prefix):
res[name[len(prefix):]] = getattr(obj, name)
return res
def lookup(tuplelist, ident, idx=0):
for t in tuplelist:
if t[idx] == ident:
return t
def runWin(WinC, maximized=True, *args, **kwargs):
app = QApplication([])
win = WinC(*args, **kwargs)
win.show()
if maximized:
win.showMaximized()
app.connect(app, SIGNAL("lastWindowClosed()"), win, SIGNAL("lastWindowClosed()"))
app.exec_()