12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328 |
- import sys
- import numpy
- from numpy.testing import (assert_, assert_equal, assert_array_equal,
- assert_array_almost_equal, assert_allclose,
- suppress_warnings)
- import pytest
- from pytest import raises as assert_raises
- import scipy.ndimage as ndimage
- from . import types
- eps = 1e-12
- ndimage_to_numpy_mode = {
- 'mirror': 'reflect',
- 'reflect': 'symmetric',
- 'grid-mirror': 'symmetric',
- 'grid-wrap': 'wrap',
- 'nearest': 'edge',
- 'grid-constant': 'constant',
- }
- class TestNdimageInterpolation:
- @pytest.mark.parametrize(
- 'mode, expected_value',
- [('nearest', [1.5, 2.5, 3.5, 4, 4, 4, 4]),
- ('wrap', [1.5, 2.5, 3.5, 1.5, 2.5, 3.5, 1.5]),
- ('grid-wrap', [1.5, 2.5, 3.5, 2.5, 1.5, 2.5, 3.5]),
- ('mirror', [1.5, 2.5, 3.5, 3.5, 2.5, 1.5, 1.5]),
- ('reflect', [1.5, 2.5, 3.5, 4, 3.5, 2.5, 1.5]),
- ('constant', [1.5, 2.5, 3.5, -1, -1, -1, -1]),
- ('grid-constant', [1.5, 2.5, 3.5, 1.5, -1, -1, -1])]
- )
- def test_boundaries(self, mode, expected_value):
- def shift(x):
- return (x[0] + 0.5,)
- data = numpy.array([1, 2, 3, 4.])
- assert_array_equal(
- expected_value,
- ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
- output_shape=(7,), order=1))
- @pytest.mark.parametrize(
- 'mode, expected_value',
- [('nearest', [1, 1, 2, 3]),
- ('wrap', [3, 1, 2, 3]),
- ('grid-wrap', [4, 1, 2, 3]),
- ('mirror', [2, 1, 2, 3]),
- ('reflect', [1, 1, 2, 3]),
- ('constant', [-1, 1, 2, 3]),
- ('grid-constant', [-1, 1, 2, 3])]
- )
- def test_boundaries2(self, mode, expected_value):
- def shift(x):
- return (x[0] - 0.9,)
- data = numpy.array([1, 2, 3, 4])
- assert_array_equal(
- expected_value,
- ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
- output_shape=(4,)))
- @pytest.mark.parametrize('mode', ['mirror', 'reflect', 'grid-mirror',
- 'grid-wrap', 'grid-constant',
- 'nearest'])
- @pytest.mark.parametrize('order', range(6))
- def test_boundary_spline_accuracy(self, mode, order):
- """Tests based on examples from gh-2640"""
- data = numpy.arange(-6, 7, dtype=float)
- x = numpy.linspace(-8, 15, num=1000)
- y = ndimage.map_coordinates(data, [x], order=order, mode=mode)
- # compute expected value using explicit padding via numpy.pad
- npad = 32
- pad_mode = ndimage_to_numpy_mode.get(mode)
- padded = numpy.pad(data, npad, mode=pad_mode)
- expected = ndimage.map_coordinates(padded, [npad + x], order=order,
- mode=mode)
- atol = 1e-5 if mode == 'grid-constant' else 1e-12
- assert_allclose(y, expected, rtol=1e-7, atol=atol)
- @pytest.mark.parametrize('order', range(2, 6))
- @pytest.mark.parametrize('dtype', types)
- def test_spline01(self, dtype, order):
- data = numpy.ones([], dtype)
- out = ndimage.spline_filter(data, order=order)
- assert_array_almost_equal(out, 1)
- @pytest.mark.parametrize('order', range(2, 6))
- @pytest.mark.parametrize('dtype', types)
- def test_spline02(self, dtype, order):
- data = numpy.array([1], dtype)
- out = ndimage.spline_filter(data, order=order)
- assert_array_almost_equal(out, [1])
- @pytest.mark.parametrize('order', range(2, 6))
- @pytest.mark.parametrize('dtype', types)
- def test_spline03(self, dtype, order):
- data = numpy.ones([], dtype)
- out = ndimage.spline_filter(data, order, output=dtype)
- assert_array_almost_equal(out, 1)
- @pytest.mark.parametrize('order', range(2, 6))
- @pytest.mark.parametrize('dtype', types)
- def test_spline04(self, dtype, order):
- data = numpy.ones([4], dtype)
- out = ndimage.spline_filter(data, order)
- assert_array_almost_equal(out, [1, 1, 1, 1])
- @pytest.mark.parametrize('order', range(2, 6))
- @pytest.mark.parametrize('dtype', types)
- def test_spline05(self, dtype, order):
- data = numpy.ones([4, 4], dtype)
- out = ndimage.spline_filter(data, order=order)
- assert_array_almost_equal(out, [[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform01(self, order):
- data = numpy.array([1])
- def mapping(x):
- return x
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform02(self, order):
- data = numpy.ones([4])
- def mapping(x):
- return x
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [1, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform03(self, order):
- data = numpy.ones([4])
- def mapping(x):
- return (x[0] - 1,)
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [0, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform04(self, order):
- data = numpy.array([4, 1, 3, 2])
- def mapping(x):
- return (x[0] - 1,)
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [0, 4, 1, 3])
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_geometric_transform05(self, order, dtype):
- data = numpy.array([[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]], dtype=dtype)
- expected = numpy.array([[0, 1, 1, 1],
- [0, 1, 1, 1],
- [0, 1, 1, 1]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- expected -= 1j * expected
- def mapping(x):
- return (x[0], x[1] - 1)
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform06(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- def mapping(x):
- return (x[0], x[1] - 1)
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [[0, 4, 1, 3],
- [0, 7, 6, 8],
- [0, 3, 5, 3]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform07(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- def mapping(x):
- return (x[0] - 1, x[1])
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [4, 1, 3, 2],
- [7, 6, 8, 5]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform08(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- def mapping(x):
- return (x[0] - 1, x[1] - 1)
- out = ndimage.geometric_transform(data, mapping, data.shape,
- order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform10(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- def mapping(x):
- return (x[0] - 1, x[1] - 1)
- if (order > 1):
- filtered = ndimage.spline_filter(data, order=order)
- else:
- filtered = data
- out = ndimage.geometric_transform(filtered, mapping, data.shape,
- order=order, prefilter=False)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform13(self, order):
- data = numpy.ones([2], numpy.float64)
- def mapping(x):
- return (x[0] // 2,)
- out = ndimage.geometric_transform(data, mapping, [4], order=order)
- assert_array_almost_equal(out, [1, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform14(self, order):
- data = [1, 5, 2, 6, 3, 7, 4, 4]
- def mapping(x):
- return (2 * x[0],)
- out = ndimage.geometric_transform(data, mapping, [4], order=order)
- assert_array_almost_equal(out, [1, 2, 3, 4])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform15(self, order):
- data = [1, 2, 3, 4]
- def mapping(x):
- return (x[0] / 2,)
- out = ndimage.geometric_transform(data, mapping, [8], order=order)
- assert_array_almost_equal(out[::2], [1, 2, 3, 4])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform16(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9.0, 10, 11, 12]]
- def mapping(x):
- return (x[0], x[1] * 2)
- out = ndimage.geometric_transform(data, mapping, (3, 2),
- order=order)
- assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform17(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (x[0] * 2, x[1])
- out = ndimage.geometric_transform(data, mapping, (1, 4),
- order=order)
- assert_array_almost_equal(out, [[1, 2, 3, 4]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform18(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (x[0] * 2, x[1] * 2)
- out = ndimage.geometric_transform(data, mapping, (1, 2),
- order=order)
- assert_array_almost_equal(out, [[1, 3]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform19(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (x[0], x[1] / 2)
- out = ndimage.geometric_transform(data, mapping, (3, 8),
- order=order)
- assert_array_almost_equal(out[..., ::2], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform20(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (x[0] / 2, x[1])
- out = ndimage.geometric_transform(data, mapping, (6, 4),
- order=order)
- assert_array_almost_equal(out[::2, ...], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform21(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (x[0] / 2, x[1] / 2)
- out = ndimage.geometric_transform(data, mapping, (6, 8),
- order=order)
- assert_array_almost_equal(out[::2, ::2], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform22(self, order):
- data = numpy.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]], numpy.float64)
- def mapping1(x):
- return (x[0] / 2, x[1] / 2)
- def mapping2(x):
- return (x[0] * 2, x[1] * 2)
- out = ndimage.geometric_transform(data, mapping1,
- (6, 8), order=order)
- out = ndimage.geometric_transform(out, mapping2,
- (3, 4), order=order)
- assert_array_almost_equal(out, data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform23(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x):
- return (1, x[0] * 2)
- out = ndimage.geometric_transform(data, mapping, (2,), order=order)
- out = out.astype(numpy.int32)
- assert_array_almost_equal(out, [5, 7])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_geometric_transform24(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- def mapping(x, a, b):
- return (a, x[0] * b)
- out = ndimage.geometric_transform(
- data, mapping, (2,), order=order, extra_arguments=(1,),
- extra_keywords={'b': 2})
- assert_array_almost_equal(out, [5, 7])
- def test_geometric_transform_grid_constant_order1(self):
- # verify interpolation outside the original bounds
- x = numpy.array([[1, 2, 3],
- [4, 5, 6]], dtype=float)
- def mapping(x):
- return (x[0] - 0.5), (x[1] - 0.5)
- expected_result = numpy.array([[0.25, 0.75, 1.25],
- [1.25, 3.00, 4.00]])
- assert_array_almost_equal(
- ndimage.geometric_transform(x, mapping, mode='grid-constant',
- order=1),
- expected_result,
- )
- @pytest.mark.parametrize('mode', ['grid-constant', 'grid-wrap', 'nearest',
- 'mirror', 'reflect'])
- @pytest.mark.parametrize('order', range(6))
- def test_geometric_transform_vs_padded(self, order, mode):
- x = numpy.arange(144, dtype=float).reshape(12, 12)
- def mapping(x):
- return (x[0] - 0.4), (x[1] + 2.3)
- # Manually pad and then extract center after the transform to get the
- # expected result.
- npad = 24
- pad_mode = ndimage_to_numpy_mode.get(mode)
- xp = numpy.pad(x, npad, mode=pad_mode)
- center_slice = tuple([slice(npad, -npad)] * x.ndim)
- expected_result = ndimage.geometric_transform(
- xp, mapping, mode=mode, order=order)[center_slice]
- assert_allclose(
- ndimage.geometric_transform(x, mapping, mode=mode,
- order=order),
- expected_result,
- rtol=1e-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_geometric_transform_with_string_output(self):
- data = numpy.array([1])
- def mapping(x):
- return x
- out = ndimage.geometric_transform(data, mapping, output='f')
- assert_(out.dtype is numpy.dtype('f'))
- assert_array_almost_equal(out, [1])
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_map_coordinates01(self, order, dtype):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- expected = numpy.array([[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- if data.dtype.kind == 'c':
- data = data - 1j * data
- expected = expected - 1j * expected
- idx = numpy.indices(data.shape)
- idx -= 1
- out = ndimage.map_coordinates(data, idx, order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_map_coordinates02(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- idx = numpy.indices(data.shape, numpy.float64)
- idx -= 0.5
- out1 = ndimage.shift(data, 0.5, order=order)
- out2 = ndimage.map_coordinates(data, idx, order=order)
- assert_array_almost_equal(out1, out2)
- def test_map_coordinates03(self):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]], order='F')
- idx = numpy.indices(data.shape) - 1
- out = ndimage.map_coordinates(data, idx)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- assert_array_almost_equal(out, ndimage.shift(data, (1, 1)))
- idx = numpy.indices(data[::2].shape) - 1
- out = ndimage.map_coordinates(data[::2], idx)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3]])
- assert_array_almost_equal(out, ndimage.shift(data[::2], (1, 1)))
- idx = numpy.indices(data[:, ::2].shape) - 1
- out = ndimage.map_coordinates(data[:, ::2], idx)
- 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)
- def test_map_coordinates_with_string_output(self):
- data = numpy.array([[1]])
- idx = numpy.indices(data.shape)
- out = ndimage.map_coordinates(data, idx, output='f')
- assert_(out.dtype is numpy.dtype('f'))
- assert_array_almost_equal(out, [[1]])
- @pytest.mark.skipif('win32' in sys.platform or numpy.intp(0).itemsize < 8,
- reason='do not run on 32 bit or windows '
- '(no sparse memory)')
- def test_map_coordinates_large_data(self):
- # check crash on large data
- try:
- n = 30000
- a = numpy.empty(n**2, dtype=numpy.float32).reshape(n, n)
- # fill the part we might read
- a[n - 3:, n - 3:] = 0
- ndimage.map_coordinates(a, [[n - 1.5], [n - 1.5]], order=1)
- except MemoryError as e:
- raise pytest.skip('Not enough memory available') from e
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform01(self, order):
- data = numpy.array([1])
- out = ndimage.affine_transform(data, [[1]], order=order)
- assert_array_almost_equal(out, [1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform02(self, order):
- data = numpy.ones([4])
- out = ndimage.affine_transform(data, [[1]], order=order)
- assert_array_almost_equal(out, [1, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform03(self, order):
- data = numpy.ones([4])
- out = ndimage.affine_transform(data, [[1]], -1, order=order)
- assert_array_almost_equal(out, [0, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform04(self, order):
- data = numpy.array([4, 1, 3, 2])
- out = ndimage.affine_transform(data, [[1]], -1, order=order)
- assert_array_almost_equal(out, [0, 4, 1, 3])
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_affine_transform05(self, order, dtype):
- data = numpy.array([[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]], dtype=dtype)
- expected = numpy.array([[0, 1, 1, 1],
- [0, 1, 1, 1],
- [0, 1, 1, 1]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- expected -= 1j * expected
- out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
- [0, -1], order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform06(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
- [0, -1], order=order)
- assert_array_almost_equal(out, [[0, 4, 1, 3],
- [0, 7, 6, 8],
- [0, 3, 5, 3]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform07(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
- [-1, 0], order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [4, 1, 3, 2],
- [7, 6, 8, 5]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform08(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
- [-1, -1], order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform09(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- if (order > 1):
- filtered = ndimage.spline_filter(data, order=order)
- else:
- filtered = data
- out = ndimage.affine_transform(filtered, [[1, 0], [0, 1]],
- [-1, -1], order=order,
- prefilter=False)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform10(self, order):
- data = numpy.ones([2], numpy.float64)
- out = ndimage.affine_transform(data, [[0.5]], output_shape=(4,),
- order=order)
- assert_array_almost_equal(out, [1, 1, 1, 0])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform11(self, order):
- data = [1, 5, 2, 6, 3, 7, 4, 4]
- out = ndimage.affine_transform(data, [[2]], 0, (4,), order=order)
- assert_array_almost_equal(out, [1, 2, 3, 4])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform12(self, order):
- data = [1, 2, 3, 4]
- out = ndimage.affine_transform(data, [[0.5]], 0, (8,), order=order)
- assert_array_almost_equal(out[::2], [1, 2, 3, 4])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform13(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9.0, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[1, 0], [0, 2]], 0, (3, 2),
- order=order)
- assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform14(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[2, 0], [0, 1]], 0, (1, 4),
- order=order)
- assert_array_almost_equal(out, [[1, 2, 3, 4]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform15(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[2, 0], [0, 2]], 0, (1, 2),
- order=order)
- assert_array_almost_equal(out, [[1, 3]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform16(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[1, 0.0], [0, 0.5]], 0,
- (3, 8), order=order)
- assert_array_almost_equal(out[..., ::2], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform17(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[0.5, 0], [0, 1]], 0,
- (6, 4), order=order)
- assert_array_almost_equal(out[::2, ...], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform18(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
- (6, 8), order=order)
- assert_array_almost_equal(out[::2, ::2], data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform19(self, order):
- data = numpy.array([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]], numpy.float64)
- out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
- (6, 8), order=order)
- out = ndimage.affine_transform(out, [[2.0, 0], [0, 2.0]], 0,
- (3, 4), order=order)
- assert_array_almost_equal(out, data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform20(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[0], [2]], 0, (2,),
- order=order)
- assert_array_almost_equal(out, [1, 3])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform21(self, order):
- data = [[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]]
- out = ndimage.affine_transform(data, [[2], [0]], 0, (2,),
- order=order)
- assert_array_almost_equal(out, [1, 9])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform22(self, order):
- # shift and offset interaction; see issue #1547
- data = numpy.array([4, 1, 3, 2])
- out = ndimage.affine_transform(data, [[2]], [-1], (3,),
- order=order)
- assert_array_almost_equal(out, [0, 1, 2])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform23(self, order):
- # shift and offset interaction; see issue #1547
- data = numpy.array([4, 1, 3, 2])
- out = ndimage.affine_transform(data, [[0.5]], [-1], (8,),
- order=order)
- assert_array_almost_equal(out[::2], [0, 4, 1, 3])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform24(self, order):
- # consistency between diagonal and non-diagonal case; see issue #1547
- data = numpy.array([4, 1, 3, 2])
- with suppress_warnings() as sup:
- sup.filter(UserWarning,
- 'The behavior of affine_transform with a 1-D array .* '
- 'has changed')
- out1 = ndimage.affine_transform(data, [2], -1, order=order)
- out2 = ndimage.affine_transform(data, [[2]], -1, order=order)
- assert_array_almost_equal(out1, out2)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform25(self, order):
- # consistency between diagonal and non-diagonal case; see issue #1547
- data = numpy.array([4, 1, 3, 2])
- with suppress_warnings() as sup:
- sup.filter(UserWarning,
- 'The behavior of affine_transform with a 1-D array .* '
- 'has changed')
- out1 = ndimage.affine_transform(data, [0.5], -1, order=order)
- out2 = ndimage.affine_transform(data, [[0.5]], -1, order=order)
- assert_array_almost_equal(out1, out2)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform26(self, order):
- # test homogeneous coordinates
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- if (order > 1):
- filtered = ndimage.spline_filter(data, order=order)
- else:
- filtered = data
- tform_original = numpy.eye(2)
- offset_original = -numpy.ones((2, 1))
- tform_h1 = numpy.hstack((tform_original, offset_original))
- tform_h2 = numpy.vstack((tform_h1, [[0, 0, 1]]))
- out1 = ndimage.affine_transform(filtered, tform_original,
- offset_original.ravel(),
- order=order, prefilter=False)
- out2 = ndimage.affine_transform(filtered, tform_h1, order=order,
- prefilter=False)
- out3 = ndimage.affine_transform(filtered, tform_h2, order=order,
- prefilter=False)
- for out in [out1, out2, out3]:
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- def test_affine_transform27(self):
- # test valid homogeneous transformation matrix
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- tform_h1 = numpy.hstack((numpy.eye(2), -numpy.ones((2, 1))))
- tform_h2 = numpy.vstack((tform_h1, [[5, 2, 1]]))
- assert_raises(ValueError, ndimage.affine_transform, data, tform_h2)
- 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()),
- data.dtype, data.dtype.newbyteorder()]:
- with suppress_warnings() as sup:
- sup.filter(UserWarning,
- 'The behavior of affine_transform with a 1-D array '
- '.* has changed')
- 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_affine_transform_output_shape(self):
- # don't require output_shape when out of a different size is given
- data = numpy.arange(8, dtype=numpy.float64)
- out = numpy.ones((16,))
- oshape = out.shape
- ndimage.affine_transform(data, [[1]], output=out)
- assert_array_almost_equal(out[:8], data)
- # mismatched output shape raises an error
- with pytest.raises(RuntimeError):
- ndimage.affine_transform(
- data, [[1]], output=out, output_shape=(12,))
- def test_affine_transform_with_string_output(self):
- data = numpy.array([1])
- out = ndimage.affine_transform(data, [[1]], output='f')
- assert_(out.dtype is numpy.dtype('f'))
- assert_array_almost_equal(out, [1])
- @pytest.mark.parametrize('shift',
- [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform_shift_via_grid_wrap(self, shift, order):
- # For mode 'grid-wrap', integer shifts should match numpy.roll
- x = numpy.array([[0, 1],
- [2, 3]])
- affine = numpy.zeros((2, 3))
- affine[:2, :2] = numpy.eye(2)
- affine[:, 2] = shift
- assert_array_almost_equal(
- ndimage.affine_transform(x, affine, mode='grid-wrap', order=order),
- numpy.roll(x, shift, axis=(0, 1)),
- )
- @pytest.mark.parametrize('order', range(0, 6))
- def test_affine_transform_shift_reflect(self, order):
- # shift by x.shape results in reflection
- x = numpy.array([[0, 1, 2],
- [3, 4, 5]])
- affine = numpy.zeros((2, 3))
- affine[:2, :2] = numpy.eye(2)
- affine[:, 2] = x.shape
- assert_array_almost_equal(
- ndimage.affine_transform(x, affine, mode='reflect', order=order),
- x[::-1, ::-1],
- )
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift01(self, order):
- data = numpy.array([1])
- out = ndimage.shift(data, [1], order=order)
- assert_array_almost_equal(out, [0])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift02(self, order):
- data = numpy.ones([4])
- out = ndimage.shift(data, [1], order=order)
- assert_array_almost_equal(out, [0, 1, 1, 1])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift03(self, order):
- data = numpy.ones([4])
- out = ndimage.shift(data, -1, order=order)
- assert_array_almost_equal(out, [1, 1, 1, 0])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift04(self, order):
- data = numpy.array([4, 1, 3, 2])
- out = ndimage.shift(data, 1, order=order)
- assert_array_almost_equal(out, [0, 4, 1, 3])
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_shift05(self, order, dtype):
- data = numpy.array([[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]], dtype=dtype)
- expected = numpy.array([[0, 1, 1, 1],
- [0, 1, 1, 1],
- [0, 1, 1, 1]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- expected -= 1j * expected
- out = ndimage.shift(data, [0, 1], order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('mode', ['constant', 'grid-constant'])
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_shift_with_nonzero_cval(self, order, mode, dtype):
- data = numpy.array([[1, 1, 1, 1],
- [1, 1, 1, 1],
- [1, 1, 1, 1]], dtype=dtype)
- expected = numpy.array([[0, 1, 1, 1],
- [0, 1, 1, 1],
- [0, 1, 1, 1]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- expected -= 1j * expected
- cval = 5.0
- expected[:, 0] = cval # specific to shift of [0, 1] used below
- out = ndimage.shift(data, [0, 1], order=order, mode=mode, cval=cval)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift06(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.shift(data, [0, 1], order=order)
- assert_array_almost_equal(out, [[0, 4, 1, 3],
- [0, 7, 6, 8],
- [0, 3, 5, 3]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift07(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.shift(data, [1, 0], order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [4, 1, 3, 2],
- [7, 6, 8, 5]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift08(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- out = ndimage.shift(data, [1, 1], order=order)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift09(self, order):
- data = numpy.array([[4, 1, 3, 2],
- [7, 6, 8, 5],
- [3, 5, 3, 6]])
- if (order > 1):
- filtered = ndimage.spline_filter(data, order=order)
- else:
- filtered = data
- out = ndimage.shift(filtered, [1, 1], order=order, prefilter=False)
- assert_array_almost_equal(out, [[0, 0, 0, 0],
- [0, 4, 1, 3],
- [0, 7, 6, 8]])
- @pytest.mark.parametrize('shift',
- [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift_grid_wrap(self, shift, order):
- # For mode 'grid-wrap', integer shifts should match numpy.roll
- x = numpy.array([[0, 1],
- [2, 3]])
- assert_array_almost_equal(
- ndimage.shift(x, shift, mode='grid-wrap', order=order),
- numpy.roll(x, shift, axis=(0, 1)),
- )
- @pytest.mark.parametrize('shift',
- [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift_grid_constant1(self, shift, order):
- # For integer shifts, 'constant' and 'grid-constant' should be equal
- x = numpy.arange(20).reshape((5, 4))
- assert_array_almost_equal(
- ndimage.shift(x, shift, mode='grid-constant', order=order),
- ndimage.shift(x, shift, mode='constant', order=order),
- )
- def test_shift_grid_constant_order1(self):
- x = numpy.array([[1, 2, 3],
- [4, 5, 6]], dtype=float)
- expected_result = numpy.array([[0.25, 0.75, 1.25],
- [1.25, 3.00, 4.00]])
- assert_array_almost_equal(
- ndimage.shift(x, (0.5, 0.5), mode='grid-constant', order=1),
- expected_result,
- )
- @pytest.mark.parametrize('order', range(0, 6))
- def test_shift_reflect(self, order):
- # shift by x.shape results in reflection
- x = numpy.array([[0, 1, 2],
- [3, 4, 5]])
- assert_array_almost_equal(
- ndimage.shift(x, x.shape, mode='reflect', order=order),
- x[::-1, ::-1],
- )
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('prefilter', [False, True])
- def test_shift_nearest_boundary(self, order, prefilter):
- # verify that shifting at least order // 2 beyond the end of the array
- # gives a value equal to the edge value.
- x = numpy.arange(16)
- kwargs = dict(mode='nearest', order=order, prefilter=prefilter)
- assert_array_almost_equal(
- ndimage.shift(x, order // 2 + 1, **kwargs)[0], x[0],
- )
- assert_array_almost_equal(
- ndimage.shift(x, -order // 2 - 1, **kwargs)[-1], x[-1],
- )
- @pytest.mark.parametrize('mode', ['grid-constant', 'grid-wrap', 'nearest',
- 'mirror', 'reflect'])
- @pytest.mark.parametrize('order', range(6))
- def test_shift_vs_padded(self, order, mode):
- x = numpy.arange(144, dtype=float).reshape(12, 12)
- shift = (0.4, -2.3)
- # manually pad and then extract center to get expected result
- npad = 32
- pad_mode = ndimage_to_numpy_mode.get(mode)
- xp = numpy.pad(x, npad, mode=pad_mode)
- center_slice = tuple([slice(npad, -npad)] * x.ndim)
- expected_result = ndimage.shift(
- xp, shift, mode=mode, order=order)[center_slice]
- assert_allclose(
- ndimage.shift(x, shift, mode=mode, order=order),
- expected_result,
- rtol=1e-7,
- )
- @pytest.mark.parametrize('order', range(0, 6))
- def test_zoom1(self, order):
- for z in [2, [2, 2]]:
- arr = numpy.array(list(range(25))).reshape((5, 5)).astype(float)
- arr = ndimage.zoom(arr, z, order=order)
- assert_equal(arr.shape, (10, 10))
- assert_(numpy.all(arr[-1, :] != 0))
- assert_(numpy.all(arr[-1, :] >= (20 - eps)))
- assert_(numpy.all(arr[0, :] <= (5 + eps)))
- assert_(numpy.all(arr >= (0 - eps)))
- assert_(numpy.all(arr <= (24 + eps)))
- def test_zoom2(self):
- arr = numpy.arange(12).reshape((3, 4))
- out = ndimage.zoom(ndimage.zoom(arr, 2), 0.5)
- assert_array_equal(out, arr)
- def test_zoom3(self):
- arr = numpy.array([[1, 2]])
- out1 = ndimage.zoom(arr, (2, 1))
- out2 = ndimage.zoom(arr, (1, 2))
- assert_array_almost_equal(out1, numpy.array([[1, 2], [1, 2]]))
- assert_array_almost_equal(out2, numpy.array([[1, 1, 2, 2]]))
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_zoom_affine01(self, order, dtype):
- data = numpy.asarray([[1, 2, 3, 4],
- [5, 6, 7, 8],
- [9, 10, 11, 12]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- with suppress_warnings() as sup:
- sup.filter(UserWarning,
- 'The behavior of affine_transform with a 1-D array .* '
- 'has changed')
- out = ndimage.affine_transform(data, [0.5, 0.5], 0,
- (6, 8), order=order)
- assert_array_almost_equal(out[::2, ::2], data)
- def test_zoom_infinity(self):
- # Ticket #1419 regression test
- dim = 8
- ndimage.zoom(numpy.zeros((dim, dim)), 1. / dim, mode='nearest')
- def test_zoom_zoomfactor_one(self):
- # Ticket #1122 regression test
- arr = numpy.zeros((1, 5, 5))
- zoom = (1.0, 2.0, 2.0)
- out = ndimage.zoom(arr, zoom, cval=7)
- ref = numpy.zeros((1, 10, 10))
- assert_array_almost_equal(out, ref)
- def test_zoom_output_shape_roundoff(self):
- arr = numpy.zeros((3, 11, 25))
- zoom = (4.0 / 3, 15.0 / 11, 29.0 / 25)
- out = ndimage.zoom(arr, zoom)
- assert_array_equal(out.shape, (4, 15, 29))
- @pytest.mark.parametrize('zoom', [(1, 1), (3, 5), (8, 2), (8, 8)])
- @pytest.mark.parametrize('mode', ['nearest', 'constant', 'wrap', 'reflect',
- 'mirror', 'grid-wrap', 'grid-mirror',
- 'grid-constant'])
- def test_zoom_by_int_order0(self, zoom, mode):
- # order 0 zoom should be the same as replication via numpy.kron
- # Note: This is not True for general x shapes when grid_mode is False,
- # but works here for all modes because the size ratio happens to
- # always be an integer when x.shape = (2, 2).
- x = numpy.array([[0, 1],
- [2, 3]], dtype=float)
- # x = numpy.arange(16, dtype=float).reshape(4, 4)
- assert_array_almost_equal(
- ndimage.zoom(x, zoom, order=0, mode=mode),
- numpy.kron(x, numpy.ones(zoom))
- )
- @pytest.mark.parametrize('shape', [(2, 3), (4, 4)])
- @pytest.mark.parametrize('zoom', [(1, 1), (3, 5), (8, 2), (8, 8)])
- @pytest.mark.parametrize('mode', ['nearest', 'reflect', 'mirror',
- 'grid-wrap', 'grid-constant'])
- def test_zoom_grid_by_int_order0(self, shape, zoom, mode):
- # When grid_mode is True, order 0 zoom should be the same as
- # replication via numpy.kron. The only exceptions to this are the
- # non-grid modes 'constant' and 'wrap'.
- x = numpy.arange(numpy.prod(shape), dtype=float).reshape(shape)
- assert_array_almost_equal(
- ndimage.zoom(x, zoom, order=0, mode=mode, grid_mode=True),
- numpy.kron(x, numpy.ones(zoom))
- )
- @pytest.mark.parametrize('mode', ['constant', 'wrap'])
- def test_zoom_grid_mode_warnings(self, mode):
- # Warn on use of non-grid modes when grid_mode is True
- x = numpy.arange(9, dtype=float).reshape((3, 3))
- with pytest.warns(UserWarning,
- match="It is recommended to use mode"):
- ndimage.zoom(x, 2, mode=mode, grid_mode=True),
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate01(self, order):
- data = numpy.array([[0, 0, 0, 0],
- [0, 1, 1, 0],
- [0, 0, 0, 0]], dtype=numpy.float64)
- out = ndimage.rotate(data, 0, order=order)
- assert_array_almost_equal(out, data)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate02(self, order):
- data = numpy.array([[0, 0, 0, 0],
- [0, 1, 0, 0],
- [0, 0, 0, 0]], dtype=numpy.float64)
- expected = numpy.array([[0, 0, 0],
- [0, 0, 0],
- [0, 1, 0],
- [0, 0, 0]], dtype=numpy.float64)
- out = ndimage.rotate(data, 90, order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
- def test_rotate03(self, order, dtype):
- data = numpy.array([[0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0]], dtype=dtype)
- expected = numpy.array([[0, 0, 0],
- [0, 0, 0],
- [0, 1, 0],
- [0, 1, 0],
- [0, 0, 0]], dtype=dtype)
- if data.dtype.kind == 'c':
- data -= 1j * data
- expected -= 1j * expected
- out = ndimage.rotate(data, 90, order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate04(self, order):
- data = numpy.array([[0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0]], dtype=numpy.float64)
- expected = numpy.array([[0, 0, 0, 0, 0],
- [0, 0, 1, 0, 0],
- [0, 0, 1, 0, 0]], dtype=numpy.float64)
- out = ndimage.rotate(data, 90, reshape=False, order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate05(self, order):
- data = numpy.empty((4, 3, 3))
- for i in range(3):
- data[:, :, i] = numpy.array([[0, 0, 0],
- [0, 1, 0],
- [0, 1, 0],
- [0, 0, 0]], dtype=numpy.float64)
- expected = numpy.array([[0, 0, 0, 0],
- [0, 1, 1, 0],
- [0, 0, 0, 0]], dtype=numpy.float64)
- out = ndimage.rotate(data, 90, order=order)
- for i in range(3):
- assert_array_almost_equal(out[:, :, i], expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate06(self, order):
- data = numpy.empty((3, 4, 3))
- for i in range(3):
- data[:, :, i] = numpy.array([[0, 0, 0, 0],
- [0, 1, 1, 0],
- [0, 0, 0, 0]], dtype=numpy.float64)
- expected = numpy.array([[0, 0, 0],
- [0, 1, 0],
- [0, 1, 0],
- [0, 0, 0]], dtype=numpy.float64)
- out = ndimage.rotate(data, 90, order=order)
- for i in range(3):
- assert_array_almost_equal(out[:, :, i], expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate07(self, order):
- data = numpy.array([[[0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
- data = data.transpose()
- expected = numpy.array([[[0, 0, 0],
- [0, 1, 0],
- [0, 1, 0],
- [0, 0, 0],
- [0, 0, 0]]] * 2, dtype=numpy.float64)
- expected = expected.transpose([2, 1, 0])
- out = ndimage.rotate(data, 90, axes=(0, 1), order=order)
- assert_array_almost_equal(out, expected)
- @pytest.mark.parametrize('order', range(0, 6))
- def test_rotate08(self, order):
- data = numpy.array([[[0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
- data = data.transpose()
- expected = numpy.array([[[0, 0, 1, 0, 0],
- [0, 0, 1, 0, 0],
- [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
- expected = expected.transpose()
- out = ndimage.rotate(data, 90, axes=(0, 1), reshape=False, order=order)
- assert_array_almost_equal(out, expected)
- def test_rotate09(self):
- data = numpy.array([[0, 0, 0, 0, 0],
- [0, 1, 1, 0, 0],
- [0, 0, 0, 0, 0]] * 2, dtype=numpy.float64)
- with assert_raises(ValueError):
- ndimage.rotate(data, 90, axes=(0, data.ndim))
- def test_rotate10(self):
- data = numpy.arange(45, dtype=numpy.float64).reshape((3, 5, 3))
- # The output of ndimage.rotate before refactoring
- expected = numpy.array([[[0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0],
- [6.54914793, 7.54914793, 8.54914793],
- [10.84520162, 11.84520162, 12.84520162],
- [0.0, 0.0, 0.0]],
- [[6.19286575, 7.19286575, 8.19286575],
- [13.4730712, 14.4730712, 15.4730712],
- [21.0, 22.0, 23.0],
- [28.5269288, 29.5269288, 30.5269288],
- [35.80713425, 36.80713425, 37.80713425]],
- [[0.0, 0.0, 0.0],
- [31.15479838, 32.15479838, 33.15479838],
- [35.45085207, 36.45085207, 37.45085207],
- [0.0, 0.0, 0.0],
- [0.0, 0.0, 0.0]]])
- out = ndimage.rotate(data, angle=12, reshape=False)
- assert_array_almost_equal(out, expected)
- def test_rotate_exact_180(self):
- a = numpy.tile(numpy.arange(5), (5, 1))
- b = ndimage.rotate(ndimage.rotate(a, 180), -180)
- assert_equal(a, b)
- def test_zoom_output_shape():
- """Ticket #643"""
- x = numpy.arange(12).reshape((3, 4))
- ndimage.zoom(x, 2, output=numpy.zeros((6, 8)))
|