Skip to content

Commit

Permalink
BUG: endianness fix with non-native dtype param
Browse files Browse the repository at this point in the history
Fixes an issue analogous to scipy#7388 and scipy#4127 but for the case where output
param is a dtype rather than an ndarray.
  • Loading branch information
cdouglass committed Jun 7, 2017
1 parent d4a9034 commit 69abd4d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 60 deletions.
2 changes: 1 addition & 1 deletion THANKS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Roman Feldbauer for improvements in scipy.sparse
Dominic Antonacci for statistics documentation.
David Hagen for the object-oriented ODE solver interface.
Arno Onken for contributions to scipy.stats.
Cathy Douglass for a bug fix in ndimage.
Cathy Douglass for bug fixes in ndimage.

Institutions
------------
Expand Down
4 changes: 3 additions & 1 deletion scipy/ndimage/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def _fix_endianness(f):
def wrapper(*args, **kwargs):
output = kwargs.get("output")
result = f(*args, **kwargs)
if isinstance(output, numpy.ndarray) and not output.dtype.isnative:
if isinstance(output, numpy.dtype) and not output.isnative:
result.byteswap(True)
elif isinstance(output, numpy.ndarray) and not output.dtype.isnative:
output.byteswap(True)
return result

Expand Down
107 changes: 49 additions & 58 deletions scipy/ndimage/tests/test_ndimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,24 +1422,6 @@ def mapping(x):
order=order)
assert_array_almost_equal(out, [1])

def test_geometric_transform01_with_output_parameter(self):
data = numpy.array([1])

def mapping(x):
return x
for order in range(0, 6):
out = numpy.empty_like(data)
ndimage.geometric_transform(data, mapping,
data.shape,
output=out)
assert_array_almost_equal(out, [1])

out = numpy.empty_like(data).astype(data.dtype.newbyteorder())
ndimage.geometric_transform(data, mapping,
data.shape,
output=out)
assert_array_almost_equal(out, [1])

def test_geometric_transform02(self):
data = numpy.ones([4])

Expand Down Expand Up @@ -1690,6 +1672,23 @@ def mapping(x, a, b):
extra_keywords={'b': 2})
assert_array_almost_equal(out, [5, 7])

def test_geometric_transform_endianness_with_output_parameter(self):
# geometric transform given output ndarray or dtype with non-native endianness
# see issue #4127
data = numpy.array([1])

def mapping(x):
return x

for out in [data.dtype, data.dtype.newbyteorder(),
numpy.empty_like(data),
numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
returned = ndimage.geometric_transform(data, mapping,
data.shape,
output=out)
result = out if returned is None else returned
assert_array_almost_equal(result, [1])

def test_map_coordinates01(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
Expand All @@ -1702,25 +1701,6 @@ def test_map_coordinates01(self):
[0, 4, 1, 3],
[0, 7, 6, 8]])

def test_map_coordinates01_with_output_parameter(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
[3, 5, 3, 6]])
idx = numpy.indices(data.shape)
idx -= 1
expected = numpy.array([[0, 0, 0, 0],
[0, 4, 1, 3],
[0, 7, 6, 8]])
for order in range(0, 6):
out = numpy.empty_like(expected)
ndimage.map_coordinates(data, idx, order=order, output=out)
assert_array_almost_equal(out, expected)

out = numpy.empty_like(expected).astype(
expected.dtype.newbyteorder())
ndimage.map_coordinates(data, idx, order=order, output=out)
assert_array_almost_equal(out, expected)

def test_map_coordinates02(self):
data = numpy.array([[4, 1, 3, 2],
[7, 6, 8, 5],
Expand Down Expand Up @@ -1753,6 +1733,19 @@ def test_map_coordinates03(self):
assert_array_almost_equal(out, [[0, 0], [0, 4], [0, 7]])
assert_array_almost_equal(out, ndimage.shift(data[:,::2], (1, 1)))

def test_map_coordinates_endianness_with_output_parameter(self):
# output parameter given as array or dtype with either endianness
# see issue #4127
data = numpy.array([[1, 2], [7, 6]])
expected = numpy.array([[0, 0], [0, 1]])
idx = numpy.indices(data.shape)
idx -= 1
for out in [data.dtype, data.dtype.newbyteorder(), numpy.empty_like(expected),
numpy.empty_like(expected).astype(expected.dtype.newbyteorder())]:
returned = ndimage.map_coordinates(data, idx, output=out)
result = out if returned is None else returned
assert_array_almost_equal(result, expected)

# do not run on 32 bit or windows (no sparse memory)
@dec.skipif('win32' in sys.platform or numpy.intp(0).itemsize < 8)
def test_map_coordinates_large_data(self):
Expand All @@ -1773,21 +1766,6 @@ def test_affine_transform01(self):
order=order)
assert_array_almost_equal(out, [1])

def test_affine_transform01_with_output_parameter(self):
data = numpy.array([1])
for order in range(0, 6):
out = numpy.empty_like(data)
ndimage.affine_transform(data, [[1]],
order=order,
output=out)
assert_array_almost_equal(out, [1])

out = numpy.empty_like(data).astype(data.dtype.newbyteorder())
ndimage.affine_transform(data, [[1]],
order=order,
output=out)
assert_array_almost_equal(out, [1])

def test_affine_transform02(self):
data = numpy.ones([4])
for order in range(0, 6):
Expand Down Expand Up @@ -2065,16 +2043,29 @@ def test_affine_transform27(self):
numpy.testing.assert_raises(ValueError,
ndimage.affine_transform, data, tform_h2)

def test_affine_transform28(self):
# 1d affine transform given output ndarray with non-native endianness
# See issue #7388
def test_affine_transform_1d_endianness_with_output_parameter(self):
# 1d affine transform given output ndarray or dtype with either endianness
# see issue #7388
data = numpy.ones((2, 2))
for out in [numpy.empty_like(data),
numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
numpy.empty_like(data).astype(data.dtype.newbyteorder()),
data.dtype, data.dtype.newbyteorder()]:
with warnings.catch_warnings():
warnings.simplefilter("ignore", UserWarning)
ndimage.affine_transform(data, [1, 1], output=out)
assert_array_almost_equal(out, [[1, 1], [1, 1]])
returned = ndimage.affine_transform(data, [1, 1], output=out)
result = out if returned is None else returned
assert_array_almost_equal(result, [[1, 1], [1, 1]])

def test_affine_transform_multi_d_endianness_with_output_parameter(self):
# affine transform given output ndarray or dtype with either endianness
# see issue #4127
data = numpy.array([1])
for out in [data.dtype, data.dtype.newbyteorder(),
numpy.empty_like(data),
numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
returned = ndimage.affine_transform(data, [[1]], output=out)
result = out if returned is None else returned
assert_array_almost_equal(result, [1])

def test_shift01(self):
data = numpy.array([1])
Expand Down

0 comments on commit 69abd4d

Please sign in to comment.