forked from BRAINSia/SlicerQAExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DerivedImageQA.py
271 lines (250 loc) · 11.4 KB
/
DerivedImageQA.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
#! /usr/bin/env python
try:
import logging
import logging.handlers
import os
from QALib.derived_images import *
from QALib.derived_images import __slicer_module__
from __main__ import ctk
from __main__ import qt
from __main__ import slicer
from __main__ import vtk
except ImportError:
print "External modules not found!"
# raise ImportError
class DerivedImageQA:
def __init__(self, parent):
parent.title = 'Derived Images'
parent.categories = ['Quality Assurance']
parent.dependencies = ['Volumes']
parent.contributors = ['Dave Welch (UIowa), Hans Johnson (UIowa)']
parent.helpText = """Image evaluation module for use in the UIowa PINC lab"""
parent.acknowledgementText = """ """
self.parent = parent
class DerivedImageQAWidget:
def __init__(self, parent=None, test=False):
logFile = os.path.join(os.environ['TMPDIR'], "DerivedImageQAWidget" + '.log')
logging.basicConfig(filename=logFile,
level=logging.DEBUG,
format='%(module)s.%(funcName)s - %(levelname)s: %(message)s')
self.logging = logging.getLogger(__name__)
self.images = ('t2_average', 't1_average') # T1 is second so that reviewers see it as background for regions
self.regions = ('labels_tissue',
'caudate_left', 'caudate_right',
'accumben_left', 'accumben_right',
'putamen_left', 'putamen_right',
'globus_left', 'globus_right',
'thalamus_left', 'thalamus_right',
'hippocampus_left', 'hippocampus_right')
self.currentSession = None
self.imageQAWidget = None
self.navigationWidget = None
self.followUpDialog = None
self.notes = None
# Handle the UI display with/without Slicer
if parent is None and not test:
self.parent = slicer.qMRMLWidget()
self.parent.setLayout(qt.QVBoxLayout())
self.parent.setMRMLScene(slicer.mrmlScene)
self.layout = self.parent.layout()
self.logic = DerivedImageQALogic(self)
self.setup()
self.parent.show()
elif not test:
self.parent = parent
self.layout = self.parent.layout()
self.logic = DerivedImageQALogic(self, test=test)
elif test:
self.logic = DerivedImageQALogic(self, test=test)
def setup(self):
self.logging.debug("call")
self.followUpDialog = self.loadUIFile('Resources/UI/followUpDialog.ui')
self.clipboard = qt.QApplication.clipboard()
self.textEditor = self.followUpDialog.findChild("QTextEdit", "textEditor")
buttonBox = self.followUpDialog.findChild("QDialogButtonBox", "buttonBox")
buttonBox.connect("accepted()", self.grabNotes)
buttonBox.connect("rejected()", self.cancelNotes)
# Batch navigation
self.navigationWidget = self.loadUIFile('Resources/UI/navigationCollapsibleButton.ui')
nLayout = qt.QVBoxLayout(self.navigationWidget)
# TODO: Fix batch list sizing
### nLayout.addWidget(self.navigationWidget.findChild("QLabel", "batchLabel"))
### nLayout.addWidget(self.navigationWidget.findChild("QListWidget", "batchList"))
nLayout.addWidget(self.navigationWidget.findChild("QWidget", "buttonContainerWidget"))
# Find navigation buttons
self.previousButton = self.navigationWidget.findChild("QPushButton", "previousButton")
self.quitButton = self.navigationWidget.findChild("QPushButton", "quitButton")
self.nextButton = self.navigationWidget.findChild("QPushButton", "nextButton")
self.connectSessionButtons()
# Evaluation subsection
self.imageQAWidget = self.loadUIFile('Resources/UI/imageQACollapsibleButton.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 image in self.images + self.regions:
reviewButton = self.reviewButtonFactory(image)
qaLayout.addWidget(reviewButton)
self.connectReviewButtons()
# resetButton
self.resetButton = qt.QPushButton()
self.resetButton.setText('Reset evaluation')
self.resetButton.connect('clicked(bool)', self.resetWidget)
# batch button
self.batchButton = qt.QPushButton()
self.batchButton.setText('Get evaluation batch')
self.batchButton.connect('clicked(bool)', self.onGetBatchFilesClicked)
# Add all to layout
self.layout.addWidget(self.navigationWidget)
nLayout.addWidget(self.resetButton)
nLayout.addWidget(self.batchButton)
self.layout.addWidget(self.imageQAWidget)
self.layout.addStretch(1)
### TESTING ###
if True:
self.logging.debug("Gui calling logic.onGetBatchFilesClicked()")
self.logic.onGetBatchFilesClicked()
### END ###
def loadUIFile(self, fileName):
""" Return the object defined in the Qt Designer file """
self.logging.debug("call")
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, image):
self.logging.debug("call")
widget = self.loadUIFile('Resources/UI/reviewButtonsWidget.ui')
# Set push button
pushButton = widget.findChild("QPushButton", "imageButton")
pushButton.objectName = image
pushButton.setText(self._formatText(image))
radioContainer = widget.findChild("QWidget", "radioWidget")
radioContainer.objectName = image + "_radioWidget"
# Set radio buttons
goodButton = widget.findChild("QRadioButton", "goodButton")
goodButton.objectName = image + "_good"
badButton = widget.findChild("QRadioButton", "badButton")
badButton.objectName = image + "_bad"
followUpButton = widget.findChild("QRadioButton", "followUpButton")
followUpButton.objectName = image + "_followUp"
return widget
def _formatText(self, text):
self.logging.debug("call")
parsed = text.split("_")
if len(parsed) > 1:
text = " ".join([parsed[1].capitalize(), parsed[0]])
else:
text = parsed[0].capitalize()
return text
def connectSessionButtons(self):
""" Connect the session navigation buttons to their logic """
self.logging.debug("call")
# TODO: Connect buttons
### self.nextButton.connect('clicked()', self.logic.onNextButtonClicked)
### self.previousButton.connect('clicked()', self.logic.onPreviousButtonClicked)
self.quitButton.connect('clicked()', self.exit)
def connectReviewButtons(self):
""" Map the region buttons clicked() signals to the function """
self.logging.debug("call")
self.buttonMapper = qt.QSignalMapper()
self.buttonMapper.connect('mapped(const QString&)', self.logic.selectRegion)
self.buttonMapper.connect('mapped(const QString&)', self.enableRadios)
for image in self.images + self.regions:
pushButton = self.imageQAWidget.findChild('QPushButton', image)
self.buttonMapper.setMapping(pushButton, image)
pushButton.connect('clicked()', self.buttonMapper, 'map()')
def enableRadios(self, image):
""" Enable the radio buttons that match the given region name """
self.logging.debug("call")
self.imageQAWidget.findChild("QWidget", image + "_radioWidget").setEnabled(True)
for suffix in ("_good", "_bad", "_followUp"):
radio = self.imageQAWidget.findChild("QRadioButton", image + suffix)
radio.setShortcutEnabled(True)
radio.setCheckable(True)
radio.setEnabled(True)
def disableRadios(self, image):
""" Disable all radio buttons that DO NOT match the given region name """
self.logging.debug("call")
radios = self.imageQAWidget.findChildren("QRadioButton")
for radio in radios:
if radio.objectName.find(image) == -1:
radio.setShortcutEnabled(False)
radio.setEnabled(False)
def resetRadioWidgets(self):
""" Disable and reset all radio buttons in the widget """
self.logging.debug("call")
radios = self.imageQAWidget.findChildren("QRadioButton")
for radio in radios:
radio.setCheckable(False)
radio.setEnabled(False)
def getRadioValues(self):
self.logging.debug("call")
values = ()
needsFollowUp = False
radios = self.imageQAWidget.findChildren("QRadioButton")
for image in self.images + self.regions:
for radio in radios:
if radio.objectName.find(image) > -1 and radio.checked:
if radio.objectName.find("_good") > -1:
values = values + (1,)
elif radio.objectName.find("_bad") > -1:
values = values + (0,)
elif radio.objectName.find("_followUp") > -1:
values = values + (-1,)
needsFollowUp = True
else:
values = values + ("NULL",)
print "Warning: No value for %s" % image
if needsFollowUp:
self.followUpDialog.exec_()
if self.followUpDialog.result() and not self.notes is None:
values = values + (self.notes,)
else:
values = values + ("NULL",)
else:
values = values + ("NULL",)
return values
def resetWidget(self):
self.logging.debug("call")
self.resetRadioWidgets()
def grabNotes(self):
self.logging.debug("call")
self.notes = None
self.notes = str(self.textEditor.toPlainText())
# TODO: Format notes
### if self.notes = '':
### self.followUpDialog.show()
### self.textEditor.setText("A comment is required!")
self.textEditor.clear()
def cancelNotes(self):
self.logging.debug("call")
# TODO:
pass
def onGetBatchFilesClicked(self):
self.logging.debug("call")
values = self.getRadioValues()
if len(values) >= len(self.images + self.regions):
self.logic.writeToDatabase(values)
self.resetWidget()
self.logic.onGetBatchFilesClicked()
else:
# TODO: Handle this case intelligently
self.logging.warning("Not enough values for the required columns!")
def exit(self):
""" When Slicer exits, prompt user if they want to write the last evaluation """
self.logging.debug("call")
values = self.getRadioValues()
if len(values) >= len(self.images + self.regions):
# TODO: Write a confirmation dialog popup
self.logic.writeToDatabase(values)
elif len(values) == 0:
self.logic.exit()
else:
# TODO: write a prompt window
print "Not enough values for the required columns!"
self.logic.exit()
# TODO: clear scene