Skip to content

Commit 966a01e

Browse files
authored
Merge pull request #916 from jond01/deprecate-flip_axis
RF: Use np.flip instead of flip_axis
2 parents 7958c0a + e537732 commit 966a01e

File tree

4 files changed

+27
-56
lines changed

4 files changed

+27
-56
lines changed

nibabel/orientations.py

+12-27
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def apply_orientation(arr, ornt):
165165
# apply ornt transformations
166166
for ax, flip in enumerate(ornt[:, 1]):
167167
if flip == -1:
168-
t_arr = flip_axis(t_arr, axis=ax)
168+
t_arr = np.flip(t_arr, axis=ax)
169169
full_transpose = np.arange(t_arr.ndim)
170170
# ornt indicates the transpose that has occurred - we reverse it
171171
full_transpose[:n] = np.argsort(ornt[:, 0])
@@ -230,20 +230,21 @@ def inv_ornt_aff(ornt, shape):
230230

231231

232232
@deprecate_with_version('orientation_affine deprecated. '
233-
'Please use inv_ornt_aff instead'
234-
'1.3',
235-
'3.0')
233+
'Please use inv_ornt_aff instead.',
234+
'3.0',
235+
'4.0')
236236
def orientation_affine(ornt, shape):
237237
return inv_ornt_aff(ornt, shape)
238238

239239

