90_array2d.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. from array2d import array2d
  2. from vmath import vec2i
  3. def exit_on_error():
  4. raise KeyboardInterrupt
  5. # test error args for __init__
  6. try:
  7. a = array2d(0, 0)
  8. exit_on_error()
  9. except ValueError:
  10. pass
  11. # test callable constructor
  12. a = array2d[int](2, 4, lambda pos: (pos.x, pos.y))
  13. assert a.width == a.n_cols == 2
  14. assert a.height == a.n_rows == 4
  15. assert a.shape == vec2i(2, 4)
  16. assert a.numel == 8
  17. assert a.tolist() == [
  18. [(0, 0), (1, 0)],
  19. [(0, 1), (1, 1)],
  20. [(0, 2), (1, 2)],
  21. [(0, 3), (1, 3)]]
  22. assert a[0, :].tolist() == [[(0, 0)], [(0, 1)], [(0, 2)], [(0, 3)]]
  23. assert a[1, :].tolist() == [[(1, 0)], [(1, 1)], [(1, 2)], [(1, 3)]]
  24. assert a[:, 0].tolist() == [[(0, 0), (1, 0)]]
  25. assert a[:, -1].tolist() == [[(0, 3), (1, 3)]]
  26. # test is_valid
  27. assert a.is_valid(0, 0) and a.is_valid(vec2i(0, 0))
  28. assert a.is_valid(1, 3) and a.is_valid(vec2i(1, 3))
  29. assert not a.is_valid(2, 0) and not a.is_valid(vec2i(2, 0))
  30. assert not a.is_valid(0, 4) and not a.is_valid(vec2i(0, 4))
  31. assert not a.is_valid(-1, 0) and not a.is_valid(vec2i(-1, 0))
  32. assert not a.is_valid(0, -1) and not a.is_valid(vec2i(0, -1))
  33. # test get
  34. assert a.get(0, 0, -1) == (0, 0)
  35. assert a.get(1, 3) == (1, 3)
  36. assert a.get(2, 0) is None
  37. assert a.get(0, 4, 'S') == 'S'
  38. # test __getitem__
  39. assert a[0, 0] == (0, 0)
  40. assert a[1, 3] == (1, 3)
  41. try:
  42. a[2, 0]
  43. exit_on_error()
  44. except IndexError:
  45. pass
  46. # test __setitem__
  47. a = array2d[int](2, 4, default=0)
  48. a[0, 0] = 5
  49. assert a[0, 0] == 5
  50. a[1, 3] = 6
  51. assert a[1, 3] == 6
  52. try:
  53. a[0, -1] = 7
  54. exit_on_error()
  55. except IndexError:
  56. pass
  57. # test tolist
  58. a_list = [[5, 0], [0, 0], [0, 0], [0, 6]]
  59. assert a_list == a.tolist()
  60. # test __eq__
  61. x = array2d(2, 4, default=0)
  62. b = array2d(2, 4, default=0)
  63. assert (x == b).all()
  64. b[0, 0] = 1
  65. assert (x != b).any()
  66. # test __repr__
  67. assert repr(a) == f'array2d(2, 4)'
  68. # test map
  69. c = a.map(lambda x: x + 1)
  70. assert c.tolist() == [[6, 1], [1, 1], [1, 1], [1, 7]]
  71. assert a.tolist() == [[5, 0], [0, 0], [0, 0], [0, 6]]
  72. assert c.width == c.n_cols == 2
  73. assert c.height == c.n_rows == 4
  74. assert c.numel == 8
  75. # test copy
  76. d = c.copy()
  77. assert (d == c).all() and d is not c
  78. # test fill_
  79. d[:, :] = -3 # d.fill_(-3)
  80. assert (d == array2d(2, 4, default=-3)).all()
  81. # test apply
  82. d.apply(lambda x: x + 3)
  83. assert (d == array2d(2, 4, default=0)).all()
  84. # test copy_
  85. a[:, :] = d
  86. assert (a == d).all() and a is not d
  87. x = array2d(2, 4, default=0)
  88. x[:, :] = d
  89. assert (x == d).all() and x is not d
  90. # test alive_neighbors
  91. a = array2d[int](3, 3, default=0)
  92. a[1, 1] = 1
  93. """ Moore von Neumann
  94. 0 0 0 1 1 1 0 1 0
  95. 0 1 0 1 0 1 1 0 1
  96. 0 0 0 1 1 1 0 1 0
  97. """
  98. moore_result = array2d(3, 3, default=1)
  99. moore_result[1, 1] = 0
  100. von_neumann_result = array2d(3, 3, default=0)
  101. von_neumann_result[0, 1] = von_neumann_result[1, 0] = von_neumann_result[1, 2] = von_neumann_result[2, 1] = 1
  102. _0 = a.count_neighbors(1, 'Moore')
  103. assert _0 == moore_result
  104. _1 = a.count_neighbors(1, 'von Neumann')
  105. assert _1 == von_neumann_result
  106. MOORE_KERNEL = array2d[int].fromlist([[1, 1, 1], [1, 0, 1], [1, 1, 1]])
  107. VON_NEUMANN_KERNEL = array2d.fromlist([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
  108. moore_conv_result = a.convolve(MOORE_KERNEL, 0)
  109. assert (moore_conv_result == moore_result).all()
  110. von_neumann_conv_result = a.convolve(VON_NEUMANN_KERNEL, 0)
  111. assert (von_neumann_conv_result == von_neumann_result).all()
  112. # test slice get
  113. a = array2d(5, 5, default=0)
  114. b = array2d(3, 2, default=1)
  115. assert a[1:4, 1:4] == array2d(3, 3, default=0)
  116. assert a[1:4, 1:3] == array2d(3, 2, default=0)
  117. assert (a[1:4, 1:3] != b).any()
  118. a[1:4, 1:3] = b
  119. assert (a[1:4, 1:3] == b).all()
  120. """
  121. 0 0 0 0 0
  122. 0 1 1 1 0
  123. 0 1 1 1 0
  124. 0 0 0 0 0
  125. 0 0 0 0 0
  126. """
  127. assert a.count(1) == 3*2
  128. assert a.get_bounding_rect(1) == (1, 1, 3, 2)
  129. assert a.get_bounding_rect(0) == (0, 0, 5, 5)
  130. try:
  131. a.get_bounding_rect(2)
  132. exit_on_error()
  133. except ValueError:
  134. pass
  135. a = array2d(3, 2, default='?')
  136. # int/float/str/bool/None
  137. for value in [0, 0.0, '0', False, None]:
  138. a[0:2, 0:1] = value
  139. assert a[2, 1] == '?'
  140. assert a[0, 0] == value
  141. a[:, :] = 3
  142. assert a == array2d(3, 2, default=3)
  143. try:
  144. a[:, :] = array2d(1, 1)
  145. exit_on_error()
  146. except ValueError:
  147. pass
  148. # test __iter__
  149. a = array2d(3, 4, default=1)
  150. for xy, val in a:
  151. assert a[xy] == x
  152. # test convolve
  153. a = array2d[int].fromlist([[1, 0, 2, 4, 0], [3, 1, 0, 5, 1]])
  154. """
  155. 1 0 2 4 0
  156. 3 1 0 5 1
  157. """
  158. assert a.tolist() == [[1, 0, 2, 4, 0], [3, 1, 0, 5, 1]]
  159. kernel = array2d[int](3, 3, default=1)
  160. res = a.convolve(kernel, -1)
  161. """
  162. 0 4 9 9 5
  163. 0 4 9 9 5
  164. """
  165. assert res.tolist() == [[0, 4, 9, 9, 5], [0, 4, 9, 9, 5]]
  166. mask = res == 9
  167. assert mask.tolist() == [
  168. [False, False, True, True, False],
  169. [False, False, True, True, False]
  170. ]
  171. assert res[mask] == [9, 9, 9, 9]
  172. mask = res != 9
  173. assert mask.tolist() == [
  174. [True, True, False, False, True],
  175. [True, True, False, False, True]
  176. ]
  177. assert res[mask] == [0, 4, 5, 0, 4, 5]
  178. res[mask] = -1
  179. assert res.tolist() == [[-1, -1, 9, 9, -1], [-1, -1, 9, 9, -1]]
  180. # test get_connected_components
  181. a = array2d[int].fromlist([
  182. [1, 1, 0, 1],
  183. [0, 2, 2, 1],
  184. [0, 1, 1, 1],
  185. [1, 0, 0, 0],
  186. ])
  187. vis, cnt = a.get_connected_components(1, 'von Neumann')
  188. assert vis == [
  189. [1, 1, 0, 2],
  190. [0, 0, 0, 2],
  191. [0, 2, 2, 2],
  192. [3, 0, 0, 0]
  193. ]
  194. assert cnt == 3
  195. vis, cnt = a.get_connected_components(1, 'Moore')
  196. assert vis == [
  197. [1, 1, 0, 2],
  198. [0, 0, 0, 2],
  199. [0, 2, 2, 2],
  200. [2, 0, 0, 0]
  201. ]
  202. assert cnt == 2
  203. vis, cnt = a.get_connected_components(2, 'von Neumann')
  204. assert cnt == 1
  205. vis, cnt = a.get_connected_components(0, 'Moore')
  206. assert cnt == 2
  207. # test zip_with
  208. a = array2d[int].fromlist([[1, 2], [3, 4]])
  209. b = array2d[int].fromlist([[5, 6], [7, 8]])
  210. c = a.zip_with(b, lambda x, y: x + y)
  211. assert c.tolist() == [[6, 8], [10, 12]]
  212. # test magic op
  213. a = array2d[int].fromlist([[1, 2], [3, 4]])
  214. assert (a <= 2).tolist() == [[True, True], [False, False]]
  215. assert (a < 2).tolist() == [[True, False], [False, False]]
  216. assert (a >= 2).tolist() == [[False, True], [True, True]]
  217. assert (a > 2).tolist() == [[False, False], [True, True]]
  218. assert (a == 2).tolist() == [[False, True], [False, False]]
  219. assert (a != 2).tolist() == [[True, False], [True, True]]
  220. assert (a + 1).tolist() == [[2, 3], [4, 5]]
  221. assert (a - 1).tolist() == [[0, 1], [2, 3]]
  222. assert (a * 2).tolist() == [[2, 4], [6, 8]]
  223. assert (a / 1).tolist() == [[1.0, 2.0], [3.0, 4.0]]
  224. assert (a // 2).tolist() == [[0, 1], [1, 2]]
  225. assert (a % 2).tolist() == [[1, 0], [1, 0]]
  226. assert (a ** 2).tolist() == [[1, 4], [9, 16]]
  227. a = array2d[bool].fromlist([[True, False], [False, True]])
  228. assert (a & True).tolist() == [[True, False], [False, True]]
  229. assert (a | True).tolist() == [[True, True], [True, True]]
  230. assert (a ^ True).tolist() == [[False, True], [True, False]]
  231. b = array2d[bool].fromlist([[True, True], [False, False]])
  232. assert (a & b).tolist() == [[True, False], [False, False]]
  233. assert (a | b).tolist() == [[True, True], [False, True]]
  234. assert (a ^ b).tolist() == [[False, True], [False, True]]
  235. assert (~a).tolist() == [[False, True], [True, False]]
  236. assert (~b).tolist() == [[False, False], [True, True]]
  237. # stackoverflow bug due to recursive mark-and-sweep
  238. # class Cell:
  239. # neighbors: list['Cell']
  240. # cells: array2d[Cell] = array2d(192, 108, default=Cell)
  241. # OutOfBounds = Cell()
  242. # for x, y, cell in cells:
  243. # cell.neighbors = [
  244. # cells.get(x-1, y-1, OutOfBounds),
  245. # cells.get(x , y-1, OutOfBounds),
  246. # cells.get(x+1, y-1, OutOfBounds),
  247. # cells.get(x-1, y , OutOfBounds),
  248. # cells.get(x+1, y , OutOfBounds),
  249. # cells.get(x , y+1, OutOfBounds),
  250. # cells.get(x+1, y+1, OutOfBounds),
  251. # ]
  252. # import gc
  253. # gc.collect()