From 5d89d2fb6c9056dba8d15bbca1445eaa467a6eb6 Mon Sep 17 00:00:00 2001 From: Guillaume Becq Date: Fri, 13 Sep 2024 14:00:55 +0200 Subject: [PATCH] test for BF viwers non RSA matrices --- nibabel/tests/test_viewers.py | 133 ++++++++++++++++------------------ 1 file changed, 64 insertions(+), 69 deletions(-) diff --git a/nibabel/tests/test_viewers.py b/nibabel/tests/test_viewers.py index dff93926d..fa22d9021 100644 --- a/nibabel/tests/test_viewers.py +++ b/nibabel/tests/test_viewers.py @@ -136,20 +136,19 @@ def test_viewer_nonRAS(): assert_array_equal(axi, data1[:, 13, :].T) - @needs_mpl def test_viewer_nonRAS_on_mouse(): """ test on_mouse selection on non RAS matrices - + """ - # This affine simulates an acquisition on a quadruped subject that is in a prone position. - # This corresponds to an acquisition with: + # This affine simulates an acquisition on a quadruped subject that is in a prone position. + # This corresponds to an acquisition with: # - LR inverted on scanner x (i) # - IS on scanner y (j) # - PA on scanner z (k) - # This example enables to test also OrthoSlicer3D properties `_flips` and `_order`. - + # This example enables to test also OrthoSlicer3D properties `_flips` and `_order`. + (I, J, K) = (10, 20, 40) data1 = np.random.rand(I, J, K) (i_target, j_target, k_target) = (2, 14, 12) @@ -159,52 +158,49 @@ def test_viewer_nonRAS_on_mouse(): j2 = j_target + 3 k1 = k_target - 4 k2 = k_target + 4 - data1[i1: i2 + 1, j1: j2 + 1, k1: k2 + 1] = 0 + data1[i1 : i2 + 1, j1 : j2 + 1, k1 : k2 + 1] = 0 data1[i_target, j_target, k_target] = 1 valp1 = 1.5 valm1 = 0.5 - data1[i_target - 1, j_target, k_target] = valp1 # x flipped - data1[i_target + 1, j_target, k_target] = valm1 # x flipped + data1[i_target - 1, j_target, k_target] = valp1 # x flipped + data1[i_target + 1, j_target, k_target] = valm1 # x flipped data1[i_target, j_target - 1, k_target] = valm1 data1[i_target, j_target + 1, k_target] = valp1 data1[i_target, j_target, k_target - 1] = valm1 data1[i_target, j_target, k_target + 1] = valp1 - - aff1 = np.array([[-1, 0, 0, 5], - [0, 0, 1, -10], - [0, 1, 0, -30], - [0, 0, 0, 1]]) - + + aff1 = np.array([[-1, 0, 0, 5], [0, 0, 1, -10], [0, 1, 0, -30], [0, 0, 0, 1]]) + o1 = OrthoSlicer3D(data1, aff1) - - class Event: - def __init__(self): - self.name = "simulated mouse event" + + class Event: + def __init__(self): + self.name = 'simulated mouse event' self.button = 1 - + event = Event() event.xdata = k_target event.ydata = j_target event.inaxes = o1._ims[0].axes o1._on_mouse(event) - + event.inaxes = o1._ims[1].axes - event.xdata = (I - 1) - i_target # x flipped + event.xdata = (I - 1) - i_target # x flipped event.ydata = j_target o1._on_mouse(event) - + event.inaxes = o1._ims[2].axes - event.xdata = (I - 1) - i_target # x flipped + event.xdata = (I - 1) - i_target # x flipped event.ydata = k_target o1._on_mouse(event) - + sag = o1._ims[0].get_array() cor = o1._ims[1].get_array() axi = o1._ims[2].get_array() - - assert_array_equal(sag, data1[i_target, :, :]) # - assert_array_equal(cor, data1[::-1, :, k_target].T) # x flipped - assert_array_equal(axi, data1[::-1, j_target, :].T) # x flipped + + assert_array_equal(sag, data1[i_target, :, :]) # + assert_array_equal(cor, data1[::-1, :, k_target].T) # x flipped + assert_array_equal(axi, data1[::-1, j_target, :].T) # x flipped return None @@ -212,15 +208,15 @@ def __init__(self): def test_viewer_nonRAS_on_scroll(): """ test scrolling on non RAS matrices - + """ - # This affine simulates an acquisition on a quadruped subject that is in a prone position. - # This corresponds to an acquisition with: + # This affine simulates an acquisition on a quadruped subject that is in a prone position. + # This corresponds to an acquisition with: # - LR inverted on scanner x (i) # - IS on scanner y (j) # - PA on scanner z (k) # This example enables to test also OrthoSlicer3D properties `_flips` and `_order`. - + (I, J, K) = (10, 20, 40) data1 = np.random.rand(I, J, K) (i_target, j_target, k_target) = (2, 14, 12) @@ -230,40 +226,35 @@ def test_viewer_nonRAS_on_scroll(): j2 = j_target + 3 k1 = k_target - 4 k2 = k_target + 4 - data1[i1: i2 + 1, j1: j2 + 1, k1: k2 + 1] = 0 + data1[i1 : i2 + 1, j1 : j2 + 1, k1 : k2 + 1] = 0 data1[i_target, j_target, k_target] = 1 valp1 = 1.5 valm1 = 0.5 - data1[i_target - 1, j_target, k_target] = valp1 # x flipped - data1[i_target + 1, j_target, k_target] = valm1 # x flipped + data1[i_target - 1, j_target, k_target] = valp1 # x flipped + data1[i_target + 1, j_target, k_target] = valm1 # x flipped data1[i_target, j_target - 1, k_target] = valm1 data1[i_target, j_target + 1, k_target] = valp1 data1[i_target, j_target, k_target - 1] = valm1 data1[i_target, j_target, k_target + 1] = valp1 - - aff1 = np.array([[-1, 0, 0, 5], - [0, 0, 1, -10], - [0, 1, 0, -30], - [0, 0, 0, 1]]) - + + aff1 = np.array([[-1, 0, 0, 5], [0, 0, 1, -10], [0, 1, 0, -30], [0, 0, 0, 1]]) + o1 = OrthoSlicer3D(data1, aff1) - - class Event: - def __init__(self): - self.name = "simulated mouse event" + + class Event: + def __init__(self): + self.name = 'simulated mouse event' self.button = None self.key = None - - i_last = data1.shape[0] - 1 - + [x_t, y_t, z_t] = list(aff1.dot(np.array([i_target, j_target, k_target, 1]))[:3]) # print(x_t, y_t, z_t) # scanner positions are x_t=3, y_t=2, z_t=16 - + event = Event() - + # Sagittal plane - one scroll up - # x coordinate is flipped so index decrease by 1 + # x coordinate is flipped so index decrease by 1 o1.set_position(x_t, y_t, z_t) event.inaxes = o1._ims[0].axes event.button = 'up' @@ -272,10 +263,10 @@ def __init__(self): cor = o1._ims[1].get_array() axi = o1._ims[2].get_array() assert_array_equal(sag, data1[i_target - 1, :, :]) - assert_array_equal(cor, data1[::-1, :, k_target].T) # ::-1 because the array is flipped in x - assert_array_equal(axi, data1[::-1, j_target, :].T) # ::-1 because the array is flipped in x - - # Sagittal plane - one scrolled down + assert_array_equal(cor, data1[::-1, :, k_target].T) # ::-1 because the array is flipped in x + assert_array_equal(axi, data1[::-1, j_target, :].T) # ::-1 because the array is flipped in x + + # Sagittal plane - one scrolled down o1.set_position(x_t, y_t, z_t) event.button = 'down' o1._on_scroll(event) @@ -285,9 +276,9 @@ def __init__(self): assert_array_equal(sag, data1[i_target + 1, :, :]) assert_array_equal(cor, data1[::-1, :, k_target].T) assert_array_equal(axi, data1[::-1, j_target, :].T) - + # Coronal plane - one scroll up - # y coordinate is increase by 1 + # y coordinate is increase by 1 o1.set_position(x_t, y_t, z_t) event.inaxes = o1._ims[1].axes event.button = 'up' @@ -296,10 +287,12 @@ def __init__(self): cor = o1._ims[1].get_array() axi = o1._ims[2].get_array() assert_array_equal(sag, data1[i_target, :, :]) - assert_array_equal(cor, data1[::-1, :, k_target + 1].T) # ::-1 because the array is flipped in x - assert_array_equal(axi, data1[::-1, j_target, :].T) # ::-1 because the array is flipped in x - - # Coronal plane - one scrolled down + assert_array_equal( + cor, data1[::-1, :, k_target + 1].T + ) # ::-1 because the array is flipped in x + assert_array_equal(axi, data1[::-1, j_target, :].T) # ::-1 because the array is flipped in x + + # Coronal plane - one scrolled down o1.set_position(x_t, y_t, z_t) event.button = 'down' o1._on_scroll(event) @@ -309,9 +302,9 @@ def __init__(self): assert_array_equal(sag, data1[i_target, :, :]) assert_array_equal(cor, data1[::-1, :, k_target - 1].T) assert_array_equal(axi, data1[::-1, j_target, :].T) - + # Axial plane - one scroll up - # y is increase by 1 + # y is increase by 1 o1.set_position(x_t, y_t, z_t) event.inaxes = o1._ims[2].axes event.button = 'up' @@ -320,10 +313,12 @@ def __init__(self): cor = o1._ims[1].get_array() axi = o1._ims[2].get_array() assert_array_equal(sag, data1[i_target, :, :]) - assert_array_equal(cor, data1[::-1, :, k_target].T) # ::-1 because the array is flipped in x - assert_array_equal(axi, data1[::-1, j_target + 1, :].T) # ::-1 because the array is flipped in x - - # Axial plane - one scrolled down + assert_array_equal(cor, data1[::-1, :, k_target].T) # ::-1 because the array is flipped in x + assert_array_equal( + axi, data1[::-1, j_target + 1, :].T + ) # ::-1 because the array is flipped in x + + # Axial plane - one scrolled down o1.set_position(x_t, y_t, z_t) event.button = 'down' o1._on_scroll(event) @@ -333,4 +328,4 @@ def __init__(self): assert_array_equal(sag, data1[i_target, :, :]) assert_array_equal(cor, data1[::-1, :, k_target].T) assert_array_equal(axi, data1[::-1, j_target - 1, :].T) - return None \ No newline at end of file + return None