test_arraypad.py 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. """Tests for the array padding functions.
  2. """
  3. import pytest
  4. import numpy as np
  5. from numpy.testing import assert_array_equal, assert_allclose, assert_equal
  6. from numpy.lib.arraypad import _as_pairs
  7. _numeric_dtypes = (
  8. np.sctypes["uint"]
  9. + np.sctypes["int"]
  10. + np.sctypes["float"]
  11. + np.sctypes["complex"]
  12. )
  13. _all_modes = {
  14. 'constant': {'constant_values': 0},
  15. 'edge': {},
  16. 'linear_ramp': {'end_values': 0},
  17. 'maximum': {'stat_length': None},
  18. 'mean': {'stat_length': None},
  19. 'median': {'stat_length': None},
  20. 'minimum': {'stat_length': None},
  21. 'reflect': {'reflect_type': 'even'},
  22. 'symmetric': {'reflect_type': 'even'},
  23. 'wrap': {},
  24. 'empty': {}
  25. }
  26. class TestAsPairs:
  27. def test_single_value(self):
  28. """Test casting for a single value."""
  29. expected = np.array([[3, 3]] * 10)
  30. for x in (3, [3], [[3]]):
  31. result = _as_pairs(x, 10)
  32. assert_equal(result, expected)
  33. # Test with dtype=object
  34. obj = object()
  35. assert_equal(
  36. _as_pairs(obj, 10),
  37. np.array([[obj, obj]] * 10)
  38. )
  39. def test_two_values(self):
  40. """Test proper casting for two different values."""
  41. # Broadcasting in the first dimension with numbers
  42. expected = np.array([[3, 4]] * 10)
  43. for x in ([3, 4], [[3, 4]]):
  44. result = _as_pairs(x, 10)
  45. assert_equal(result, expected)
  46. # and with dtype=object
  47. obj = object()
  48. assert_equal(
  49. _as_pairs(["a", obj], 10),
  50. np.array([["a", obj]] * 10)
  51. )
  52. # Broadcasting in the second / last dimension with numbers
  53. assert_equal(
  54. _as_pairs([[3], [4]], 2),
  55. np.array([[3, 3], [4, 4]])
  56. )
  57. # and with dtype=object
  58. assert_equal(
  59. _as_pairs([["a"], [obj]], 2),
  60. np.array([["a", "a"], [obj, obj]])
  61. )
  62. def test_with_none(self):
  63. expected = ((None, None), (None, None), (None, None))
  64. assert_equal(
  65. _as_pairs(None, 3, as_index=False),
  66. expected
  67. )
  68. assert_equal(
  69. _as_pairs(None, 3, as_index=True),
  70. expected
  71. )
  72. def test_pass_through(self):
  73. """Test if `x` already matching desired output are passed through."""
  74. expected = np.arange(12).reshape((6, 2))
  75. assert_equal(
  76. _as_pairs(expected, 6),
  77. expected
  78. )
  79. def test_as_index(self):
  80. """Test results if `as_index=True`."""
  81. assert_equal(
  82. _as_pairs([2.6, 3.3], 10, as_index=True),
  83. np.array([[3, 3]] * 10, dtype=np.intp)
  84. )
  85. assert_equal(
  86. _as_pairs([2.6, 4.49], 10, as_index=True),
  87. np.array([[3, 4]] * 10, dtype=np.intp)
  88. )
  89. for x in (-3, [-3], [[-3]], [-3, 4], [3, -4], [[-3, 4]], [[4, -3]],
  90. [[1, 2]] * 9 + [[1, -2]]):
  91. with pytest.raises(ValueError, match="negative values"):
  92. _as_pairs(x, 10, as_index=True)
  93. def test_exceptions(self):
  94. """Ensure faulty usage is discovered."""
  95. with pytest.raises(ValueError, match="more dimensions than allowed"):
  96. _as_pairs([[[3]]], 10)
  97. with pytest.raises(ValueError, match="could not be broadcast"):
  98. _as_pairs([[1, 2], [3, 4]], 3)
  99. with pytest.raises(ValueError, match="could not be broadcast"):
  100. _as_pairs(np.ones((2, 3)), 3)
  101. class TestConditionalShortcuts:
  102. @pytest.mark.parametrize("mode", _all_modes.keys())
  103. def test_zero_padding_shortcuts(self, mode):
  104. test = np.arange(120).reshape(4, 5, 6)
  105. pad_amt = [(0, 0) for _ in test.shape]
  106. assert_array_equal(test, np.pad(test, pad_amt, mode=mode))
  107. @pytest.mark.parametrize("mode", ['maximum', 'mean', 'median', 'minimum',])
  108. def test_shallow_statistic_range(self, mode):
  109. test = np.arange(120).reshape(4, 5, 6)
  110. pad_amt = [(1, 1) for _ in test.shape]
  111. assert_array_equal(np.pad(test, pad_amt, mode='edge'),
  112. np.pad(test, pad_amt, mode=mode, stat_length=1))
  113. @pytest.mark.parametrize("mode", ['maximum', 'mean', 'median', 'minimum',])
  114. def test_clip_statistic_range(self, mode):
  115. test = np.arange(30).reshape(5, 6)
  116. pad_amt = [(3, 3) for _ in test.shape]
  117. assert_array_equal(np.pad(test, pad_amt, mode=mode),
  118. np.pad(test, pad_amt, mode=mode, stat_length=30))
  119. class TestStatistic:
  120. def test_check_mean_stat_length(self):
  121. a = np.arange(100).astype('f')
  122. a = np.pad(a, ((25, 20), ), 'mean', stat_length=((2, 3), ))
  123. b = np.array(
  124. [0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  125. 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
  126. 0.5, 0.5, 0.5, 0.5, 0.5,
  127. 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
  128. 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
  129. 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
  130. 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
  131. 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
  132. 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
  133. 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
  134. 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
  135. 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
  136. 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
  137. 98., 98., 98., 98., 98., 98., 98., 98., 98., 98.,
  138. 98., 98., 98., 98., 98., 98., 98., 98., 98., 98.
  139. ])
  140. assert_array_equal(a, b)
  141. def test_check_maximum_1(self):
  142. a = np.arange(100)
  143. a = np.pad(a, (25, 20), 'maximum')
  144. b = np.array(
  145. [99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
  146. 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
  147. 99, 99, 99, 99, 99,
  148. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  149. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  150. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  151. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  152. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  153. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  154. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  155. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  156. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  157. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  158. 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
  159. 99, 99, 99, 99, 99, 99, 99, 99, 99, 99]
  160. )
  161. assert_array_equal(a, b)
  162. def test_check_maximum_2(self):
  163. a = np.arange(100) + 1
  164. a = np.pad(a, (25, 20), 'maximum')
  165. b = np.array(
  166. [100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  167. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  168. 100, 100, 100, 100, 100,
  169. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  170. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  171. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  172. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  173. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  174. 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  175. 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
  176. 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  177. 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  178. 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
  179. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  180. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
  181. )
  182. assert_array_equal(a, b)
  183. def test_check_maximum_stat_length(self):
  184. a = np.arange(100) + 1
  185. a = np.pad(a, (25, 20), 'maximum', stat_length=10)
  186. b = np.array(
  187. [10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  188. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  189. 10, 10, 10, 10, 10,
  190. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  191. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  192. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  193. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  194. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  195. 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  196. 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
  197. 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  198. 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  199. 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
  200. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
  201. 100, 100, 100, 100, 100, 100, 100, 100, 100, 100]
  202. )
  203. assert_array_equal(a, b)
  204. def test_check_minimum_1(self):
  205. a = np.arange(100)
  206. a = np.pad(a, (25, 20), 'minimum')
  207. b = np.array(
  208. [0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  209. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  210. 0, 0, 0, 0, 0,
  211. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  212. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  213. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  214. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  215. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  216. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  217. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  218. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  219. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  220. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  221. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  222. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  223. )
  224. assert_array_equal(a, b)
  225. def test_check_minimum_2(self):
  226. a = np.arange(100) + 2
  227. a = np.pad(a, (25, 20), 'minimum')
  228. b = np.array(
  229. [2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  230. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  231. 2, 2, 2, 2, 2,
  232. 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  233. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  234. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  235. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
  236. 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,
  237. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  238. 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
  239. 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,
  240. 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  241. 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
  242. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  243. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
  244. )
  245. assert_array_equal(a, b)
  246. def test_check_minimum_stat_length(self):
  247. a = np.arange(100) + 1
  248. a = np.pad(a, (25, 20), 'minimum', stat_length=10)
  249. b = np.array(
  250. [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  251. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  252. 1, 1, 1, 1, 1,
  253. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  254. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  255. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
  256. 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  257. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  258. 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
  259. 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
  260. 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
  261. 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  262. 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,
  263. 91, 91, 91, 91, 91, 91, 91, 91, 91, 91,
  264. 91, 91, 91, 91, 91, 91, 91, 91, 91, 91]
  265. )
  266. assert_array_equal(a, b)
  267. def test_check_median(self):
  268. a = np.arange(100).astype('f')
  269. a = np.pad(a, (25, 20), 'median')
  270. b = np.array(
  271. [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  272. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  273. 49.5, 49.5, 49.5, 49.5, 49.5,
  274. 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
  275. 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
  276. 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
  277. 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
  278. 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
  279. 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
  280. 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
  281. 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
  282. 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
  283. 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
  284. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  285. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
  286. )
  287. assert_array_equal(a, b)
  288. def test_check_median_01(self):
  289. a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
  290. a = np.pad(a, 1, 'median')
  291. b = np.array(
  292. [[4, 4, 5, 4, 4],
  293. [3, 3, 1, 4, 3],
  294. [5, 4, 5, 9, 5],
  295. [8, 9, 8, 2, 8],
  296. [4, 4, 5, 4, 4]]
  297. )
  298. assert_array_equal(a, b)
  299. def test_check_median_02(self):
  300. a = np.array([[3, 1, 4], [4, 5, 9], [9, 8, 2]])
  301. a = np.pad(a.T, 1, 'median').T
  302. b = np.array(
  303. [[5, 4, 5, 4, 5],
  304. [3, 3, 1, 4, 3],
  305. [5, 4, 5, 9, 5],
  306. [8, 9, 8, 2, 8],
  307. [5, 4, 5, 4, 5]]
  308. )
  309. assert_array_equal(a, b)
  310. def test_check_median_stat_length(self):
  311. a = np.arange(100).astype('f')
  312. a[1] = 2.
  313. a[97] = 96.
  314. a = np.pad(a, (25, 20), 'median', stat_length=(3, 5))
  315. b = np.array(
  316. [ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
  317. 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,
  318. 2., 2., 2., 2., 2.,
  319. 0., 2., 2., 3., 4., 5., 6., 7., 8., 9.,
  320. 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
  321. 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
  322. 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
  323. 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
  324. 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
  325. 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
  326. 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
  327. 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
  328. 90., 91., 92., 93., 94., 95., 96., 96., 98., 99.,
  329. 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.,
  330. 96., 96., 96., 96., 96., 96., 96., 96., 96., 96.]
  331. )
  332. assert_array_equal(a, b)
  333. def test_check_mean_shape_one(self):
  334. a = [[4, 5, 6]]
  335. a = np.pad(a, (5, 7), 'mean', stat_length=2)
  336. b = np.array(
  337. [[4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  338. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  339. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  340. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  341. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  342. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  343. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  344. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  345. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  346. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  347. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  348. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6],
  349. [4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6]]
  350. )
  351. assert_array_equal(a, b)
  352. def test_check_mean_2(self):
  353. a = np.arange(100).astype('f')
  354. a = np.pad(a, (25, 20), 'mean')
  355. b = np.array(
  356. [49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  357. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  358. 49.5, 49.5, 49.5, 49.5, 49.5,
  359. 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.,
  360. 10., 11., 12., 13., 14., 15., 16., 17., 18., 19.,
  361. 20., 21., 22., 23., 24., 25., 26., 27., 28., 29.,
  362. 30., 31., 32., 33., 34., 35., 36., 37., 38., 39.,
  363. 40., 41., 42., 43., 44., 45., 46., 47., 48., 49.,
  364. 50., 51., 52., 53., 54., 55., 56., 57., 58., 59.,
  365. 60., 61., 62., 63., 64., 65., 66., 67., 68., 69.,
  366. 70., 71., 72., 73., 74., 75., 76., 77., 78., 79.,
  367. 80., 81., 82., 83., 84., 85., 86., 87., 88., 89.,
  368. 90., 91., 92., 93., 94., 95., 96., 97., 98., 99.,
  369. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5,
  370. 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5, 49.5]
  371. )
  372. assert_array_equal(a, b)
  373. @pytest.mark.parametrize("mode", [
  374. "mean",
  375. "median",
  376. "minimum",
  377. "maximum"
  378. ])
  379. def test_same_prepend_append(self, mode):
  380. """ Test that appended and prepended values are equal """
  381. # This test is constructed to trigger floating point rounding errors in
  382. # a way that caused gh-11216 for mode=='mean'
  383. a = np.array([-1, 2, -1]) + np.array([0, 1e-12, 0], dtype=np.float64)
  384. a = np.pad(a, (1, 1), mode)
  385. assert_equal(a[0], a[-1])
  386. @pytest.mark.parametrize("mode", ["mean", "median", "minimum", "maximum"])
  387. @pytest.mark.parametrize(
  388. "stat_length", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))]
  389. )
  390. def test_check_negative_stat_length(self, mode, stat_length):
  391. arr = np.arange(30).reshape((6, 5))
  392. match = "index can't contain negative values"
  393. with pytest.raises(ValueError, match=match):
  394. np.pad(arr, 2, mode, stat_length=stat_length)
  395. def test_simple_stat_length(self):
  396. a = np.arange(30)
  397. a = np.reshape(a, (6, 5))
  398. a = np.pad(a, ((2, 3), (3, 2)), mode='mean', stat_length=(3,))
  399. b = np.array(
  400. [[6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
  401. [6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
  402. [1, 1, 1, 0, 1, 2, 3, 4, 3, 3],
  403. [6, 6, 6, 5, 6, 7, 8, 9, 8, 8],
  404. [11, 11, 11, 10, 11, 12, 13, 14, 13, 13],
  405. [16, 16, 16, 15, 16, 17, 18, 19, 18, 18],
  406. [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
  407. [26, 26, 26, 25, 26, 27, 28, 29, 28, 28],
  408. [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
  409. [21, 21, 21, 20, 21, 22, 23, 24, 23, 23],
  410. [21, 21, 21, 20, 21, 22, 23, 24, 23, 23]]
  411. )
  412. assert_array_equal(a, b)
  413. @pytest.mark.filterwarnings("ignore:Mean of empty slice:RuntimeWarning")
  414. @pytest.mark.filterwarnings(
  415. "ignore:invalid value encountered in( scalar)? divide:RuntimeWarning"
  416. )
  417. @pytest.mark.parametrize("mode", ["mean", "median"])
  418. def test_zero_stat_length_valid(self, mode):
  419. arr = np.pad([1., 2.], (1, 2), mode, stat_length=0)
  420. expected = np.array([np.nan, 1., 2., np.nan, np.nan])
  421. assert_equal(arr, expected)
  422. @pytest.mark.parametrize("mode", ["minimum", "maximum"])
  423. def test_zero_stat_length_invalid(self, mode):
  424. match = "stat_length of 0 yields no value for padding"
  425. with pytest.raises(ValueError, match=match):
  426. np.pad([1., 2.], 0, mode, stat_length=0)
  427. with pytest.raises(ValueError, match=match):
  428. np.pad([1., 2.], 0, mode, stat_length=(1, 0))
  429. with pytest.raises(ValueError, match=match):
  430. np.pad([1., 2.], 1, mode, stat_length=0)
  431. with pytest.raises(ValueError, match=match):
  432. np.pad([1., 2.], 1, mode, stat_length=(1, 0))
  433. class TestConstant:
  434. def test_check_constant(self):
  435. a = np.arange(100)
  436. a = np.pad(a, (25, 20), 'constant', constant_values=(10, 20))
  437. b = np.array(
  438. [10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  439. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  440. 10, 10, 10, 10, 10,
  441. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  442. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  443. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  444. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  445. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  446. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  447. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  448. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  449. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  450. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  451. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  452. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
  453. )
  454. assert_array_equal(a, b)
  455. def test_check_constant_zeros(self):
  456. a = np.arange(100)
  457. a = np.pad(a, (25, 20), 'constant')
  458. b = np.array(
  459. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  460. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  461. 0, 0, 0, 0, 0,
  462. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  463. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  464. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  465. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  466. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  467. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  468. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  469. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  470. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  471. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  472. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  473. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  474. )
  475. assert_array_equal(a, b)
  476. def test_check_constant_float(self):
  477. # If input array is int, but constant_values are float, the dtype of
  478. # the array to be padded is kept
  479. arr = np.arange(30).reshape(5, 6)
  480. test = np.pad(arr, (1, 2), mode='constant',
  481. constant_values=1.1)
  482. expected = np.array(
  483. [[ 1, 1, 1, 1, 1, 1, 1, 1, 1],
  484. [ 1, 0, 1, 2, 3, 4, 5, 1, 1],
  485. [ 1, 6, 7, 8, 9, 10, 11, 1, 1],
  486. [ 1, 12, 13, 14, 15, 16, 17, 1, 1],
  487. [ 1, 18, 19, 20, 21, 22, 23, 1, 1],
  488. [ 1, 24, 25, 26, 27, 28, 29, 1, 1],
  489. [ 1, 1, 1, 1, 1, 1, 1, 1, 1],
  490. [ 1, 1, 1, 1, 1, 1, 1, 1, 1]]
  491. )
  492. assert_allclose(test, expected)
  493. def test_check_constant_float2(self):
  494. # If input array is float, and constant_values are float, the dtype of
  495. # the array to be padded is kept - here retaining the float constants
  496. arr = np.arange(30).reshape(5, 6)
  497. arr_float = arr.astype(np.float64)
  498. test = np.pad(arr_float, ((1, 2), (1, 2)), mode='constant',
  499. constant_values=1.1)
  500. expected = np.array(
  501. [[ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
  502. [ 1.1, 0. , 1. , 2. , 3. , 4. , 5. , 1.1, 1.1],
  503. [ 1.1, 6. , 7. , 8. , 9. , 10. , 11. , 1.1, 1.1],
  504. [ 1.1, 12. , 13. , 14. , 15. , 16. , 17. , 1.1, 1.1],
  505. [ 1.1, 18. , 19. , 20. , 21. , 22. , 23. , 1.1, 1.1],
  506. [ 1.1, 24. , 25. , 26. , 27. , 28. , 29. , 1.1, 1.1],
  507. [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
  508. [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]]
  509. )
  510. assert_allclose(test, expected)
  511. def test_check_constant_float3(self):
  512. a = np.arange(100, dtype=float)
  513. a = np.pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2))
  514. b = np.array(
  515. [-1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
  516. -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1, -1.1,
  517. -1.1, -1.1, -1.1, -1.1, -1.1,
  518. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  519. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  520. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  521. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  522. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  523. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  524. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  525. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  526. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  527. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  528. -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2,
  529. -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2]
  530. )
  531. assert_allclose(a, b)
  532. def test_check_constant_odd_pad_amount(self):
  533. arr = np.arange(30).reshape(5, 6)
  534. test = np.pad(arr, ((1,), (2,)), mode='constant',
  535. constant_values=3)
  536. expected = np.array(
  537. [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
  538. [ 3, 3, 0, 1, 2, 3, 4, 5, 3, 3],
  539. [ 3, 3, 6, 7, 8, 9, 10, 11, 3, 3],
  540. [ 3, 3, 12, 13, 14, 15, 16, 17, 3, 3],
  541. [ 3, 3, 18, 19, 20, 21, 22, 23, 3, 3],
  542. [ 3, 3, 24, 25, 26, 27, 28, 29, 3, 3],
  543. [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]
  544. )
  545. assert_allclose(test, expected)
  546. def test_check_constant_pad_2d(self):
  547. arr = np.arange(4).reshape(2, 2)
  548. test = np.lib.pad(arr, ((1, 2), (1, 3)), mode='constant',
  549. constant_values=((1, 2), (3, 4)))
  550. expected = np.array(
  551. [[3, 1, 1, 4, 4, 4],
  552. [3, 0, 1, 4, 4, 4],
  553. [3, 2, 3, 4, 4, 4],
  554. [3, 2, 2, 4, 4, 4],
  555. [3, 2, 2, 4, 4, 4]]
  556. )
  557. assert_allclose(test, expected)
  558. def test_check_large_integers(self):
  559. uint64_max = 2 ** 64 - 1
  560. arr = np.full(5, uint64_max, dtype=np.uint64)
  561. test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
  562. expected = np.full(7, uint64_max, dtype=np.uint64)
  563. assert_array_equal(test, expected)
  564. int64_max = 2 ** 63 - 1
  565. arr = np.full(5, int64_max, dtype=np.int64)
  566. test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
  567. expected = np.full(7, int64_max, dtype=np.int64)
  568. assert_array_equal(test, expected)
  569. def test_check_object_array(self):
  570. arr = np.empty(1, dtype=object)
  571. obj_a = object()
  572. arr[0] = obj_a
  573. obj_b = object()
  574. obj_c = object()
  575. arr = np.pad(arr, pad_width=1, mode='constant',
  576. constant_values=(obj_b, obj_c))
  577. expected = np.empty((3,), dtype=object)
  578. expected[0] = obj_b
  579. expected[1] = obj_a
  580. expected[2] = obj_c
  581. assert_array_equal(arr, expected)
  582. def test_pad_empty_dimension(self):
  583. arr = np.zeros((3, 0, 2))
  584. result = np.pad(arr, [(0,), (2,), (1,)], mode="constant")
  585. assert result.shape == (3, 4, 4)
  586. class TestLinearRamp:
  587. def test_check_simple(self):
  588. a = np.arange(100).astype('f')
  589. a = np.pad(a, (25, 20), 'linear_ramp', end_values=(4, 5))
  590. b = np.array(
  591. [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
  592. 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
  593. 0.80, 0.64, 0.48, 0.32, 0.16,
  594. 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
  595. 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
  596. 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
  597. 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
  598. 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
  599. 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
  600. 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
  601. 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
  602. 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
  603. 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
  604. 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
  605. 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]
  606. )
  607. assert_allclose(a, b, rtol=1e-5, atol=1e-5)
  608. def test_check_2d(self):
  609. arr = np.arange(20).reshape(4, 5).astype(np.float64)
  610. test = np.pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0))
  611. expected = np.array(
  612. [[0., 0., 0., 0., 0., 0., 0., 0., 0.],
  613. [0., 0., 0., 0.5, 1., 1.5, 2., 1., 0.],
  614. [0., 0., 0., 1., 2., 3., 4., 2., 0.],
  615. [0., 2.5, 5., 6., 7., 8., 9., 4.5, 0.],
  616. [0., 5., 10., 11., 12., 13., 14., 7., 0.],
  617. [0., 7.5, 15., 16., 17., 18., 19., 9.5, 0.],
  618. [0., 3.75, 7.5, 8., 8.5, 9., 9.5, 4.75, 0.],
  619. [0., 0., 0., 0., 0., 0., 0., 0., 0.]])
  620. assert_allclose(test, expected)
  621. @pytest.mark.xfail(exceptions=(AssertionError,))
  622. def test_object_array(self):
  623. from fractions import Fraction
  624. arr = np.array([Fraction(1, 2), Fraction(-1, 2)])
  625. actual = np.pad(arr, (2, 3), mode='linear_ramp', end_values=0)
  626. # deliberately chosen to have a non-power-of-2 denominator such that
  627. # rounding to floats causes a failure.
  628. expected = np.array([
  629. Fraction( 0, 12),
  630. Fraction( 3, 12),
  631. Fraction( 6, 12),
  632. Fraction(-6, 12),
  633. Fraction(-4, 12),
  634. Fraction(-2, 12),
  635. Fraction(-0, 12),
  636. ])
  637. assert_equal(actual, expected)
  638. def test_end_values(self):
  639. """Ensure that end values are exact."""
  640. a = np.pad(np.ones(10).reshape(2, 5), (223, 123), mode="linear_ramp")
  641. assert_equal(a[:, 0], 0.)
  642. assert_equal(a[:, -1], 0.)
  643. assert_equal(a[0, :], 0.)
  644. assert_equal(a[-1, :], 0.)
  645. @pytest.mark.parametrize("dtype", _numeric_dtypes)
  646. def test_negative_difference(self, dtype):
  647. """
  648. Check correct behavior of unsigned dtypes if there is a negative
  649. difference between the edge to pad and `end_values`. Check both cases
  650. to be independent of implementation. Test behavior for all other dtypes
  651. in case dtype casting interferes with complex dtypes. See gh-14191.
  652. """
  653. x = np.array([3], dtype=dtype)
  654. result = np.pad(x, 3, mode="linear_ramp", end_values=0)
  655. expected = np.array([0, 1, 2, 3, 2, 1, 0], dtype=dtype)
  656. assert_equal(result, expected)
  657. x = np.array([0], dtype=dtype)
  658. result = np.pad(x, 3, mode="linear_ramp", end_values=3)
  659. expected = np.array([3, 2, 1, 0, 1, 2, 3], dtype=dtype)
  660. assert_equal(result, expected)
  661. class TestReflect:
  662. def test_check_simple(self):
  663. a = np.arange(100)
  664. a = np.pad(a, (25, 20), 'reflect')
  665. b = np.array(
  666. [25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
  667. 15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
  668. 5, 4, 3, 2, 1,
  669. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  670. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  671. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  672. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  673. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  674. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  675. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  676. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  677. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  678. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  679. 98, 97, 96, 95, 94, 93, 92, 91, 90, 89,
  680. 88, 87, 86, 85, 84, 83, 82, 81, 80, 79]
  681. )
  682. assert_array_equal(a, b)
  683. def test_check_odd_method(self):
  684. a = np.arange(100)
  685. a = np.pad(a, (25, 20), 'reflect', reflect_type='odd')
  686. b = np.array(
  687. [-25, -24, -23, -22, -21, -20, -19, -18, -17, -16,
  688. -15, -14, -13, -12, -11, -10, -9, -8, -7, -6,
  689. -5, -4, -3, -2, -1,
  690. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  691. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  692. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  693. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  694. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  695. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  696. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  697. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  698. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  699. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  700. 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  701. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
  702. )
  703. assert_array_equal(a, b)
  704. def test_check_large_pad(self):
  705. a = [[4, 5, 6], [6, 7, 8]]
  706. a = np.pad(a, (5, 7), 'reflect')
  707. b = np.array(
  708. [[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  709. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  710. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  711. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  712. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  713. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  714. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  715. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  716. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  717. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  718. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  719. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  720. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  721. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
  722. )
  723. assert_array_equal(a, b)
  724. def test_check_shape(self):
  725. a = [[4, 5, 6]]
  726. a = np.pad(a, (5, 7), 'reflect')
  727. b = np.array(
  728. [[5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  729. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  730. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  731. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  732. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  733. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  734. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  735. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  736. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  737. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  738. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  739. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  740. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
  741. )
  742. assert_array_equal(a, b)
  743. def test_check_01(self):
  744. a = np.pad([1, 2, 3], 2, 'reflect')
  745. b = np.array([3, 2, 1, 2, 3, 2, 1])
  746. assert_array_equal(a, b)
  747. def test_check_02(self):
  748. a = np.pad([1, 2, 3], 3, 'reflect')
  749. b = np.array([2, 3, 2, 1, 2, 3, 2, 1, 2])
  750. assert_array_equal(a, b)
  751. def test_check_03(self):
  752. a = np.pad([1, 2, 3], 4, 'reflect')
  753. b = np.array([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3])
  754. assert_array_equal(a, b)
  755. class TestEmptyArray:
  756. """Check how padding behaves on arrays with an empty dimension."""
  757. @pytest.mark.parametrize(
  758. # Keep parametrization ordered, otherwise pytest-xdist might believe
  759. # that different tests were collected during parallelization
  760. "mode", sorted(_all_modes.keys() - {"constant", "empty"})
  761. )
  762. def test_pad_empty_dimension(self, mode):
  763. match = ("can't extend empty axis 0 using modes other than 'constant' "
  764. "or 'empty'")
  765. with pytest.raises(ValueError, match=match):
  766. np.pad([], 4, mode=mode)
  767. with pytest.raises(ValueError, match=match):
  768. np.pad(np.ndarray(0), 4, mode=mode)
  769. with pytest.raises(ValueError, match=match):
  770. np.pad(np.zeros((0, 3)), ((1,), (0,)), mode=mode)
  771. @pytest.mark.parametrize("mode", _all_modes.keys())
  772. def test_pad_non_empty_dimension(self, mode):
  773. result = np.pad(np.ones((2, 0, 2)), ((3,), (0,), (1,)), mode=mode)
  774. assert result.shape == (8, 0, 4)
  775. class TestSymmetric:
  776. def test_check_simple(self):
  777. a = np.arange(100)
  778. a = np.pad(a, (25, 20), 'symmetric')
  779. b = np.array(
  780. [24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
  781. 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
  782. 4, 3, 2, 1, 0,
  783. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  784. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  785. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  786. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  787. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  788. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  789. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  790. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  791. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  792. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  793. 99, 98, 97, 96, 95, 94, 93, 92, 91, 90,
  794. 89, 88, 87, 86, 85, 84, 83, 82, 81, 80]
  795. )
  796. assert_array_equal(a, b)
  797. def test_check_odd_method(self):
  798. a = np.arange(100)
  799. a = np.pad(a, (25, 20), 'symmetric', reflect_type='odd')
  800. b = np.array(
  801. [-24, -23, -22, -21, -20, -19, -18, -17, -16, -15,
  802. -14, -13, -12, -11, -10, -9, -8, -7, -6, -5,
  803. -4, -3, -2, -1, 0,
  804. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  805. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  806. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  807. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  808. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  809. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  810. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  811. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  812. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  813. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  814. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
  815. 109, 110, 111, 112, 113, 114, 115, 116, 117, 118]
  816. )
  817. assert_array_equal(a, b)
  818. def test_check_large_pad(self):
  819. a = [[4, 5, 6], [6, 7, 8]]
  820. a = np.pad(a, (5, 7), 'symmetric')
  821. b = np.array(
  822. [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  823. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  824. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  825. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  826. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  827. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  828. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  829. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  830. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  831. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  832. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  833. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  834. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  835. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
  836. )
  837. assert_array_equal(a, b)
  838. def test_check_large_pad_odd(self):
  839. a = [[4, 5, 6], [6, 7, 8]]
  840. a = np.pad(a, (5, 7), 'symmetric', reflect_type='odd')
  841. b = np.array(
  842. [[-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
  843. [-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
  844. [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
  845. [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
  846. [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
  847. [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
  848. [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
  849. [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
  850. [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
  851. [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
  852. [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
  853. [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
  854. [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18],
  855. [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18]]
  856. )
  857. assert_array_equal(a, b)
  858. def test_check_shape(self):
  859. a = [[4, 5, 6]]
  860. a = np.pad(a, (5, 7), 'symmetric')
  861. b = np.array(
  862. [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  863. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  864. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  865. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  866. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  867. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  868. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  869. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  870. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  871. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  872. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  873. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  874. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
  875. )
  876. assert_array_equal(a, b)
  877. def test_check_01(self):
  878. a = np.pad([1, 2, 3], 2, 'symmetric')
  879. b = np.array([2, 1, 1, 2, 3, 3, 2])
  880. assert_array_equal(a, b)
  881. def test_check_02(self):
  882. a = np.pad([1, 2, 3], 3, 'symmetric')
  883. b = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
  884. assert_array_equal(a, b)
  885. def test_check_03(self):
  886. a = np.pad([1, 2, 3], 6, 'symmetric')
  887. b = np.array([1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3])
  888. assert_array_equal(a, b)
  889. class TestWrap:
  890. def test_check_simple(self):
  891. a = np.arange(100)
  892. a = np.pad(a, (25, 20), 'wrap')
  893. b = np.array(
  894. [75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
  895. 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
  896. 95, 96, 97, 98, 99,
  897. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  898. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  899. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  900. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  901. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  902. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  903. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  904. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  905. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  906. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  907. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  908. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  909. )
  910. assert_array_equal(a, b)
  911. def test_check_large_pad(self):
  912. a = np.arange(12)
  913. a = np.reshape(a, (3, 4))
  914. a = np.pad(a, (10, 12), 'wrap')
  915. b = np.array(
  916. [[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  917. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  918. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  919. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  920. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  921. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  922. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  923. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  924. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  925. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  926. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  927. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  928. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  929. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  930. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  931. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  932. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  933. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  934. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  935. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  936. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  937. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  938. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  939. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  940. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  941. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  942. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  943. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  944. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  945. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  946. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  947. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  948. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  949. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  950. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  951. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  952. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  953. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  954. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  955. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  956. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  957. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  958. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  959. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  960. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  961. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  962. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  963. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  964. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  965. 11, 8, 9, 10, 11, 8, 9, 10, 11]]
  966. )
  967. assert_array_equal(a, b)
  968. def test_check_01(self):
  969. a = np.pad([1, 2, 3], 3, 'wrap')
  970. b = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3])
  971. assert_array_equal(a, b)
  972. def test_check_02(self):
  973. a = np.pad([1, 2, 3], 4, 'wrap')
  974. b = np.array([3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1])
  975. assert_array_equal(a, b)
  976. def test_pad_with_zero(self):
  977. a = np.ones((3, 5))
  978. b = np.pad(a, (0, 5), mode="wrap")
  979. assert_array_equal(a, b[:-5, :-5])
  980. def test_repeated_wrapping(self):
  981. """
  982. Check wrapping on each side individually if the wrapped area is longer
  983. than the original array.
  984. """
  985. a = np.arange(5)
  986. b = np.pad(a, (12, 0), mode="wrap")
  987. assert_array_equal(np.r_[a, a, a, a][3:], b)
  988. a = np.arange(5)
  989. b = np.pad(a, (0, 12), mode="wrap")
  990. assert_array_equal(np.r_[a, a, a, a][:-3], b)
  991. class TestEdge:
  992. def test_check_simple(self):
  993. a = np.arange(12)
  994. a = np.reshape(a, (4, 3))
  995. a = np.pad(a, ((2, 3), (3, 2)), 'edge')
  996. b = np.array(
  997. [[0, 0, 0, 0, 1, 2, 2, 2],
  998. [0, 0, 0, 0, 1, 2, 2, 2],
  999. [0, 0, 0, 0, 1, 2, 2, 2],
  1000. [3, 3, 3, 3, 4, 5, 5, 5],
  1001. [6, 6, 6, 6, 7, 8, 8, 8],
  1002. [9, 9, 9, 9, 10, 11, 11, 11],
  1003. [9, 9, 9, 9, 10, 11, 11, 11],
  1004. [9, 9, 9, 9, 10, 11, 11, 11],
  1005. [9, 9, 9, 9, 10, 11, 11, 11]]
  1006. )
  1007. assert_array_equal(a, b)
  1008. def test_check_width_shape_1_2(self):
  1009. # Check a pad_width of the form ((1, 2),).
  1010. # Regression test for issue gh-7808.
  1011. a = np.array([1, 2, 3])
  1012. padded = np.pad(a, ((1, 2),), 'edge')
  1013. expected = np.array([1, 1, 2, 3, 3, 3])
  1014. assert_array_equal(padded, expected)
  1015. a = np.array([[1, 2, 3], [4, 5, 6]])
  1016. padded = np.pad(a, ((1, 2),), 'edge')
  1017. expected = np.pad(a, ((1, 2), (1, 2)), 'edge')
  1018. assert_array_equal(padded, expected)
  1019. a = np.arange(24).reshape(2, 3, 4)
  1020. padded = np.pad(a, ((1, 2),), 'edge')
  1021. expected = np.pad(a, ((1, 2), (1, 2), (1, 2)), 'edge')
  1022. assert_array_equal(padded, expected)
  1023. class TestEmpty:
  1024. def test_simple(self):
  1025. arr = np.arange(24).reshape(4, 6)
  1026. result = np.pad(arr, [(2, 3), (3, 1)], mode="empty")
  1027. assert result.shape == (9, 10)
  1028. assert_equal(arr, result[2:-3, 3:-1])
  1029. def test_pad_empty_dimension(self):
  1030. arr = np.zeros((3, 0, 2))
  1031. result = np.pad(arr, [(0,), (2,), (1,)], mode="empty")
  1032. assert result.shape == (3, 4, 4)
  1033. def test_legacy_vector_functionality():
  1034. def _padwithtens(vector, pad_width, iaxis, kwargs):
  1035. vector[:pad_width[0]] = 10
  1036. vector[-pad_width[1]:] = 10
  1037. a = np.arange(6).reshape(2, 3)
  1038. a = np.pad(a, 2, _padwithtens)
  1039. b = np.array(
  1040. [[10, 10, 10, 10, 10, 10, 10],
  1041. [10, 10, 10, 10, 10, 10, 10],
  1042. [10, 10, 0, 1, 2, 10, 10],
  1043. [10, 10, 3, 4, 5, 10, 10],
  1044. [10, 10, 10, 10, 10, 10, 10],
  1045. [10, 10, 10, 10, 10, 10, 10]]
  1046. )
  1047. assert_array_equal(a, b)
  1048. def test_unicode_mode():
  1049. a = np.pad([1], 2, mode='constant')
  1050. b = np.array([0, 0, 1, 0, 0])
  1051. assert_array_equal(a, b)
  1052. @pytest.mark.parametrize("mode", ["edge", "symmetric", "reflect", "wrap"])
  1053. def test_object_input(mode):
  1054. # Regression test for issue gh-11395.
  1055. a = np.full((4, 3), fill_value=None)
  1056. pad_amt = ((2, 3), (3, 2))
  1057. b = np.full((9, 8), fill_value=None)
  1058. assert_array_equal(np.pad(a, pad_amt, mode=mode), b)
  1059. class TestPadWidth:
  1060. @pytest.mark.parametrize("pad_width", [
  1061. (4, 5, 6, 7),
  1062. ((1,), (2,), (3,)),
  1063. ((1, 2), (3, 4), (5, 6)),
  1064. ((3, 4, 5), (0, 1, 2)),
  1065. ])
  1066. @pytest.mark.parametrize("mode", _all_modes.keys())
  1067. def test_misshaped_pad_width(self, pad_width, mode):
  1068. arr = np.arange(30).reshape((6, 5))
  1069. match = "operands could not be broadcast together"
  1070. with pytest.raises(ValueError, match=match):
  1071. np.pad(arr, pad_width, mode)
  1072. @pytest.mark.parametrize("mode", _all_modes.keys())
  1073. def test_misshaped_pad_width_2(self, mode):
  1074. arr = np.arange(30).reshape((6, 5))
  1075. match = ("input operand has more dimensions than allowed by the axis "
  1076. "remapping")
  1077. with pytest.raises(ValueError, match=match):
  1078. np.pad(arr, (((3,), (4,), (5,)), ((0,), (1,), (2,))), mode)
  1079. @pytest.mark.parametrize(
  1080. "pad_width", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))])
  1081. @pytest.mark.parametrize("mode", _all_modes.keys())
  1082. def test_negative_pad_width(self, pad_width, mode):
  1083. arr = np.arange(30).reshape((6, 5))
  1084. match = "index can't contain negative values"
  1085. with pytest.raises(ValueError, match=match):
  1086. np.pad(arr, pad_width, mode)
  1087. @pytest.mark.parametrize("pad_width, dtype", [
  1088. ("3", None),
  1089. ("word", None),
  1090. (None, None),
  1091. (object(), None),
  1092. (3.4, None),
  1093. (((2, 3, 4), (3, 2)), object),
  1094. (complex(1, -1), None),
  1095. (((-2.1, 3), (3, 2)), None),
  1096. ])
  1097. @pytest.mark.parametrize("mode", _all_modes.keys())
  1098. def test_bad_type(self, pad_width, dtype, mode):
  1099. arr = np.arange(30).reshape((6, 5))
  1100. match = "`pad_width` must be of integral type."
  1101. if dtype is not None:
  1102. # avoid DeprecationWarning when not specifying dtype
  1103. with pytest.raises(TypeError, match=match):
  1104. np.pad(arr, np.array(pad_width, dtype=dtype), mode)
  1105. else:
  1106. with pytest.raises(TypeError, match=match):
  1107. np.pad(arr, pad_width, mode)
  1108. with pytest.raises(TypeError, match=match):
  1109. np.pad(arr, np.array(pad_width), mode)
  1110. def test_pad_width_as_ndarray(self):
  1111. a = np.arange(12)
  1112. a = np.reshape(a, (4, 3))
  1113. a = np.pad(a, np.array(((2, 3), (3, 2))), 'edge')
  1114. b = np.array(
  1115. [[0, 0, 0, 0, 1, 2, 2, 2],
  1116. [0, 0, 0, 0, 1, 2, 2, 2],
  1117. [0, 0, 0, 0, 1, 2, 2, 2],
  1118. [3, 3, 3, 3, 4, 5, 5, 5],
  1119. [6, 6, 6, 6, 7, 8, 8, 8],
  1120. [9, 9, 9, 9, 10, 11, 11, 11],
  1121. [9, 9, 9, 9, 10, 11, 11, 11],
  1122. [9, 9, 9, 9, 10, 11, 11, 11],
  1123. [9, 9, 9, 9, 10, 11, 11, 11]]
  1124. )
  1125. assert_array_equal(a, b)
  1126. @pytest.mark.parametrize("pad_width", [0, (0, 0), ((0, 0), (0, 0))])
  1127. @pytest.mark.parametrize("mode", _all_modes.keys())
  1128. def test_zero_pad_width(self, pad_width, mode):
  1129. arr = np.arange(30).reshape(6, 5)
  1130. assert_array_equal(arr, np.pad(arr, pad_width, mode=mode))
  1131. @pytest.mark.parametrize("mode", _all_modes.keys())
  1132. def test_kwargs(mode):
  1133. """Test behavior of pad's kwargs for the given mode."""
  1134. allowed = _all_modes[mode]
  1135. not_allowed = {}
  1136. for kwargs in _all_modes.values():
  1137. if kwargs != allowed:
  1138. not_allowed.update(kwargs)
  1139. # Test if allowed keyword arguments pass
  1140. np.pad([1, 2, 3], 1, mode, **allowed)
  1141. # Test if prohibited keyword arguments of other modes raise an error
  1142. for key, value in not_allowed.items():
  1143. match = "unsupported keyword arguments for mode '{}'".format(mode)
  1144. with pytest.raises(ValueError, match=match):
  1145. np.pad([1, 2, 3], 1, mode, **{key: value})
  1146. def test_constant_zero_default():
  1147. arr = np.array([1, 1])
  1148. assert_array_equal(np.pad(arr, 2), [0, 0, 1, 1, 0, 0])
  1149. @pytest.mark.parametrize("mode", [1, "const", object(), None, True, False])
  1150. def test_unsupported_mode(mode):
  1151. match= "mode '{}' is not supported".format(mode)
  1152. with pytest.raises(ValueError, match=match):
  1153. np.pad([1, 2, 3], 4, mode=mode)
  1154. @pytest.mark.parametrize("mode", _all_modes.keys())
  1155. def test_non_contiguous_array(mode):
  1156. arr = np.arange(24).reshape(4, 6)[::2, ::2]
  1157. result = np.pad(arr, (2, 3), mode)
  1158. assert result.shape == (7, 8)
  1159. assert_equal(result[2:-3, 2:-3], arr)
  1160. @pytest.mark.parametrize("mode", _all_modes.keys())
  1161. def test_memory_layout_persistence(mode):
  1162. """Test if C and F order is preserved for all pad modes."""
  1163. x = np.ones((5, 10), order='C')
  1164. assert np.pad(x, 5, mode).flags["C_CONTIGUOUS"]
  1165. x = np.ones((5, 10), order='F')
  1166. assert np.pad(x, 5, mode).flags["F_CONTIGUOUS"]
  1167. @pytest.mark.parametrize("dtype", _numeric_dtypes)
  1168. @pytest.mark.parametrize("mode", _all_modes.keys())
  1169. def test_dtype_persistence(dtype, mode):
  1170. arr = np.zeros((3, 2, 1), dtype=dtype)
  1171. result = np.pad(arr, 1, mode=mode)
  1172. assert result.dtype == dtype