-
Notifications
You must be signed in to change notification settings - Fork 0
/
mr_magic.py
306 lines (246 loc) · 10.6 KB
/
mr_magic.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
"""
General purpose MRI image display.
**Classes:**
* MainWindow: Core GUI element for MR Magic.
**Dependencies:**
**Standard modules:**
* numpy: Primary math and processing
* nmrglue: Varian FID importer, needs to be the modified version
* PyQt4: The GUI is built on this
**Custom modules, general purpose:**
* filters: Builds the various filters and masks used to process images
**GUI element modules:**
* main_window: Machine generated class for the main windos
* startCMPUI: GUI for setting the colormaps of the images
* filter_config_class: GUI to create and manage the filter stack
.. moduleauthor:: Neal Hollingsworth
Main MR Magic todo List
_______________________
.. todo:: Add procpar explorer
.. todo:: Add GE pFile importer
.. todo:: Add SNR comutation
.. todo:: Add configuration window
.. todo:: Add save configuration
"""
import sys
import os
import platform
import numpy as np
import nmrglue as ng
from PyQt4 import QtGui, QtCore
import filters as filt
from main_window import Ui_MainWindow
import startCMPUI
from filter_config_class import FilterConfig
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
"""
The primary GUI element
"""
dic = [] # sequence descriptor
data = [] # raw data
datafilt = [] # filtered data
image = [] # filtered reconstructed image
aspectRatio = 1 # aspect ratio of the image
subwindow = None # dummy for any popup menus
filterStack = [] # stack of filters
# color maps for the different images
CMaps = {'kspace': 'gray',
'kphase': 'gist_rainbow',
'mag': 'gray',
'phase': 'gist_rainbow'}
def __init__(self, Parent=None):
"""
Setup the main window GUI
"""
QtGui.QMainWindow.__init__(self, Parent)
self.setupUi(self)
# connect the open and colormap menu items
self.actionFID.triggered.connect(self._openFID)
self.actionColor_Maps.triggered.connect(self._setCMaps)
self.actionConfigureFilters.triggered.connect(self._filtConfigure)
self.actionKspace.triggered.connect(self.kspace.setContrast)
self.actionKspace_Phase.triggered.connect(self.kspacePhase.setContrast)
self.actionMagnitude.triggered.connect(self.magnitudeImage.setContrast)
self.actionPhase_Map.triggered.connect(self.phaseImage.setContrast)
self.actionExit.triggered.connect(self.exitProg)
# Connect the show/hide control for the sub-windows
self.actionData_Explorer.triggered.connect(self.dataExplorerToggle)
self.actionMagnitude_Image.triggered.connect(self.magnitudeImageToggle)
self.actionPhase_Image.triggered.connect(self.phaseImageToggle)
self.actionK_space.triggered.connect(self.kspaceToggle)
self.actionK_space_Phase.triggered.connect(self.kphaseToggle)
# Connect control for marking the plots
self.clearIButton.clicked.connect(self.clearImageMarkers)
self.clearKButton.clicked.connect(self.clearKspaceMarkers)
self.magnitudeImage.clearMarks.connect(self.clearImageMarkers)
self.phaseImage.clearMarks.connect(self.clearImageMarkers)
self.kspace.clearMarks.connect(self.clearKspaceMarkers)
self.kspacePhase.clearMarks.connect(self.clearKspaceMarkers)
# Connect the click signals from the plots
self.magnitudeImage.setMark.connect(self._plotClick)
self.phaseImage.setMark.connect(self._plotClick)
self.kspace.setMark.connect(self._plotClick)
self.kspacePhase.setMark.connect(self._plotClick)
# Default to not showing a few subwindows
self.dataExplorerDock.hide()
self.kspacePhaseDock.hide()
self.phaseDock.hide()
self.phaseImage.maskable = True
self.phaseImage.toggleMask.connect(self.maskPhase)
self.magnitudeImage.maskable = True
self.magnitudeImage.toggleMask.connect(self.maskImage)
@QtCore.pyqtSlot()
def _openFID(self):
""" QT slot that launches the files selector to open a Varian FID file
Selecting the top level \*.fid folder will open it correctly, loading
both the image data and the procpar file.
"""
# launch the folder selection dialog
if platform.system() == 'Windows':
home = os.environ['USERPROFILE']
elif platform.system() == 'Linux':
home = os.environ['HOME']
FID = QtGui.QFileDialog.getExistingDirectory(caption="Open FID",
directory=home,
options=QtGui.QFileDialog.ShowDirsOnly)
# Open the FID file
if FID:
self.dic, self.data = ng.varian.read(str(FID))
temp = (float(self.dic['procpar']['lpe']['values'][0]) /
float(self.dic['procpar']['lro']['values'][0]))
self.aspectRatio = temp*(2*float(
self.dic['procpar']['nv']['values'][0]) /
float(self.dic['procpar']['np']['values'][0]))
# Add a DC offset corection filter
self.filterStack.insert(0, filt.makeDCO(Size=10))
# apply any default filters and recon
self.updateAll()
@QtCore.pyqtSlot()
def _filtConfigure(self):
""" QT slot that opens the Filter configuration window """
self.subwindow = FilterConfig(Dim=np.shape(self.data),
FilterStack=self.filterStack)
if self.subwindow.exec_():
self.filterStack = self.subwindow.filterStack
self.updateAll()
@QtCore.pyqtSlot()
def _setCMaps(self):
""" QT slot that opens the Color Map Configuration window"""
self.subwindow = startCMPUI.startCMPUI(self.CMaps)
if self.subwindow.exec_(): # on exit, set the new color maps
self.CMaps = self.subwindow.CMaps
self.updateAll()
def updateAll(self):
"""
Refreshes all of the displays with the current data and color maps
as well as applying the filter stack.
"""
if self.data == []:
return
# apply all of the filters
self.datafilt = filt.applyStack(self.data, self.filterStack)
self.image = np.fft.ifftshift(np.fft.ifft2(
np.fft.ifftshift(self.datafilt)))
# Display the kspace data
self.kspace.imshow(np.abs(self.datafilt), self.CMaps['kspace'],
self.aspectRatio)
# display the phase data
self.kspacePhase.imshow(np.angle(self.datafilt), self.CMaps['kphase'],
self.aspectRatio)
# display the Image
self.magnitudeImage.imshow(np.abs(self.image), self.CMaps['mag'],
self.aspectRatio)
# display the phase map
self.phaseImage.imshow(np.angle(self.image), self.CMaps['phase'],
self.aspectRatio)
@QtCore.pyqtSlot(int)
def dataExplorerToggle(self):
""" QT slot that toggles the data explorer display"""
if self.dataExplorerDock.isVisible():
self.dataExplorerDock.hide()
else:
self.dataExplorerDock.show()
@QtCore.pyqtSlot()
def magnitudeImageToggle(self):
""" QT slot that toggles the image display"""
if self.magnitudeDock.isVisible():
self.magnitudeDock.hide()
else:
self.magnitudeDock.show()
@QtCore.pyqtSlot()
def phaseImageToggle(self):
""" QT slot that toggles the phase display"""
if self.phaseDock.isVisible():
self.phaseDock.hide()
else:
self.phaseDock.show()
@QtCore.pyqtSlot()
def kspaceToggle(self):
""" QT slot that toggles the k-space display"""
if self.kspaceDock.isVisible():
self.kspaceDock.hide()
else:
self.kspaceDock.show()
@QtCore.pyqtSlot()
def kphaseToggle(self):
""" QT slot that toggles the k-space phase display"""
if self.kspacePhaseDock.isVisible():
self.kspacePhaseDock.hide()
else:
self.kspacePhaseDock.show()
@QtCore.pyqtSlot(int)
def _plotClick(self, Event, DataPoint, PlotPoint):
""" QT slot that handles clicks to place markers on paired plots"""
if (Event.canvas == self.magnitudeImage):
self.iMag.setText(str(np.abs(self.image[DataPoint])))
self.iPhase.setText(str(np.angle(self.image[DataPoint])))
self.phaseImage.addMarker(PlotPoint)
elif (Event.canvas == self.phaseImage):
self.iMag.setText(str(np.abs(self.image[DataPoint])))
self.iPhase.setText(str(np.angle(self.image[DataPoint])))
self.magnitudeImage.addMarker(PlotPoint)
elif (Event.canvas == self.kspace):
self.kMag.setText(str(np.abs(self.datafilt[DataPoint])))
self.kPhase.setText(str(np.angle(self.datafilt[DataPoint])))
self.kspacePhase.addMarker(PlotPoint)
elif (Event.canvas == self.kspacePhase):
self.kMag.setText(str(np.abs(self.datafilt[DataPoint])))
self.kPhase.setText(str(np.angle(self.datafilt[DataPoint])))
self.kspace.addMarker(PlotPoint)
@QtCore.pyqtSlot(int)
def clearImageMarkers(self):
""" QT slot that clears all markers from the image space plots """
self.magnitudeImage.clearMarkers()
self.phaseImage.clearMarkers()
self.iMag.setText(' ')
self.iPhase.setText(' ')
@QtCore.pyqtSlot(int)
def clearKspaceMarkers(self):
""" QT slot that clears all markers from the k-space plots """
self.kspace.clearMarkers()
self.kspacePhase.clearMarkers()
self.kMag.setText(' ')
self.kPhase.setText(' ')
@QtCore.pyqtSlot()
def maskPhase(self):
""" QT slot that toggles the mask for the phase map """
if self.phaseImage.mask is None:
self.phaseImage.setMask(Mask=filt.mask(self.image))
else:
self.phaseImage.setMask(None)
@QtCore.pyqtSlot()
def maskImage(self):
""" QT slot that toggles the mask for the image """
if self.magnitudeImage.mask is None:
self.magnitudeImage.setMask(Mask=filt.mask(self.image))
else:
self.magnitudeImage.removeMask()
@QtCore.pyqtSlot(int)
def exitProg(self):
""" QT slot that exits the program"""
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = MainWindow()
myapp.show()
sys.exit(app.exec_())