test_packbits.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. from __future__ import division, absolute_import, print_function
  2. import numpy as np
  3. from numpy.testing import assert_array_equal, assert_equal, assert_raises
  4. def test_packbits():
  5. # Copied from the docstring.
  6. a = [[[1, 0, 1], [0, 1, 0]],
  7. [[1, 1, 0], [0, 0, 1]]]
  8. for dt in '?bBhHiIlLqQ':
  9. arr = np.array(a, dtype=dt)
  10. b = np.packbits(arr, axis=-1)
  11. assert_equal(b.dtype, np.uint8)
  12. assert_array_equal(b, np.array([[[160], [64]], [[192], [32]]]))
  13. assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
  14. def test_packbits_empty():
  15. shapes = [
  16. (0,), (10, 20, 0), (10, 0, 20), (0, 10, 20), (20, 0, 0), (0, 20, 0),
  17. (0, 0, 20), (0, 0, 0),
  18. ]
  19. for dt in '?bBhHiIlLqQ':
  20. for shape in shapes:
  21. a = np.empty(shape, dtype=dt)
  22. b = np.packbits(a)
  23. assert_equal(b.dtype, np.uint8)
  24. assert_equal(b.shape, (0,))
  25. def test_packbits_empty_with_axis():
  26. # Original shapes and lists of packed shapes for different axes.
  27. shapes = [
  28. ((0,), [(0,)]),
  29. ((10, 20, 0), [(2, 20, 0), (10, 3, 0), (10, 20, 0)]),
  30. ((10, 0, 20), [(2, 0, 20), (10, 0, 20), (10, 0, 3)]),
  31. ((0, 10, 20), [(0, 10, 20), (0, 2, 20), (0, 10, 3)]),
  32. ((20, 0, 0), [(3, 0, 0), (20, 0, 0), (20, 0, 0)]),
  33. ((0, 20, 0), [(0, 20, 0), (0, 3, 0), (0, 20, 0)]),
  34. ((0, 0, 20), [(0, 0, 20), (0, 0, 20), (0, 0, 3)]),
  35. ((0, 0, 0), [(0, 0, 0), (0, 0, 0), (0, 0, 0)]),
  36. ]
  37. for dt in '?bBhHiIlLqQ':
  38. for in_shape, out_shapes in shapes:
  39. for ax, out_shape in enumerate(out_shapes):
  40. a = np.empty(in_shape, dtype=dt)
  41. b = np.packbits(a, axis=ax)
  42. assert_equal(b.dtype, np.uint8)
  43. assert_equal(b.shape, out_shape)
  44. def test_packbits_large():
  45. # test data large enough for 16 byte vectorization
  46. a = np.array([1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0,
  47. 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1,
  48. 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,
  49. 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1,
  50. 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1,
  51. 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1,
  52. 1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1,
  53. 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1,
  54. 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0,
  55. 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1,
  56. 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0,
  57. 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1,
  58. 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0,
  59. 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0,
  60. 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0])
  61. a = a.repeat(3)
  62. for dtype in '?bBhHiIlLqQ':
  63. arr = np.array(a, dtype=dtype)
  64. b = np.packbits(arr, axis=None)
  65. assert_equal(b.dtype, np.uint8)
  66. r = [252, 127, 192, 3, 254, 7, 252, 0, 7, 31, 240, 0, 28, 1, 255, 252,
  67. 113, 248, 3, 255, 192, 28, 15, 192, 28, 126, 0, 224, 127, 255,
  68. 227, 142, 7, 31, 142, 63, 28, 126, 56, 227, 240, 0, 227, 128, 63,
  69. 224, 14, 56, 252, 112, 56, 255, 241, 248, 3, 240, 56, 224, 112,
  70. 63, 255, 255, 199, 224, 14, 0, 31, 143, 192, 3, 255, 199, 0, 1,
  71. 255, 224, 1, 255, 252, 126, 63, 0, 1, 192, 252, 14, 63, 0, 15,
  72. 199, 252, 113, 255, 3, 128, 56, 252, 14, 7, 0, 113, 255, 255, 142, 56, 227,
  73. 129, 248, 227, 129, 199, 31, 128]
  74. assert_array_equal(b, r)
  75. # equal for size being multiple of 8
  76. assert_array_equal(np.unpackbits(b)[:-4], a)
  77. # check last byte of different remainders (16 byte vectorization)
  78. b = [np.packbits(arr[:-i], axis=None)[-1] for i in range(1, 16)]
  79. assert_array_equal(b, [128, 128, 128, 31, 30, 28, 24, 16, 0, 0, 0, 199,
  80. 198, 196, 192])
  81. arr = arr.reshape(36, 25)
  82. b = np.packbits(arr, axis=0)
  83. assert_equal(b.dtype, np.uint8)
  84. assert_array_equal(b, [[190, 186, 178, 178, 150, 215, 87, 83, 83, 195,
  85. 199, 206, 204, 204, 140, 140, 136, 136, 8, 40, 105,
  86. 107, 75, 74, 88],
  87. [72, 216, 248, 241, 227, 195, 202, 90, 90, 83,
  88. 83, 119, 127, 109, 73, 64, 208, 244, 189, 45,
  89. 41, 104, 122, 90, 18],
  90. [113, 120, 248, 216, 152, 24, 60, 52, 182, 150,
  91. 150, 150, 146, 210, 210, 246, 255, 255, 223,
  92. 151, 21, 17, 17, 131, 163],
  93. [214, 210, 210, 64, 68, 5, 5, 1, 72, 88, 92,
  94. 92, 78, 110, 39, 181, 149, 220, 222, 218, 218,
  95. 202, 234, 170, 168],
  96. [0, 128, 128, 192, 80, 112, 48, 160, 160, 224,
  97. 240, 208, 144, 128, 160, 224, 240, 208, 144,
  98. 144, 176, 240, 224, 192, 128]])
  99. b = np.packbits(arr, axis=1)
  100. assert_equal(b.dtype, np.uint8)
  101. assert_array_equal(b, [[252, 127, 192, 0],
  102. [ 7, 252, 15, 128],
  103. [240, 0, 28, 0],
  104. [255, 128, 0, 128],
  105. [192, 31, 255, 128],
  106. [142, 63, 0, 0],
  107. [255, 240, 7, 0],
  108. [ 7, 224, 14, 0],
  109. [126, 0, 224, 0],
  110. [255, 255, 199, 0],
  111. [ 56, 28, 126, 0],
  112. [113, 248, 227, 128],
  113. [227, 142, 63, 0],
  114. [ 0, 28, 112, 0],
  115. [ 15, 248, 3, 128],
  116. [ 28, 126, 56, 0],
  117. [ 56, 255, 241, 128],
  118. [240, 7, 224, 0],
  119. [227, 129, 192, 128],
  120. [255, 255, 254, 0],
  121. [126, 0, 224, 0],
  122. [ 3, 241, 248, 0],
  123. [ 0, 255, 241, 128],
  124. [128, 0, 255, 128],
  125. [224, 1, 255, 128],
  126. [248, 252, 126, 0],
  127. [ 0, 7, 3, 128],
  128. [224, 113, 248, 0],
  129. [ 0, 252, 127, 128],
  130. [142, 63, 224, 0],
  131. [224, 14, 63, 0],
  132. [ 7, 3, 128, 0],
  133. [113, 255, 255, 128],
  134. [ 28, 113, 199, 0],
  135. [ 7, 227, 142, 0],
  136. [ 14, 56, 252, 0]])
  137. arr = arr.T.copy()
  138. b = np.packbits(arr, axis=0)
  139. assert_equal(b.dtype, np.uint8)
  140. assert_array_equal(b, [[252, 7, 240, 255, 192, 142, 255, 7, 126, 255,
  141. 56, 113, 227, 0, 15, 28, 56, 240, 227, 255,
  142. 126, 3, 0, 128, 224, 248, 0, 224, 0, 142, 224,
  143. 7, 113, 28, 7, 14],
  144. [127, 252, 0, 128, 31, 63, 240, 224, 0, 255,
  145. 28, 248, 142, 28, 248, 126, 255, 7, 129, 255,
  146. 0, 241, 255, 0, 1, 252, 7, 113, 252, 63, 14,
  147. 3, 255, 113, 227, 56],
  148. [192, 15, 28, 0, 255, 0, 7, 14, 224, 199, 126,
  149. 227, 63, 112, 3, 56, 241, 224, 192, 254, 224,
  150. 248, 241, 255, 255, 126, 3, 248, 127, 224, 63,
  151. 128, 255, 199, 142, 252],
  152. [0, 128, 0, 128, 128, 0, 0, 0, 0, 0, 0, 128, 0,
  153. 0, 128, 0, 128, 0, 128, 0, 0, 0, 128, 128,
  154. 128, 0, 128, 0, 128, 0, 0, 0, 128, 0, 0, 0]])
  155. b = np.packbits(arr, axis=1)
  156. assert_equal(b.dtype, np.uint8)
  157. assert_array_equal(b, [[190, 72, 113, 214, 0],
  158. [186, 216, 120, 210, 128],
  159. [178, 248, 248, 210, 128],
  160. [178, 241, 216, 64, 192],
  161. [150, 227, 152, 68, 80],
  162. [215, 195, 24, 5, 112],
  163. [ 87, 202, 60, 5, 48],
  164. [ 83, 90, 52, 1, 160],
  165. [ 83, 90, 182, 72, 160],
  166. [195, 83, 150, 88, 224],
  167. [199, 83, 150, 92, 240],
  168. [206, 119, 150, 92, 208],
  169. [204, 127, 146, 78, 144],
  170. [204, 109, 210, 110, 128],
  171. [140, 73, 210, 39, 160],
  172. [140, 64, 246, 181, 224],
  173. [136, 208, 255, 149, 240],
  174. [136, 244, 255, 220, 208],
  175. [ 8, 189, 223, 222, 144],
  176. [ 40, 45, 151, 218, 144],
  177. [105, 41, 21, 218, 176],
  178. [107, 104, 17, 202, 240],
  179. [ 75, 122, 17, 234, 224],
  180. [ 74, 90, 131, 170, 192],
  181. [ 88, 18, 163, 168, 128]])
  182. # result is the same if input is multiplied with a nonzero value
  183. for dtype in 'bBhHiIlLqQ':
  184. arr = np.array(a, dtype=dtype)
  185. rnd = np.random.randint(low=np.iinfo(dtype).min,
  186. high=np.iinfo(dtype).max, size=arr.size,
  187. dtype=dtype)
  188. rnd[rnd == 0] = 1
  189. arr *= rnd.astype(dtype)
  190. b = np.packbits(arr, axis=-1)
  191. assert_array_equal(np.unpackbits(b)[:-4], a)
  192. assert_raises(TypeError, np.packbits, np.array(a, dtype=float))
  193. def test_packbits_very_large():
  194. # test some with a larger arrays gh-8637
  195. # code is covered earlier but larger array makes crash on bug more likely
  196. for s in range(950, 1050):
  197. for dt in '?bBhHiIlLqQ':
  198. x = np.ones((200, s), dtype=bool)
  199. np.packbits(x, axis=1)
  200. def test_unpackbits():
  201. # Copied from the docstring.
  202. a = np.array([[2], [7], [23]], dtype=np.uint8)
  203. b = np.unpackbits(a, axis=1)
  204. assert_equal(b.dtype, np.uint8)
  205. assert_array_equal(b, np.array([[0, 0, 0, 0, 0, 0, 1, 0],
  206. [0, 0, 0, 0, 0, 1, 1, 1],
  207. [0, 0, 0, 1, 0, 1, 1, 1]]))
  208. def test_unpackbits_empty():
  209. a = np.empty((0,), dtype=np.uint8)
  210. b = np.unpackbits(a)
  211. assert_equal(b.dtype, np.uint8)
  212. assert_array_equal(b, np.empty((0,)))
  213. def test_unpackbits_empty_with_axis():
  214. # Lists of packed shapes for different axes and unpacked shapes.
  215. shapes = [
  216. ([(0,)], (0,)),
  217. ([(2, 24, 0), (16, 3, 0), (16, 24, 0)], (16, 24, 0)),
  218. ([(2, 0, 24), (16, 0, 24), (16, 0, 3)], (16, 0, 24)),
  219. ([(0, 16, 24), (0, 2, 24), (0, 16, 3)], (0, 16, 24)),
  220. ([(3, 0, 0), (24, 0, 0), (24, 0, 0)], (24, 0, 0)),
  221. ([(0, 24, 0), (0, 3, 0), (0, 24, 0)], (0, 24, 0)),
  222. ([(0, 0, 24), (0, 0, 24), (0, 0, 3)], (0, 0, 24)),
  223. ([(0, 0, 0), (0, 0, 0), (0, 0, 0)], (0, 0, 0)),
  224. ]
  225. for in_shapes, out_shape in shapes:
  226. for ax, in_shape in enumerate(in_shapes):
  227. a = np.empty(in_shape, dtype=np.uint8)
  228. b = np.unpackbits(a, axis=ax)
  229. assert_equal(b.dtype, np.uint8)
  230. assert_equal(b.shape, out_shape)
  231. def test_unpackbits_large():
  232. # test all possible numbers via comparison to already tested packbits
  233. d = np.arange(277, dtype=np.uint8)
  234. assert_array_equal(np.packbits(np.unpackbits(d)), d)
  235. assert_array_equal(np.packbits(np.unpackbits(d[::2])), d[::2])
  236. d = np.tile(d, (3, 1))
  237. assert_array_equal(np.packbits(np.unpackbits(d, axis=1), axis=1), d)
  238. d = d.T.copy()
  239. assert_array_equal(np.packbits(np.unpackbits(d, axis=0), axis=0), d)