forked from davidgrier/pyfab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyfab.py
166 lines (145 loc) · 4.84 KB
/
pyfab.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
#!/usr/bin/env python
"""pyfab.py: Application that implements GUI holographic optical trapping.
Dependecies: pyqtgraph=0.10, ipython=2
Commands for usage:
1) ipython
2) from pyfab import pyfab
3) pyfab()
"""
from pyqtgraph.Qt import QtGui, QtCore
from traps import QTrappingPattern, QTrapWidget
from QFabGraphicsView import QFabGraphicsView
from QSLM import QSLM
try:
from cudaCGH import cudaCGH
except ImportError:
from CGH import CGH
from QCGH import QCGH
from QFabDVR import QFabDVR
from QFabVideo import QFabVideo
from QFabFilter import QFabFilter
import sys
import io
import datetime
import os
class pyfab(QtGui.QMainWindow):
def __init__(self, size=(640, 480)):
self.app = QtGui.QApplication(sys.argv)
super(pyfab, self).__init__()
self.init_hardware(size)
self.init_ui()
self.init_configuration()
def init_hardware(self, size):
# video screen
self.fabscreen = QFabGraphicsView(size=size, gray=True)
self.video = QFabVideo(self.fabscreen.video)
self.filters = QFabFilter(self.fabscreen.video)
# DVR
self.dvr = QFabDVR(source=self.fabscreen.video)
self.dvr.recording.connect(self.handleRecording)
# spatial light modulator
self.slm = QSLM()
# computation pipeline for the trapping pattern
try:
self.cgh = cudaCGH(self.slm)
except NameError:
self.cgh = CGH(self.slm)
self.pattern = QTrappingPattern(self.fabscreen)
self.pattern.pipeline = self.cgh
def init_ui(self):
self.setWindowTitle("Pyfab")
#geometry
desktop = QtGui.QDesktopWidget()
w = desktop.screenGeometry().width()
h = desktop.screenGeometry().height()
r = w/h
self.setGeometry(w/4, w/5, w/2, h/2)
#set layout
wpyfab = QtGui.QWidget()
layout = QtGui.QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(1)
layout.addWidget(self.fabscreen)
wpyfab.setLayout(layout)
#menu bar
self.menu = QtGui.QMenuBar()
self.calibrationMenu()
self.setMenuBar(self.menu)
#tabs
tabs = QtGui.QTabWidget()
tabs.addTab(self.controlTab(), 'Controls')
tabs.addTab(self.trapTab(), 'Traps')
layout.addWidget(tabs)
layout.setStretch(0,2)
layout.setStretch(1,1)
#tabs.setFixedSize(tabs.size())
layout.setAlignment(tabs, QtCore.Qt.AlignTop)
wpyfab.setLayout(layout)
self.setCentralWidget(wpyfab)
self.show()
def calibrationMenu(self):
calMenu = self.menu.addMenu('Calibration')
self._save = QtGui.QAction('&Save', self)
self.save = self._save
calMenu.addAction(self.save)
self._restore = QtGui.QAction('&Restore', self)
self.restore = self._restore
calMenu.addAction(self._restore)
def controlTab(self):
wcontrols = QtGui.QWidget()
layout = QtGui.QVBoxLayout()
layout.setAlignment(QtCore.Qt.AlignTop)
layout.setSpacing(1)
layout.addWidget(self.dvr)
layout.addWidget(self.video)
layout.addWidget(self.filters)
layout.addWidget(QCGH(self.cgh))
wcontrols.setLayout(layout)
return wcontrols
def trapTab(self):
wtraps = QtGui.QWidget()
layout = QtGui.QVBoxLayout()
layout.setAlignment(QtCore.Qt.AlignTop)
layout.setSpacing(1)
layout.addWidget(QTrapWidget(self.pattern))
wtraps.setLayout(layout)
return wtraps
def handleRecording(self, recording):
self.video.enabled = not recording
def init_configuration(self):
sz = self.fabscreen.video.device.size
self.cgh.rc = (sz.width() / 2, sz.height() / 2, 0.)
sz = self.slm.size()
self.cgh.rs = (sz.width() / 2, sz.height() / 2)
def save_configuration(self):
scgh = self.cgh.serialize()
tn = datetime.datetime.now()
fn = '~/.pyfab/pyfab_{:%Y%b%d_%H:%M:%S}.json'.format(tn)
with io.open(fn, 'w', encoding='utf8') as configfile:
configfile.write(unicode(scgh))
def closeEvent(self, event):
self.save_configuration()
self.slm.close()
self.fabscreen.camera.close()
self.app.closeAllWindows()
self.app.exit()
#QActions
@property
def save(self):
return self._save
@save.setter
def save(self, save):
self._save = save
self._save.setShortcut('Ctrl+S')
self._save.triggered.connect(self.save_configuration)
@property
def restore(self):
return self._restore
@restore.setter
def restore(self, restore):
self._restore = restore
self._restore.setShortcut('Ctrl+R')
#self._restore.triggered.connect(self.)
if __name__ == '__main__':
instrument = pyfab()
sys.exit(app.exec_())