-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipelineApp.py
More file actions
477 lines (411 loc) · 18 KB
/
PipelineApp.py
File metadata and controls
477 lines (411 loc) · 18 KB
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
import sys, os, argparse
## from multiprocessing import Process
from Scripts.Util import Debug, StatusChecker, FetchFiles
from Scripts import niftiToBids, runPipeline, runQC, setupPipeline, addLowBToBIDS#, qualityDistributions
from PyQt6.QtWidgets import (
QApplication, QMainWindow, QLabel, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QDialog,
QSizePolicy, QToolBar, QTextEdit, QStackedWidget, QComboBox, QDialogButtonBox#, QGraphicsScene, QSpinBox
)
from PyQt6.QtGui import QFont, QPixmap
from PyQt6.QtCore import Qt
# Dev Options:
VERSION = '''
0.4.0
'''
# End Dev Options
# class DSIButton():
# def __init__(self, qButton: QPushButton, name: str, action: function):
# self.name = name
# self.button = qButton
# self.action = action
# def doAction(self):
# print(f'DEBUG: Beginning action: {self.action} for button named: {self.name}')
# self.action()
class WarningPopUp(QDialog):
def __init__(self, parent=None)->None:
super().__init__(parent)
self.setWindowTitle(f'WARNING')
self.buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ignore | QDialogButtonBox.StandardButton.Cancel)
self.buttons.accepted.connect(self.accept)
self.buttons.rejected.connect(self.reject)
layout = QVBoxLayout()
warningMessage = QLabel(f'It is NOT recommended to update your image files if analysis has been performed already.\nIf you proceed, the old image files will be renamed. not overwritten.')
layout.addWidget(warningMessage)
layout.addWidget(self.buttons)
self.setLayout(layout)
class MainWindow(QMainWindow):
actionButtons:list[QPushButton] = []
def __init__(self):
super().__init__()
self.setWindowTitle(f"DSI Studio Pipeline")
self.Xstart = 150
self.WIDTH = 1500
self.Ystart = 100
self.HEIGHT = 750
self.setGeometry(self.Xstart, self.Ystart, self.WIDTH, self.HEIGHT)
self.makeToolbar() # makes toolbar at top of screen
self.centralStack = QStackedWidget()
self.setCentralWidget(self.centralStack)
self.centralStack.addWidget(self.MakeFunctionalWidget()) # makes central widget, status and buttons
self.centralStack.addWidget(self.MakeVisualisationWidget())
"#617977"
for button in self.actionButtons:
#button.clicked.connect(self.updateStatus)
button.setProperty("class", 'functionalButton')
self.setStyleSheet('''
QPushButton[class="functionalButton"]:disabled {
background: #617977;
}
QPushButton[class="functionalButton"]:hover {
background: #002E2E
}
''')
### END __init__()
def setupButtonClick(self)->None:
warningResponse = WarningPopUp()
if not warningResponse.exec():
return
self.label.setText("Attempting to set up directories...")
self.makeButtonInactive(self.setupButton)
self.setupAction() # determined when button is initialized
self.label.setText("Setup complete!")
def niftiButtonClick(self)->None:
self.clearExecute()
self.execButton.clicked.connect(self.niftiExecute)
self.displayRegion.clear()
self.displayRegion.append("SELECTED NIFTI TO BIDS\n")
self.displayRegion.append("The following ID's will be converted to BIDS format:")
source, ids, target = StatusChecker.niftiStatus()
Debug.Log(StatusChecker.niftiStatus())
for id in ids:
self.displayRegion.append(id)
def niftiExecute(self)->None:
self.label.setText("Moving nifti files to BIDS format...")
self.makeButtonInactive(self.niftiButton)
restoreThese = self.timeOutButtons()
# proc = Process(target=niftiToBids.NiftiToBIDS)
# proc.start()
# proc.join()
niftiToBids.NiftiToBIDS()
self.label.setText("BIDS re-format complete!")
self.restoreTimedOutButtons(restoreThese)
self.clearExecute()
def mriqcButtonClick(self)->None:
self.clearExecute()
self.execButton.clicked.connect(self.mriqcButtonExecute)
self.displayRegion.clear()
self.displayRegion.append("SELECTED MRIQC\n")
self.displayRegion.append("The following ID's will be evaluated by MRIQC:")
source, ids, target = StatusChecker.qcStatus()
Debug.Log(StatusChecker.qcStatus())
for id in ids:
self.displayRegion.append(id)
def mriqcButtonExecute(self)->None:
self.label.setText("Running MRIQC...")
self.makeButtonInactive(self.mriqcButton)
restoreThese = self.timeOutButtons()
# proc = Process(target=runQC.RunMRIQC)
# proc.start()
# proc.join()
runQC.RunMRIQC()
self.label.setText("MRIQC Complete!")
self.restoreTimedOutButtons(restoreThese)
self.clearExecute()
def srcButtonClick(self)->None:
self.clearExecute()
self.execButton.clicked.connect(self.srcButtonExecute)
self.displayRegion.clear()
self.displayRegion.append("SELECTED SRC\n")
self.displayRegion.append("The following ID's will go through Dsi Studio's SRC action:")
source, ids, target = StatusChecker.srcStatus()
Debug.Log(StatusChecker.srcStatus())
for id in ids:
self.displayRegion.append(id)
def srcButtonExecute(self)->None:
self.label.setText("Running SRC action")
self.makeButtonInactive(self.srcButton)
restoreThese = self.timeOutButtons()
# proc = Process(target=runPipeline.RunSRC)
# proc.start()
# proc.join()
runPipeline.RunSRC()
self.label.setText("SRC Complete!")
self.restoreTimedOutButtons(restoreThese)
self.clearExecute()
def recButtonClick(self)->None:
self.clearExecute()
self.execButton.clicked.connect(self.recButtonExecute)
self.displayRegion.clear()
self.displayRegion.append("SELECTED REC\n")
self.displayRegion.append("The following ID's will go through Dsi Studio's REC action:")
source, ids, target = StatusChecker.recStatus()
Debug.Log(StatusChecker.recStatus())
for id in ids:
self.displayRegion.append(id)
def recButtonExecute(self)->None:
self.label.setText("Running REC action")
self.makeButtonInactive(self.recButton)
restoreThese = self.timeOutButtons()
# proc = Process(target=runPipeline.RunREC)
# proc.start()
# proc.join()
runPipeline.RunREC()
self.label.setText("REC Complete!")
self.restoreTimedOutButtons(restoreThese)
self.clearExecute()
def clearExecute(self)->None:
'''Disconnects Execute Button from any click signals, if connected.'''
try:
self.execButton.disconnect()
except Exception as e:
Debug.Log(f'{e}\n\tAttempet to disconnect execute button.', DEBUG)
def refreshAll(self)->None:
for b in self.actionButtons:
b.setDisabled(False)
b.setToolTip(None)
self.clearExecute()
self.label.setText('All buttons are reset.')
self.displayRegion.clear()
return
def flipLowB(self)->None:
result = addLowBToBIDS.FlipLOWBLocation()
self.label.setText(result)
return
def toggleDisplay(self)->None:
textOptions = ['Quality\nGraphs', 'Pipeline\nFunctions']
newIndex = abs(self.centralStack.currentIndex() - 1)
self.centralStack.setCurrentIndex(newIndex)
self.visualiserButton.setText(textOptions[newIndex])
return
def timeOutButtons(self)->list:
timedOut = []
for button in self.actionButtons:
if button.isEnabled():
timedOut.append(button)
button.setDisabled(True)
button.setToolTip('Action in progress, please wait.')
return timedOut
def restoreTimedOutButtons(self, timedOut:list[QPushButton])->None:
for button in timedOut:
button.setDisabled(False)
button.setToolTip(None)
def updateStatus(self)->None:
Debug.Log(f'Function not added', DEBUG)
def makeToolbar(self)->None:
# making toolbar widget
toolbar = QToolBar("ToolBox")
toolbar.setMovable(False)
self.addToolBar(Qt.ToolBarArea.TopToolBarArea, toolbar)
spacer = QWidget()
spacer.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Preferred)
toolbar.addWidget(spacer)
# buttons
self.refreshButton = QPushButton("Refresh")
self.refreshButton.clicked.connect(self.refreshAll)
self.refreshButton.setFixedSize(70, 45)
self.flipLowBButton = QPushButton("Flip\nLow B")
self.flipLowBButton.clicked.connect(self.flipLowB)
self.flipLowBButton.setFixedSize(80, 45)
self.visualiserButton = QPushButton("Quality\nGraphs")
self.visualiserButton.clicked.connect(self.toggleDisplay)
self.visualiserButton.setFixedSize(80, 45)
toolbar.setStyleSheet("""
QToolBar {
background: #323D3D;
border: 1px solid #555;
spacing: 5px;
}
QPushButton {
background: #004242;
padding: 5px;
}
QPushButton:hover {
background: #002E2E;
}
""")
toolbar.addWidget(self.visualiserButton)
toolbar.addWidget(self.flipLowBButton)
toolbar.addWidget(self.refreshButton)
return
def handleFigureTypeSelection(self, newType:str)->None:
self.possibleFigures = self.figurePaths[newType]
self.currFigureIndex = 0
self.drawFigure(self.currFigureIndex)
Debug.Log(f'{newType}')
self.fillMeasurePullDown()
#self.indexController.setMaximum(len(self.possibleFigures))
pass
def drawFigure(self, index:int)->None:
Debug.Log(f"drawing... {index}", DEBUG)
# if index >= len(self.possibleFigures):
# Debug.Log(f"Attempted index was out of range. Resetting to zero.")
# self.currFigureIndex = 0
# index = 0
try:
self.imagePixmap = QPixmap(self.possibleFigures[index])
self.imageDisplayArea.setPixmap(self.imagePixmap)
except Exception as e:
Debug.Log(f'No images in Figures/ directory', DEBUG)
self.imagePixmap = QPixmap()
def fillMeasurePullDown(self)->None:
self.measurePullDown.clear()
for i, path in enumerate(self.possibleFigures):
tokens = os.path.basename(path).split('_distribution')
currMeasure = tokens[0]
if 'dwi_' in currMeasure:
currMeasure = currMeasure.replace('dwi_', '')
measureLabel = currMeasure.upper()
self.measurePullDown.insertItem(i, measureLabel)
def MakeVisualisationWidget(self)->QWidget:
visWidget = QWidget()
layout = QHBoxLayout(visWidget)
self.controlLabel = QLabel("Controls", self)
self.controlLabel.setFont(QFont("Serif", 20))
self.controlLabel.setStyleSheet(
"color: #00695B;"
"font-weight: bold;"
)
controls = QVBoxLayout()
controls.addWidget(self.controlLabel)
#T1Results, T2Results = qualityDistributions.RunAnatomical()
#FuncResults = qualityDistributions.RunFunctional()
t1Paths, t2Paths, dwiPaths = FetchFiles.FetchFigures()
self.figurePaths = {
'T1w': t1Paths,
'T2w': t2Paths,
'dwi': dwiPaths
}
self.typePullDown = QComboBox()
for i, figType in enumerate(self.figurePaths.keys()):
self.typePullDown.insertItem(i, figType)
self.typePullDown.currentTextChanged.connect(self.handleFigureTypeSelection)
self.possibleFigures = self.figurePaths[self.typePullDown.currentText()]
self.currFigureIndex = 0
self.drawFigure(self.currFigureIndex)
controls.addWidget(self.typePullDown)
self.measurePullDown = QComboBox()
self.fillMeasurePullDown()
self.measurePullDown.currentIndexChanged.connect(self.drawFigure)
controls.addWidget(self.measurePullDown)
# self.indexController = QSpinBox() # initialized in handle type selection method
# self.indexController.setMinimum(0)
# self.indexController.setMaximum(len(self.possibleFigures))
# self.indexController.valueChanged.connect(self.drawFigure)
# controls.addWidget(self.indexController)
try:
self.imageDisplayArea = QLabel()
self.imagePixmap = QPixmap(self.possibleFigures[0])
self.imageDisplayArea.setPixmap(self.imagePixmap)
except IndexError:
Debug.Log(f'No figures found for initialization.', DEBUG)
controls.setAlignment(Qt.AlignmentFlag.AlignVCenter)
self.imageDisplayArea.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addLayout(controls, stretch=1)
layout.addWidget(self.imageDisplayArea, stretch=4)
return visWidget
def MakeFunctionalWidget(self)->QWidget:
centralWidgetFunctions = QWidget()
#self.setCentralWidget(centralWidgetFunctions)
layout = QHBoxLayout(centralWidgetFunctions) # main layout
self.label = QLabel("Welcome!", self)
self.label.setFont(QFont("Serif", 20))
self.label.setStyleSheet(
"color: #00695B;"
"font-weight: bold;"
)
# making vertical buttons widget
vButtons = QVBoxLayout()
vButtons.addWidget(self.label)
### make setup button
if os.path.isdir('convertToBids'):
# only refresh images if directory setup already is complete
self.setupButton = QPushButton("Pull Singularity images (create or update)")
self.setupAction = setupPipeline.UpdateImages
else:
Debug.Log(f'Directories have not yet been set up. Doing that now...', DEBUG)
self.setupButton = QPushButton("Set up directories")
self.setupAction = setupPipeline.main
self.setupButton.clicked.connect(self.setupButtonClick)
self.setupButton.setFixedSize(int(.6*self.WIDTH), int(.1*self.HEIGHT))
vButtons.addWidget(self.setupButton)
self.actionButtons.append(self.setupButton)
### make nifti button
self.niftiButton = QPushButton("Move nifti files to BIDS directory")
self.niftiButton.clicked.connect(self.niftiButtonClick)
self.niftiButton.setFixedSize(int(.6*self.WIDTH), int(.1*self.HEIGHT))
vButtons.addWidget(self.niftiButton)
self.actionButtons.append(self.niftiButton)
### make mriqc button
self.mriqcButton = QPushButton("Run MRIQC for anatomical data")
self.mriqcButton.clicked.connect(self.mriqcButtonClick)
self.mriqcButton.setFixedSize(int(.6*self.WIDTH), int(.1*self.HEIGHT))
vButtons.addWidget(self.mriqcButton)
self.actionButtons.append(self.mriqcButton)
### make src button
self.srcButton = QPushButton("Run DSI Studio src action for diffusion data")
self.srcButton.clicked.connect(self.srcButtonClick)
self.srcButton.setFixedSize(int(.6*self.WIDTH), int(.1*self.HEIGHT))
vButtons.addWidget(self.srcButton)
self.actionButtons.append(self.srcButton)
### make rec button
self.recButton = QPushButton("Run DSI Studio rec action for diffusion data")
self.recButton.clicked.connect(self.recButtonClick)
self.recButton.setFixedSize(int(.6*self.WIDTH), int(.1*self.HEIGHT))
vButtons.addWidget(self.recButton)
self.actionButtons.append(self.recButton)
### make run button
self.execButton = QPushButton("Execute Selected")
#self.execButton.clicked.connect(self.updateStatus)
self.execButton.setFixedSize(int(.25*self.width()), int(.125*self.height()))
self.execButton.setObjectName("ExecButton")
execButtonStyle = """
#ExecButton {
background-color: #1D5357;
border: none;
color: white;
border-radius: 5px;
padding: 10px 20px;
}
#ExecButton:hover {
background-color: #3CA9B1;
}
"""
"#3CA9B1"
self.execButton.setStyleSheet(execButtonStyle)
vButtons.addWidget(self.execButton, alignment=Qt.AlignmentFlag.AlignHCenter)
self.displayRegion = QTextEdit()
self.displayRegion.setReadOnly(True)
layout.addLayout(vButtons, stretch=2)
layout.addWidget(self.displayRegion, stretch=1)
### end buttons
vButtons.setAlignment(Qt.AlignmentFlag.AlignCenter)
#self.createMenuBar()
self.statusBar().showMessage(f"Running DSI Studio Pipeline Interface App Version: {VERSION}")
Debug.Log(len(self.findChildren(QPushButton)), DEBUG)
return centralWidgetFunctions
def makeButtonInactive(self, button:QPushButton)->None:
'''
Disable any QPushButton
'''
button.setDisabled(True)
button.setToolTip("Action already complete!")
def createMenuBar(self)->None:
'''
Add menu bar dropdowns and tools here.
'''
menuBar = self.menuBar()
fileMenu = menuBar.addMenu("&File")
exitAction = fileMenu.addAction("Exit")
exitAction.triggered.connect(self.close)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--debug', action='store_true', help='flag to print out debugging statements to terminal')
args = parser.parse_args()
global DEBUG
DEBUG = args.debug
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()