test_arraysetops.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. """Test functions for 1D array set operations.
  2. """
  3. import numpy as np
  4. from numpy.testing import (assert_array_equal, assert_equal,
  5. assert_raises, assert_raises_regex)
  6. from numpy.lib.arraysetops import (
  7. ediff1d, intersect1d, setxor1d, union1d, setdiff1d, unique, in1d, isin
  8. )
  9. import pytest
  10. class TestSetOps:
  11. def test_intersect1d(self):
  12. # unique inputs
  13. a = np.array([5, 7, 1, 2])
  14. b = np.array([2, 4, 3, 1, 5])
  15. ec = np.array([1, 2, 5])
  16. c = intersect1d(a, b, assume_unique=True)
  17. assert_array_equal(c, ec)
  18. # non-unique inputs
  19. a = np.array([5, 5, 7, 1, 2])
  20. b = np.array([2, 1, 4, 3, 3, 1, 5])
  21. ed = np.array([1, 2, 5])
  22. c = intersect1d(a, b)
  23. assert_array_equal(c, ed)
  24. assert_array_equal([], intersect1d([], []))
  25. def test_intersect1d_array_like(self):
  26. # See gh-11772
  27. class Test:
  28. def __array__(self):
  29. return np.arange(3)
  30. a = Test()
  31. res = intersect1d(a, a)
  32. assert_array_equal(res, a)
  33. res = intersect1d([1, 2, 3], [1, 2, 3])
  34. assert_array_equal(res, [1, 2, 3])
  35. def test_intersect1d_indices(self):
  36. # unique inputs
  37. a = np.array([1, 2, 3, 4])
  38. b = np.array([2, 1, 4, 6])
  39. c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True)
  40. ee = np.array([1, 2, 4])
  41. assert_array_equal(c, ee)
  42. assert_array_equal(a[i1], ee)
  43. assert_array_equal(b[i2], ee)
  44. # non-unique inputs
  45. a = np.array([1, 2, 2, 3, 4, 3, 2])
  46. b = np.array([1, 8, 4, 2, 2, 3, 2, 3])
  47. c, i1, i2 = intersect1d(a, b, return_indices=True)
  48. ef = np.array([1, 2, 3, 4])
  49. assert_array_equal(c, ef)
  50. assert_array_equal(a[i1], ef)
  51. assert_array_equal(b[i2], ef)
  52. # non1d, unique inputs
  53. a = np.array([[2, 4, 5, 6], [7, 8, 1, 15]])
  54. b = np.array([[3, 2, 7, 6], [10, 12, 8, 9]])
  55. c, i1, i2 = intersect1d(a, b, assume_unique=True, return_indices=True)
  56. ui1 = np.unravel_index(i1, a.shape)
  57. ui2 = np.unravel_index(i2, b.shape)
  58. ea = np.array([2, 6, 7, 8])
  59. assert_array_equal(ea, a[ui1])
  60. assert_array_equal(ea, b[ui2])
  61. # non1d, not assumed to be uniqueinputs
  62. a = np.array([[2, 4, 5, 6, 6], [4, 7, 8, 7, 2]])
  63. b = np.array([[3, 2, 7, 7], [10, 12, 8, 7]])
  64. c, i1, i2 = intersect1d(a, b, return_indices=True)
  65. ui1 = np.unravel_index(i1, a.shape)
  66. ui2 = np.unravel_index(i2, b.shape)
  67. ea = np.array([2, 7, 8])
  68. assert_array_equal(ea, a[ui1])
  69. assert_array_equal(ea, b[ui2])
  70. def test_setxor1d(self):
  71. a = np.array([5, 7, 1, 2])
  72. b = np.array([2, 4, 3, 1, 5])
  73. ec = np.array([3, 4, 7])
  74. c = setxor1d(a, b)
  75. assert_array_equal(c, ec)
  76. a = np.array([1, 2, 3])
  77. b = np.array([6, 5, 4])
  78. ec = np.array([1, 2, 3, 4, 5, 6])
  79. c = setxor1d(a, b)
  80. assert_array_equal(c, ec)
  81. a = np.array([1, 8, 2, 3])
  82. b = np.array([6, 5, 4, 8])
  83. ec = np.array([1, 2, 3, 4, 5, 6])
  84. c = setxor1d(a, b)
  85. assert_array_equal(c, ec)
  86. assert_array_equal([], setxor1d([], []))
  87. def test_ediff1d(self):
  88. zero_elem = np.array([])
  89. one_elem = np.array([1])
  90. two_elem = np.array([1, 2])
  91. assert_array_equal([], ediff1d(zero_elem))
  92. assert_array_equal([0], ediff1d(zero_elem, to_begin=0))
  93. assert_array_equal([0], ediff1d(zero_elem, to_end=0))
  94. assert_array_equal([-1, 0], ediff1d(zero_elem, to_begin=-1, to_end=0))
  95. assert_array_equal([], ediff1d(one_elem))
  96. assert_array_equal([1], ediff1d(two_elem))
  97. assert_array_equal([7, 1, 9], ediff1d(two_elem, to_begin=7, to_end=9))
  98. assert_array_equal([5, 6, 1, 7, 8],
  99. ediff1d(two_elem, to_begin=[5, 6], to_end=[7, 8]))
  100. assert_array_equal([1, 9], ediff1d(two_elem, to_end=9))
  101. assert_array_equal([1, 7, 8], ediff1d(two_elem, to_end=[7, 8]))
  102. assert_array_equal([7, 1], ediff1d(two_elem, to_begin=7))
  103. assert_array_equal([5, 6, 1], ediff1d(two_elem, to_begin=[5, 6]))
  104. @pytest.mark.parametrize("ary, prepend, append, expected", [
  105. # should fail because trying to cast
  106. # np.nan standard floating point value
  107. # into an integer array:
  108. (np.array([1, 2, 3], dtype=np.int64),
  109. None,
  110. np.nan,
  111. 'to_end'),
  112. # should fail because attempting
  113. # to downcast to int type:
  114. (np.array([1, 2, 3], dtype=np.int64),
  115. np.array([5, 7, 2], dtype=np.float32),
  116. None,
  117. 'to_begin'),
  118. # should fail because attempting to cast
  119. # two special floating point values
  120. # to integers (on both sides of ary),
  121. # `to_begin` is in the error message as the impl checks this first:
  122. (np.array([1., 3., 9.], dtype=np.int8),
  123. np.nan,
  124. np.nan,
  125. 'to_begin'),
  126. ])
  127. def test_ediff1d_forbidden_type_casts(self, ary, prepend, append, expected):
  128. # verify resolution of gh-11490
  129. # specifically, raise an appropriate
  130. # Exception when attempting to append or
  131. # prepend with an incompatible type
  132. msg = 'dtype of `{}` must be compatible'.format(expected)
  133. with assert_raises_regex(TypeError, msg):
  134. ediff1d(ary=ary,
  135. to_end=append,
  136. to_begin=prepend)
  137. @pytest.mark.parametrize(
  138. "ary,prepend,append,expected",
  139. [
  140. (np.array([1, 2, 3], dtype=np.int16),
  141. 2**16, # will be cast to int16 under same kind rule.
  142. 2**16 + 4,
  143. np.array([0, 1, 1, 4], dtype=np.int16)),
  144. (np.array([1, 2, 3], dtype=np.float32),
  145. np.array([5], dtype=np.float64),
  146. None,
  147. np.array([5, 1, 1], dtype=np.float32)),
  148. (np.array([1, 2, 3], dtype=np.int32),
  149. 0,
  150. 0,
  151. np.array([0, 1, 1, 0], dtype=np.int32)),
  152. (np.array([1, 2, 3], dtype=np.int64),
  153. 3,
  154. -9,
  155. np.array([3, 1, 1, -9], dtype=np.int64)),
  156. ]
  157. )
  158. def test_ediff1d_scalar_handling(self,
  159. ary,
  160. prepend,
  161. append,
  162. expected):
  163. # maintain backwards-compatibility
  164. # of scalar prepend / append behavior
  165. # in ediff1d following fix for gh-11490
  166. actual = np.ediff1d(ary=ary,
  167. to_end=append,
  168. to_begin=prepend)
  169. assert_equal(actual, expected)
  170. assert actual.dtype == expected.dtype
  171. def test_isin(self):
  172. # the tests for in1d cover most of isin's behavior
  173. # if in1d is removed, would need to change those tests to test
  174. # isin instead.
  175. def _isin_slow(a, b):
  176. b = np.asarray(b).flatten().tolist()
  177. return a in b
  178. isin_slow = np.vectorize(_isin_slow, otypes=[bool], excluded={1})
  179. def assert_isin_equal(a, b):
  180. x = isin(a, b)
  181. y = isin_slow(a, b)
  182. assert_array_equal(x, y)
  183. # multidimensional arrays in both arguments
  184. a = np.arange(24).reshape([2, 3, 4])
  185. b = np.array([[10, 20, 30], [0, 1, 3], [11, 22, 33]])
  186. assert_isin_equal(a, b)
  187. # array-likes as both arguments
  188. c = [(9, 8), (7, 6)]
  189. d = (9, 7)
  190. assert_isin_equal(c, d)
  191. # zero-d array:
  192. f = np.array(3)
  193. assert_isin_equal(f, b)
  194. assert_isin_equal(a, f)
  195. assert_isin_equal(f, f)
  196. # scalar:
  197. assert_isin_equal(5, b)
  198. assert_isin_equal(a, 6)
  199. assert_isin_equal(5, 6)
  200. # empty array-like:
  201. x = []
  202. assert_isin_equal(x, b)
  203. assert_isin_equal(a, x)
  204. assert_isin_equal(x, x)
  205. def test_in1d(self):
  206. # we use two different sizes for the b array here to test the
  207. # two different paths in in1d().
  208. for mult in (1, 10):
  209. # One check without np.array to make sure lists are handled correct
  210. a = [5, 7, 1, 2]
  211. b = [2, 4, 3, 1, 5] * mult
  212. ec = np.array([True, False, True, True])
  213. c = in1d(a, b, assume_unique=True)
  214. assert_array_equal(c, ec)
  215. a[0] = 8
  216. ec = np.array([False, False, True, True])
  217. c = in1d(a, b, assume_unique=True)
  218. assert_array_equal(c, ec)
  219. a[0], a[3] = 4, 8
  220. ec = np.array([True, False, True, False])
  221. c = in1d(a, b, assume_unique=True)
  222. assert_array_equal(c, ec)
  223. a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5])
  224. b = [2, 3, 4] * mult
  225. ec = [False, True, False, True, True, True, True, True, True,
  226. False, True, False, False, False]
  227. c = in1d(a, b)
  228. assert_array_equal(c, ec)
  229. b = b + [5, 5, 4] * mult
  230. ec = [True, True, True, True, True, True, True, True, True, True,
  231. True, False, True, True]
  232. c = in1d(a, b)
  233. assert_array_equal(c, ec)
  234. a = np.array([5, 7, 1, 2])
  235. b = np.array([2, 4, 3, 1, 5] * mult)
  236. ec = np.array([True, False, True, True])
  237. c = in1d(a, b)
  238. assert_array_equal(c, ec)
  239. a = np.array([5, 7, 1, 1, 2])
  240. b = np.array([2, 4, 3, 3, 1, 5] * mult)
  241. ec = np.array([True, False, True, True, True])
  242. c = in1d(a, b)
  243. assert_array_equal(c, ec)
  244. a = np.array([5, 5])
  245. b = np.array([2, 2] * mult)
  246. ec = np.array([False, False])
  247. c = in1d(a, b)
  248. assert_array_equal(c, ec)
  249. a = np.array([5])
  250. b = np.array([2])
  251. ec = np.array([False])
  252. c = in1d(a, b)
  253. assert_array_equal(c, ec)
  254. assert_array_equal(in1d([], []), [])
  255. def test_in1d_char_array(self):
  256. a = np.array(['a', 'b', 'c', 'd', 'e', 'c', 'e', 'b'])
  257. b = np.array(['a', 'c'])
  258. ec = np.array([True, False, True, False, False, True, False, False])
  259. c = in1d(a, b)
  260. assert_array_equal(c, ec)
  261. def test_in1d_invert(self):
  262. "Test in1d's invert parameter"
  263. # We use two different sizes for the b array here to test the
  264. # two different paths in in1d().
  265. for mult in (1, 10):
  266. a = np.array([5, 4, 5, 3, 4, 4, 3, 4, 3, 5, 2, 1, 5, 5])
  267. b = [2, 3, 4] * mult
  268. assert_array_equal(np.invert(in1d(a, b)), in1d(a, b, invert=True))
  269. def test_in1d_ravel(self):
  270. # Test that in1d ravels its input arrays. This is not documented
  271. # behavior however. The test is to ensure consistentency.
  272. a = np.arange(6).reshape(2, 3)
  273. b = np.arange(3, 9).reshape(3, 2)
  274. long_b = np.arange(3, 63).reshape(30, 2)
  275. ec = np.array([False, False, False, True, True, True])
  276. assert_array_equal(in1d(a, b, assume_unique=True), ec)
  277. assert_array_equal(in1d(a, b, assume_unique=False), ec)
  278. assert_array_equal(in1d(a, long_b, assume_unique=True), ec)
  279. assert_array_equal(in1d(a, long_b, assume_unique=False), ec)
  280. def test_in1d_first_array_is_object(self):
  281. ar1 = [None]
  282. ar2 = np.array([1]*10)
  283. expected = np.array([False])
  284. result = np.in1d(ar1, ar2)
  285. assert_array_equal(result, expected)
  286. def test_in1d_second_array_is_object(self):
  287. ar1 = 1
  288. ar2 = np.array([None]*10)
  289. expected = np.array([False])
  290. result = np.in1d(ar1, ar2)
  291. assert_array_equal(result, expected)
  292. def test_in1d_both_arrays_are_object(self):
  293. ar1 = [None]
  294. ar2 = np.array([None]*10)
  295. expected = np.array([True])
  296. result = np.in1d(ar1, ar2)
  297. assert_array_equal(result, expected)
  298. def test_in1d_both_arrays_have_structured_dtype(self):
  299. # Test arrays of a structured data type containing an integer field
  300. # and a field of dtype `object` allowing for arbitrary Python objects
  301. dt = np.dtype([('field1', int), ('field2', object)])
  302. ar1 = np.array([(1, None)], dtype=dt)
  303. ar2 = np.array([(1, None)]*10, dtype=dt)
  304. expected = np.array([True])
  305. result = np.in1d(ar1, ar2)
  306. assert_array_equal(result, expected)
  307. def test_union1d(self):
  308. a = np.array([5, 4, 7, 1, 2])
  309. b = np.array([2, 4, 3, 3, 2, 1, 5])
  310. ec = np.array([1, 2, 3, 4, 5, 7])
  311. c = union1d(a, b)
  312. assert_array_equal(c, ec)
  313. # Tests gh-10340, arguments to union1d should be
  314. # flattened if they are not already 1D
  315. x = np.array([[0, 1, 2], [3, 4, 5]])
  316. y = np.array([0, 1, 2, 3, 4])
  317. ez = np.array([0, 1, 2, 3, 4, 5])
  318. z = union1d(x, y)
  319. assert_array_equal(z, ez)
  320. assert_array_equal([], union1d([], []))
  321. def test_setdiff1d(self):
  322. a = np.array([6, 5, 4, 7, 1, 2, 7, 4])
  323. b = np.array([2, 4, 3, 3, 2, 1, 5])
  324. ec = np.array([6, 7])
  325. c = setdiff1d(a, b)
  326. assert_array_equal(c, ec)
  327. a = np.arange(21)
  328. b = np.arange(19)
  329. ec = np.array([19, 20])
  330. c = setdiff1d(a, b)
  331. assert_array_equal(c, ec)
  332. assert_array_equal([], setdiff1d([], []))
  333. a = np.array((), np.uint32)
  334. assert_equal(setdiff1d(a, []).dtype, np.uint32)
  335. def test_setdiff1d_unique(self):
  336. a = np.array([3, 2, 1])
  337. b = np.array([7, 5, 2])
  338. expected = np.array([3, 1])
  339. actual = setdiff1d(a, b, assume_unique=True)
  340. assert_equal(actual, expected)
  341. def test_setdiff1d_char_array(self):
  342. a = np.array(['a', 'b', 'c'])
  343. b = np.array(['a', 'b', 's'])
  344. assert_array_equal(setdiff1d(a, b), np.array(['c']))
  345. def test_manyways(self):
  346. a = np.array([5, 7, 1, 2, 8])
  347. b = np.array([9, 8, 2, 4, 3, 1, 5])
  348. c1 = setxor1d(a, b)
  349. aux1 = intersect1d(a, b)
  350. aux2 = union1d(a, b)
  351. c2 = setdiff1d(aux2, aux1)
  352. assert_array_equal(c1, c2)
  353. class TestUnique:
  354. def test_unique_1d(self):
  355. def check_all(a, b, i1, i2, c, dt):
  356. base_msg = 'check {0} failed for type {1}'
  357. msg = base_msg.format('values', dt)
  358. v = unique(a)
  359. assert_array_equal(v, b, msg)
  360. msg = base_msg.format('return_index', dt)
  361. v, j = unique(a, True, False, False)
  362. assert_array_equal(v, b, msg)
  363. assert_array_equal(j, i1, msg)
  364. msg = base_msg.format('return_inverse', dt)
  365. v, j = unique(a, False, True, False)
  366. assert_array_equal(v, b, msg)
  367. assert_array_equal(j, i2, msg)
  368. msg = base_msg.format('return_counts', dt)
  369. v, j = unique(a, False, False, True)
  370. assert_array_equal(v, b, msg)
  371. assert_array_equal(j, c, msg)
  372. msg = base_msg.format('return_index and return_inverse', dt)
  373. v, j1, j2 = unique(a, True, True, False)
  374. assert_array_equal(v, b, msg)
  375. assert_array_equal(j1, i1, msg)
  376. assert_array_equal(j2, i2, msg)
  377. msg = base_msg.format('return_index and return_counts', dt)
  378. v, j1, j2 = unique(a, True, False, True)
  379. assert_array_equal(v, b, msg)
  380. assert_array_equal(j1, i1, msg)
  381. assert_array_equal(j2, c, msg)
  382. msg = base_msg.format('return_inverse and return_counts', dt)
  383. v, j1, j2 = unique(a, False, True, True)
  384. assert_array_equal(v, b, msg)
  385. assert_array_equal(j1, i2, msg)
  386. assert_array_equal(j2, c, msg)
  387. msg = base_msg.format(('return_index, return_inverse '
  388. 'and return_counts'), dt)
  389. v, j1, j2, j3 = unique(a, True, True, True)
  390. assert_array_equal(v, b, msg)
  391. assert_array_equal(j1, i1, msg)
  392. assert_array_equal(j2, i2, msg)
  393. assert_array_equal(j3, c, msg)
  394. a = [5, 7, 1, 2, 1, 5, 7]*10
  395. b = [1, 2, 5, 7]
  396. i1 = [2, 3, 0, 1]
  397. i2 = [2, 3, 0, 1, 0, 2, 3]*10
  398. c = np.multiply([2, 1, 2, 2], 10)
  399. # test for numeric arrays
  400. types = []
  401. types.extend(np.typecodes['AllInteger'])
  402. types.extend(np.typecodes['AllFloat'])
  403. types.append('datetime64[D]')
  404. types.append('timedelta64[D]')
  405. for dt in types:
  406. aa = np.array(a, dt)
  407. bb = np.array(b, dt)
  408. check_all(aa, bb, i1, i2, c, dt)
  409. # test for object arrays
  410. dt = 'O'
  411. aa = np.empty(len(a), dt)
  412. aa[:] = a
  413. bb = np.empty(len(b), dt)
  414. bb[:] = b
  415. check_all(aa, bb, i1, i2, c, dt)
  416. # test for structured arrays
  417. dt = [('', 'i'), ('', 'i')]
  418. aa = np.array(list(zip(a, a)), dt)
  419. bb = np.array(list(zip(b, b)), dt)
  420. check_all(aa, bb, i1, i2, c, dt)
  421. # test for ticket #2799
  422. aa = [1. + 0.j, 1 - 1.j, 1]
  423. assert_array_equal(np.unique(aa), [1. - 1.j, 1. + 0.j])
  424. # test for ticket #4785
  425. a = [(1, 2), (1, 2), (2, 3)]
  426. unq = [1, 2, 3]
  427. inv = [0, 1, 0, 1, 1, 2]
  428. a1 = unique(a)
  429. assert_array_equal(a1, unq)
  430. a2, a2_inv = unique(a, return_inverse=True)
  431. assert_array_equal(a2, unq)
  432. assert_array_equal(a2_inv, inv)
  433. # test for chararrays with return_inverse (gh-5099)
  434. a = np.chararray(5)
  435. a[...] = ''
  436. a2, a2_inv = np.unique(a, return_inverse=True)
  437. assert_array_equal(a2_inv, np.zeros(5))
  438. # test for ticket #9137
  439. a = []
  440. a1_idx = np.unique(a, return_index=True)[1]
  441. a2_inv = np.unique(a, return_inverse=True)[1]
  442. a3_idx, a3_inv = np.unique(a, return_index=True,
  443. return_inverse=True)[1:]
  444. assert_equal(a1_idx.dtype, np.intp)
  445. assert_equal(a2_inv.dtype, np.intp)
  446. assert_equal(a3_idx.dtype, np.intp)
  447. assert_equal(a3_inv.dtype, np.intp)
  448. def test_unique_axis_errors(self):
  449. assert_raises(TypeError, self._run_axis_tests, object)
  450. assert_raises(TypeError, self._run_axis_tests,
  451. [('a', int), ('b', object)])
  452. assert_raises(np.AxisError, unique, np.arange(10), axis=2)
  453. assert_raises(np.AxisError, unique, np.arange(10), axis=-2)
  454. def test_unique_axis_list(self):
  455. msg = "Unique failed on list of lists"
  456. inp = [[0, 1, 0], [0, 1, 0]]
  457. inp_arr = np.asarray(inp)
  458. assert_array_equal(unique(inp, axis=0), unique(inp_arr, axis=0), msg)
  459. assert_array_equal(unique(inp, axis=1), unique(inp_arr, axis=1), msg)
  460. def test_unique_axis(self):
  461. types = []
  462. types.extend(np.typecodes['AllInteger'])
  463. types.extend(np.typecodes['AllFloat'])
  464. types.append('datetime64[D]')
  465. types.append('timedelta64[D]')
  466. types.append([('a', int), ('b', int)])
  467. types.append([('a', int), ('b', float)])
  468. for dtype in types:
  469. self._run_axis_tests(dtype)
  470. msg = 'Non-bitwise-equal booleans test failed'
  471. data = np.arange(10, dtype=np.uint8).reshape(-1, 2).view(bool)
  472. result = np.array([[False, True], [True, True]], dtype=bool)
  473. assert_array_equal(unique(data, axis=0), result, msg)
  474. msg = 'Negative zero equality test failed'
  475. data = np.array([[-0.0, 0.0], [0.0, -0.0], [-0.0, 0.0], [0.0, -0.0]])
  476. result = np.array([[-0.0, 0.0]])
  477. assert_array_equal(unique(data, axis=0), result, msg)
  478. @pytest.mark.parametrize("axis", [0, -1])
  479. def test_unique_1d_with_axis(self, axis):
  480. x = np.array([4, 3, 2, 3, 2, 1, 2, 2])
  481. uniq = unique(x, axis=axis)
  482. assert_array_equal(uniq, [1, 2, 3, 4])
  483. def test_unique_axis_zeros(self):
  484. # issue 15559
  485. single_zero = np.empty(shape=(2, 0), dtype=np.int8)
  486. uniq, idx, inv, cnt = unique(single_zero, axis=0, return_index=True,
  487. return_inverse=True, return_counts=True)
  488. # there's 1 element of shape (0,) along axis 0
  489. assert_equal(uniq.dtype, single_zero.dtype)
  490. assert_array_equal(uniq, np.empty(shape=(1, 0)))
  491. assert_array_equal(idx, np.array([0]))
  492. assert_array_equal(inv, np.array([0, 0]))
  493. assert_array_equal(cnt, np.array([2]))
  494. # there's 0 elements of shape (2,) along axis 1
  495. uniq, idx, inv, cnt = unique(single_zero, axis=1, return_index=True,
  496. return_inverse=True, return_counts=True)
  497. assert_equal(uniq.dtype, single_zero.dtype)
  498. assert_array_equal(uniq, np.empty(shape=(2, 0)))
  499. assert_array_equal(idx, np.array([]))
  500. assert_array_equal(inv, np.array([]))
  501. assert_array_equal(cnt, np.array([]))
  502. # test a "complicated" shape
  503. shape = (0, 2, 0, 3, 0, 4, 0)
  504. multiple_zeros = np.empty(shape=shape)
  505. for axis in range(len(shape)):
  506. expected_shape = list(shape)
  507. if shape[axis] == 0:
  508. expected_shape[axis] = 0
  509. else:
  510. expected_shape[axis] = 1
  511. assert_array_equal(unique(multiple_zeros, axis=axis),
  512. np.empty(shape=expected_shape))
  513. def test_unique_masked(self):
  514. # issue 8664
  515. x = np.array([64, 0, 1, 2, 3, 63, 63, 0, 0, 0, 1, 2, 0, 63, 0],
  516. dtype='uint8')
  517. y = np.ma.masked_equal(x, 0)
  518. v = np.unique(y)
  519. v2, i, c = np.unique(y, return_index=True, return_counts=True)
  520. msg = 'Unique returned different results when asked for index'
  521. assert_array_equal(v.data, v2.data, msg)
  522. assert_array_equal(v.mask, v2.mask, msg)
  523. def test_unique_sort_order_with_axis(self):
  524. # These tests fail if sorting along axis is done by treating subarrays
  525. # as unsigned byte strings. See gh-10495.
  526. fmt = "sort order incorrect for integer type '%s'"
  527. for dt in 'bhilq':
  528. a = np.array([[-1], [0]], dt)
  529. b = np.unique(a, axis=0)
  530. assert_array_equal(a, b, fmt % dt)
  531. def _run_axis_tests(self, dtype):
  532. data = np.array([[0, 1, 0, 0],
  533. [1, 0, 0, 0],
  534. [0, 1, 0, 0],
  535. [1, 0, 0, 0]]).astype(dtype)
  536. msg = 'Unique with 1d array and axis=0 failed'
  537. result = np.array([0, 1])
  538. assert_array_equal(unique(data), result.astype(dtype), msg)
  539. msg = 'Unique with 2d array and axis=0 failed'
  540. result = np.array([[0, 1, 0, 0], [1, 0, 0, 0]])
  541. assert_array_equal(unique(data, axis=0), result.astype(dtype), msg)
  542. msg = 'Unique with 2d array and axis=1 failed'
  543. result = np.array([[0, 0, 1], [0, 1, 0], [0, 0, 1], [0, 1, 0]])
  544. assert_array_equal(unique(data, axis=1), result.astype(dtype), msg)
  545. msg = 'Unique with 3d array and axis=2 failed'
  546. data3d = np.array([[[1, 1],
  547. [1, 0]],
  548. [[0, 1],
  549. [0, 0]]]).astype(dtype)
  550. result = np.take(data3d, [1, 0], axis=2)
  551. assert_array_equal(unique(data3d, axis=2), result, msg)
  552. uniq, idx, inv, cnt = unique(data, axis=0, return_index=True,
  553. return_inverse=True, return_counts=True)
  554. msg = "Unique's return_index=True failed with axis=0"
  555. assert_array_equal(data[idx], uniq, msg)
  556. msg = "Unique's return_inverse=True failed with axis=0"
  557. assert_array_equal(uniq[inv], data)
  558. msg = "Unique's return_counts=True failed with axis=0"
  559. assert_array_equal(cnt, np.array([2, 2]), msg)
  560. uniq, idx, inv, cnt = unique(data, axis=1, return_index=True,
  561. return_inverse=True, return_counts=True)
  562. msg = "Unique's return_index=True failed with axis=1"
  563. assert_array_equal(data[:, idx], uniq)
  564. msg = "Unique's return_inverse=True failed with axis=1"
  565. assert_array_equal(uniq[:, inv], data)
  566. msg = "Unique's return_counts=True failed with axis=1"
  567. assert_array_equal(cnt, np.array([2, 1, 1]), msg)