Skip to content

Commit

Permalink
Merge pull request #1319 from gjpcbecq/correct-bug-OrthoSlicer3D
Browse files Browse the repository at this point in the history
BUG: wrong origin location in orthoview, update OrthoSlicer3D._set_position in viewers.py
  • Loading branch information
effigies authored Sep 5, 2024
2 parents d15ec58 + cdc788d commit abe765f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
32 changes: 32 additions & 0 deletions nibabel/tests/test_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,35 @@ def test_viewer():
v2.link_to(v1) # shouldn't do anything
v1.close()
v2.close()


@needs_mpl
def test_viewer_nonRAS():
data1 = np.random.rand(10, 20, 40)
data1[5, 10, :] = 0
data1[5, :, 30] = 0
data1[:, 10, 30] = 0
# RSA affine
aff1 = np.array([[1, 0, 0, -5], [0, 0, 1, -30], [0, 1, 0, -10], [0, 0, 0, 1]])
o1 = OrthoSlicer3D(data1, aff1)
sag = o1._ims[0].get_array()
cor = o1._ims[1].get_array()
axi = o1._ims[2].get_array()

# Sagittal view: [0, I->S, P->A], so data is transposed, matching plot array
assert_array_equal(sag, data1[5, :, :])
# Coronal view: [L->R, I->S, 0]. Data is not transposed, transpose to match plot array
assert_array_equal(cor, data1[:, :, 30].T)
# Axial view: [L->R, 0, P->A]. Data is not transposed, transpose to match plot array
assert_array_equal(axi, data1[:, 10, :].T)

o1.set_position(1, 2, 3) # R, A, S coordinates

sag = o1._ims[0].get_array()
cor = o1._ims[1].get_array()
axi = o1._ims[2].get_array()

# Shift 1 right, 2 anterior, 3 superior
assert_array_equal(sag, data1[6, :, :])
assert_array_equal(cor, data1[:, :, 32].T)
assert_array_equal(axi, data1[:, 13, :].T)
6 changes: 4 additions & 2 deletions nibabel/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ def _set_position(self, x, y, z, notify=True):
# deal with slicing appropriately
self._position[:3] = [x, y, z]
idxs = np.dot(self._inv_affine, self._position)[:3]
for ii, (size, idx) in enumerate(zip(self._sizes, idxs)):
idxs_new_order = idxs[self._order]
for ii, (size, idx) in enumerate(zip(self._sizes, idxs_new_order)):
self._data_idx[ii] = max(min(int(round(idx)), size - 1), 0)
for ii in range(3):
# sagittal: get to S/A
Expand Down Expand Up @@ -491,10 +492,11 @@ def _on_mouse(self, event):
x, y = event.xdata, event.ydata
x = self._sizes[xax] - x if self._flips[xax] else x
y = self._sizes[yax] - y if self._flips[yax] else y
idxs = [None, None, None, 1.0]
idxs = np.ones(4)
idxs[xax] = x
idxs[yax] = y
idxs[ii] = self._data_idx[ii]
idxs[:3] = idxs[self._order]
self._set_position(*np.dot(self._affine, idxs)[:3])
self._draw()

Expand Down

0 comments on commit abe765f

Please sign in to comment.