图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1364 lines
53 KiB

  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 (true_divide|double_scalars):"
  416. "RuntimeWarning"
  417. )
  418. @pytest.mark.parametrize("mode", ["mean", "median"])
  419. def test_zero_stat_length_valid(self, mode):
  420. arr = np.pad([1., 2.], (1, 2), mode, stat_length=0)
  421. expected = np.array([np.nan, 1., 2., np.nan, np.nan])
  422. assert_equal(arr, expected)
  423. @pytest.mark.parametrize("mode", ["minimum", "maximum"])
  424. def test_zero_stat_length_invalid(self, mode):
  425. match = "stat_length of 0 yields no value for padding"
  426. with pytest.raises(ValueError, match=match):
  427. np.pad([1., 2.], 0, mode, stat_length=0)
  428. with pytest.raises(ValueError, match=match):
  429. np.pad([1., 2.], 0, mode, stat_length=(1, 0))
  430. with pytest.raises(ValueError, match=match):
  431. np.pad([1., 2.], 1, mode, stat_length=0)
  432. with pytest.raises(ValueError, match=match):
  433. np.pad([1., 2.], 1, mode, stat_length=(1, 0))
  434. class TestConstant:
  435. def test_check_constant(self):
  436. a = np.arange(100)
  437. a = np.pad(a, (25, 20), 'constant', constant_values=(10, 20))
  438. b = np.array(
  439. [10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  440. 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
  441. 10, 10, 10, 10, 10,
  442. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  443. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  444. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  445. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  446. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  447. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  448. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  449. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  450. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  451. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  452. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
  453. 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]
  454. )
  455. assert_array_equal(a, b)
  456. def test_check_constant_zeros(self):
  457. a = np.arange(100)
  458. a = np.pad(a, (25, 20), 'constant')
  459. b = np.array(
  460. [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  461. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  462. 0, 0, 0, 0, 0,
  463. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  464. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  465. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  466. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  467. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  468. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  469. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  470. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  471. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  472. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  473. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  474. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  475. )
  476. assert_array_equal(a, b)
  477. def test_check_constant_float(self):
  478. # If input array is int, but constant_values are float, the dtype of
  479. # the array to be padded is kept
  480. arr = np.arange(30).reshape(5, 6)
  481. test = np.pad(arr, (1, 2), mode='constant',
  482. constant_values=1.1)
  483. expected = np.array(
  484. [[ 1, 1, 1, 1, 1, 1, 1, 1, 1],
  485. [ 1, 0, 1, 2, 3, 4, 5, 1, 1],
  486. [ 1, 6, 7, 8, 9, 10, 11, 1, 1],
  487. [ 1, 12, 13, 14, 15, 16, 17, 1, 1],
  488. [ 1, 18, 19, 20, 21, 22, 23, 1, 1],
  489. [ 1, 24, 25, 26, 27, 28, 29, 1, 1],
  490. [ 1, 1, 1, 1, 1, 1, 1, 1, 1],
  491. [ 1, 1, 1, 1, 1, 1, 1, 1, 1]]
  492. )
  493. assert_allclose(test, expected)
  494. def test_check_constant_float2(self):
  495. # If input array is float, and constant_values are float, the dtype of
  496. # the array to be padded is kept - here retaining the float constants
  497. arr = np.arange(30).reshape(5, 6)
  498. arr_float = arr.astype(np.float64)
  499. test = np.pad(arr_float, ((1, 2), (1, 2)), mode='constant',
  500. constant_values=1.1)
  501. expected = np.array(
  502. [[ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
  503. [ 1.1, 0. , 1. , 2. , 3. , 4. , 5. , 1.1, 1.1],
  504. [ 1.1, 6. , 7. , 8. , 9. , 10. , 11. , 1.1, 1.1],
  505. [ 1.1, 12. , 13. , 14. , 15. , 16. , 17. , 1.1, 1.1],
  506. [ 1.1, 18. , 19. , 20. , 21. , 22. , 23. , 1.1, 1.1],
  507. [ 1.1, 24. , 25. , 26. , 27. , 28. , 29. , 1.1, 1.1],
  508. [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1],
  509. [ 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]]
  510. )
  511. assert_allclose(test, expected)
  512. def test_check_constant_float3(self):
  513. a = np.arange(100, dtype=float)
  514. a = np.pad(a, (25, 20), 'constant', constant_values=(-1.1, -1.2))
  515. b = np.array(
  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, -1.1, -1.1, -1.1, -1.1, -1.1,
  518. -1.1, -1.1, -1.1, -1.1, -1.1,
  519. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  520. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  521. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  522. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  523. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  524. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  525. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  526. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  527. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  528. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  529. -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2,
  530. -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2, -1.2]
  531. )
  532. assert_allclose(a, b)
  533. def test_check_constant_odd_pad_amount(self):
  534. arr = np.arange(30).reshape(5, 6)
  535. test = np.pad(arr, ((1,), (2,)), mode='constant',
  536. constant_values=3)
  537. expected = np.array(
  538. [[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
  539. [ 3, 3, 0, 1, 2, 3, 4, 5, 3, 3],
  540. [ 3, 3, 6, 7, 8, 9, 10, 11, 3, 3],
  541. [ 3, 3, 12, 13, 14, 15, 16, 17, 3, 3],
  542. [ 3, 3, 18, 19, 20, 21, 22, 23, 3, 3],
  543. [ 3, 3, 24, 25, 26, 27, 28, 29, 3, 3],
  544. [ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]
  545. )
  546. assert_allclose(test, expected)
  547. def test_check_constant_pad_2d(self):
  548. arr = np.arange(4).reshape(2, 2)
  549. test = np.lib.pad(arr, ((1, 2), (1, 3)), mode='constant',
  550. constant_values=((1, 2), (3, 4)))
  551. expected = np.array(
  552. [[3, 1, 1, 4, 4, 4],
  553. [3, 0, 1, 4, 4, 4],
  554. [3, 2, 3, 4, 4, 4],
  555. [3, 2, 2, 4, 4, 4],
  556. [3, 2, 2, 4, 4, 4]]
  557. )
  558. assert_allclose(test, expected)
  559. def test_check_large_integers(self):
  560. uint64_max = 2 ** 64 - 1
  561. arr = np.full(5, uint64_max, dtype=np.uint64)
  562. test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
  563. expected = np.full(7, uint64_max, dtype=np.uint64)
  564. assert_array_equal(test, expected)
  565. int64_max = 2 ** 63 - 1
  566. arr = np.full(5, int64_max, dtype=np.int64)
  567. test = np.pad(arr, 1, mode="constant", constant_values=arr.min())
  568. expected = np.full(7, int64_max, dtype=np.int64)
  569. assert_array_equal(test, expected)
  570. def test_check_object_array(self):
  571. arr = np.empty(1, dtype=object)
  572. obj_a = object()
  573. arr[0] = obj_a
  574. obj_b = object()
  575. obj_c = object()
  576. arr = np.pad(arr, pad_width=1, mode='constant',
  577. constant_values=(obj_b, obj_c))
  578. expected = np.empty((3,), dtype=object)
  579. expected[0] = obj_b
  580. expected[1] = obj_a
  581. expected[2] = obj_c
  582. assert_array_equal(arr, expected)
  583. def test_pad_empty_dimension(self):
  584. arr = np.zeros((3, 0, 2))
  585. result = np.pad(arr, [(0,), (2,), (1,)], mode="constant")
  586. assert result.shape == (3, 4, 4)
  587. class TestLinearRamp:
  588. def test_check_simple(self):
  589. a = np.arange(100).astype('f')
  590. a = np.pad(a, (25, 20), 'linear_ramp', end_values=(4, 5))
  591. b = np.array(
  592. [4.00, 3.84, 3.68, 3.52, 3.36, 3.20, 3.04, 2.88, 2.72, 2.56,
  593. 2.40, 2.24, 2.08, 1.92, 1.76, 1.60, 1.44, 1.28, 1.12, 0.96,
  594. 0.80, 0.64, 0.48, 0.32, 0.16,
  595. 0.00, 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00, 8.00, 9.00,
  596. 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0,
  597. 20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0,
  598. 30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0,
  599. 40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0,
  600. 50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0,
  601. 60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0,
  602. 70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0,
  603. 80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0,
  604. 90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0,
  605. 94.3, 89.6, 84.9, 80.2, 75.5, 70.8, 66.1, 61.4, 56.7, 52.0,
  606. 47.3, 42.6, 37.9, 33.2, 28.5, 23.8, 19.1, 14.4, 9.7, 5.]
  607. )
  608. assert_allclose(a, b, rtol=1e-5, atol=1e-5)
  609. def test_check_2d(self):
  610. arr = np.arange(20).reshape(4, 5).astype(np.float64)
  611. test = np.pad(arr, (2, 2), mode='linear_ramp', end_values=(0, 0))
  612. expected = np.array(
  613. [[0., 0., 0., 0., 0., 0., 0., 0., 0.],
  614. [0., 0., 0., 0.5, 1., 1.5, 2., 1., 0.],
  615. [0., 0., 0., 1., 2., 3., 4., 2., 0.],
  616. [0., 2.5, 5., 6., 7., 8., 9., 4.5, 0.],
  617. [0., 5., 10., 11., 12., 13., 14., 7., 0.],
  618. [0., 7.5, 15., 16., 17., 18., 19., 9.5, 0.],
  619. [0., 3.75, 7.5, 8., 8.5, 9., 9.5, 4.75, 0.],
  620. [0., 0., 0., 0., 0., 0., 0., 0., 0.]])
  621. assert_allclose(test, expected)
  622. @pytest.mark.xfail(exceptions=(AssertionError,))
  623. def test_object_array(self):
  624. from fractions import Fraction
  625. arr = np.array([Fraction(1, 2), Fraction(-1, 2)])
  626. actual = np.pad(arr, (2, 3), mode='linear_ramp', end_values=0)
  627. # deliberately chosen to have a non-power-of-2 denominator such that
  628. # rounding to floats causes a failure.
  629. expected = np.array([
  630. Fraction( 0, 12),
  631. Fraction( 3, 12),
  632. Fraction( 6, 12),
  633. Fraction(-6, 12),
  634. Fraction(-4, 12),
  635. Fraction(-2, 12),
  636. Fraction(-0, 12),
  637. ])
  638. assert_equal(actual, expected)
  639. def test_end_values(self):
  640. """Ensure that end values are exact."""
  641. a = np.pad(np.ones(10).reshape(2, 5), (223, 123), mode="linear_ramp")
  642. assert_equal(a[:, 0], 0.)
  643. assert_equal(a[:, -1], 0.)
  644. assert_equal(a[0, :], 0.)
  645. assert_equal(a[-1, :], 0.)
  646. @pytest.mark.parametrize("dtype", _numeric_dtypes)
  647. def test_negative_difference(self, dtype):
  648. """
  649. Check correct behavior of unsigned dtypes if there is a negative
  650. difference between the edge to pad and `end_values`. Check both cases
  651. to be independent of implementation. Test behavior for all other dtypes
  652. in case dtype casting interferes with complex dtypes. See gh-14191.
  653. """
  654. x = np.array([3], dtype=dtype)
  655. result = np.pad(x, 3, mode="linear_ramp", end_values=0)
  656. expected = np.array([0, 1, 2, 3, 2, 1, 0], dtype=dtype)
  657. assert_equal(result, expected)
  658. x = np.array([0], dtype=dtype)
  659. result = np.pad(x, 3, mode="linear_ramp", end_values=3)
  660. expected = np.array([3, 2, 1, 0, 1, 2, 3], dtype=dtype)
  661. assert_equal(result, expected)
  662. class TestReflect:
  663. def test_check_simple(self):
  664. a = np.arange(100)
  665. a = np.pad(a, (25, 20), 'reflect')
  666. b = np.array(
  667. [25, 24, 23, 22, 21, 20, 19, 18, 17, 16,
  668. 15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
  669. 5, 4, 3, 2, 1,
  670. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  671. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  672. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  673. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  674. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  675. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  676. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  677. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  678. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  679. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  680. 98, 97, 96, 95, 94, 93, 92, 91, 90, 89,
  681. 88, 87, 86, 85, 84, 83, 82, 81, 80, 79]
  682. )
  683. assert_array_equal(a, b)
  684. def test_check_odd_method(self):
  685. a = np.arange(100)
  686. a = np.pad(a, (25, 20), 'reflect', reflect_type='odd')
  687. b = np.array(
  688. [-25, -24, -23, -22, -21, -20, -19, -18, -17, -16,
  689. -15, -14, -13, -12, -11, -10, -9, -8, -7, -6,
  690. -5, -4, -3, -2, -1,
  691. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  692. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  693. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  694. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  695. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  696. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  697. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  698. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  699. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  700. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  701. 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  702. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119]
  703. )
  704. assert_array_equal(a, b)
  705. def test_check_large_pad(self):
  706. a = [[4, 5, 6], [6, 7, 8]]
  707. a = np.pad(a, (5, 7), 'reflect')
  708. b = np.array(
  709. [[7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  710. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  711. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  712. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  713. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  714. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  715. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  716. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  717. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  718. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  719. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  720. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5],
  721. [7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7, 8, 7, 6, 7],
  722. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
  723. )
  724. assert_array_equal(a, b)
  725. def test_check_shape(self):
  726. a = [[4, 5, 6]]
  727. a = np.pad(a, (5, 7), 'reflect')
  728. b = np.array(
  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. [5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5]]
  742. )
  743. assert_array_equal(a, b)
  744. def test_check_01(self):
  745. a = np.pad([1, 2, 3], 2, 'reflect')
  746. b = np.array([3, 2, 1, 2, 3, 2, 1])
  747. assert_array_equal(a, b)
  748. def test_check_02(self):
  749. a = np.pad([1, 2, 3], 3, 'reflect')
  750. b = np.array([2, 3, 2, 1, 2, 3, 2, 1, 2])
  751. assert_array_equal(a, b)
  752. def test_check_03(self):
  753. a = np.pad([1, 2, 3], 4, 'reflect')
  754. b = np.array([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3])
  755. assert_array_equal(a, b)
  756. class TestEmptyArray:
  757. """Check how padding behaves on arrays with an empty dimension."""
  758. @pytest.mark.parametrize(
  759. # Keep parametrization ordered, otherwise pytest-xdist might believe
  760. # that different tests were collected during parallelization
  761. "mode", sorted(_all_modes.keys() - {"constant", "empty"})
  762. )
  763. def test_pad_empty_dimension(self, mode):
  764. match = ("can't extend empty axis 0 using modes other than 'constant' "
  765. "or 'empty'")
  766. with pytest.raises(ValueError, match=match):
  767. np.pad([], 4, mode=mode)
  768. with pytest.raises(ValueError, match=match):
  769. np.pad(np.ndarray(0), 4, mode=mode)
  770. with pytest.raises(ValueError, match=match):
  771. np.pad(np.zeros((0, 3)), ((1,), (0,)), mode=mode)
  772. @pytest.mark.parametrize("mode", _all_modes.keys())
  773. def test_pad_non_empty_dimension(self, mode):
  774. result = np.pad(np.ones((2, 0, 2)), ((3,), (0,), (1,)), mode=mode)
  775. assert result.shape == (8, 0, 4)
  776. class TestSymmetric:
  777. def test_check_simple(self):
  778. a = np.arange(100)
  779. a = np.pad(a, (25, 20), 'symmetric')
  780. b = np.array(
  781. [24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
  782. 14, 13, 12, 11, 10, 9, 8, 7, 6, 5,
  783. 4, 3, 2, 1, 0,
  784. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  785. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  786. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  787. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  788. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  789. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  790. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  791. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  792. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  793. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  794. 99, 98, 97, 96, 95, 94, 93, 92, 91, 90,
  795. 89, 88, 87, 86, 85, 84, 83, 82, 81, 80]
  796. )
  797. assert_array_equal(a, b)
  798. def test_check_odd_method(self):
  799. a = np.arange(100)
  800. a = np.pad(a, (25, 20), 'symmetric', reflect_type='odd')
  801. b = np.array(
  802. [-24, -23, -22, -21, -20, -19, -18, -17, -16, -15,
  803. -14, -13, -12, -11, -10, -9, -8, -7, -6, -5,
  804. -4, -3, -2, -1, 0,
  805. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  806. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  807. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  808. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  809. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  810. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  811. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  812. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  813. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  814. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  815. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
  816. 109, 110, 111, 112, 113, 114, 115, 116, 117, 118]
  817. )
  818. assert_array_equal(a, b)
  819. def test_check_large_pad(self):
  820. a = [[4, 5, 6], [6, 7, 8]]
  821. a = np.pad(a, (5, 7), 'symmetric')
  822. b = np.array(
  823. [[5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  824. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  825. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  826. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  827. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  828. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  829. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  830. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  831. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  832. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  833. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  834. [7, 8, 8, 7, 6, 6, 7, 8, 8, 7, 6, 6, 7, 8, 8],
  835. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6],
  836. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
  837. )
  838. assert_array_equal(a, b)
  839. def test_check_large_pad_odd(self):
  840. a = [[4, 5, 6], [6, 7, 8]]
  841. a = np.pad(a, (5, 7), 'symmetric', reflect_type='odd')
  842. b = np.array(
  843. [[-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
  844. [-3, -2, -2, -1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6],
  845. [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
  846. [-1, 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8],
  847. [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
  848. [ 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10],
  849. [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
  850. [ 3, 4, 4, 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12],
  851. [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
  852. [ 5, 6, 6, 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14],
  853. [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
  854. [ 7, 8, 8, 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16],
  855. [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18],
  856. [ 9, 10, 10, 11, 12, 12, 13, 14, 14, 15, 16, 16, 17, 18, 18]]
  857. )
  858. assert_array_equal(a, b)
  859. def test_check_shape(self):
  860. a = [[4, 5, 6]]
  861. a = np.pad(a, (5, 7), 'symmetric')
  862. b = np.array(
  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. [5, 6, 6, 5, 4, 4, 5, 6, 6, 5, 4, 4, 5, 6, 6]]
  876. )
  877. assert_array_equal(a, b)
  878. def test_check_01(self):
  879. a = np.pad([1, 2, 3], 2, 'symmetric')
  880. b = np.array([2, 1, 1, 2, 3, 3, 2])
  881. assert_array_equal(a, b)
  882. def test_check_02(self):
  883. a = np.pad([1, 2, 3], 3, 'symmetric')
  884. b = np.array([3, 2, 1, 1, 2, 3, 3, 2, 1])
  885. assert_array_equal(a, b)
  886. def test_check_03(self):
  887. a = np.pad([1, 2, 3], 6, 'symmetric')
  888. b = np.array([1, 2, 3, 3, 2, 1, 1, 2, 3, 3, 2, 1, 1, 2, 3])
  889. assert_array_equal(a, b)
  890. class TestWrap:
  891. def test_check_simple(self):
  892. a = np.arange(100)
  893. a = np.pad(a, (25, 20), 'wrap')
  894. b = np.array(
  895. [75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
  896. 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
  897. 95, 96, 97, 98, 99,
  898. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  899. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  900. 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
  901. 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  902. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,
  903. 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
  904. 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,
  905. 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  906. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
  907. 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  908. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  909. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
  910. )
  911. assert_array_equal(a, b)
  912. def test_check_large_pad(self):
  913. a = np.arange(12)
  914. a = np.reshape(a, (3, 4))
  915. a = np.pad(a, (10, 12), 'wrap')
  916. b = np.array(
  917. [[10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  918. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  919. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  920. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  921. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  922. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  923. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  924. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  925. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  926. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  927. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  928. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  929. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  930. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  931. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  932. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  933. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  934. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  935. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  936. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  937. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  938. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  939. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  940. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  941. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  942. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  943. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  944. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  945. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  946. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  947. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  948. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  949. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  950. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  951. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  952. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  953. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  954. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  955. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  956. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  957. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  958. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  959. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  960. 11, 8, 9, 10, 11, 8, 9, 10, 11],
  961. [2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2,
  962. 3, 0, 1, 2, 3, 0, 1, 2, 3],
  963. [6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6, 7, 4, 5, 6,
  964. 7, 4, 5, 6, 7, 4, 5, 6, 7],
  965. [10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10, 11, 8, 9, 10,
  966. 11, 8, 9, 10, 11, 8, 9, 10, 11]]
  967. )
  968. assert_array_equal(a, b)
  969. def test_check_01(self):
  970. a = np.pad([1, 2, 3], 3, 'wrap')
  971. b = np.array([1, 2, 3, 1, 2, 3, 1, 2, 3])
  972. assert_array_equal(a, b)
  973. def test_check_02(self):
  974. a = np.pad([1, 2, 3], 4, 'wrap')
  975. b = np.array([3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1])
  976. assert_array_equal(a, b)
  977. def test_pad_with_zero(self):
  978. a = np.ones((3, 5))
  979. b = np.pad(a, (0, 5), mode="wrap")
  980. assert_array_equal(a, b[:-5, :-5])
  981. def test_repeated_wrapping(self):
  982. """
  983. Check wrapping on each side individually if the wrapped area is longer
  984. than the original array.
  985. """
  986. a = np.arange(5)
  987. b = np.pad(a, (12, 0), mode="wrap")
  988. assert_array_equal(np.r_[a, a, a, a][3:], b)
  989. a = np.arange(5)
  990. b = np.pad(a, (0, 12), mode="wrap")
  991. assert_array_equal(np.r_[a, a, a, a][:-3], b)
  992. class TestEdge:
  993. def test_check_simple(self):
  994. a = np.arange(12)
  995. a = np.reshape(a, (4, 3))
  996. a = np.pad(a, ((2, 3), (3, 2)), 'edge')
  997. b = np.array(
  998. [[0, 0, 0, 0, 1, 2, 2, 2],
  999. [0, 0, 0, 0, 1, 2, 2, 2],
  1000. [0, 0, 0, 0, 1, 2, 2, 2],
  1001. [3, 3, 3, 3, 4, 5, 5, 5],
  1002. [6, 6, 6, 6, 7, 8, 8, 8],
  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. [9, 9, 9, 9, 10, 11, 11, 11]]
  1007. )
  1008. assert_array_equal(a, b)
  1009. def test_check_width_shape_1_2(self):
  1010. # Check a pad_width of the form ((1, 2),).
  1011. # Regression test for issue gh-7808.
  1012. a = np.array([1, 2, 3])
  1013. padded = np.pad(a, ((1, 2),), 'edge')
  1014. expected = np.array([1, 1, 2, 3, 3, 3])
  1015. assert_array_equal(padded, expected)
  1016. a = np.array([[1, 2, 3], [4, 5, 6]])
  1017. padded = np.pad(a, ((1, 2),), 'edge')
  1018. expected = np.pad(a, ((1, 2), (1, 2)), 'edge')
  1019. assert_array_equal(padded, expected)
  1020. a = np.arange(24).reshape(2, 3, 4)
  1021. padded = np.pad(a, ((1, 2),), 'edge')
  1022. expected = np.pad(a, ((1, 2), (1, 2), (1, 2)), 'edge')
  1023. assert_array_equal(padded, expected)
  1024. class TestEmpty:
  1025. def test_simple(self):
  1026. arr = np.arange(24).reshape(4, 6)
  1027. result = np.pad(arr, [(2, 3), (3, 1)], mode="empty")
  1028. assert result.shape == (9, 10)
  1029. assert_equal(arr, result[2:-3, 3:-1])
  1030. def test_pad_empty_dimension(self):
  1031. arr = np.zeros((3, 0, 2))
  1032. result = np.pad(arr, [(0,), (2,), (1,)], mode="empty")
  1033. assert result.shape == (3, 4, 4)
  1034. def test_legacy_vector_functionality():
  1035. def _padwithtens(vector, pad_width, iaxis, kwargs):
  1036. vector[:pad_width[0]] = 10
  1037. vector[-pad_width[1]:] = 10
  1038. a = np.arange(6).reshape(2, 3)
  1039. a = np.pad(a, 2, _padwithtens)
  1040. b = np.array(
  1041. [[10, 10, 10, 10, 10, 10, 10],
  1042. [10, 10, 10, 10, 10, 10, 10],
  1043. [10, 10, 0, 1, 2, 10, 10],
  1044. [10, 10, 3, 4, 5, 10, 10],
  1045. [10, 10, 10, 10, 10, 10, 10],
  1046. [10, 10, 10, 10, 10, 10, 10]]
  1047. )
  1048. assert_array_equal(a, b)
  1049. def test_unicode_mode():
  1050. a = np.pad([1], 2, mode=u'constant')
  1051. b = np.array([0, 0, 1, 0, 0])
  1052. assert_array_equal(a, b)
  1053. @pytest.mark.parametrize("mode", ["edge", "symmetric", "reflect", "wrap"])
  1054. def test_object_input(mode):
  1055. # Regression test for issue gh-11395.
  1056. a = np.full((4, 3), fill_value=None)
  1057. pad_amt = ((2, 3), (3, 2))
  1058. b = np.full((9, 8), fill_value=None)
  1059. assert_array_equal(np.pad(a, pad_amt, mode=mode), b)
  1060. class TestPadWidth:
  1061. @pytest.mark.parametrize("pad_width", [
  1062. (4, 5, 6, 7),
  1063. ((1,), (2,), (3,)),
  1064. ((1, 2), (3, 4), (5, 6)),
  1065. ((3, 4, 5), (0, 1, 2)),
  1066. ])
  1067. @pytest.mark.parametrize("mode", _all_modes.keys())
  1068. def test_misshaped_pad_width(self, pad_width, mode):
  1069. arr = np.arange(30).reshape((6, 5))
  1070. match = "operands could not be broadcast together"
  1071. with pytest.raises(ValueError, match=match):
  1072. np.pad(arr, pad_width, mode)
  1073. @pytest.mark.parametrize("mode", _all_modes.keys())
  1074. def test_misshaped_pad_width_2(self, mode):
  1075. arr = np.arange(30).reshape((6, 5))
  1076. match = ("input operand has more dimensions than allowed by the axis "
  1077. "remapping")
  1078. with pytest.raises(ValueError, match=match):
  1079. np.pad(arr, (((3,), (4,), (5,)), ((0,), (1,), (2,))), mode)
  1080. @pytest.mark.parametrize(
  1081. "pad_width", [-2, (-2,), (3, -1), ((5, 2), (-2, 3)), ((-4,), (2,))])
  1082. @pytest.mark.parametrize("mode", _all_modes.keys())
  1083. def test_negative_pad_width(self, pad_width, mode):
  1084. arr = np.arange(30).reshape((6, 5))
  1085. match = "index can't contain negative values"
  1086. with pytest.raises(ValueError, match=match):
  1087. np.pad(arr, pad_width, mode)
  1088. @pytest.mark.parametrize("pad_width, dtype", [
  1089. ("3", None),
  1090. ("word", None),
  1091. (None, None),
  1092. (object(), None),
  1093. (3.4, None),
  1094. (((2, 3, 4), (3, 2)), object),
  1095. (complex(1, -1), None),
  1096. (((-2.1, 3), (3, 2)), None),
  1097. ])
  1098. @pytest.mark.parametrize("mode", _all_modes.keys())
  1099. def test_bad_type(self, pad_width, dtype, mode):
  1100. arr = np.arange(30).reshape((6, 5))
  1101. match = "`pad_width` must be of integral type."
  1102. if dtype is not None:
  1103. # avoid DeprecationWarning when not specifying dtype
  1104. with pytest.raises(TypeError, match=match):
  1105. np.pad(arr, np.array(pad_width, dtype=dtype), mode)
  1106. else:
  1107. with pytest.raises(TypeError, match=match):
  1108. np.pad(arr, pad_width, mode)
  1109. with pytest.raises(TypeError, match=match):
  1110. np.pad(arr, np.array(pad_width), mode)
  1111. def test_pad_width_as_ndarray(self):
  1112. a = np.arange(12)
  1113. a = np.reshape(a, (4, 3))
  1114. a = np.pad(a, np.array(((2, 3), (3, 2))), 'edge')
  1115. b = np.array(
  1116. [[0, 0, 0, 0, 1, 2, 2, 2],
  1117. [0, 0, 0, 0, 1, 2, 2, 2],
  1118. [0, 0, 0, 0, 1, 2, 2, 2],
  1119. [3, 3, 3, 3, 4, 5, 5, 5],
  1120. [6, 6, 6, 6, 7, 8, 8, 8],
  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. [9, 9, 9, 9, 10, 11, 11, 11]]
  1125. )
  1126. assert_array_equal(a, b)
  1127. @pytest.mark.parametrize("pad_width", [0, (0, 0), ((0, 0), (0, 0))])
  1128. @pytest.mark.parametrize("mode", _all_modes.keys())
  1129. def test_zero_pad_width(self, pad_width, mode):
  1130. arr = np.arange(30).reshape(6, 5)
  1131. assert_array_equal(arr, np.pad(arr, pad_width, mode=mode))
  1132. @pytest.mark.parametrize("mode", _all_modes.keys())
  1133. def test_kwargs(mode):
  1134. """Test behavior of pad's kwargs for the given mode."""
  1135. allowed = _all_modes[mode]
  1136. not_allowed = {}
  1137. for kwargs in _all_modes.values():
  1138. if kwargs != allowed:
  1139. not_allowed.update(kwargs)
  1140. # Test if allowed keyword arguments pass
  1141. np.pad([1, 2, 3], 1, mode, **allowed)
  1142. # Test if prohibited keyword arguments of other modes raise an error
  1143. for key, value in not_allowed.items():
  1144. match = "unsupported keyword arguments for mode '{}'".format(mode)
  1145. with pytest.raises(ValueError, match=match):
  1146. np.pad([1, 2, 3], 1, mode, **{key: value})
  1147. def test_constant_zero_default():
  1148. arr = np.array([1, 1])
  1149. assert_array_equal(np.pad(arr, 2), [0, 0, 1, 1, 0, 0])
  1150. @pytest.mark.parametrize("mode", [1, "const", object(), None, True, False])
  1151. def test_unsupported_mode(mode):
  1152. match= "mode '{}' is not supported".format(mode)
  1153. with pytest.raises(ValueError, match=match):
  1154. np.pad([1, 2, 3], 4, mode=mode)
  1155. @pytest.mark.parametrize("mode", _all_modes.keys())
  1156. def test_non_contiguous_array(mode):
  1157. arr = np.arange(24).reshape(4, 6)[::2, ::2]
  1158. result = np.pad(arr, (2, 3), mode)
  1159. assert result.shape == (7, 8)
  1160. assert_equal(result[2:-3, 2:-3], arr)
  1161. @pytest.mark.parametrize("mode", _all_modes.keys())
  1162. def test_memory_layout_persistence(mode):
  1163. """Test if C and F order is preserved for all pad modes."""
  1164. x = np.ones((5, 10), order='C')
  1165. assert np.pad(x, 5, mode).flags["C_CONTIGUOUS"]
  1166. x = np.ones((5, 10), order='F')
  1167. assert np.pad(x, 5, mode).flags["F_CONTIGUOUS"]
  1168. @pytest.mark.parametrize("dtype", _numeric_dtypes)
  1169. @pytest.mark.parametrize("mode", _all_modes.keys())
  1170. def test_dtype_persistence(dtype, mode):
  1171. arr = np.zeros((3, 2, 1), dtype=dtype)
  1172. result = np.pad(arr, 1, mode=mode)
  1173. assert result.dtype == dtype