Skip to content

Commit

Permalink
Merge pull request nipy#1 from satra/patch-1
Browse files Browse the repository at this point in the history
volume renderer update
  • Loading branch information
bilgelm committed Sep 8, 2017
2 parents fadbcea + b309af9 commit 77ebbb7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
18 changes: 8 additions & 10 deletions figure_handle_update_test.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@
"ExecuteTime": {
"end_time": "2017-09-07T23:06:36.271667Z",
"start_time": "2017-09-07T23:06:32.589796Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"from niwidgets import NiftiWidget"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2017-09-07T23:06:37.103129Z",
Expand All @@ -28,7 +28,7 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "5aad6094a9da47e8b65417a47bdccf7f",
"model_id": "bea47ec3870b4fb0a9887c62d4489c00",
"version_major": 2,
"version_minor": 0
},
Expand All @@ -41,17 +41,15 @@
}
],
"source": [
"filename = './niwidgets/data/T1.nii.gz'\n",
"filename = '/data/ds000114/sub-01/ses-test/anat/sub-01_ses-test_T1w.nii.gz'\n",
"my_widget = NiftiWidget(filename)\n",
"my_widget.nifti_plotter()"
"a = my_widget.nifti_plotter();"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": []
}
Expand All @@ -72,7 +70,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.1"
"version": "3.6.2"
},
"toc": {
"nav_menu": {},
Expand Down
31 changes: 18 additions & 13 deletions niwidgets/niwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import nibabel as nib
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interact, fixed
from ipywidgets import interact, fixed, IntSlider
import os
import inspect
import scipy.ndimage
Expand Down Expand Up @@ -100,15 +100,18 @@ def _default_plotter(self, mask_background=True, **kwargs):
# set default x y z values
for dim, label in enumerate(['x', 'y', 'z']):
if label not in kwargs.keys():
kwargs[label] = (0, self.data.shape[dim] - 1)
kwargs[label] = IntSlider((self.data.shape[dim] - 1)/2,
min=0, max=self.data.shape[dim] - 1, continuous_update=False)

interact(self._plot_slices, data=fixed(self.data), **kwargs)

# DUPLICATE to delete
def _init_figure(self, datashape, colormap, figsize):

def _init_figure(self, data, colormap, figsize):
# put chunk below in _init_figure

fig, axes = plt.subplots(1, 3, figsize=figsize)
self.fig, axes = plt.subplots(1, 3, figsize=figsize)

data_max = data.max()

for ii, ax in enumerate(axes):

Expand All @@ -119,18 +122,19 @@ def _init_figure(self, datashape, colormap, figsize):
labelbottom='off', right='off', left='off', labelleft='off'
)
# fix the axis limits
axis_limits = [limit for jj, limit in enumerate(datashape)
axis_limits = [limit for jj, limit in enumerate(data.shape)
if jj != ii]
ax.set_xlim(0, axis_limits[0])
ax.set_ylim(0, axis_limits[1])

im = ax.imshow(np.zeros(axis_limits[::-1]), cmap=colormap)
img = np.zeros(axis_limits[::-1])
img[1] = data_max
im = ax.imshow(img, cmap=colormap)

ax.axvline(x=0, color='gray', alpha=0.8)
ax.axhline(y=0, color='gray', alpha=0.8)

self.image_handles.append(im)
plt.show()


def _plot_slices(self, data, x, y, z, colormap='viridis', figsize=(15, 5)):
Expand All @@ -141,7 +145,7 @@ def _plot_slices(self, data, x, y, z, colormap='viridis', figsize=(15, 5)):
"""
if self.image_handles is None:
self.image_handles = []
self._init_figure(data.shape, colormap, figsize)
self._init_figure(data, colormap, figsize)

coords = [x, y, z]
views = ['Sagittal', 'Coronal', 'Axial']
Expand All @@ -165,11 +169,12 @@ def _plot_slices(self, data, x, y, z, colormap='viridis', figsize=(15, 5)):
imh.axes.lines[1].set_ydata(2*[guide_positions[1]])

# show the plot
plt.show()
# plt.show()
# print the value at that point in case people need to know
print('Value at point {x}, {y}, {z}: {intensity}'.format(
x=x, y=y, z=z, intensity=data[x, y , z]
))
#print('Value at point {x}, {y}, {z}: {intensity}'.format(
# x=x, y=y, z=z, intensity=data[x, y , z]
#))
return self.fig


def _custom_plotter(self, plotting_func, **kwargs):
Expand Down

0 comments on commit 77ebbb7

Please sign in to comment.