Skip to content

Commit c25f2fc

Browse files
committed
Merge pull request #3 from grlee77/viewer_tweaks
improve handling of complex inputs and 4d data
2 parents 4815e1e + ab3b6c7 commit c25f2fc

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

nibabel/tests/test_viewers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from ..viewers import OrthoSlicer3D
1616

1717
from numpy.testing.decorators import skipif
18-
from numpy.testing import assert_array_equal
18+
from numpy.testing import assert_array_equal, assert_equal
1919

2020
from nose.tools import assert_raises, assert_true
2121

@@ -48,14 +48,29 @@ def test_viewer():
4848
v._on_mouse(nt('event', 'xdata ydata inaxes button')(0.5, 0.5, ax, 1))
4949
v._on_mouse(nt('event', 'xdata ydata inaxes button')(0.5, 0.5, None, None))
5050
v.set_volume_idx(1)
51+
52+
# decrement/increment volume numbers via keypress
5153
v.set_volume_idx(1) # should just pass
54+
v._on_keypress(nt('event', 'key')('-')) # decrement
55+
assert_equal(v._data_idx[3], 0)
56+
v._on_keypress(nt('event', 'key')('+')) # increment
57+
assert_equal(v._data_idx[3], 1)
58+
v._on_keypress(nt('event', 'key')('-'))
59+
v._on_keypress(nt('event', 'key')('=')) # alternative increment key
60+
assert_equal(v._data_idx[3], 1)
61+
5262
v.close()
5363
v._draw() # should be safe
5464

5565
# non-multi-volume
5666
v = OrthoSlicer3D(data[:, :, :, 0])
5767
v._on_scroll(nt('event', 'button inaxes key')('up', v._axes[0], 'shift'))
5868
v._on_keypress(nt('event', 'key')('escape'))
69+
v.close()
70+
71+
# complex input should raise a TypeError prior to figure creation
72+
assert_raises(TypeError, OrthoSlicer3D,
73+
data[:, :, :, 0].astype(np.complex64))
5974

6075
# other cases
6176
fig, axes = plt.subplots(1, 4)

nibabel/viewers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class OrthoSlicer3D(object):
2222
corresponding slices in the other two. Scrolling up and
2323
down moves the slice up and down in the current axis.
2424
25+
OrthoSlicer3d also supports 4-dimensional data, where multiple
26+
3-dimensional volumes are stacked along the last axis. For 4-dimensional
27+
data the fourth figure axis can be used to control which 3-dimensional
28+
volume is displayed. Alternatively, the - key can be used to decrement the
29+
displayed volume and the + or = keys can be used to increment it.
30+
2531
Example
2632
-------
2733
>>> import numpy as np
@@ -66,6 +72,8 @@ def __init__(self, data, affine=None, axes=None, cmap='gray',
6672
data = np.asanyarray(data)
6773
if data.ndim < 3:
6874
raise ValueError('data must have at least 3 dimensions')
75+
if np.iscomplexobj(data):
76+
raise TypeError("Complex data not supported")
6977
affine = np.array(affine, float) if affine is not None else np.eye(4)
7078
if affine.ndim != 2 or affine.shape != (4, 4):
7179
raise ValueError('affine must be a 4x4 matrix')
@@ -436,6 +444,16 @@ def _on_keypress(self, event):
436444
"""Handle mpl keypress events"""
437445
if event.key is not None and 'escape' in event.key:
438446
self.close()
447+
elif event.key in ["=", '+']:
448+
# increment volume index
449+
new_idx = min(self._data_idx[3]+1, self.n_volumes)
450+
self._set_volume_index(new_idx, update_slices=True)
451+
self._draw()
452+
elif event.key == '-':
453+
# decrement volume index
454+
new_idx = max(self._data_idx[3]-1, 0)
455+
self._set_volume_index(new_idx, update_slices=True)
456+
self._draw()
439457

440458
def _draw(self):
441459
"""Update all four (or three) plots"""

0 commit comments

Comments
 (0)