test_arraypad.py 49 KB

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