test_interpolation.py 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. import sys
  2. import numpy
  3. from numpy.testing import (assert_, assert_equal, assert_array_equal,
  4. assert_array_almost_equal, assert_allclose,
  5. suppress_warnings)
  6. import pytest
  7. from pytest import raises as assert_raises
  8. import scipy.ndimage as ndimage
  9. from . import types
  10. eps = 1e-12
  11. ndimage_to_numpy_mode = {
  12. 'mirror': 'reflect',
  13. 'reflect': 'symmetric',
  14. 'grid-mirror': 'symmetric',
  15. 'grid-wrap': 'wrap',
  16. 'nearest': 'edge',
  17. 'grid-constant': 'constant',
  18. }
  19. class TestNdimageInterpolation:
  20. @pytest.mark.parametrize(
  21. 'mode, expected_value',
  22. [('nearest', [1.5, 2.5, 3.5, 4, 4, 4, 4]),
  23. ('wrap', [1.5, 2.5, 3.5, 1.5, 2.5, 3.5, 1.5]),
  24. ('grid-wrap', [1.5, 2.5, 3.5, 2.5, 1.5, 2.5, 3.5]),
  25. ('mirror', [1.5, 2.5, 3.5, 3.5, 2.5, 1.5, 1.5]),
  26. ('reflect', [1.5, 2.5, 3.5, 4, 3.5, 2.5, 1.5]),
  27. ('constant', [1.5, 2.5, 3.5, -1, -1, -1, -1]),
  28. ('grid-constant', [1.5, 2.5, 3.5, 1.5, -1, -1, -1])]
  29. )
  30. def test_boundaries(self, mode, expected_value):
  31. def shift(x):
  32. return (x[0] + 0.5,)
  33. data = numpy.array([1, 2, 3, 4.])
  34. assert_array_equal(
  35. expected_value,
  36. ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
  37. output_shape=(7,), order=1))
  38. @pytest.mark.parametrize(
  39. 'mode, expected_value',
  40. [('nearest', [1, 1, 2, 3]),
  41. ('wrap', [3, 1, 2, 3]),
  42. ('grid-wrap', [4, 1, 2, 3]),
  43. ('mirror', [2, 1, 2, 3]),
  44. ('reflect', [1, 1, 2, 3]),
  45. ('constant', [-1, 1, 2, 3]),
  46. ('grid-constant', [-1, 1, 2, 3])]
  47. )
  48. def test_boundaries2(self, mode, expected_value):
  49. def shift(x):
  50. return (x[0] - 0.9,)
  51. data = numpy.array([1, 2, 3, 4])
  52. assert_array_equal(
  53. expected_value,
  54. ndimage.geometric_transform(data, shift, cval=-1, mode=mode,
  55. output_shape=(4,)))
  56. @pytest.mark.parametrize('mode', ['mirror', 'reflect', 'grid-mirror',
  57. 'grid-wrap', 'grid-constant',
  58. 'nearest'])
  59. @pytest.mark.parametrize('order', range(6))
  60. def test_boundary_spline_accuracy(self, mode, order):
  61. """Tests based on examples from gh-2640"""
  62. data = numpy.arange(-6, 7, dtype=float)
  63. x = numpy.linspace(-8, 15, num=1000)
  64. y = ndimage.map_coordinates(data, [x], order=order, mode=mode)
  65. # compute expected value using explicit padding via numpy.pad
  66. npad = 32
  67. pad_mode = ndimage_to_numpy_mode.get(mode)
  68. padded = numpy.pad(data, npad, mode=pad_mode)
  69. expected = ndimage.map_coordinates(padded, [npad + x], order=order,
  70. mode=mode)
  71. atol = 1e-5 if mode == 'grid-constant' else 1e-12
  72. assert_allclose(y, expected, rtol=1e-7, atol=atol)
  73. @pytest.mark.parametrize('order', range(2, 6))
  74. @pytest.mark.parametrize('dtype', types)
  75. def test_spline01(self, dtype, order):
  76. data = numpy.ones([], dtype)
  77. out = ndimage.spline_filter(data, order=order)
  78. assert_array_almost_equal(out, 1)
  79. @pytest.mark.parametrize('order', range(2, 6))
  80. @pytest.mark.parametrize('dtype', types)
  81. def test_spline02(self, dtype, order):
  82. data = numpy.array([1], dtype)
  83. out = ndimage.spline_filter(data, order=order)
  84. assert_array_almost_equal(out, [1])
  85. @pytest.mark.parametrize('order', range(2, 6))
  86. @pytest.mark.parametrize('dtype', types)
  87. def test_spline03(self, dtype, order):
  88. data = numpy.ones([], dtype)
  89. out = ndimage.spline_filter(data, order, output=dtype)
  90. assert_array_almost_equal(out, 1)
  91. @pytest.mark.parametrize('order', range(2, 6))
  92. @pytest.mark.parametrize('dtype', types)
  93. def test_spline04(self, dtype, order):
  94. data = numpy.ones([4], dtype)
  95. out = ndimage.spline_filter(data, order)
  96. assert_array_almost_equal(out, [1, 1, 1, 1])
  97. @pytest.mark.parametrize('order', range(2, 6))
  98. @pytest.mark.parametrize('dtype', types)
  99. def test_spline05(self, dtype, order):
  100. data = numpy.ones([4, 4], dtype)
  101. out = ndimage.spline_filter(data, order=order)
  102. assert_array_almost_equal(out, [[1, 1, 1, 1],
  103. [1, 1, 1, 1],
  104. [1, 1, 1, 1],
  105. [1, 1, 1, 1]])
  106. @pytest.mark.parametrize('order', range(0, 6))
  107. def test_geometric_transform01(self, order):
  108. data = numpy.array([1])
  109. def mapping(x):
  110. return x
  111. out = ndimage.geometric_transform(data, mapping, data.shape,
  112. order=order)
  113. assert_array_almost_equal(out, [1])
  114. @pytest.mark.parametrize('order', range(0, 6))
  115. def test_geometric_transform02(self, order):
  116. data = numpy.ones([4])
  117. def mapping(x):
  118. return x
  119. out = ndimage.geometric_transform(data, mapping, data.shape,
  120. order=order)
  121. assert_array_almost_equal(out, [1, 1, 1, 1])
  122. @pytest.mark.parametrize('order', range(0, 6))
  123. def test_geometric_transform03(self, order):
  124. data = numpy.ones([4])
  125. def mapping(x):
  126. return (x[0] - 1,)
  127. out = ndimage.geometric_transform(data, mapping, data.shape,
  128. order=order)
  129. assert_array_almost_equal(out, [0, 1, 1, 1])
  130. @pytest.mark.parametrize('order', range(0, 6))
  131. def test_geometric_transform04(self, order):
  132. data = numpy.array([4, 1, 3, 2])
  133. def mapping(x):
  134. return (x[0] - 1,)
  135. out = ndimage.geometric_transform(data, mapping, data.shape,
  136. order=order)
  137. assert_array_almost_equal(out, [0, 4, 1, 3])
  138. @pytest.mark.parametrize('order', range(0, 6))
  139. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  140. def test_geometric_transform05(self, order, dtype):
  141. data = numpy.array([[1, 1, 1, 1],
  142. [1, 1, 1, 1],
  143. [1, 1, 1, 1]], dtype=dtype)
  144. expected = numpy.array([[0, 1, 1, 1],
  145. [0, 1, 1, 1],
  146. [0, 1, 1, 1]], dtype=dtype)
  147. if data.dtype.kind == 'c':
  148. data -= 1j * data
  149. expected -= 1j * expected
  150. def mapping(x):
  151. return (x[0], x[1] - 1)
  152. out = ndimage.geometric_transform(data, mapping, data.shape,
  153. order=order)
  154. assert_array_almost_equal(out, expected)
  155. @pytest.mark.parametrize('order', range(0, 6))
  156. def test_geometric_transform06(self, order):
  157. data = numpy.array([[4, 1, 3, 2],
  158. [7, 6, 8, 5],
  159. [3, 5, 3, 6]])
  160. def mapping(x):
  161. return (x[0], x[1] - 1)
  162. out = ndimage.geometric_transform(data, mapping, data.shape,
  163. order=order)
  164. assert_array_almost_equal(out, [[0, 4, 1, 3],
  165. [0, 7, 6, 8],
  166. [0, 3, 5, 3]])
  167. @pytest.mark.parametrize('order', range(0, 6))
  168. def test_geometric_transform07(self, order):
  169. data = numpy.array([[4, 1, 3, 2],
  170. [7, 6, 8, 5],
  171. [3, 5, 3, 6]])
  172. def mapping(x):
  173. return (x[0] - 1, x[1])
  174. out = ndimage.geometric_transform(data, mapping, data.shape,
  175. order=order)
  176. assert_array_almost_equal(out, [[0, 0, 0, 0],
  177. [4, 1, 3, 2],
  178. [7, 6, 8, 5]])
  179. @pytest.mark.parametrize('order', range(0, 6))
  180. def test_geometric_transform08(self, order):
  181. data = numpy.array([[4, 1, 3, 2],
  182. [7, 6, 8, 5],
  183. [3, 5, 3, 6]])
  184. def mapping(x):
  185. return (x[0] - 1, x[1] - 1)
  186. out = ndimage.geometric_transform(data, mapping, data.shape,
  187. order=order)
  188. assert_array_almost_equal(out, [[0, 0, 0, 0],
  189. [0, 4, 1, 3],
  190. [0, 7, 6, 8]])
  191. @pytest.mark.parametrize('order', range(0, 6))
  192. def test_geometric_transform10(self, order):
  193. data = numpy.array([[4, 1, 3, 2],
  194. [7, 6, 8, 5],
  195. [3, 5, 3, 6]])
  196. def mapping(x):
  197. return (x[0] - 1, x[1] - 1)
  198. if (order > 1):
  199. filtered = ndimage.spline_filter(data, order=order)
  200. else:
  201. filtered = data
  202. out = ndimage.geometric_transform(filtered, mapping, data.shape,
  203. order=order, prefilter=False)
  204. assert_array_almost_equal(out, [[0, 0, 0, 0],
  205. [0, 4, 1, 3],
  206. [0, 7, 6, 8]])
  207. @pytest.mark.parametrize('order', range(0, 6))
  208. def test_geometric_transform13(self, order):
  209. data = numpy.ones([2], numpy.float64)
  210. def mapping(x):
  211. return (x[0] // 2,)
  212. out = ndimage.geometric_transform(data, mapping, [4], order=order)
  213. assert_array_almost_equal(out, [1, 1, 1, 1])
  214. @pytest.mark.parametrize('order', range(0, 6))
  215. def test_geometric_transform14(self, order):
  216. data = [1, 5, 2, 6, 3, 7, 4, 4]
  217. def mapping(x):
  218. return (2 * x[0],)
  219. out = ndimage.geometric_transform(data, mapping, [4], order=order)
  220. assert_array_almost_equal(out, [1, 2, 3, 4])
  221. @pytest.mark.parametrize('order', range(0, 6))
  222. def test_geometric_transform15(self, order):
  223. data = [1, 2, 3, 4]
  224. def mapping(x):
  225. return (x[0] / 2,)
  226. out = ndimage.geometric_transform(data, mapping, [8], order=order)
  227. assert_array_almost_equal(out[::2], [1, 2, 3, 4])
  228. @pytest.mark.parametrize('order', range(0, 6))
  229. def test_geometric_transform16(self, order):
  230. data = [[1, 2, 3, 4],
  231. [5, 6, 7, 8],
  232. [9.0, 10, 11, 12]]
  233. def mapping(x):
  234. return (x[0], x[1] * 2)
  235. out = ndimage.geometric_transform(data, mapping, (3, 2),
  236. order=order)
  237. assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
  238. @pytest.mark.parametrize('order', range(0, 6))
  239. def test_geometric_transform17(self, order):
  240. data = [[1, 2, 3, 4],
  241. [5, 6, 7, 8],
  242. [9, 10, 11, 12]]
  243. def mapping(x):
  244. return (x[0] * 2, x[1])
  245. out = ndimage.geometric_transform(data, mapping, (1, 4),
  246. order=order)
  247. assert_array_almost_equal(out, [[1, 2, 3, 4]])
  248. @pytest.mark.parametrize('order', range(0, 6))
  249. def test_geometric_transform18(self, order):
  250. data = [[1, 2, 3, 4],
  251. [5, 6, 7, 8],
  252. [9, 10, 11, 12]]
  253. def mapping(x):
  254. return (x[0] * 2, x[1] * 2)
  255. out = ndimage.geometric_transform(data, mapping, (1, 2),
  256. order=order)
  257. assert_array_almost_equal(out, [[1, 3]])
  258. @pytest.mark.parametrize('order', range(0, 6))
  259. def test_geometric_transform19(self, order):
  260. data = [[1, 2, 3, 4],
  261. [5, 6, 7, 8],
  262. [9, 10, 11, 12]]
  263. def mapping(x):
  264. return (x[0], x[1] / 2)
  265. out = ndimage.geometric_transform(data, mapping, (3, 8),
  266. order=order)
  267. assert_array_almost_equal(out[..., ::2], data)
  268. @pytest.mark.parametrize('order', range(0, 6))
  269. def test_geometric_transform20(self, order):
  270. data = [[1, 2, 3, 4],
  271. [5, 6, 7, 8],
  272. [9, 10, 11, 12]]
  273. def mapping(x):
  274. return (x[0] / 2, x[1])
  275. out = ndimage.geometric_transform(data, mapping, (6, 4),
  276. order=order)
  277. assert_array_almost_equal(out[::2, ...], data)
  278. @pytest.mark.parametrize('order', range(0, 6))
  279. def test_geometric_transform21(self, order):
  280. data = [[1, 2, 3, 4],
  281. [5, 6, 7, 8],
  282. [9, 10, 11, 12]]
  283. def mapping(x):
  284. return (x[0] / 2, x[1] / 2)
  285. out = ndimage.geometric_transform(data, mapping, (6, 8),
  286. order=order)
  287. assert_array_almost_equal(out[::2, ::2], data)
  288. @pytest.mark.parametrize('order', range(0, 6))
  289. def test_geometric_transform22(self, order):
  290. data = numpy.array([[1, 2, 3, 4],
  291. [5, 6, 7, 8],
  292. [9, 10, 11, 12]], numpy.float64)
  293. def mapping1(x):
  294. return (x[0] / 2, x[1] / 2)
  295. def mapping2(x):
  296. return (x[0] * 2, x[1] * 2)
  297. out = ndimage.geometric_transform(data, mapping1,
  298. (6, 8), order=order)
  299. out = ndimage.geometric_transform(out, mapping2,
  300. (3, 4), order=order)
  301. assert_array_almost_equal(out, data)
  302. @pytest.mark.parametrize('order', range(0, 6))
  303. def test_geometric_transform23(self, order):
  304. data = [[1, 2, 3, 4],
  305. [5, 6, 7, 8],
  306. [9, 10, 11, 12]]
  307. def mapping(x):
  308. return (1, x[0] * 2)
  309. out = ndimage.geometric_transform(data, mapping, (2,), order=order)
  310. out = out.astype(numpy.int32)
  311. assert_array_almost_equal(out, [5, 7])
  312. @pytest.mark.parametrize('order', range(0, 6))
  313. def test_geometric_transform24(self, order):
  314. data = [[1, 2, 3, 4],
  315. [5, 6, 7, 8],
  316. [9, 10, 11, 12]]
  317. def mapping(x, a, b):
  318. return (a, x[0] * b)
  319. out = ndimage.geometric_transform(
  320. data, mapping, (2,), order=order, extra_arguments=(1,),
  321. extra_keywords={'b': 2})
  322. assert_array_almost_equal(out, [5, 7])
  323. def test_geometric_transform_grid_constant_order1(self):
  324. # verify interpolation outside the original bounds
  325. x = numpy.array([[1, 2, 3],
  326. [4, 5, 6]], dtype=float)
  327. def mapping(x):
  328. return (x[0] - 0.5), (x[1] - 0.5)
  329. expected_result = numpy.array([[0.25, 0.75, 1.25],
  330. [1.25, 3.00, 4.00]])
  331. assert_array_almost_equal(
  332. ndimage.geometric_transform(x, mapping, mode='grid-constant',
  333. order=1),
  334. expected_result,
  335. )
  336. @pytest.mark.parametrize('mode', ['grid-constant', 'grid-wrap', 'nearest',
  337. 'mirror', 'reflect'])
  338. @pytest.mark.parametrize('order', range(6))
  339. def test_geometric_transform_vs_padded(self, order, mode):
  340. x = numpy.arange(144, dtype=float).reshape(12, 12)
  341. def mapping(x):
  342. return (x[0] - 0.4), (x[1] + 2.3)
  343. # Manually pad and then extract center after the transform to get the
  344. # expected result.
  345. npad = 24
  346. pad_mode = ndimage_to_numpy_mode.get(mode)
  347. xp = numpy.pad(x, npad, mode=pad_mode)
  348. center_slice = tuple([slice(npad, -npad)] * x.ndim)
  349. expected_result = ndimage.geometric_transform(
  350. xp, mapping, mode=mode, order=order)[center_slice]
  351. assert_allclose(
  352. ndimage.geometric_transform(x, mapping, mode=mode,
  353. order=order),
  354. expected_result,
  355. rtol=1e-7,
  356. )
  357. def test_geometric_transform_endianness_with_output_parameter(self):
  358. # geometric transform given output ndarray or dtype with
  359. # non-native endianness. see issue #4127
  360. data = numpy.array([1])
  361. def mapping(x):
  362. return x
  363. for out in [data.dtype, data.dtype.newbyteorder(),
  364. numpy.empty_like(data),
  365. numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
  366. returned = ndimage.geometric_transform(data, mapping, data.shape,
  367. output=out)
  368. result = out if returned is None else returned
  369. assert_array_almost_equal(result, [1])
  370. def test_geometric_transform_with_string_output(self):
  371. data = numpy.array([1])
  372. def mapping(x):
  373. return x
  374. out = ndimage.geometric_transform(data, mapping, output='f')
  375. assert_(out.dtype is numpy.dtype('f'))
  376. assert_array_almost_equal(out, [1])
  377. @pytest.mark.parametrize('order', range(0, 6))
  378. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  379. def test_map_coordinates01(self, order, dtype):
  380. data = numpy.array([[4, 1, 3, 2],
  381. [7, 6, 8, 5],
  382. [3, 5, 3, 6]])
  383. expected = numpy.array([[0, 0, 0, 0],
  384. [0, 4, 1, 3],
  385. [0, 7, 6, 8]])
  386. if data.dtype.kind == 'c':
  387. data = data - 1j * data
  388. expected = expected - 1j * expected
  389. idx = numpy.indices(data.shape)
  390. idx -= 1
  391. out = ndimage.map_coordinates(data, idx, order=order)
  392. assert_array_almost_equal(out, expected)
  393. @pytest.mark.parametrize('order', range(0, 6))
  394. def test_map_coordinates02(self, order):
  395. data = numpy.array([[4, 1, 3, 2],
  396. [7, 6, 8, 5],
  397. [3, 5, 3, 6]])
  398. idx = numpy.indices(data.shape, numpy.float64)
  399. idx -= 0.5
  400. out1 = ndimage.shift(data, 0.5, order=order)
  401. out2 = ndimage.map_coordinates(data, idx, order=order)
  402. assert_array_almost_equal(out1, out2)
  403. def test_map_coordinates03(self):
  404. data = numpy.array([[4, 1, 3, 2],
  405. [7, 6, 8, 5],
  406. [3, 5, 3, 6]], order='F')
  407. idx = numpy.indices(data.shape) - 1
  408. out = ndimage.map_coordinates(data, idx)
  409. assert_array_almost_equal(out, [[0, 0, 0, 0],
  410. [0, 4, 1, 3],
  411. [0, 7, 6, 8]])
  412. assert_array_almost_equal(out, ndimage.shift(data, (1, 1)))
  413. idx = numpy.indices(data[::2].shape) - 1
  414. out = ndimage.map_coordinates(data[::2], idx)
  415. assert_array_almost_equal(out, [[0, 0, 0, 0],
  416. [0, 4, 1, 3]])
  417. assert_array_almost_equal(out, ndimage.shift(data[::2], (1, 1)))
  418. idx = numpy.indices(data[:, ::2].shape) - 1
  419. out = ndimage.map_coordinates(data[:, ::2], idx)
  420. assert_array_almost_equal(out, [[0, 0], [0, 4], [0, 7]])
  421. assert_array_almost_equal(out, ndimage.shift(data[:, ::2], (1, 1)))
  422. def test_map_coordinates_endianness_with_output_parameter(self):
  423. # output parameter given as array or dtype with either endianness
  424. # see issue #4127
  425. data = numpy.array([[1, 2], [7, 6]])
  426. expected = numpy.array([[0, 0], [0, 1]])
  427. idx = numpy.indices(data.shape)
  428. idx -= 1
  429. for out in [
  430. data.dtype,
  431. data.dtype.newbyteorder(),
  432. numpy.empty_like(expected),
  433. numpy.empty_like(expected).astype(expected.dtype.newbyteorder())
  434. ]:
  435. returned = ndimage.map_coordinates(data, idx, output=out)
  436. result = out if returned is None else returned
  437. assert_array_almost_equal(result, expected)
  438. def test_map_coordinates_with_string_output(self):
  439. data = numpy.array([[1]])
  440. idx = numpy.indices(data.shape)
  441. out = ndimage.map_coordinates(data, idx, output='f')
  442. assert_(out.dtype is numpy.dtype('f'))
  443. assert_array_almost_equal(out, [[1]])
  444. @pytest.mark.skipif('win32' in sys.platform or numpy.intp(0).itemsize < 8,
  445. reason='do not run on 32 bit or windows '
  446. '(no sparse memory)')
  447. def test_map_coordinates_large_data(self):
  448. # check crash on large data
  449. try:
  450. n = 30000
  451. a = numpy.empty(n**2, dtype=numpy.float32).reshape(n, n)
  452. # fill the part we might read
  453. a[n - 3:, n - 3:] = 0
  454. ndimage.map_coordinates(a, [[n - 1.5], [n - 1.5]], order=1)
  455. except MemoryError as e:
  456. raise pytest.skip('Not enough memory available') from e
  457. @pytest.mark.parametrize('order', range(0, 6))
  458. def test_affine_transform01(self, order):
  459. data = numpy.array([1])
  460. out = ndimage.affine_transform(data, [[1]], order=order)
  461. assert_array_almost_equal(out, [1])
  462. @pytest.mark.parametrize('order', range(0, 6))
  463. def test_affine_transform02(self, order):
  464. data = numpy.ones([4])
  465. out = ndimage.affine_transform(data, [[1]], order=order)
  466. assert_array_almost_equal(out, [1, 1, 1, 1])
  467. @pytest.mark.parametrize('order', range(0, 6))
  468. def test_affine_transform03(self, order):
  469. data = numpy.ones([4])
  470. out = ndimage.affine_transform(data, [[1]], -1, order=order)
  471. assert_array_almost_equal(out, [0, 1, 1, 1])
  472. @pytest.mark.parametrize('order', range(0, 6))
  473. def test_affine_transform04(self, order):
  474. data = numpy.array([4, 1, 3, 2])
  475. out = ndimage.affine_transform(data, [[1]], -1, order=order)
  476. assert_array_almost_equal(out, [0, 4, 1, 3])
  477. @pytest.mark.parametrize('order', range(0, 6))
  478. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  479. def test_affine_transform05(self, order, dtype):
  480. data = numpy.array([[1, 1, 1, 1],
  481. [1, 1, 1, 1],
  482. [1, 1, 1, 1]], dtype=dtype)
  483. expected = numpy.array([[0, 1, 1, 1],
  484. [0, 1, 1, 1],
  485. [0, 1, 1, 1]], dtype=dtype)
  486. if data.dtype.kind == 'c':
  487. data -= 1j * data
  488. expected -= 1j * expected
  489. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  490. [0, -1], order=order)
  491. assert_array_almost_equal(out, expected)
  492. @pytest.mark.parametrize('order', range(0, 6))
  493. def test_affine_transform06(self, order):
  494. data = numpy.array([[4, 1, 3, 2],
  495. [7, 6, 8, 5],
  496. [3, 5, 3, 6]])
  497. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  498. [0, -1], order=order)
  499. assert_array_almost_equal(out, [[0, 4, 1, 3],
  500. [0, 7, 6, 8],
  501. [0, 3, 5, 3]])
  502. @pytest.mark.parametrize('order', range(0, 6))
  503. def test_affine_transform07(self, order):
  504. data = numpy.array([[4, 1, 3, 2],
  505. [7, 6, 8, 5],
  506. [3, 5, 3, 6]])
  507. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  508. [-1, 0], order=order)
  509. assert_array_almost_equal(out, [[0, 0, 0, 0],
  510. [4, 1, 3, 2],
  511. [7, 6, 8, 5]])
  512. @pytest.mark.parametrize('order', range(0, 6))
  513. def test_affine_transform08(self, order):
  514. data = numpy.array([[4, 1, 3, 2],
  515. [7, 6, 8, 5],
  516. [3, 5, 3, 6]])
  517. out = ndimage.affine_transform(data, [[1, 0], [0, 1]],
  518. [-1, -1], order=order)
  519. assert_array_almost_equal(out, [[0, 0, 0, 0],
  520. [0, 4, 1, 3],
  521. [0, 7, 6, 8]])
  522. @pytest.mark.parametrize('order', range(0, 6))
  523. def test_affine_transform09(self, order):
  524. data = numpy.array([[4, 1, 3, 2],
  525. [7, 6, 8, 5],
  526. [3, 5, 3, 6]])
  527. if (order > 1):
  528. filtered = ndimage.spline_filter(data, order=order)
  529. else:
  530. filtered = data
  531. out = ndimage.affine_transform(filtered, [[1, 0], [0, 1]],
  532. [-1, -1], order=order,
  533. prefilter=False)
  534. assert_array_almost_equal(out, [[0, 0, 0, 0],
  535. [0, 4, 1, 3],
  536. [0, 7, 6, 8]])
  537. @pytest.mark.parametrize('order', range(0, 6))
  538. def test_affine_transform10(self, order):
  539. data = numpy.ones([2], numpy.float64)
  540. out = ndimage.affine_transform(data, [[0.5]], output_shape=(4,),
  541. order=order)
  542. assert_array_almost_equal(out, [1, 1, 1, 0])
  543. @pytest.mark.parametrize('order', range(0, 6))
  544. def test_affine_transform11(self, order):
  545. data = [1, 5, 2, 6, 3, 7, 4, 4]
  546. out = ndimage.affine_transform(data, [[2]], 0, (4,), order=order)
  547. assert_array_almost_equal(out, [1, 2, 3, 4])
  548. @pytest.mark.parametrize('order', range(0, 6))
  549. def test_affine_transform12(self, order):
  550. data = [1, 2, 3, 4]
  551. out = ndimage.affine_transform(data, [[0.5]], 0, (8,), order=order)
  552. assert_array_almost_equal(out[::2], [1, 2, 3, 4])
  553. @pytest.mark.parametrize('order', range(0, 6))
  554. def test_affine_transform13(self, order):
  555. data = [[1, 2, 3, 4],
  556. [5, 6, 7, 8],
  557. [9.0, 10, 11, 12]]
  558. out = ndimage.affine_transform(data, [[1, 0], [0, 2]], 0, (3, 2),
  559. order=order)
  560. assert_array_almost_equal(out, [[1, 3], [5, 7], [9, 11]])
  561. @pytest.mark.parametrize('order', range(0, 6))
  562. def test_affine_transform14(self, order):
  563. data = [[1, 2, 3, 4],
  564. [5, 6, 7, 8],
  565. [9, 10, 11, 12]]
  566. out = ndimage.affine_transform(data, [[2, 0], [0, 1]], 0, (1, 4),
  567. order=order)
  568. assert_array_almost_equal(out, [[1, 2, 3, 4]])
  569. @pytest.mark.parametrize('order', range(0, 6))
  570. def test_affine_transform15(self, order):
  571. data = [[1, 2, 3, 4],
  572. [5, 6, 7, 8],
  573. [9, 10, 11, 12]]
  574. out = ndimage.affine_transform(data, [[2, 0], [0, 2]], 0, (1, 2),
  575. order=order)
  576. assert_array_almost_equal(out, [[1, 3]])
  577. @pytest.mark.parametrize('order', range(0, 6))
  578. def test_affine_transform16(self, order):
  579. data = [[1, 2, 3, 4],
  580. [5, 6, 7, 8],
  581. [9, 10, 11, 12]]
  582. out = ndimage.affine_transform(data, [[1, 0.0], [0, 0.5]], 0,
  583. (3, 8), order=order)
  584. assert_array_almost_equal(out[..., ::2], data)
  585. @pytest.mark.parametrize('order', range(0, 6))
  586. def test_affine_transform17(self, order):
  587. data = [[1, 2, 3, 4],
  588. [5, 6, 7, 8],
  589. [9, 10, 11, 12]]
  590. out = ndimage.affine_transform(data, [[0.5, 0], [0, 1]], 0,
  591. (6, 4), order=order)
  592. assert_array_almost_equal(out[::2, ...], data)
  593. @pytest.mark.parametrize('order', range(0, 6))
  594. def test_affine_transform18(self, order):
  595. data = [[1, 2, 3, 4],
  596. [5, 6, 7, 8],
  597. [9, 10, 11, 12]]
  598. out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
  599. (6, 8), order=order)
  600. assert_array_almost_equal(out[::2, ::2], data)
  601. @pytest.mark.parametrize('order', range(0, 6))
  602. def test_affine_transform19(self, order):
  603. data = numpy.array([[1, 2, 3, 4],
  604. [5, 6, 7, 8],
  605. [9, 10, 11, 12]], numpy.float64)
  606. out = ndimage.affine_transform(data, [[0.5, 0], [0, 0.5]], 0,
  607. (6, 8), order=order)
  608. out = ndimage.affine_transform(out, [[2.0, 0], [0, 2.0]], 0,
  609. (3, 4), order=order)
  610. assert_array_almost_equal(out, data)
  611. @pytest.mark.parametrize('order', range(0, 6))
  612. def test_affine_transform20(self, order):
  613. data = [[1, 2, 3, 4],
  614. [5, 6, 7, 8],
  615. [9, 10, 11, 12]]
  616. out = ndimage.affine_transform(data, [[0], [2]], 0, (2,),
  617. order=order)
  618. assert_array_almost_equal(out, [1, 3])
  619. @pytest.mark.parametrize('order', range(0, 6))
  620. def test_affine_transform21(self, order):
  621. data = [[1, 2, 3, 4],
  622. [5, 6, 7, 8],
  623. [9, 10, 11, 12]]
  624. out = ndimage.affine_transform(data, [[2], [0]], 0, (2,),
  625. order=order)
  626. assert_array_almost_equal(out, [1, 9])
  627. @pytest.mark.parametrize('order', range(0, 6))
  628. def test_affine_transform22(self, order):
  629. # shift and offset interaction; see issue #1547
  630. data = numpy.array([4, 1, 3, 2])
  631. out = ndimage.affine_transform(data, [[2]], [-1], (3,),
  632. order=order)
  633. assert_array_almost_equal(out, [0, 1, 2])
  634. @pytest.mark.parametrize('order', range(0, 6))
  635. def test_affine_transform23(self, order):
  636. # shift and offset interaction; see issue #1547
  637. data = numpy.array([4, 1, 3, 2])
  638. out = ndimage.affine_transform(data, [[0.5]], [-1], (8,),
  639. order=order)
  640. assert_array_almost_equal(out[::2], [0, 4, 1, 3])
  641. @pytest.mark.parametrize('order', range(0, 6))
  642. def test_affine_transform24(self, order):
  643. # consistency between diagonal and non-diagonal case; see issue #1547
  644. data = numpy.array([4, 1, 3, 2])
  645. with suppress_warnings() as sup:
  646. sup.filter(UserWarning,
  647. 'The behavior of affine_transform with a 1-D array .* '
  648. 'has changed')
  649. out1 = ndimage.affine_transform(data, [2], -1, order=order)
  650. out2 = ndimage.affine_transform(data, [[2]], -1, order=order)
  651. assert_array_almost_equal(out1, out2)
  652. @pytest.mark.parametrize('order', range(0, 6))
  653. def test_affine_transform25(self, order):
  654. # consistency between diagonal and non-diagonal case; see issue #1547
  655. data = numpy.array([4, 1, 3, 2])
  656. with suppress_warnings() as sup:
  657. sup.filter(UserWarning,
  658. 'The behavior of affine_transform with a 1-D array .* '
  659. 'has changed')
  660. out1 = ndimage.affine_transform(data, [0.5], -1, order=order)
  661. out2 = ndimage.affine_transform(data, [[0.5]], -1, order=order)
  662. assert_array_almost_equal(out1, out2)
  663. @pytest.mark.parametrize('order', range(0, 6))
  664. def test_affine_transform26(self, order):
  665. # test homogeneous coordinates
  666. data = numpy.array([[4, 1, 3, 2],
  667. [7, 6, 8, 5],
  668. [3, 5, 3, 6]])
  669. if (order > 1):
  670. filtered = ndimage.spline_filter(data, order=order)
  671. else:
  672. filtered = data
  673. tform_original = numpy.eye(2)
  674. offset_original = -numpy.ones((2, 1))
  675. tform_h1 = numpy.hstack((tform_original, offset_original))
  676. tform_h2 = numpy.vstack((tform_h1, [[0, 0, 1]]))
  677. out1 = ndimage.affine_transform(filtered, tform_original,
  678. offset_original.ravel(),
  679. order=order, prefilter=False)
  680. out2 = ndimage.affine_transform(filtered, tform_h1, order=order,
  681. prefilter=False)
  682. out3 = ndimage.affine_transform(filtered, tform_h2, order=order,
  683. prefilter=False)
  684. for out in [out1, out2, out3]:
  685. assert_array_almost_equal(out, [[0, 0, 0, 0],
  686. [0, 4, 1, 3],
  687. [0, 7, 6, 8]])
  688. def test_affine_transform27(self):
  689. # test valid homogeneous transformation matrix
  690. data = numpy.array([[4, 1, 3, 2],
  691. [7, 6, 8, 5],
  692. [3, 5, 3, 6]])
  693. tform_h1 = numpy.hstack((numpy.eye(2), -numpy.ones((2, 1))))
  694. tform_h2 = numpy.vstack((tform_h1, [[5, 2, 1]]))
  695. assert_raises(ValueError, ndimage.affine_transform, data, tform_h2)
  696. def test_affine_transform_1d_endianness_with_output_parameter(self):
  697. # 1d affine transform given output ndarray or dtype with
  698. # either endianness. see issue #7388
  699. data = numpy.ones((2, 2))
  700. for out in [numpy.empty_like(data),
  701. numpy.empty_like(data).astype(data.dtype.newbyteorder()),
  702. data.dtype, data.dtype.newbyteorder()]:
  703. with suppress_warnings() as sup:
  704. sup.filter(UserWarning,
  705. 'The behavior of affine_transform with a 1-D array '
  706. '.* has changed')
  707. returned = ndimage.affine_transform(data, [1, 1], output=out)
  708. result = out if returned is None else returned
  709. assert_array_almost_equal(result, [[1, 1], [1, 1]])
  710. def test_affine_transform_multi_d_endianness_with_output_parameter(self):
  711. # affine transform given output ndarray or dtype with either endianness
  712. # see issue #4127
  713. data = numpy.array([1])
  714. for out in [data.dtype, data.dtype.newbyteorder(),
  715. numpy.empty_like(data),
  716. numpy.empty_like(data).astype(data.dtype.newbyteorder())]:
  717. returned = ndimage.affine_transform(data, [[1]], output=out)
  718. result = out if returned is None else returned
  719. assert_array_almost_equal(result, [1])
  720. def test_affine_transform_output_shape(self):
  721. # don't require output_shape when out of a different size is given
  722. data = numpy.arange(8, dtype=numpy.float64)
  723. out = numpy.ones((16,))
  724. oshape = out.shape
  725. ndimage.affine_transform(data, [[1]], output=out)
  726. assert_array_almost_equal(out[:8], data)
  727. # mismatched output shape raises an error
  728. with pytest.raises(RuntimeError):
  729. ndimage.affine_transform(
  730. data, [[1]], output=out, output_shape=(12,))
  731. def test_affine_transform_with_string_output(self):
  732. data = numpy.array([1])
  733. out = ndimage.affine_transform(data, [[1]], output='f')
  734. assert_(out.dtype is numpy.dtype('f'))
  735. assert_array_almost_equal(out, [1])
  736. @pytest.mark.parametrize('shift',
  737. [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
  738. @pytest.mark.parametrize('order', range(0, 6))
  739. def test_affine_transform_shift_via_grid_wrap(self, shift, order):
  740. # For mode 'grid-wrap', integer shifts should match numpy.roll
  741. x = numpy.array([[0, 1],
  742. [2, 3]])
  743. affine = numpy.zeros((2, 3))
  744. affine[:2, :2] = numpy.eye(2)
  745. affine[:, 2] = shift
  746. assert_array_almost_equal(
  747. ndimage.affine_transform(x, affine, mode='grid-wrap', order=order),
  748. numpy.roll(x, shift, axis=(0, 1)),
  749. )
  750. @pytest.mark.parametrize('order', range(0, 6))
  751. def test_affine_transform_shift_reflect(self, order):
  752. # shift by x.shape results in reflection
  753. x = numpy.array([[0, 1, 2],
  754. [3, 4, 5]])
  755. affine = numpy.zeros((2, 3))
  756. affine[:2, :2] = numpy.eye(2)
  757. affine[:, 2] = x.shape
  758. assert_array_almost_equal(
  759. ndimage.affine_transform(x, affine, mode='reflect', order=order),
  760. x[::-1, ::-1],
  761. )
  762. @pytest.mark.parametrize('order', range(0, 6))
  763. def test_shift01(self, order):
  764. data = numpy.array([1])
  765. out = ndimage.shift(data, [1], order=order)
  766. assert_array_almost_equal(out, [0])
  767. @pytest.mark.parametrize('order', range(0, 6))
  768. def test_shift02(self, order):
  769. data = numpy.ones([4])
  770. out = ndimage.shift(data, [1], order=order)
  771. assert_array_almost_equal(out, [0, 1, 1, 1])
  772. @pytest.mark.parametrize('order', range(0, 6))
  773. def test_shift03(self, order):
  774. data = numpy.ones([4])
  775. out = ndimage.shift(data, -1, order=order)
  776. assert_array_almost_equal(out, [1, 1, 1, 0])
  777. @pytest.mark.parametrize('order', range(0, 6))
  778. def test_shift04(self, order):
  779. data = numpy.array([4, 1, 3, 2])
  780. out = ndimage.shift(data, 1, order=order)
  781. assert_array_almost_equal(out, [0, 4, 1, 3])
  782. @pytest.mark.parametrize('order', range(0, 6))
  783. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  784. def test_shift05(self, order, dtype):
  785. data = numpy.array([[1, 1, 1, 1],
  786. [1, 1, 1, 1],
  787. [1, 1, 1, 1]], dtype=dtype)
  788. expected = numpy.array([[0, 1, 1, 1],
  789. [0, 1, 1, 1],
  790. [0, 1, 1, 1]], dtype=dtype)
  791. if data.dtype.kind == 'c':
  792. data -= 1j * data
  793. expected -= 1j * expected
  794. out = ndimage.shift(data, [0, 1], order=order)
  795. assert_array_almost_equal(out, expected)
  796. @pytest.mark.parametrize('order', range(0, 6))
  797. @pytest.mark.parametrize('mode', ['constant', 'grid-constant'])
  798. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  799. def test_shift_with_nonzero_cval(self, order, mode, dtype):
  800. data = numpy.array([[1, 1, 1, 1],
  801. [1, 1, 1, 1],
  802. [1, 1, 1, 1]], dtype=dtype)
  803. expected = numpy.array([[0, 1, 1, 1],
  804. [0, 1, 1, 1],
  805. [0, 1, 1, 1]], dtype=dtype)
  806. if data.dtype.kind == 'c':
  807. data -= 1j * data
  808. expected -= 1j * expected
  809. cval = 5.0
  810. expected[:, 0] = cval # specific to shift of [0, 1] used below
  811. out = ndimage.shift(data, [0, 1], order=order, mode=mode, cval=cval)
  812. assert_array_almost_equal(out, expected)
  813. @pytest.mark.parametrize('order', range(0, 6))
  814. def test_shift06(self, order):
  815. data = numpy.array([[4, 1, 3, 2],
  816. [7, 6, 8, 5],
  817. [3, 5, 3, 6]])
  818. out = ndimage.shift(data, [0, 1], order=order)
  819. assert_array_almost_equal(out, [[0, 4, 1, 3],
  820. [0, 7, 6, 8],
  821. [0, 3, 5, 3]])
  822. @pytest.mark.parametrize('order', range(0, 6))
  823. def test_shift07(self, order):
  824. data = numpy.array([[4, 1, 3, 2],
  825. [7, 6, 8, 5],
  826. [3, 5, 3, 6]])
  827. out = ndimage.shift(data, [1, 0], order=order)
  828. assert_array_almost_equal(out, [[0, 0, 0, 0],
  829. [4, 1, 3, 2],
  830. [7, 6, 8, 5]])
  831. @pytest.mark.parametrize('order', range(0, 6))
  832. def test_shift08(self, order):
  833. data = numpy.array([[4, 1, 3, 2],
  834. [7, 6, 8, 5],
  835. [3, 5, 3, 6]])
  836. out = ndimage.shift(data, [1, 1], order=order)
  837. assert_array_almost_equal(out, [[0, 0, 0, 0],
  838. [0, 4, 1, 3],
  839. [0, 7, 6, 8]])
  840. @pytest.mark.parametrize('order', range(0, 6))
  841. def test_shift09(self, order):
  842. data = numpy.array([[4, 1, 3, 2],
  843. [7, 6, 8, 5],
  844. [3, 5, 3, 6]])
  845. if (order > 1):
  846. filtered = ndimage.spline_filter(data, order=order)
  847. else:
  848. filtered = data
  849. out = ndimage.shift(filtered, [1, 1], order=order, prefilter=False)
  850. assert_array_almost_equal(out, [[0, 0, 0, 0],
  851. [0, 4, 1, 3],
  852. [0, 7, 6, 8]])
  853. @pytest.mark.parametrize('shift',
  854. [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
  855. @pytest.mark.parametrize('order', range(0, 6))
  856. def test_shift_grid_wrap(self, shift, order):
  857. # For mode 'grid-wrap', integer shifts should match numpy.roll
  858. x = numpy.array([[0, 1],
  859. [2, 3]])
  860. assert_array_almost_equal(
  861. ndimage.shift(x, shift, mode='grid-wrap', order=order),
  862. numpy.roll(x, shift, axis=(0, 1)),
  863. )
  864. @pytest.mark.parametrize('shift',
  865. [(1, 0), (0, 1), (-1, 1), (3, -5), (2, 7)])
  866. @pytest.mark.parametrize('order', range(0, 6))
  867. def test_shift_grid_constant1(self, shift, order):
  868. # For integer shifts, 'constant' and 'grid-constant' should be equal
  869. x = numpy.arange(20).reshape((5, 4))
  870. assert_array_almost_equal(
  871. ndimage.shift(x, shift, mode='grid-constant', order=order),
  872. ndimage.shift(x, shift, mode='constant', order=order),
  873. )
  874. def test_shift_grid_constant_order1(self):
  875. x = numpy.array([[1, 2, 3],
  876. [4, 5, 6]], dtype=float)
  877. expected_result = numpy.array([[0.25, 0.75, 1.25],
  878. [1.25, 3.00, 4.00]])
  879. assert_array_almost_equal(
  880. ndimage.shift(x, (0.5, 0.5), mode='grid-constant', order=1),
  881. expected_result,
  882. )
  883. @pytest.mark.parametrize('order', range(0, 6))
  884. def test_shift_reflect(self, order):
  885. # shift by x.shape results in reflection
  886. x = numpy.array([[0, 1, 2],
  887. [3, 4, 5]])
  888. assert_array_almost_equal(
  889. ndimage.shift(x, x.shape, mode='reflect', order=order),
  890. x[::-1, ::-1],
  891. )
  892. @pytest.mark.parametrize('order', range(0, 6))
  893. @pytest.mark.parametrize('prefilter', [False, True])
  894. def test_shift_nearest_boundary(self, order, prefilter):
  895. # verify that shifting at least order // 2 beyond the end of the array
  896. # gives a value equal to the edge value.
  897. x = numpy.arange(16)
  898. kwargs = dict(mode='nearest', order=order, prefilter=prefilter)
  899. assert_array_almost_equal(
  900. ndimage.shift(x, order // 2 + 1, **kwargs)[0], x[0],
  901. )
  902. assert_array_almost_equal(
  903. ndimage.shift(x, -order // 2 - 1, **kwargs)[-1], x[-1],
  904. )
  905. @pytest.mark.parametrize('mode', ['grid-constant', 'grid-wrap', 'nearest',
  906. 'mirror', 'reflect'])
  907. @pytest.mark.parametrize('order', range(6))
  908. def test_shift_vs_padded(self, order, mode):
  909. x = numpy.arange(144, dtype=float).reshape(12, 12)
  910. shift = (0.4, -2.3)
  911. # manually pad and then extract center to get expected result
  912. npad = 32
  913. pad_mode = ndimage_to_numpy_mode.get(mode)
  914. xp = numpy.pad(x, npad, mode=pad_mode)
  915. center_slice = tuple([slice(npad, -npad)] * x.ndim)
  916. expected_result = ndimage.shift(
  917. xp, shift, mode=mode, order=order)[center_slice]
  918. assert_allclose(
  919. ndimage.shift(x, shift, mode=mode, order=order),
  920. expected_result,
  921. rtol=1e-7,
  922. )
  923. @pytest.mark.parametrize('order', range(0, 6))
  924. def test_zoom1(self, order):
  925. for z in [2, [2, 2]]:
  926. arr = numpy.array(list(range(25))).reshape((5, 5)).astype(float)
  927. arr = ndimage.zoom(arr, z, order=order)
  928. assert_equal(arr.shape, (10, 10))
  929. assert_(numpy.all(arr[-1, :] != 0))
  930. assert_(numpy.all(arr[-1, :] >= (20 - eps)))
  931. assert_(numpy.all(arr[0, :] <= (5 + eps)))
  932. assert_(numpy.all(arr >= (0 - eps)))
  933. assert_(numpy.all(arr <= (24 + eps)))
  934. def test_zoom2(self):
  935. arr = numpy.arange(12).reshape((3, 4))
  936. out = ndimage.zoom(ndimage.zoom(arr, 2), 0.5)
  937. assert_array_equal(out, arr)
  938. def test_zoom3(self):
  939. arr = numpy.array([[1, 2]])
  940. out1 = ndimage.zoom(arr, (2, 1))
  941. out2 = ndimage.zoom(arr, (1, 2))
  942. assert_array_almost_equal(out1, numpy.array([[1, 2], [1, 2]]))
  943. assert_array_almost_equal(out2, numpy.array([[1, 1, 2, 2]]))
  944. @pytest.mark.parametrize('order', range(0, 6))
  945. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  946. def test_zoom_affine01(self, order, dtype):
  947. data = numpy.asarray([[1, 2, 3, 4],
  948. [5, 6, 7, 8],
  949. [9, 10, 11, 12]], dtype=dtype)
  950. if data.dtype.kind == 'c':
  951. data -= 1j * data
  952. with suppress_warnings() as sup:
  953. sup.filter(UserWarning,
  954. 'The behavior of affine_transform with a 1-D array .* '
  955. 'has changed')
  956. out = ndimage.affine_transform(data, [0.5, 0.5], 0,
  957. (6, 8), order=order)
  958. assert_array_almost_equal(out[::2, ::2], data)
  959. def test_zoom_infinity(self):
  960. # Ticket #1419 regression test
  961. dim = 8
  962. ndimage.zoom(numpy.zeros((dim, dim)), 1. / dim, mode='nearest')
  963. def test_zoom_zoomfactor_one(self):
  964. # Ticket #1122 regression test
  965. arr = numpy.zeros((1, 5, 5))
  966. zoom = (1.0, 2.0, 2.0)
  967. out = ndimage.zoom(arr, zoom, cval=7)
  968. ref = numpy.zeros((1, 10, 10))
  969. assert_array_almost_equal(out, ref)
  970. def test_zoom_output_shape_roundoff(self):
  971. arr = numpy.zeros((3, 11, 25))
  972. zoom = (4.0 / 3, 15.0 / 11, 29.0 / 25)
  973. out = ndimage.zoom(arr, zoom)
  974. assert_array_equal(out.shape, (4, 15, 29))
  975. @pytest.mark.parametrize('zoom', [(1, 1), (3, 5), (8, 2), (8, 8)])
  976. @pytest.mark.parametrize('mode', ['nearest', 'constant', 'wrap', 'reflect',
  977. 'mirror', 'grid-wrap', 'grid-mirror',
  978. 'grid-constant'])
  979. def test_zoom_by_int_order0(self, zoom, mode):
  980. # order 0 zoom should be the same as replication via numpy.kron
  981. # Note: This is not True for general x shapes when grid_mode is False,
  982. # but works here for all modes because the size ratio happens to
  983. # always be an integer when x.shape = (2, 2).
  984. x = numpy.array([[0, 1],
  985. [2, 3]], dtype=float)
  986. # x = numpy.arange(16, dtype=float).reshape(4, 4)
  987. assert_array_almost_equal(
  988. ndimage.zoom(x, zoom, order=0, mode=mode),
  989. numpy.kron(x, numpy.ones(zoom))
  990. )
  991. @pytest.mark.parametrize('shape', [(2, 3), (4, 4)])
  992. @pytest.mark.parametrize('zoom', [(1, 1), (3, 5), (8, 2), (8, 8)])
  993. @pytest.mark.parametrize('mode', ['nearest', 'reflect', 'mirror',
  994. 'grid-wrap', 'grid-constant'])
  995. def test_zoom_grid_by_int_order0(self, shape, zoom, mode):
  996. # When grid_mode is True, order 0 zoom should be the same as
  997. # replication via numpy.kron. The only exceptions to this are the
  998. # non-grid modes 'constant' and 'wrap'.
  999. x = numpy.arange(numpy.prod(shape), dtype=float).reshape(shape)
  1000. assert_array_almost_equal(
  1001. ndimage.zoom(x, zoom, order=0, mode=mode, grid_mode=True),
  1002. numpy.kron(x, numpy.ones(zoom))
  1003. )
  1004. @pytest.mark.parametrize('mode', ['constant', 'wrap'])
  1005. def test_zoom_grid_mode_warnings(self, mode):
  1006. # Warn on use of non-grid modes when grid_mode is True
  1007. x = numpy.arange(9, dtype=float).reshape((3, 3))
  1008. with pytest.warns(UserWarning,
  1009. match="It is recommended to use mode"):
  1010. ndimage.zoom(x, 2, mode=mode, grid_mode=True),
  1011. @pytest.mark.parametrize('order', range(0, 6))
  1012. def test_rotate01(self, order):
  1013. data = numpy.array([[0, 0, 0, 0],
  1014. [0, 1, 1, 0],
  1015. [0, 0, 0, 0]], dtype=numpy.float64)
  1016. out = ndimage.rotate(data, 0, order=order)
  1017. assert_array_almost_equal(out, data)
  1018. @pytest.mark.parametrize('order', range(0, 6))
  1019. def test_rotate02(self, order):
  1020. data = numpy.array([[0, 0, 0, 0],
  1021. [0, 1, 0, 0],
  1022. [0, 0, 0, 0]], dtype=numpy.float64)
  1023. expected = numpy.array([[0, 0, 0],
  1024. [0, 0, 0],
  1025. [0, 1, 0],
  1026. [0, 0, 0]], dtype=numpy.float64)
  1027. out = ndimage.rotate(data, 90, order=order)
  1028. assert_array_almost_equal(out, expected)
  1029. @pytest.mark.parametrize('order', range(0, 6))
  1030. @pytest.mark.parametrize('dtype', [numpy.float64, numpy.complex128])
  1031. def test_rotate03(self, order, dtype):
  1032. data = numpy.array([[0, 0, 0, 0, 0],
  1033. [0, 1, 1, 0, 0],
  1034. [0, 0, 0, 0, 0]], dtype=dtype)
  1035. expected = numpy.array([[0, 0, 0],
  1036. [0, 0, 0],
  1037. [0, 1, 0],
  1038. [0, 1, 0],
  1039. [0, 0, 0]], dtype=dtype)
  1040. if data.dtype.kind == 'c':
  1041. data -= 1j * data
  1042. expected -= 1j * expected
  1043. out = ndimage.rotate(data, 90, order=order)
  1044. assert_array_almost_equal(out, expected)
  1045. @pytest.mark.parametrize('order', range(0, 6))
  1046. def test_rotate04(self, order):
  1047. data = numpy.array([[0, 0, 0, 0, 0],
  1048. [0, 1, 1, 0, 0],
  1049. [0, 0, 0, 0, 0]], dtype=numpy.float64)
  1050. expected = numpy.array([[0, 0, 0, 0, 0],
  1051. [0, 0, 1, 0, 0],
  1052. [0, 0, 1, 0, 0]], dtype=numpy.float64)
  1053. out = ndimage.rotate(data, 90, reshape=False, order=order)
  1054. assert_array_almost_equal(out, expected)
  1055. @pytest.mark.parametrize('order', range(0, 6))
  1056. def test_rotate05(self, order):
  1057. data = numpy.empty((4, 3, 3))
  1058. for i in range(3):
  1059. data[:, :, i] = numpy.array([[0, 0, 0],
  1060. [0, 1, 0],
  1061. [0, 1, 0],
  1062. [0, 0, 0]], dtype=numpy.float64)
  1063. expected = numpy.array([[0, 0, 0, 0],
  1064. [0, 1, 1, 0],
  1065. [0, 0, 0, 0]], dtype=numpy.float64)
  1066. out = ndimage.rotate(data, 90, order=order)
  1067. for i in range(3):
  1068. assert_array_almost_equal(out[:, :, i], expected)
  1069. @pytest.mark.parametrize('order', range(0, 6))
  1070. def test_rotate06(self, order):
  1071. data = numpy.empty((3, 4, 3))
  1072. for i in range(3):
  1073. data[:, :, i] = numpy.array([[0, 0, 0, 0],
  1074. [0, 1, 1, 0],
  1075. [0, 0, 0, 0]], dtype=numpy.float64)
  1076. expected = numpy.array([[0, 0, 0],
  1077. [0, 1, 0],
  1078. [0, 1, 0],
  1079. [0, 0, 0]], dtype=numpy.float64)
  1080. out = ndimage.rotate(data, 90, order=order)
  1081. for i in range(3):
  1082. assert_array_almost_equal(out[:, :, i], expected)
  1083. @pytest.mark.parametrize('order', range(0, 6))
  1084. def test_rotate07(self, order):
  1085. data = numpy.array([[[0, 0, 0, 0, 0],
  1086. [0, 1, 1, 0, 0],
  1087. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1088. data = data.transpose()
  1089. expected = numpy.array([[[0, 0, 0],
  1090. [0, 1, 0],
  1091. [0, 1, 0],
  1092. [0, 0, 0],
  1093. [0, 0, 0]]] * 2, dtype=numpy.float64)
  1094. expected = expected.transpose([2, 1, 0])
  1095. out = ndimage.rotate(data, 90, axes=(0, 1), order=order)
  1096. assert_array_almost_equal(out, expected)
  1097. @pytest.mark.parametrize('order', range(0, 6))
  1098. def test_rotate08(self, order):
  1099. data = numpy.array([[[0, 0, 0, 0, 0],
  1100. [0, 1, 1, 0, 0],
  1101. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1102. data = data.transpose()
  1103. expected = numpy.array([[[0, 0, 1, 0, 0],
  1104. [0, 0, 1, 0, 0],
  1105. [0, 0, 0, 0, 0]]] * 2, dtype=numpy.float64)
  1106. expected = expected.transpose()
  1107. out = ndimage.rotate(data, 90, axes=(0, 1), reshape=False, order=order)
  1108. assert_array_almost_equal(out, expected)
  1109. def test_rotate09(self):
  1110. data = numpy.array([[0, 0, 0, 0, 0],
  1111. [0, 1, 1, 0, 0],
  1112. [0, 0, 0, 0, 0]] * 2, dtype=numpy.float64)
  1113. with assert_raises(ValueError):
  1114. ndimage.rotate(data, 90, axes=(0, data.ndim))
  1115. def test_rotate10(self):
  1116. data = numpy.arange(45, dtype=numpy.float64).reshape((3, 5, 3))
  1117. # The output of ndimage.rotate before refactoring
  1118. expected = numpy.array([[[0.0, 0.0, 0.0],
  1119. [0.0, 0.0, 0.0],
  1120. [6.54914793, 7.54914793, 8.54914793],
  1121. [10.84520162, 11.84520162, 12.84520162],
  1122. [0.0, 0.0, 0.0]],
  1123. [[6.19286575, 7.19286575, 8.19286575],
  1124. [13.4730712, 14.4730712, 15.4730712],
  1125. [21.0, 22.0, 23.0],
  1126. [28.5269288, 29.5269288, 30.5269288],
  1127. [35.80713425, 36.80713425, 37.80713425]],
  1128. [[0.0, 0.0, 0.0],
  1129. [31.15479838, 32.15479838, 33.15479838],
  1130. [35.45085207, 36.45085207, 37.45085207],
  1131. [0.0, 0.0, 0.0],
  1132. [0.0, 0.0, 0.0]]])
  1133. out = ndimage.rotate(data, angle=12, reshape=False)
  1134. assert_array_almost_equal(out, expected)
  1135. def test_rotate_exact_180(self):
  1136. a = numpy.tile(numpy.arange(5), (5, 1))
  1137. b = ndimage.rotate(ndimage.rotate(a, 180), -180)
  1138. assert_equal(a, b)
  1139. def test_zoom_output_shape():
  1140. """Ticket #643"""
  1141. x = numpy.arange(12).reshape((3, 4))
  1142. ndimage.zoom(x, 2, output=numpy.zeros((6, 8)))