Created
January 30, 2016 01:10
-
-
Save kingjr/bb9e732fe8df6a8cd571 to your computer and use it in GitHub Desktop.
This allows plotting an MRI interactively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from nilearn.plotting.img_plotting import _load_anat | |
fname = '/home/jrking/nilearn_data/haxby2001/subj1/anat.nii.gz' | |
class MRI_viewer(): | |
def __init__(self, fname): | |
# setup figure | |
fig, axes = plt.subplots(1, 3) | |
self.fig = fig | |
self.axes = axes | |
# setup mri | |
anat_img, black_bg, anat_vmin, anat_vmax = _load_anat(fname) | |
self.anat_img = anat_img | |
ny, nx, nz = np.shape(anat_img.dataobj) | |
self.x = 50 | |
self.y = 50 | |
self.z = 50 | |
self.ims = [[], [], []] | |
self.sagittal = axes[0].matshow(anat_img.dataobj[self.y, :, :].T, | |
extent=[0, nx, 0, nz], | |
aspect='auto', origin='lower', | |
cmap='gray') | |
self.coronal = axes[1].matshow(anat_img.dataobj[:, self.x, :].T, | |
extent=[0, ny, 0, nx], | |
aspect='auto', origin='lower', | |
cmap='gray') | |
self.axial = axes[2].matshow(anat_img.dataobj[:, :, self.z].T, | |
extent=[0, ny, 0, ny], | |
aspect='auto', origin='lower', | |
cmap='gray') | |
# setup cursor | |
self.lh = dict() | |
self.lv = dict() | |
for view, ax in zip(['sagittal', 'coronal', 'axial'], axes): | |
self.lv[view] = ax.axvline(color='r', zorder=20) | |
self.lh[view] = ax.axhline(color='r', zorder=20) | |
# interaction | |
fig.canvas.callbacks.connect('motion_notify_event', self.on_move) | |
fig.canvas.callbacks.connect('button_press_event', self.on_click) | |
def get_xyz(self, event): | |
if event.inaxes is None: | |
return | |
ax = np.where(event.inaxes == self.axes)[0][0] | |
view = ['sagittal', 'coronal', 'axial'][ax] | |
print event.xdata, event.ydata, view | |
if view == 'sagittal': | |
self.x = int(np.floor(event.xdata)) | |
self.z = int(np.floor(event.ydata)) | |
elif view == 'coronal': | |
self.y = int(np.floor(event.xdata)) | |
self.z = int(np.floor(event.ydata)) | |
elif view == 'axial': | |
self.y = int(np.floor(event.xdata)) | |
self.x = int(np.floor(event.ydata)) | |
print(self.x, self.y, self.z) | |
def on_click(self, event): | |
self.get_xyz(event) | |
self.sagittal.set_data(self.anat_img.dataobj[self.y, :, :].T) | |
self.coronal.set_data(self.anat_img.dataobj[:, self.x, :].T) | |
self.axial.set_data(self.anat_img.dataobj[:, :, self.z].T) | |
self.lv['sagittal'].set_xdata(self.x) | |
self.lh['sagittal'].set_ydata(self.z) | |
self.lv['coronal'].set_xdata(self.y) | |
self.lh['coronal'].set_ydata(self.z) | |
self.lv['axial'].set_xdata(self.y) | |
self.lh['axial'].set_ydata(self.x) | |
plt.draw() | |
def on_move(self, event): | |
pass | |
cursor = MRI_viewer(fname) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment