Skip to content

Commit abe765f

Browse files
authored
Merge pull request #1319 from gjpcbecq/correct-bug-OrthoSlicer3D
BUG: wrong origin location in orthoview, update OrthoSlicer3D._set_position in viewers.py
2 parents d15ec58 + cdc788d commit abe765f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

nibabel/tests/test_viewers.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,35 @@ def test_viewer():
102102
v2.link_to(v1) # shouldn't do anything
103103
v1.close()
104104
v2.close()
105+
106+
107+
@needs_mpl
108+
def test_viewer_nonRAS():
109+
data1 = np.random.rand(10, 20, 40)
110+
data1[5, 10, :] = 0
111+
data1[5, :, 30] = 0
112+
data1[:, 10, 30] = 0
113+
# RSA affine
114+
aff1 = np.array([[1, 0, 0, -5], [0, 0, 1, -30], [0, 1, 0, -10], [0, 0, 0, 1]])
115+
o1 = OrthoSlicer3D(data1, aff1)
116+
sag = o1._ims[0].get_array()
117+
cor = o1._ims[1].get_array()
118+
axi = o1._ims[2].get_array()
119+
120+
# Sagittal view: [0, I->S, P->A], so data is transposed, matching plot array
121+
assert_array_equal(sag, data1[5, :, :])
122+
# Coronal view: [L->R, I->S, 0]. Data is not transposed, transpose to match plot array
123+
assert_array_equal(cor, data1[:, :, 30].T)
124+
# Axial view: [L->R, 0, P->A]. Data is not transposed, transpose to match plot array
125+
assert_array_equal(axi, data1[:, 10, :].T)
126+
127+
o1.set_position(1, 2, 3) # R, A, S coordinates
128+
129+
sag = o1._ims[0].get_array()
130+
cor = o1._ims[1].get_array()
131+
axi = o1._ims[2].get_array()
132+
133+
# Shift 1 right, 2 anterior, 3 superior
134+
assert_array_equal(sag, data1[6, :, :])
135+
assert_array_equal(cor, data1[:, :, 32].T)
136+
assert_array_equal(axi, data1[:, 13, :].T)

nibabel/viewers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ def _set_position(self, x, y, z, notify=True):
399399
# deal with slicing appropriately
400400
self._position[:3] = [x, y, z]
401401
idxs = np.dot(self._inv_affine, self._position)[:3]
402-
for ii, (size, idx) in enumerate(zip(self._sizes, idxs)):
402+
idxs_new_order = idxs[self._order]
403+
for ii, (size, idx) in enumerate(zip(self._sizes, idxs_new_order)):
403404
self._data_idx[ii] = max(min(int(round(idx)), size - 1), 0)
404405
for ii in range(3):
405406
# sagittal: get to S/A
@@ -491,10 +492,11 @@ def _on_mouse(self, event):
491492
x, y = event.xdata, event.ydata
492493
x = self._sizes[xax] - x if self._flips[xax] else x
493494
y = self._sizes[yax] - y if self._flips[yax] else y
494-
idxs = [None, None, None, 1.0]
495+
idxs = np.ones(4)
495496
idxs[xax] = x
496497
idxs[yax] = y
497498
idxs[ii] = self._data_idx[ii]
499+
idxs[:3] = idxs[self._order]
498500
self._set_position(*np.dot(self._affine, idxs)[:3])
499501
self._draw()
500502

0 commit comments

Comments
 (0)