test_index_tricks.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. import pytest
  2. import numpy as np
  3. from numpy.testing import (
  4. assert_, assert_equal, assert_array_equal, assert_almost_equal,
  5. assert_array_almost_equal, assert_raises, assert_raises_regex,
  6. assert_warns
  7. )
  8. from numpy.lib.index_tricks import (
  9. mgrid, ogrid, ndenumerate, fill_diagonal, diag_indices, diag_indices_from,
  10. index_exp, ndindex, r_, s_, ix_
  11. )
  12. class TestRavelUnravelIndex:
  13. def test_basic(self):
  14. assert_equal(np.unravel_index(2, (2, 2)), (1, 0))
  15. # test backwards compatibility with older dims
  16. # keyword argument; see Issue #10586
  17. with assert_warns(DeprecationWarning):
  18. # we should achieve the correct result
  19. # AND raise the appropriate warning
  20. # when using older "dims" kw argument
  21. assert_equal(np.unravel_index(indices=2,
  22. dims=(2, 2)),
  23. (1, 0))
  24. # test that new shape argument works properly
  25. assert_equal(np.unravel_index(indices=2,
  26. shape=(2, 2)),
  27. (1, 0))
  28. # test that an invalid second keyword argument
  29. # is properly handled
  30. with assert_raises(TypeError):
  31. np.unravel_index(indices=2, hape=(2, 2))
  32. with assert_raises(TypeError):
  33. np.unravel_index(2, hape=(2, 2))
  34. with assert_raises(TypeError):
  35. np.unravel_index(254, ims=(17, 94))
  36. assert_equal(np.ravel_multi_index((1, 0), (2, 2)), 2)
  37. assert_equal(np.unravel_index(254, (17, 94)), (2, 66))
  38. assert_equal(np.ravel_multi_index((2, 66), (17, 94)), 254)
  39. assert_raises(ValueError, np.unravel_index, -1, (2, 2))
  40. assert_raises(TypeError, np.unravel_index, 0.5, (2, 2))
  41. assert_raises(ValueError, np.unravel_index, 4, (2, 2))
  42. assert_raises(ValueError, np.ravel_multi_index, (-3, 1), (2, 2))
  43. assert_raises(ValueError, np.ravel_multi_index, (2, 1), (2, 2))
  44. assert_raises(ValueError, np.ravel_multi_index, (0, -3), (2, 2))
  45. assert_raises(ValueError, np.ravel_multi_index, (0, 2), (2, 2))
  46. assert_raises(TypeError, np.ravel_multi_index, (0.1, 0.), (2, 2))
  47. assert_equal(np.unravel_index((2*3 + 1)*6 + 4, (4, 3, 6)), [2, 1, 4])
  48. assert_equal(
  49. np.ravel_multi_index([2, 1, 4], (4, 3, 6)), (2*3 + 1)*6 + 4)
  50. arr = np.array([[3, 6, 6], [4, 5, 1]])
  51. assert_equal(np.ravel_multi_index(arr, (7, 6)), [22, 41, 37])
  52. assert_equal(
  53. np.ravel_multi_index(arr, (7, 6), order='F'), [31, 41, 13])
  54. assert_equal(
  55. np.ravel_multi_index(arr, (4, 6), mode='clip'), [22, 23, 19])
  56. assert_equal(np.ravel_multi_index(arr, (4, 4), mode=('clip', 'wrap')),
  57. [12, 13, 13])
  58. assert_equal(np.ravel_multi_index((3, 1, 4, 1), (6, 7, 8, 9)), 1621)
  59. assert_equal(np.unravel_index(np.array([22, 41, 37]), (7, 6)),
  60. [[3, 6, 6], [4, 5, 1]])
  61. assert_equal(
  62. np.unravel_index(np.array([31, 41, 13]), (7, 6), order='F'),
  63. [[3, 6, 6], [4, 5, 1]])
  64. assert_equal(np.unravel_index(1621, (6, 7, 8, 9)), [3, 1, 4, 1])
  65. def test_empty_indices(self):
  66. msg1 = 'indices must be integral: the provided empty sequence was'
  67. msg2 = 'only int indices permitted'
  68. assert_raises_regex(TypeError, msg1, np.unravel_index, [], (10, 3, 5))
  69. assert_raises_regex(TypeError, msg1, np.unravel_index, (), (10, 3, 5))
  70. assert_raises_regex(TypeError, msg2, np.unravel_index, np.array([]),
  71. (10, 3, 5))
  72. assert_equal(np.unravel_index(np.array([],dtype=int), (10, 3, 5)),
  73. [[], [], []])
  74. assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], []),
  75. (10, 3))
  76. assert_raises_regex(TypeError, msg1, np.ravel_multi_index, ([], ['abc']),
  77. (10, 3))
  78. assert_raises_regex(TypeError, msg2, np.ravel_multi_index,
  79. (np.array([]), np.array([])), (5, 3))
  80. assert_equal(np.ravel_multi_index(
  81. (np.array([], dtype=int), np.array([], dtype=int)), (5, 3)), [])
  82. assert_equal(np.ravel_multi_index(np.array([[], []], dtype=int),
  83. (5, 3)), [])
  84. def test_big_indices(self):
  85. # ravel_multi_index for big indices (issue #7546)
  86. if np.intp == np.int64:
  87. arr = ([1, 29], [3, 5], [3, 117], [19, 2],
  88. [2379, 1284], [2, 2], [0, 1])
  89. assert_equal(
  90. np.ravel_multi_index(arr, (41, 7, 120, 36, 2706, 8, 6)),
  91. [5627771580, 117259570957])
  92. # test unravel_index for big indices (issue #9538)
  93. assert_raises(ValueError, np.unravel_index, 1, (2**32-1, 2**31+1))
  94. # test overflow checking for too big array (issue #7546)
  95. dummy_arr = ([0],[0])
  96. half_max = np.iinfo(np.intp).max // 2
  97. assert_equal(
  98. np.ravel_multi_index(dummy_arr, (half_max, 2)), [0])
  99. assert_raises(ValueError,
  100. np.ravel_multi_index, dummy_arr, (half_max+1, 2))
  101. assert_equal(
  102. np.ravel_multi_index(dummy_arr, (half_max, 2), order='F'), [0])
  103. assert_raises(ValueError,
  104. np.ravel_multi_index, dummy_arr, (half_max+1, 2), order='F')
  105. def test_dtypes(self):
  106. # Test with different data types
  107. for dtype in [np.int16, np.uint16, np.int32,
  108. np.uint32, np.int64, np.uint64]:
  109. coords = np.array(
  110. [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0]], dtype=dtype)
  111. shape = (5, 8)
  112. uncoords = 8*coords[0]+coords[1]
  113. assert_equal(np.ravel_multi_index(coords, shape), uncoords)
  114. assert_equal(coords, np.unravel_index(uncoords, shape))
  115. uncoords = coords[0]+5*coords[1]
  116. assert_equal(
  117. np.ravel_multi_index(coords, shape, order='F'), uncoords)
  118. assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
  119. coords = np.array(
  120. [[1, 0, 1, 2, 3, 4], [1, 6, 1, 3, 2, 0], [1, 3, 1, 0, 9, 5]],
  121. dtype=dtype)
  122. shape = (5, 8, 10)
  123. uncoords = 10*(8*coords[0]+coords[1])+coords[2]
  124. assert_equal(np.ravel_multi_index(coords, shape), uncoords)
  125. assert_equal(coords, np.unravel_index(uncoords, shape))
  126. uncoords = coords[0]+5*(coords[1]+8*coords[2])
  127. assert_equal(
  128. np.ravel_multi_index(coords, shape, order='F'), uncoords)
  129. assert_equal(coords, np.unravel_index(uncoords, shape, order='F'))
  130. def test_clipmodes(self):
  131. # Test clipmodes
  132. assert_equal(
  133. np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12), mode='wrap'),
  134. np.ravel_multi_index([1, 1, 6, 2], (4, 3, 7, 12)))
  135. assert_equal(np.ravel_multi_index([5, 1, -1, 2], (4, 3, 7, 12),
  136. mode=(
  137. 'wrap', 'raise', 'clip', 'raise')),
  138. np.ravel_multi_index([1, 1, 0, 2], (4, 3, 7, 12)))
  139. assert_raises(
  140. ValueError, np.ravel_multi_index, [5, 1, -1, 2], (4, 3, 7, 12))
  141. def test_writeability(self):
  142. # See gh-7269
  143. x, y = np.unravel_index([1, 2, 3], (4, 5))
  144. assert_(x.flags.writeable)
  145. assert_(y.flags.writeable)
  146. def test_0d(self):
  147. # gh-580
  148. x = np.unravel_index(0, ())
  149. assert_equal(x, ())
  150. assert_raises_regex(ValueError, "0d array", np.unravel_index, [0], ())
  151. assert_raises_regex(
  152. ValueError, "out of bounds", np.unravel_index, [1], ())
  153. @pytest.mark.parametrize("mode", ["clip", "wrap", "raise"])
  154. def test_empty_array_ravel(self, mode):
  155. res = np.ravel_multi_index(
  156. np.zeros((3, 0), dtype=np.intp), (2, 1, 0), mode=mode)
  157. assert(res.shape == (0,))
  158. with assert_raises(ValueError):
  159. np.ravel_multi_index(
  160. np.zeros((3, 1), dtype=np.intp), (2, 1, 0), mode=mode)
  161. def test_empty_array_unravel(self):
  162. res = np.unravel_index(np.zeros(0, dtype=np.intp), (2, 1, 0))
  163. # res is a tuple of three empty arrays
  164. assert(len(res) == 3)
  165. assert(all(a.shape == (0,) for a in res))
  166. with assert_raises(ValueError):
  167. np.unravel_index([1], (2, 1, 0))
  168. class TestGrid:
  169. def test_basic(self):
  170. a = mgrid[-1:1:10j]
  171. b = mgrid[-1:1:0.1]
  172. assert_(a.shape == (10,))
  173. assert_(b.shape == (20,))
  174. assert_(a[0] == -1)
  175. assert_almost_equal(a[-1], 1)
  176. assert_(b[0] == -1)
  177. assert_almost_equal(b[1]-b[0], 0.1, 11)
  178. assert_almost_equal(b[-1], b[0]+19*0.1, 11)
  179. assert_almost_equal(a[1]-a[0], 2.0/9.0, 11)
  180. def test_linspace_equivalence(self):
  181. y, st = np.linspace(2, 10, retstep=True)
  182. assert_almost_equal(st, 8/49.0)
  183. assert_array_almost_equal(y, mgrid[2:10:50j], 13)
  184. def test_nd(self):
  185. c = mgrid[-1:1:10j, -2:2:10j]
  186. d = mgrid[-1:1:0.1, -2:2:0.2]
  187. assert_(c.shape == (2, 10, 10))
  188. assert_(d.shape == (2, 20, 20))
  189. assert_array_equal(c[0][0, :], -np.ones(10, 'd'))
  190. assert_array_equal(c[1][:, 0], -2*np.ones(10, 'd'))
  191. assert_array_almost_equal(c[0][-1, :], np.ones(10, 'd'), 11)
  192. assert_array_almost_equal(c[1][:, -1], 2*np.ones(10, 'd'), 11)
  193. assert_array_almost_equal(d[0, 1, :] - d[0, 0, :],
  194. 0.1*np.ones(20, 'd'), 11)
  195. assert_array_almost_equal(d[1, :, 1] - d[1, :, 0],
  196. 0.2*np.ones(20, 'd'), 11)
  197. def test_sparse(self):
  198. grid_full = mgrid[-1:1:10j, -2:2:10j]
  199. grid_sparse = ogrid[-1:1:10j, -2:2:10j]
  200. # sparse grids can be made dense by broadcasting
  201. grid_broadcast = np.broadcast_arrays(*grid_sparse)
  202. for f, b in zip(grid_full, grid_broadcast):
  203. assert_equal(f, b)
  204. @pytest.mark.parametrize("start, stop, step, expected", [
  205. (None, 10, 10j, (200, 10)),
  206. (-10, 20, None, (1800, 30)),
  207. ])
  208. def test_mgrid_size_none_handling(self, start, stop, step, expected):
  209. # regression test None value handling for
  210. # start and step values used by mgrid;
  211. # internally, this aims to cover previously
  212. # unexplored code paths in nd_grid()
  213. grid = mgrid[start:stop:step, start:stop:step]
  214. # need a smaller grid to explore one of the
  215. # untested code paths
  216. grid_small = mgrid[start:stop:step]
  217. assert_equal(grid.size, expected[0])
  218. assert_equal(grid_small.size, expected[1])
  219. def test_accepts_npfloating(self):
  220. # regression test for #16466
  221. grid64 = mgrid[0.1:0.33:0.1, ]
  222. grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1), ]
  223. assert_(grid32.dtype == np.float64)
  224. assert_array_almost_equal(grid64, grid32)
  225. # different code path for single slice
  226. grid64 = mgrid[0.1:0.33:0.1]
  227. grid32 = mgrid[np.float32(0.1):np.float32(0.33):np.float32(0.1)]
  228. assert_(grid32.dtype == np.float64)
  229. assert_array_almost_equal(grid64, grid32)
  230. def test_accepts_npcomplexfloating(self):
  231. # Related to #16466
  232. assert_array_almost_equal(
  233. mgrid[0.1:0.3:3j, ], mgrid[0.1:0.3:np.complex64(3j), ]
  234. )
  235. # different code path for single slice
  236. assert_array_almost_equal(
  237. mgrid[0.1:0.3:3j], mgrid[0.1:0.3:np.complex64(3j)]
  238. )
  239. class TestConcatenator:
  240. def test_1d(self):
  241. assert_array_equal(r_[1, 2, 3, 4, 5, 6], np.array([1, 2, 3, 4, 5, 6]))
  242. b = np.ones(5)
  243. c = r_[b, 0, 0, b]
  244. assert_array_equal(c, [1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1])
  245. def test_mixed_type(self):
  246. g = r_[10.1, 1:10]
  247. assert_(g.dtype == 'f8')
  248. def test_more_mixed_type(self):
  249. g = r_[-10.1, np.array([1]), np.array([2, 3, 4]), 10.0]
  250. assert_(g.dtype == 'f8')
  251. def test_complex_step(self):
  252. # Regression test for #12262
  253. g = r_[0:36:100j]
  254. assert_(g.shape == (100,))
  255. # Related to #16466
  256. g = r_[0:36:np.complex64(100j)]
  257. assert_(g.shape == (100,))
  258. def test_2d(self):
  259. b = np.random.rand(5, 5)
  260. c = np.random.rand(5, 5)
  261. d = r_['1', b, c] # append columns
  262. assert_(d.shape == (5, 10))
  263. assert_array_equal(d[:, :5], b)
  264. assert_array_equal(d[:, 5:], c)
  265. d = r_[b, c]
  266. assert_(d.shape == (10, 5))
  267. assert_array_equal(d[:5, :], b)
  268. assert_array_equal(d[5:, :], c)
  269. def test_0d(self):
  270. assert_equal(r_[0, np.array(1), 2], [0, 1, 2])
  271. assert_equal(r_[[0, 1, 2], np.array(3)], [0, 1, 2, 3])
  272. assert_equal(r_[np.array(0), [1, 2, 3]], [0, 1, 2, 3])
  273. class TestNdenumerate:
  274. def test_basic(self):
  275. a = np.array([[1, 2], [3, 4]])
  276. assert_equal(list(ndenumerate(a)),
  277. [((0, 0), 1), ((0, 1), 2), ((1, 0), 3), ((1, 1), 4)])
  278. class TestIndexExpression:
  279. def test_regression_1(self):
  280. # ticket #1196
  281. a = np.arange(2)
  282. assert_equal(a[:-1], a[s_[:-1]])
  283. assert_equal(a[:-1], a[index_exp[:-1]])
  284. def test_simple_1(self):
  285. a = np.random.rand(4, 5, 6)
  286. assert_equal(a[:, :3, [1, 2]], a[index_exp[:, :3, [1, 2]]])
  287. assert_equal(a[:, :3, [1, 2]], a[s_[:, :3, [1, 2]]])
  288. class TestIx_:
  289. def test_regression_1(self):
  290. # Test empty untyped inputs create outputs of indexing type, gh-5804
  291. a, = np.ix_(range(0))
  292. assert_equal(a.dtype, np.intp)
  293. a, = np.ix_([])
  294. assert_equal(a.dtype, np.intp)
  295. # but if the type is specified, don't change it
  296. a, = np.ix_(np.array([], dtype=np.float32))
  297. assert_equal(a.dtype, np.float32)
  298. def test_shape_and_dtype(self):
  299. sizes = (4, 5, 3, 2)
  300. # Test both lists and arrays
  301. for func in (range, np.arange):
  302. arrays = np.ix_(*[func(sz) for sz in sizes])
  303. for k, (a, sz) in enumerate(zip(arrays, sizes)):
  304. assert_equal(a.shape[k], sz)
  305. assert_(all(sh == 1 for j, sh in enumerate(a.shape) if j != k))
  306. assert_(np.issubdtype(a.dtype, np.integer))
  307. def test_bool(self):
  308. bool_a = [True, False, True, True]
  309. int_a, = np.nonzero(bool_a)
  310. assert_equal(np.ix_(bool_a)[0], int_a)
  311. def test_1d_only(self):
  312. idx2d = [[1, 2, 3], [4, 5, 6]]
  313. assert_raises(ValueError, np.ix_, idx2d)
  314. def test_repeated_input(self):
  315. length_of_vector = 5
  316. x = np.arange(length_of_vector)
  317. out = ix_(x, x)
  318. assert_equal(out[0].shape, (length_of_vector, 1))
  319. assert_equal(out[1].shape, (1, length_of_vector))
  320. # check that input shape is not modified
  321. assert_equal(x.shape, (length_of_vector,))
  322. def test_c_():
  323. a = np.c_[np.array([[1, 2, 3]]), 0, 0, np.array([[4, 5, 6]])]
  324. assert_equal(a, [[1, 2, 3, 0, 0, 4, 5, 6]])
  325. class TestFillDiagonal:
  326. def test_basic(self):
  327. a = np.zeros((3, 3), int)
  328. fill_diagonal(a, 5)
  329. assert_array_equal(
  330. a, np.array([[5, 0, 0],
  331. [0, 5, 0],
  332. [0, 0, 5]])
  333. )
  334. def test_tall_matrix(self):
  335. a = np.zeros((10, 3), int)
  336. fill_diagonal(a, 5)
  337. assert_array_equal(
  338. a, np.array([[5, 0, 0],
  339. [0, 5, 0],
  340. [0, 0, 5],
  341. [0, 0, 0],
  342. [0, 0, 0],
  343. [0, 0, 0],
  344. [0, 0, 0],
  345. [0, 0, 0],
  346. [0, 0, 0],
  347. [0, 0, 0]])
  348. )
  349. def test_tall_matrix_wrap(self):
  350. a = np.zeros((10, 3), int)
  351. fill_diagonal(a, 5, True)
  352. assert_array_equal(
  353. a, np.array([[5, 0, 0],
  354. [0, 5, 0],
  355. [0, 0, 5],
  356. [0, 0, 0],
  357. [5, 0, 0],
  358. [0, 5, 0],
  359. [0, 0, 5],
  360. [0, 0, 0],
  361. [5, 0, 0],
  362. [0, 5, 0]])
  363. )
  364. def test_wide_matrix(self):
  365. a = np.zeros((3, 10), int)
  366. fill_diagonal(a, 5)
  367. assert_array_equal(
  368. a, np.array([[5, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  369. [0, 5, 0, 0, 0, 0, 0, 0, 0, 0],
  370. [0, 0, 5, 0, 0, 0, 0, 0, 0, 0]])
  371. )
  372. def test_operate_4d_array(self):
  373. a = np.zeros((3, 3, 3, 3), int)
  374. fill_diagonal(a, 4)
  375. i = np.array([0, 1, 2])
  376. assert_equal(np.where(a != 0), (i, i, i, i))
  377. def test_low_dim_handling(self):
  378. # raise error with low dimensionality
  379. a = np.zeros(3, int)
  380. with assert_raises_regex(ValueError, "at least 2-d"):
  381. fill_diagonal(a, 5)
  382. def test_hetero_shape_handling(self):
  383. # raise error with high dimensionality and
  384. # shape mismatch
  385. a = np.zeros((3,3,7,3), int)
  386. with assert_raises_regex(ValueError, "equal length"):
  387. fill_diagonal(a, 2)
  388. def test_diag_indices():
  389. di = diag_indices(4)
  390. a = np.array([[1, 2, 3, 4],
  391. [5, 6, 7, 8],
  392. [9, 10, 11, 12],
  393. [13, 14, 15, 16]])
  394. a[di] = 100
  395. assert_array_equal(
  396. a, np.array([[100, 2, 3, 4],
  397. [5, 100, 7, 8],
  398. [9, 10, 100, 12],
  399. [13, 14, 15, 100]])
  400. )
  401. # Now, we create indices to manipulate a 3-d array:
  402. d3 = diag_indices(2, 3)
  403. # And use it to set the diagonal of a zeros array to 1:
  404. a = np.zeros((2, 2, 2), int)
  405. a[d3] = 1
  406. assert_array_equal(
  407. a, np.array([[[1, 0],
  408. [0, 0]],
  409. [[0, 0],
  410. [0, 1]]])
  411. )
  412. class TestDiagIndicesFrom:
  413. def test_diag_indices_from(self):
  414. x = np.random.random((4, 4))
  415. r, c = diag_indices_from(x)
  416. assert_array_equal(r, np.arange(4))
  417. assert_array_equal(c, np.arange(4))
  418. def test_error_small_input(self):
  419. x = np.ones(7)
  420. with assert_raises_regex(ValueError, "at least 2-d"):
  421. diag_indices_from(x)
  422. def test_error_shape_mismatch(self):
  423. x = np.zeros((3, 3, 2, 3), int)
  424. with assert_raises_regex(ValueError, "equal length"):
  425. diag_indices_from(x)
  426. def test_ndindex():
  427. x = list(ndindex(1, 2, 3))
  428. expected = [ix for ix, e in ndenumerate(np.zeros((1, 2, 3)))]
  429. assert_array_equal(x, expected)
  430. x = list(ndindex((1, 2, 3)))
  431. assert_array_equal(x, expected)
  432. # Test use of scalars and tuples
  433. x = list(ndindex((3,)))
  434. assert_array_equal(x, list(ndindex(3)))
  435. # Make sure size argument is optional
  436. x = list(ndindex())
  437. assert_equal(x, [()])
  438. x = list(ndindex(()))
  439. assert_equal(x, [()])
  440. # Make sure 0-sized ndindex works correctly
  441. x = list(ndindex(*[0]))
  442. assert_equal(x, [])