Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_data_types.py 12 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
  1. import wandb
  2. import numpy as np
  3. import pytest
  4. import PIL
  5. import os
  6. import matplotlib
  7. import six
  8. import sys
  9. matplotlib.use("Agg")
  10. from click.testing import CliRunner
  11. import matplotlib.pyplot as plt
  12. from click.testing import CliRunner
  13. from . import utils
  14. data = np.random.randint(255, size=(1000))
  15. def test_raw_data():
  16. wbhist = wandb.Histogram(data)
  17. assert len(wbhist.histogram) == 64
  18. def test_np_histogram():
  19. wbhist = wandb.Histogram(np_histogram=np.histogram(data))
  20. assert len(wbhist.histogram) == 10
  21. def test_manual_histogram():
  22. wbhist = wandb.Histogram(np_histogram=([1, 2, 4], [3, 10, 20, 0]))
  23. assert len(wbhist.histogram) == 3
  24. def test_invalid_histogram():
  25. with pytest.raises(ValueError):
  26. wbhist = wandb.Histogram(np_histogram=([1, 2, 3], [1]))
  27. def test_histogram_to_json():
  28. wbhist = wandb.Histogram(data)
  29. json = wbhist.to_json()
  30. assert json["_type"] == "histogram"
  31. assert len(json["values"]) == 64
  32. image = np.zeros((28, 28))
  33. def test_captions():
  34. wbone = wandb.Image(image, caption="Cool")
  35. wbtwo = wandb.Image(image, caption="Nice")
  36. assert wandb.Image.captions([wbone, wbtwo]) == ["Cool", "Nice"]
  37. def test_bind_image():
  38. with CliRunner().isolated_filesystem():
  39. run = wandb.wandb_run.Run()
  40. wb_image = wandb.Image(image)
  41. wb_image.bind_to_run(run, 'stuff', 10)
  42. assert wb_image.is_bound()
  43. with pytest.raises(RuntimeError):
  44. wb_image.bind_to_run(run, 'stuff', 10)
  45. def test_cant_serialize_to_other_run():
  46. """This isn't implemented yet. Should work eventually.
  47. """
  48. with CliRunner().isolated_filesystem():
  49. run = wandb.wandb_run.Run()
  50. other_run = wandb.wandb_run.Run()
  51. wb_image = wandb.Image(image)
  52. wb_image.bind_to_run(run, 'stuff', 10)
  53. with pytest.raises(AssertionError):
  54. wb_image.to_json(other_run)
  55. def test_image_seq_to_json():
  56. with CliRunner().isolated_filesystem():
  57. run = wandb.wandb_run.Run()
  58. wb_image = wandb.Image(image)
  59. meta = wandb.Image.seq_to_json([wb_image], run, "test", 'summary')
  60. assert os.path.exists(os.path.join(run.dir, 'media', 'images', 'test_summary.png'))
  61. meta_expected = {
  62. '_type': 'images',
  63. 'count': 1,
  64. 'height': 28,
  65. 'width': 28,
  66. }
  67. assert utils.subdict(meta, meta_expected) == meta_expected
  68. def test_transform_caps_at_65500(caplog):
  69. large_image = np.random.randint(255, size=(10, 1000))
  70. large_list = [wandb.Image(large_image)] * 100
  71. with CliRunner().isolated_filesystem():
  72. run = wandb.wandb_run.Run()
  73. meta = wandb.Image.seq_to_json(large_list, run, "test2", 0)
  74. expected = {'_type': 'images', 'count': 65, 'height': 10, 'width': 1000}
  75. assert utils.subdict(meta, expected) == expected
  76. assert os.path.exists(os.path.join(run.dir, "media/images/test2_0.png"))
  77. assert 'Only 65 images will be uploaded. The maximum total width for a set of thumbnails is 65,500px, or 65 images, each with a width of 1000 pixels.' in caplog.text
  78. def test_audio_sample_rates():
  79. audio1 = np.random.uniform(-1, 1, 44100)
  80. audio2 = np.random.uniform(-1, 1, 88200)
  81. wbaudio1 = wandb.Audio(audio1, sample_rate=44100)
  82. wbaudio2 = wandb.Audio(audio2, sample_rate=88200)
  83. assert wandb.Audio.sample_rates([wbaudio1, wbaudio2]) == [44100, 88200]
  84. # test with missing sample rate
  85. with pytest.raises(ValueError):
  86. wbaudio3 = wandb.Audio(audio1)
  87. def test_audio_durations():
  88. audio1 = np.random.uniform(-1, 1, 44100)
  89. audio2 = np.random.uniform(-1, 1, 88200)
  90. wbaudio1 = wandb.Audio(audio1, sample_rate=44100)
  91. wbaudio2 = wandb.Audio(audio2, sample_rate=44100)
  92. assert wandb.Audio.durations([wbaudio1, wbaudio2]) == [1.0, 2.0]
  93. def test_audio_captions():
  94. audio = np.random.uniform(-1, 1, 44100)
  95. sample_rate = 44100
  96. caption1 = "This is what a dog sounds like"
  97. caption2 = "This is what a chicken sounds like"
  98. # test with all captions
  99. wbaudio1 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption1)
  100. wbaudio2 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption2)
  101. assert wandb.Audio.captions([wbaudio1, wbaudio2]) == [caption1, caption2]
  102. # test with no captions
  103. wbaudio3 = wandb.Audio(audio, sample_rate=sample_rate)
  104. wbaudio4 = wandb.Audio(audio, sample_rate=sample_rate)
  105. assert wandb.Audio.captions([wbaudio3, wbaudio4]) == False
  106. # test with some captions
  107. wbaudio5 = wandb.Audio(audio, sample_rate=sample_rate)
  108. wbaudio6 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption2)
  109. assert wandb.Audio.captions([wbaudio5, wbaudio6]) == ['', caption2]
  110. def test_audio_to_json():
  111. audio = np.zeros(44100)
  112. with CliRunner().isolated_filesystem():
  113. run = wandb.wandb_run.Run()
  114. meta = wandb.Audio.seq_to_json(
  115. [wandb.Audio(audio, sample_rate=44100)], run, "test", 0)
  116. assert os.path.exists(os.path.join(run.dir, meta['audio'][0]['path']))
  117. meta_expected = {
  118. '_type': 'audio',
  119. 'count': 1,
  120. 'sampleRates': [44100],
  121. 'durations': [1.0],
  122. }
  123. assert utils.subdict(meta, meta_expected) == meta_expected
  124. audio_expected = {
  125. '_type': 'audio-file',
  126. 'caption': None,
  127. 'sample_rate': 44100,
  128. 'size': 88244,
  129. }
  130. assert utils.subdict(meta['audio'][0], audio_expected) == audio_expected
  131. def test_guess_mode():
  132. image = np.random.randint(255, size=(28, 28, 3))
  133. wbimg = wandb.Image(image)
  134. assert wbimg._image.mode == "RGB"
  135. def test_pil():
  136. pil = PIL.Image.new("L", (28, 28))
  137. img = wandb.Image(pil)
  138. assert img._image == pil
  139. def test_matplotlib_image():
  140. plt.plot([1, 2, 2, 4])
  141. img = wandb.Image(plt)
  142. assert img._image.width == 640
  143. @pytest.mark.skipif(sys.version_info < (3, 6), reason="No moviepy.editor in py2")
  144. def test_video_numpy():
  145. with CliRunner().isolated_filesystem():
  146. run = wandb.wandb_run.Run()
  147. video = np.random.randint(255, size=(10,3,28,28))
  148. vid = wandb.Video(video)
  149. vid.bind_to_run(run, "videos", 0)
  150. assert vid.to_json(run)["path"].endswith(".gif")
  151. @pytest.mark.skipif(sys.version_info < (3, 6), reason="No moviepy.editor in py2")
  152. def test_video_numpy_multi():
  153. with CliRunner().isolated_filesystem():
  154. run = wandb.wandb_run.Run()
  155. video = np.random.random(size=(2,10,3,28,28))
  156. vid = wandb.Video(video)
  157. vid.bind_to_run(run, "videos", 0)
  158. assert vid.to_json(run)["path"].endswith(".gif")
  159. @pytest.mark.skipif(sys.version_info < (3, 6), reason="No moviepy.editor in py2")
  160. def test_video_numpy_invalid():
  161. run = wandb.wandb_run.Run()
  162. video = np.random.random(size=(3,28,28))
  163. with pytest.raises(ValueError):
  164. vid = wandb.Video(video)
  165. def test_video_path():
  166. with CliRunner().isolated_filesystem():
  167. run = wandb.wandb_run.Run()
  168. with open("video.mp4", "w") as f:
  169. f.write("00000")
  170. vid = wandb.Video("video.mp4")
  171. vid.bind_to_run(run, "videos", 0)
  172. assert vid.to_json(run)["path"].endswith(".mp4")
  173. def test_video_path_invalid():
  174. run = wandb.wandb_run.Run()
  175. with CliRunner().isolated_filesystem():
  176. with open("video.avi", "w") as f:
  177. f.write("00000")
  178. with pytest.raises(ValueError):
  179. vid = wandb.Video("video.avi")
  180. def test_html_str():
  181. with CliRunner().isolated_filesystem():
  182. run = wandb.wandb_run.Run()
  183. html = wandb.Html("<html><body><h1>Hello</h1></body></html>")
  184. wandb.Html.seq_to_json([html], run, "rad", "summary")
  185. assert os.path.exists(os.path.join(run.dir, "media/html/rad_summary_0.html"))
  186. def test_html_styles():
  187. with CliRunner().isolated_filesystem():
  188. pre = '<base target="_blank"><link rel="stylesheet" type="text/css" href="https://app.wandb.ai/normalize.css" />'
  189. html = wandb.Html("<html><body><h1>Hello</h1></body></html>")
  190. assert html.html == "<html><head>"+pre + \
  191. "</head><body><h1>Hello</h1></body></html>"
  192. html = wandb.Html(
  193. "<html><head></head><body><h1>Hello</h1></body></html>")
  194. assert html.html == "<html><head>"+pre + \
  195. "</head><body><h1>Hello</h1></body></html>"
  196. html = wandb.Html("<h1>Hello</h1>")
  197. assert html.html == pre + "<h1>Hello</h1>"
  198. html = wandb.Html("<h1>Hello</h1>", inject=False)
  199. assert html.html == "<h1>Hello</h1>"
  200. def test_html_file():
  201. with CliRunner().isolated_filesystem():
  202. run = wandb.wandb_run.Run()
  203. with open("test.html", "w") as f:
  204. f.write("<html><body><h1>Hello</h1></body></html>")
  205. html = wandb.Html(open("test.html"))
  206. wandb.Html.seq_to_json([html, html], run, "rad", "summary")
  207. assert os.path.exists(os.path.join(run.dir, "media/html/rad_summary_0.html"))
  208. assert os.path.exists(os.path.join(run.dir, "media/html/rad_summary_0.html"))
  209. def test_table_default():
  210. table = wandb.Table()
  211. table.add_data("Some awesome text", "Positive", "Negative")
  212. assert table.to_json() == {
  213. "_type": "table",
  214. "data": [["Some awesome text", "Positive", "Negative"]],
  215. "columns": ["Input", "Output", "Expected"]
  216. }
  217. def test_table_custom():
  218. table = wandb.Table(["Foo", "Bar"])
  219. table.add_data("So", "Cool")
  220. table.add_row("&", "Rad")
  221. assert table.to_json() == {
  222. "_type": "table",
  223. "data": [["So", "Cool"], ["&", "Rad"]],
  224. "columns": ["Foo", "Bar"]
  225. }
  226. point_cloud_1 = np.array([[0, 0, 0, 1],
  227. [0, 0, 1, 13],
  228. [0, 1, 0, 2],
  229. [0, 1, 0, 4]])
  230. point_cloud_2 = np.array([[0, 0, 0],
  231. [0, 0, 1],
  232. [0, 1, 0],
  233. [0, 1, 0]])
  234. point_cloud_3 = np.array([[0, 0, 0, 100, 100, 100],
  235. [0, 0, 1, 100, 100, 100],
  236. [0, 1, 0, 100, 100, 100],
  237. [0, 1, 0, 100, 100, 100]])
  238. def test_object3d_numpy():
  239. obj = wandb.Object3D(point_cloud_1)
  240. obj = wandb.Object3D(point_cloud_2)
  241. obj = wandb.Object3D(point_cloud_3)
  242. def test_object3d_obj():
  243. obj = wandb.Object3D(open("tests/fixtures/cube.obj"))
  244. def test_object3d_gltf():
  245. obj = wandb.Object3D(open("tests/fixtures/Box.gltf"))
  246. def test_object3d_io():
  247. f = open("tests/fixtures/Box.gltf")
  248. body = f.read()
  249. ioObj = six.StringIO(six.u(body))
  250. obj = wandb.Object3D(ioObj, file_type="obj")
  251. def test_object3d_unsupported_numpy():
  252. with pytest.raises(ValueError):
  253. wandb.Object3D(np.array([1]))
  254. with pytest.raises(ValueError):
  255. wandb.Object3D(np.array([[1, 2], [3, 4], [1, 2]]))
  256. with pytest.raises(ValueError):
  257. wandb.Object3D(np.array([1, 3, 4, 5, 6, 7, 8, 8, 3]))
  258. with pytest.raises(ValueError):
  259. wandb.Object3D(np.array([[1, 3, 4, 5, 6, 7, 8, 8, 3]]))
  260. f = open("tests/fixtures/Box.gltf")
  261. body = f.read()
  262. ioObj = six.StringIO(six.u(body))
  263. with pytest.raises(ValueError):
  264. obj = wandb.Object3D(ioObj)
  265. def test_object3d_seq_to_json():
  266. cwd = os.getcwd()
  267. with CliRunner().isolated_filesystem():
  268. run = wandb.wandb_run.Run()
  269. obj = wandb.Object3D.seq_to_json([
  270. wandb.Object3D(open(os.path.join(cwd, "tests/fixtures/Box.gltf"))),
  271. wandb.Object3D(open(os.path.join(cwd, "tests/fixtures/cube.obj"))),
  272. wandb.Object3D(point_cloud_1)
  273. ], run, "pc", 1)
  274. print(obj)
  275. assert os.path.exists(os.path.join(run.dir, "media/object3D/Box_be115756.gltf"))
  276. assert os.path.exists(os.path.join(run.dir, "media/object3D/cube_afff12bc.obj"))
  277. assert os.path.exists(os.path.join(run.dir,
  278. "media/object3D/pc_1_2.pts.json"))
  279. assert obj["_type"] == "object3D"
  280. assert obj["filenames"] == [
  281. "Box_be115756.gltf",
  282. "cube_afff12bc.obj",
  283. "pc_1_2.pts.json",
  284. ]
  285. def test_table_init():
  286. table = wandb.Table(data=[["Some awesome text", "Positive", "Negative"]])
  287. assert table.to_json() == {"_type": "table",
  288. "data": [["Some awesome text", "Positive", "Negative"]],
  289. "columns": ["Input", "Output", "Expected"]}
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...