240+
@deprecate_with_version('flip_axis is deprecated. '
241+
'Please use numpy.flip instead.',
242+
'3.2',
243+
'5.0')
240244
def flip_axis(arr, axis=0):
241-
''' Flip contents of `axis` in array `arr`
245+
""" Flip contents of `axis` in array `arr`
242246
243-
``flip_axis`` is the same transform as ``np.flipud``, but for any
244-
axis. For example ``flip_axis(arr, axis=0)`` is the same transform
245-
as ``np.flipud(arr)``, and ``flip_axis(arr, axis=1)`` is the same
246-
transform as ``np.fliplr(arr)``
247+
Equivalent to ``np.flip(arr, axis)``.
247248
248249
Parameters
249250
----------
@@ -255,24 +256,8 @@ def flip_axis(arr, axis=0):
255256
-------
256257
farr : array
257258
Array with axis `axis` flipped
258-
259-
Examples
260-
--------
261-
>>> a = np.arange(6).reshape((2,3))
262-
>>> a
263-
array([[0, 1, 2],
264-
[3, 4, 5]])
265-
>>> flip_axis(a, axis=0)
266-
array([[3, 4, 5],
267-
[0, 1, 2]])
268-
>>> flip_axis(a, axis=1)
269-
array([[2, 1, 0],
270-
[5, 4, 3]])
271-
'''
272-
arr = np.asanyarray(arr)
273-
arr = arr.swapaxes(0, axis)
274-
arr = np.flipud(arr)
275-
return arr.swapaxes(axis, 0)
259+
"""
260+
return np.flip(arr, axis)
276261

277262

278263
def ornt2axcodes(ornt, labels=None):

nibabel/tests/test_orientations.py

+8-22
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,6 @@ def test_apply():
140140
assert_array_equal(a.shape, np.array(t_arr.shape)[np.array(ornt)[:, 0]])
141141

142142

143-
def test_flip_axis():
144-
a = np.arange(24).reshape((2, 3, 4))
145-
assert_array_equal(
146-
flip_axis(a),
147-
np.flipud(a))
148-
assert_array_equal(
149-
flip_axis(a, axis=0),
150-
np.flipud(a))
151-
assert_array_equal(
152-
flip_axis(a, axis=1),
153-
np.fliplr(a))
154-
# check accepts array-like
155-
assert_array_equal(
156-
flip_axis(a.tolist(), axis=0),
157-
np.flipud(a))
158-
# third dimension
159-
b = a.transpose()
160-
b = np.flipud(b)
161-
b = b.transpose()
162-
assert_array_equal(flip_axis(a, axis=2), b)
163-
164-
165143
def test_io_orientation():
166144
for shape in ((2, 3, 4), (20, 15, 7)):
167145
for in_arr, out_ornt in zip(IN_ARRS, OUT_ORNTS):
@@ -381,3 +359,11 @@ def test_orientation_affine_deprecation():
381359
with pytest.deprecated_call():
382360
aff2 = orientation_affine([[0, 1], [1, -1], [2, 1]], (3, 4, 5))
383361
assert_array_equal(aff1, aff2)
362+
363+
364+
def test_flip_axis_deprecation():
365+
a = np.arange(24).reshape((2, 3, 4))
366+
axis = 1
367+
with pytest.deprecated_call():
368+
a_flipped = flip_axis(a, axis)
369+
assert_array_equal(a_flipped, np.flip(a, axis))

nibabel/tests/test_processing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
conform)
2424
from nibabel.nifti1 import Nifti1Image
2525
from nibabel.nifti2 import Nifti2Image
26-
from nibabel.orientations import aff2axcodes, flip_axis, inv_ornt_aff
26+
from nibabel.orientations import aff2axcodes, inv_ornt_aff
2727
from nibabel.affines import (AffineError, from_matvec, to_matvec, apply_affine,
2828
voxel_sizes)
2929
from nibabel.eulerangles import euler2mat
@@ -110,7 +110,7 @@ def test_resample_from_to():
110110
ax_flip_ornt = flip_ornt.copy()
111111
ax_flip_ornt[axis, 1] = -1
112112
aff_flip_i = inv_ornt_aff(ax_flip_ornt, (2, 3, 4))
113-
flipped_img = Nifti1Image(flip_axis(data, axis),
113+
flipped_img = Nifti1Image(np.flip(data, axis),
114114
np.dot(affine, aff_flip_i))
115115
out = resample_from_to(flipped_img, ((2, 3, 4), affine))
116116
assert_almost_equal(img.dataobj, out.dataobj)

nibabel/tests/test_scripts.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import nibabel as nib
1919
from ..tmpdirs import InTemporaryDirectory
2020
from ..loadsave import load
21-
from ..orientations import flip_axis, aff2axcodes, inv_ornt_aff
21+
from ..orientations import aff2axcodes, inv_ornt_aff
2222

2323
import unittest
2424
import pytest
@@ -273,20 +273,20 @@ def test_parrec2nii():
273273
assert code == 1
274274
# Default scaling is dv
275275
pr_img = load(fname)
276-
flipped_data = flip_axis(pr_img.get_fdata(), 1)
276+
flipped_data = np.flip(pr_img.get_fdata(), 1)
277277
base_cmd = ['parrec2nii', '--overwrite', fname]
278278
check_conversion(base_cmd, flipped_data, out_froot)
279279
check_conversion(base_cmd + ['--scaling=dv'],
280280
flipped_data,
281281
out_froot)
282282
# fp
283283
pr_img = load(fname, scaling='fp')
284-
flipped_data = flip_axis(pr_img.get_fdata(), 1)
284+
flipped_data = np.flip(pr_img.get_fdata(), 1)
285285
check_conversion(base_cmd + ['--scaling=fp'],
286286
flipped_data,
287287
out_froot)
288288
# no scaling
289-
unscaled_flipped = flip_axis(pr_img.dataobj.get_unscaled(), 1)
289+
unscaled_flipped = np.flip(pr_img.dataobj.get_unscaled(), 1)
290290
check_conversion(base_cmd + ['--scaling=off'],
291291
unscaled_flipped,
292292
out_froot)
@@ -335,7 +335,7 @@ def test_parrec2nii_with_data():
335335
assert np.all(np.abs(aff_off / vox_sizes) <= 0.501)
336336
# The data is very close, unless it's the fieldmap
337337
if par_root != 'fieldmap':
338-
conved_data_lps = flip_axis(conved_img.dataobj, 1)
338+
conved_data_lps = np.flip(conved_img.dataobj, 1)
339339
assert np.allclose(conved_data_lps, philips_img.dataobj)
340340
with InTemporaryDirectory():
341341
# Test some options

0 commit comments

Comments
 (0)