You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_utils.py 12 kB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. """TODO: Add docstring."""
  2. import numpy as np
  3. import pyarrow as pa
  4. # Marker Message Example
  5. TEST_ARRAYS = [
  6. ("std_msgs", "UInt8", pa.array([{"data": np.uint8(2)}])),
  7. (
  8. "std_msgs",
  9. "String",
  10. pa.array([{"data": "hello"}]),
  11. ),
  12. (
  13. "std_msgs",
  14. "UInt8MultiArray",
  15. pa.array(
  16. [
  17. {
  18. "data": np.array([1, 2, 3, 4], np.uint8),
  19. "layout": {
  20. "dim": [
  21. {
  22. "label": "a",
  23. "size": np.uint32(10),
  24. "stride": np.uint32(20),
  25. },
  26. ],
  27. "data_offset": np.uint32(30),
  28. },
  29. },
  30. ],
  31. ),
  32. ),
  33. (
  34. "std_msgs",
  35. "Float32MultiArray",
  36. pa.array(
  37. [
  38. {
  39. "data": np.array([1, 2, 3, 4], np.float32),
  40. "layout": {
  41. "dim": [
  42. {
  43. "label": "a",
  44. "size": np.uint32(10),
  45. "stride": np.uint32(20),
  46. },
  47. ],
  48. "data_offset": np.uint32(30),
  49. },
  50. },
  51. ],
  52. ),
  53. ),
  54. (
  55. "visualization_msgs",
  56. "Marker",
  57. pa.array(
  58. [
  59. {
  60. "header": {
  61. "frame_id": "world", # Placeholder value (String type, no numpy equivalent)
  62. },
  63. "ns": "my_namespace", # Placeholder value (String type, no numpy equivalent)
  64. "id": np.int32(1), # Numpy type
  65. "type": np.int32(0), # Numpy type (ARROW)
  66. "action": np.int32(0), # Numpy type (ADD)
  67. "lifetime": {
  68. "sec": np.int32(1),
  69. "nanosec": np.uint32(2),
  70. }, # Numpy type
  71. "pose": {
  72. "position": {
  73. "x": np.float64(1.0), # Numpy type
  74. "y": np.float64(2.0), # Numpy type
  75. "z": np.float64(3.0), # Numpy type
  76. },
  77. "orientation": {
  78. "x": np.float64(0.0), # Numpy type
  79. "y": np.float64(0.0), # Numpy type
  80. "z": np.float64(0.0), # Numpy type
  81. "w": np.float64(1.0), # Numpy type
  82. },
  83. },
  84. "scale": {
  85. "x": np.float64(1.0), # Numpy type
  86. "y": np.float64(1.0), # Numpy type
  87. "z": np.float64(1.0), # Numpy type
  88. },
  89. "color": {
  90. "r": np.float32(1.0), # Numpy type
  91. "g": np.float32(0.0), # Numpy type
  92. "b": np.float32(0.0), # Numpy type
  93. "a": np.float32(1.0), # Numpy type (alpha)
  94. },
  95. "frame_locked": False, # Boolean type, no numpy equivalent
  96. "points": [ # Numpy array for points
  97. {
  98. "x": np.float64(1.0), # Numpy type
  99. "y": np.float64(1.0), # Numpy type
  100. "z": np.float64(1.0), # Numpy type
  101. },
  102. ],
  103. "colors": [
  104. {
  105. "r": np.float32(1.0), # Numpy type
  106. "g": np.float32(1.0), # Numpy type
  107. "b": np.float32(1.0), # Numpy type
  108. "a": np.float32(1.0), # Numpy type (alpha)
  109. }, # Numpy array for colors
  110. ],
  111. "texture_resource": "",
  112. "uv_coordinates": [{}],
  113. "text": "",
  114. "mesh_resource": "",
  115. "mesh_use_embedded_materials": False, # Boolean type, no numpy equivalent
  116. },
  117. ],
  118. ),
  119. ),
  120. (
  121. "visualization_msgs",
  122. "MarkerArray",
  123. pa.array(
  124. [
  125. {
  126. "markers": [
  127. {
  128. "header": {
  129. "frame_id": "world", # Placeholder value (String type, no numpy equivalent)
  130. },
  131. "ns": "my_namespace", # Placeholder value (String type, no numpy equivalent)
  132. "id": np.int32(1), # Numpy type
  133. "type": np.int32(0), # Numpy type (ARROW)
  134. "action": np.int32(0), # Numpy type (ADD)
  135. "lifetime": {
  136. "sec": np.int32(1),
  137. "nanosec": np.uint32(2),
  138. }, # Numpy type
  139. "pose": {
  140. "position": {
  141. "x": np.float64(1.0), # Numpy type
  142. "y": np.float64(2.0), # Numpy type
  143. "z": np.float64(3.0), # Numpy type
  144. },
  145. "orientation": {
  146. "x": np.float64(0.0), # Numpy type
  147. "y": np.float64(0.0), # Numpy type
  148. "z": np.float64(0.0), # Numpy type
  149. "w": np.float64(1.0), # Numpy type
  150. },
  151. },
  152. "scale": {
  153. "x": np.float64(1.0), # Numpy type
  154. "y": np.float64(1.0), # Numpy type
  155. "z": np.float64(1.0), # Numpy type
  156. },
  157. "color": {
  158. "r": np.float32(1.0), # Numpy type
  159. "g": np.float32(0.0), # Numpy type
  160. "b": np.float32(0.0), # Numpy type
  161. "a": np.float32(1.0), # Numpy type (alpha)
  162. },
  163. "frame_locked": False, # Boolean type, no numpy equivalent
  164. "points": [ # Numpy array for points
  165. {
  166. "x": np.float64(1.0), # Numpy type
  167. "y": np.float64(1.0), # Numpy type
  168. "z": np.float64(1.0), # Numpy type
  169. },
  170. ],
  171. "colors": [
  172. {
  173. "r": np.float32(1.0), # Numpy type
  174. "g": np.float32(1.0), # Numpy type
  175. "b": np.float32(1.0), # Numpy type
  176. "a": np.float32(1.0), # Numpy type (alpha)
  177. }, # Numpy array for colors
  178. ],
  179. "texture_resource": "",
  180. "uv_coordinates": [{}],
  181. "text": "",
  182. "mesh_resource": "",
  183. "mesh_use_embedded_materials": False, # Boolean type, no numpy equivalent
  184. },
  185. ],
  186. },
  187. ],
  188. ),
  189. ),
  190. (
  191. "visualization_msgs",
  192. "ImageMarker",
  193. pa.array(
  194. [
  195. {
  196. "header": {
  197. "stamp": {
  198. "sec": np.int32(123456), # 32-bit integer
  199. "nanosec": np.uint32(789), # 32-bit unsigned integer
  200. },
  201. "frame_id": "frame_example",
  202. },
  203. "ns": "namespace",
  204. "id": np.int32(1), # 32-bit integer
  205. "type": np.int32(0), # 32-bit integer, e.g., CIRCLE = 0
  206. "action": np.int32(0), # 32-bit integer, e.g., ADD = 0
  207. "position": {
  208. "x": np.float64(1.0), # 32-bit float
  209. "y": np.float64(2.0), # 32-bit float
  210. "z": np.float64(3.0), # 32-bit float
  211. },
  212. "scale": np.float32(1.0), # 32-bit float
  213. "outline_color": {
  214. "r": np.float32(255.0), # 32-bit float
  215. "g": np.float32(0.0), # 32-bit float
  216. "b": np.float32(0.0), # 32-bit float
  217. "a": np.float32(1.0), # 32-bit float
  218. },
  219. "filled": np.uint8(1), # 8-bit unsigned integer
  220. "fill_color": {
  221. "r": np.float32(0.0), # 32-bit float
  222. "g": np.float32(255.0), # 32-bit float
  223. "b": np.float32(0.0), # 32-bit float
  224. "a": np.float32(1.0), # 32-bit float
  225. },
  226. "lifetime": {
  227. "sec": np.int32(300), # 32-bit integer
  228. "nanosec": np.uint32(0), # 32-bit unsigned integer
  229. },
  230. "points": [
  231. {
  232. "x": np.float64(1.0), # 32-bit float
  233. "y": np.float64(2.0), # 32-bit float
  234. "z": np.float64(3.0), # 32-bit float
  235. },
  236. {
  237. "x": np.float64(4.0), # 32-bit float
  238. "y": np.float64(5.0), # 32-bit float
  239. "z": np.float64(6.0), # 32-bit float
  240. },
  241. ],
  242. "outline_colors": [
  243. {
  244. "r": np.float32(255.0), # 32-bit float
  245. "g": np.float32(0.0), # 32-bit float
  246. "b": np.float32(0.0), # 32-bit float
  247. "a": np.float32(1.0), # 32-bit float
  248. },
  249. {
  250. "r": np.float32(0.0), # 32-bit float
  251. "g": np.float32(255.0), # 32-bit float
  252. "b": np.float32(0.0), # 32-bit float
  253. "a": np.float32(1.0), # 32-bit float
  254. },
  255. ],
  256. },
  257. ],
  258. ),
  259. ),
  260. ]
  261. def is_subset(subset, superset):
  262. """Check if subset is a subset of superset, to avoid false negatives linked to default values."""
  263. if isinstance(subset, pa.Array):
  264. return is_subset(subset.to_pylist(), superset.to_pylist())
  265. match subset:
  266. case dict(_):
  267. return all(
  268. key in superset and is_subset(val, superset[key])
  269. for key, val in subset.items()
  270. )
  271. case list(_) | set(_):
  272. return all(
  273. any(is_subset(subitem, superitem) for superitem in superset)
  274. for subitem in subset
  275. )
  276. # assume that subset is a plain value if none of the above match
  277. case _:
  278. return subset == superset