test_arraysetops.py 22 KB

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