forked from aaron-parsons/i14testgui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
executable file
·308 lines (257 loc) · 10.6 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
'''
A quick gui for the pymca fitting
'''
from content import Content
from PyQt5.QtWidgets import QApplication, QWidget, QFormLayout, QSpinBox, QLineEdit, QDoubleSpinBox, QGridLayout, QLabel,QPushButton, QTextEdit
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer, Qt
import sys
import os
from subprocess import Popen, PIPE
try:
import savu
except:
ImportError("Savu not on the path")
# the following classes could use a refactor since it's all duplication
DEBUG = False
class SavuIntSpinBox(QSpinBox):
'''
widget that does spinners for ints
'''
def __init__(self, parent, key, value):
super(SavuIntSpinBox, self).__init__(parent)
self.parent = parent
# self.widget = QSpinBox(parent)
self.setValue(value)
self.key = key
self.setFixedHeight(3.0*self.fontMetrics().height())
self.setSingleStep(1)
self.valueChanged.connect(self.updatevalue)
def updatevalue(self):
val = str(self.value())
# print val
self.parent.model.modify(self.parent.plugin_number, self.key, val)
class SavuDoubleSpinBox(QDoubleSpinBox):
'''
widget that does spinners for floats
'''
def __init__(self, parent, key, value):
super(SavuDoubleSpinBox, self).__init__(parent)
self.parent = parent
# self.widget = QSpinBox(parent)
self.setValue(value)
self.key = key
self.setFixedHeight(3.0*self.fontMetrics().height())
self.setSingleStep(0.1)
self.valueChanged.connect(self.updatevalue)
def updatevalue(self):
val = str(self.value())
# print val
self.parent.model.modify(self.parent.plugin_number, self.key, val)
class SavuTextBox(QLineEdit):
'''
widget that does text editing
'''
def __init__(self, parent, key, value):
super(SavuTextBox, self).__init__(parent)
self.parent = parent
# self.widget = QSpinBox(parent)
self.setText(str(value))
self.key = key
self.setFixedHeight(3.0*self.fontMetrics().height())
self.textChanged.connect(self.updatevalue)
def updatevalue(self):
value = str(self.text().rstrip('\n'))
# print value
self.parent.model.modify(self.parent.plugin_number, self.key, value)
class SavuLogFileTextBox(QTextEdit):
def __init__(self):
super(SavuLogFileTextBox, self).__init__()
self.setReadOnly(True)
self.filehandle = None
self.filepath = None
self.setDisabled(True)
def setFile(self, filepath):
self.filepath = filepath
def update_text(self):
if self.filepath:
if not os.path.exists(self.filepath):
self.setText("Nothing to display")
elif os.path.exists(self.filepath) and not self.filehandle:
self.filehandle = open(self.filepath,"r")
self.clear()
elif os.path.exists(self.filepath) and self.filehandle:
lines = self.filehandle.readlines()
self.insertPlainText("".join(lines))
self.verticalScrollBar().setValue(self.verticalScrollBar().maximum()) # autoscroll to the bottom to view updated content.
# self.setText(lines)
else:
print "munchkins"
else:
self.setText("Nothing to display")
class PluginForm(QWidget):
'''
widget that does the form display for the plugins
'''
def __init__(self,parent, plugin_number):
super(PluginForm, self).__init__()
self.model = parent.model
self.plugin_list = self.model.plugin_list.plugin_list
self.initUI(plugin_number)
def initUI(self, plugin_number):
self.plugin_number = str(plugin_number + 1) # used in the widgets
form = QFormLayout()
for key, value in self.plugin_list[plugin_number]['data'].iteritems():
if type(value) == float:
form.addRow(key, SavuDoubleSpinBox(self, key, value))
if type(value) == int:
form.addRow(key, SavuIntSpinBox(self, key, value))
if type(value) == str:
form.addRow(key, SavuTextBox(self, key, value))
self.setLayout(form)
# self.resize(250, 250)
# self.move(300, 300)
def getModel(self):
return self.model
class PluginEditor(QWidget):
'''
this does the display for all the plugins including titles and buttons to do with the model
'''
def __init__(self, model):
super(PluginEditor, self).__init__()
layout = QGridLayout()
layout.setColumnStretch(0,3)
layout.setColumnStretch(1,3)
self.model = model
self.plugin_list = self.model.plugin_list.plugin_list
k=0
for plugin_number in range(len(self.plugin_list)):
name = self.plugin_list[plugin_number]['name']
myFont=QFont()
myFont.setBold(True)
myFont.setUnderline(True)
l1 = QLabel()
l1.setText(name)
l1.setFont(myFont)
pluginform = PluginForm(self, plugin_number)
layout.addWidget(l1,k,0)
layout.addWidget(pluginform,k+1,0)
k+=2
save_stuff = SaveDialog(self.model)
layout.addWidget(save_stuff,1,1,k,1)
self.setLayout(layout)
w = 2280; h = 1520
self.resize(w/2,h/2)
self.setWindowTitle('Savu Processing')
self.show()
class SaveDialog(QWidget):
def __init__(self, model):
self.model = model
self.output_dir_exists = None
super(SaveDialog, self).__init__()
self.visit = QLineEdit()
self.visit.setFixedHeight(2.0*self.visit.fontMetrics().height())
self.visit.setText(str(self.getCurrentVisit())) # by default its the current visit for $BEAMLINE
self.save_name = QLineEdit()
self.save_name.setFixedHeight(2.0*self.save_name.fontMetrics().height())
self.save_name.setText(str(self.getInitialProcessList()))
self.scan = QLineEdit()
self.scan.setFixedHeight(2.0*self.scan.fontMetrics().height())
form = QFormLayout()
form.addRow('Visit:',self.visit)
form.addRow('Process List Name:',self.save_name)
self.save_button = QPushButton()
# self.save_button.setCheckable(True)
self.save_button.setText('Save process list')
self.save_button.pressed.connect(self.saveButtonChecked)
form.addWidget(self.save_button)
form.addRow('Scan Number:',self.scan)
self.run_button = QPushButton()
# self.run_button.setCheckable(True)
self.run_button.setText('Run process!')
self.run_button.pressed.connect(self.runButtonChecked)
form.addWidget(self.run_button)
self.log_file_display = SavuLogFileTextBox()
#
form.addWidget(self.log_file_display)
#
self.timer = QTimer()
self.timer.timeout.connect(self.log_file_display.update_text)
self.timer.start(1000)
self.setLayout(form)
def getInitialProcessList(self):
return self.model.filename.split(os.sep)[-1]
def getCurrentVisit(self):
p = Popen(['sh','/dls_sw/apps/mx-scripts/visit_tools/currentvisit',os.environ["BEAMLINE"]], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, __err = p.communicate()
return output
def getVisitDirectory(self):
visit = str(self.visit.text().rstrip('\n').rstrip())
return '/dls/%s/data/2017/%s/' % (str(os.environ['BEAMLINE']),visit)
def getSaveName(self):
return str(self.save_name.text().rstrip('\n').rstrip())
def getOutputDirectory(self):
op = self.getVisitDirectory() + 'processing/savu/'
if not self.output_dir_exists:
print("Creating output directory....")
try:
os.mkdir(op)
except OSError:
pass
except Exception as e:
raise e
print("Done.")
self.output_dir_exists = True
return op
def getScanNumber(self):
scan_number = str(self.scan.text().rstrip('\n').rstrip())
return scan_number
def getDataPath(self):
scan_number = self.getScanNumber()
if str(os.environ['BEAMLINE'])=='i14':
return self.getVisitDirectory()+'i14-%s.nxs' % scan_number
elif str(os.environ['BEAMLINE'])=='i08':
return self.getVisitDirectory()+'nexus/i08-%s.nxs' % scan_number
else:
raise NameError("I don't recognise this beamline!")
def saveButtonChecked(self):
if self.save_button.isDown():
full_save_path = self.getOutputDirectory()+os.sep + self.getSaveName()
self.model.save(full_save_path)
def runButtonChecked(self):
if self.run_button.isDown():
global DEBUG
if DEBUG:
print "I should run something here"
print "On data" + self.getDataPath()
else:
self.runSavu(self.getDataPath(), self.getOutputDirectory()+os.sep + self.getSaveName(), self.getOutputDirectory())
def getSavuOutputDirectory(self):
path = self.getOutputDirectory() + os.sep + self.getScanNumber()+'_'+self.getSaveName().split('.')[0]
return path
def getProcessFolder(self):
return self.getScanNumber() + '_' + self.getSaveName().split('.')[0]
def runSavu(self, datafile, process_list, output_directory):
#launcher_script = savu.savuPath.split('savu')[0]+'mpi/dls/savu_launcher.sh'#
a = Popen(['which','savu_launcher.sh'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
b = a.stdout.readline()
launcher_script = b.splitlines()[0]
print launcher_script
savu_version = '2.0_stable'
p = Popen(['sh',launcher_script,savu_version,datafile,process_list,output_directory,'-f',self.getProcessFolder()], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, __err = p.communicate()
self.log_file_display.clear()
self.log_file_display.filehandle = None # need to reinitialise these for second call.
self.log_file_display.filepath = None # need to reinitialise these
self.log_file_display.setText('Process started. Waiting for log file....')
self.log_file_display.setFile(self.getSavuOutputDirectory()+os.sep+'user.log')
# print "thing the log should be in:", self.getSavuOutputDirectory()+os.sep+'user.log'
def main(process_list):
app = QApplication([])
model = Content()
model.fopen(process_list)
ex = PluginEditor(model)#
sys.exit(app.exec_())
if __name__ == '__main__':
process_list =main(sys.argv[1])
main()