forked from BRAINSia/SlicerQAExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDWIrawQA.py
More file actions
233 lines (212 loc) · 8.72 KB
/
DWIrawQA.py
File metadata and controls
233 lines (212 loc) · 8.72 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
#! /usr/bin/env python
import os
import re
from __main__ import ctk
from __main__ import qt
from __main__ import slicer
from __main__ import vtk
from QALib.dwi_raw import *
from QALib.dwi_raw import __slicer_module__
### TODO: Add logging
# try:
# import logging
# import logging.handlers
# except ImportError:
# print "External modules not found!"
# raise ImportError
class DWIrawQA:
def __init__(self, parent):
parent.title = 'DWI Raw Inspection'
parent.categories = ['Quality Assurance']
parent.dependencies = ['Volumes']
parent.contributors = ['Dave Welch (UIowa)']
parent.helpText = """DWI raw image evaluation module for use in the UIowa PINC lab"""
parent.acknowledgementText = """ """
self.parent = parent
class DWIrawQAWidget:
def __init__(self, parent=None):
self.htmlFileName = ('dwi_raw_1', 'dwi_raw_2',
'dwi_raw_3', 'dwi_raw_4')
self.css = None
self.currentSession = None
self.navigationWidget = None
self.gradientDisplayWidget = None
# Handle the UI display with/without Slicer
if parent is None:
self.parent = slicer.qMRMLWidget()
self.parent.setLayout(qt.QVBoxLayout())
self.parent.setMRMLScene(slicer.mrmlScene)
self.layout = self.parent.layout()
self.logic = DWIRawQALogic(self, False)
self.setup()
self.parent.show()
else:
self.parent = parent
self.layout = self.parent.layout()
self.logic = DWIRawQALogic(self, False)
def setup(self):
# Evaluation subsection
self.imageQAWidget = self.loadUIFile('Resources/UI/queryQACollapsibleButton.ui')
qaLayout = qt.QVBoxLayout(self.imageQAWidget)
qaLayout.addWidget(self.imageQAWidget.findChild("QFrame", "titleFrame"))
qaLayout.addWidget(self.imageQAWidget.findChild("QFrame", "tableVLine"))
# Create review buttons on the fly
for question in self.htmlFileName:
reviewButton = self.reviewButtonFactory(question)
qaLayout.addWidget(reviewButton)
self.enableRadios(question)
# DWI display widget
self.dwiWidget = slicer.modulewidget.qSlicerDiffusionWeightedVolumeDisplayWidget()
qaLayout.addWidget(self.dwiWidget)
# batch button
self.nextButton = qt.QPushButton()
self.nextButton.setText('Get next raw DWI')
self.nextButton.connect('clicked(bool)', self.onGetBatchFilesClicked)
qaLayout.addWidget(self.nextButton)
# Add all to layout
self.layout.addWidget(self.imageQAWidget)
self.layout.addWidget(self.dwiWidget)
self.layout.addStretch(1)
# Get popup window widget
self.gradientDisplayWidget = self.loadUIFile('Resources/UI/followUpDialog.ui')
# Initialize data
self.logic.onGetBatchFilesClicked()
def loadUIFile(self, fileName):
""" Return the object defined in the Qt Designer file """
uiloader = qt.QUiLoader()
qfile = qt.QFile(os.path.join(__slicer_module__, fileName))
qfile.open(qt.QFile.ReadOnly)
try:
return uiloader.load(qfile)
finally:
qfile.close()
def reviewButtonFactory(self, question):
widget = self.loadUIFile('Resources/UI/dwiRawQuestionWidget.ui')
# Set question label text
questionLabel= widget.findChild("QLabel", "questionTextLabel")
questionLabel.objectName = question
questionLabel.setText(self._readHTML(question))
# Set and rename radio buttons
radioContainer = widget.findChild("QWidget", "radioWidget")
radioContainer.objectName = question + "_radioWidget"
yesButton = widget.findChild("QRadioButton", "yesButton")
yesButton.objectName = question + "_yes"
noButton = widget.findChild("QRadioButton", "noButton")
noButton.objectName = question + "_no"
return widget
def _readCSS(self):
fullPath = os.path.join(__slicer_module__,
'Resources/HTML',
'dwi_raw.css')
fID = open(fullPath)
try:
self.css = fID.read()
finally:
fID.close()
def _readHTML(self, question):
if self.css is None:
self._readCSS()
fullPath = os.path.join(__slicer_module__,
'Resources/HTML',
question + '.html')
fID = open(fullPath)
text = ''
try:
text = fID.read()
finally:
fID.close()
if not text == '':
text = re.sub(r'CSS_FILE', self.css, text)
return text
# def connectSessionButtons(self):
# """ Connect the session navigation buttons to their logic """
# self.quitButton.connect('clicked()', self.exit)
def enableRadios(self, question):
""" Enable the radio buttons that match the given region name """
self.imageQAWidget.findChild("QWidget", question + "_radioWidget").setEnabled(True)
for suffix in ("_yes", "_no"):
radio = self.imageQAWidget.findChild("QRadioButton", question + suffix)
radio.setShortcutEnabled(True)
radio.setCheckable(True)
radio.setEnabled(True)
def resetRadioWidgets(self):
""" Disable and reset all radio buttons in the widget """
print "Resetting radio buttons..."
radios = self.imageQAWidget.findChildren("QRadioButton")
for question in self.htmlFileName:
for radio in radios:
if radio.objectName.find(question) > -1 and radio.checked:
# Fix for a bug in QT: see http://qtforum.org/article/19619/qradiobutton-setchecked-bug.html
radio.setCheckable(False)
radio.update()
radio.setCheckable(True)
# This SHOULD reset the autoexclusive radio buttons...
if radio.isChecked():
raise Exception("Radio is NOT reset!")
else:
print "Resetting radio {0}...".format(radio.objectName)
def getRadioValues(self):
values = ()
needsFollowUp = False
radios = self.imageQAWidget.findChildren("QRadioButton")
for question in self.htmlFileName:
for radio in radios:
if radio.objectName.find(question) > -1 and radio.checked:
if radio.objectName.find("_yes") > -1:
values = values + (True,)
elif radio.objectName.find("_no") > -1:
values = values + (False,)
else:
values = values + ("NULL",)
print "Warning: No value for %s" % question
return values
def resetWidget(self):
print "Resetting widgets..."
self.resetRadioWidgets()
self.gradientDisplayWidget.close()
def getValues(self):
values = self.getRadioValues()
return values
def checkValues(self):
values = self.getValues()
if len(values) == len(self.htmlFileName):
self.resetWidget()
return (0, values)
elif len(values) == 0:
print "No values at all!"
return (-1, values)
else:
# TODO: Handle this case intelligently
print "Not enough values for the required columns!"
print values
return (-2, values)
def onGetBatchFilesClicked(self):
(code, values) = self.checkValues()
if code == 0:
self.logic.writeToDatabase(values)
self.gradientDisplayWidget.close()
self.logic.onGetBatchFilesClicked()
else:
pass
def exit(self):
""" When Slicer exits, prompt user if they want to write the last evaluation """
(code, values) = self.checkValues()
if code == 0:
# TODO: Write a confirmation dialog popup
self.logic.writeToDatabase(values)
self.logic.exit()
# self.logic.onGetBatchFilesClicked()
elif code == -1:
self.logic.exit()
else:
# TODO: write a prompt window
self.logic.exit()
# TODO: clear scene
self.gradientDisplayWidget.close()
def displayGradients(self, gradients):
string = '\n'.join(item for item in gradients)
self.gradientDisplayWidget.setWindowTitle('Gradient directions: %s' % self.logic.sessionFile['file'])
editor = self.gradientDisplayWidget.findChild('QTextEdit')
editor.setText(string)
editor.setReadOnly(True)
self.gradientDisplayWidget.show()