Skip to content

Commit 416f784

Browse files
grlee77larsoner
authored andcommitted
ENH: Add +/- key support for incrementing/decrementing the volume for 4D data sets
Add tests and update the OrthoSlicer3D docstring to describe 4D support.
1 parent f08525f commit 416f784

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

nibabel/tests/test_viewers.py

Lines changed: 11 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,7 +48,17 @@ 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

nibabel/viewers.py

Lines changed: 16 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
@@ -436,6 +442,16 @@ def _on_keypress(self, event):
436442
"""Handle mpl keypress events"""
437443
if event.key is not None and 'escape' in event.key:
438444
self.close()
445+
elif event.key in ["=", '+']:
446+
# increment volume index
447+
new_idx = min(self._data_idx[3]+1, self.n_volumes)
448+
self._set_volume_index(new_idx, update_slices=True)
449+
self._draw()
450+
elif event.key == '-':
451+
# decrement volume index
452+
new_idx = max(self._data_idx[3]-1, 0)
453+
self._set_volume_index(new_idx, update_slices=True)
454+
self._draw()
439455

440456
def _draw(self):
441457
"""Update all four (or three) plots"""

0 commit comments

Comments
 (0)