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 4.5 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
  1. import wandb
  2. import numpy as np
  3. import pytest
  4. import PIL
  5. import os
  6. import matplotlib
  7. matplotlib.use("Agg")
  8. import matplotlib.pyplot as plt
  9. import soundfile
  10. from click.testing import CliRunner
  11. data = np.random.randint(255, size=(1000))
  12. def test_raw_data():
  13. wbhist = wandb.Histogram(data)
  14. assert len(wbhist.histogram) == 64
  15. def test_np_histogram():
  16. wbhist = wandb.Histogram(np_histogram=np.histogram(data))
  17. assert len(wbhist.histogram) == 10
  18. def test_manual_histogram():
  19. wbhist = wandb.Histogram(np_histogram=([1, 2, 4], [3, 10, 20, 0]))
  20. assert len(wbhist.histogram) == 3
  21. def test_fucked_up_histogram():
  22. with pytest.raises(ValueError):
  23. wbhist = wandb.Histogram(np_histogram=([1, 2, 3], [1]))
  24. def test_transform():
  25. wbhist = wandb.Histogram(data)
  26. json = wandb.Histogram.transform(wbhist)
  27. assert json["_type"] == "histogram"
  28. assert len(json["values"]) == 64
  29. image = np.random.randint(255, size=(28, 28))
  30. def test_captions():
  31. wbone = wandb.Image(image, caption="Cool")
  32. wbtwo = wandb.Image(image, caption="Nice")
  33. assert wandb.Image.captions([wbone, wbtwo]) == ["Cool", "Nice"]
  34. def test_transform():
  35. with CliRunner().isolated_filesystem():
  36. meta = wandb.Image.transform([wandb.Image(image)], ".", "test.jpg")
  37. assert meta == {'_type': 'images',
  38. 'count': 1, 'height': 28, 'width': 28}
  39. assert os.path.exists("media/images/test.jpg")
  40. def test_audio_sample_rates():
  41. audio1 = np.random.uniform(-1, 1, 44100)
  42. audio2 = np.random.uniform(-1, 1, 88200)
  43. wbaudio1 = wandb.Audio(audio1, sample_rate=44100)
  44. wbaudio2 = wandb.Audio(audio2, sample_rate=88200)
  45. assert wandb.Audio.sample_rates([wbaudio1, wbaudio2]) == [44100, 88200]
  46. # test with missing sample rate
  47. with pytest.raises(ValueError):
  48. wbaudio3 = wandb.Audio(audio1)
  49. def test_audio_durations():
  50. audio1 = np.random.uniform(-1, 1, 44100)
  51. audio2 = np.random.uniform(-1, 1, 88200)
  52. wbaudio1 = wandb.Audio(audio1, sample_rate=44100)
  53. wbaudio2 = wandb.Audio(audio2, sample_rate=44100)
  54. assert wandb.Audio.durations([wbaudio1, wbaudio2]) == [1.0, 2.0]
  55. def test_audio_captions():
  56. audio = np.random.uniform(-1, 1, 44100)
  57. sample_rate = 44100
  58. caption1 = "This is what a dog sounds like"
  59. caption2 = "This is what a chicken sounds like"
  60. # test with all captions
  61. wbaudio1 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption1)
  62. wbaudio2 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption2)
  63. assert wandb.Audio.captions([wbaudio1, wbaudio2]) == [caption1, caption2]
  64. # test with no captions
  65. wbaudio3 = wandb.Audio(audio, sample_rate=sample_rate)
  66. wbaudio4 = wandb.Audio(audio, sample_rate=sample_rate)
  67. assert wandb.Audio.captions([wbaudio3, wbaudio4]) == False
  68. # test with some captions
  69. wbaudio5 = wandb.Audio(audio, sample_rate=sample_rate)
  70. wbaudio6 = wandb.Audio(audio, sample_rate=sample_rate, caption=caption2)
  71. assert wandb.Audio.captions([wbaudio5, wbaudio6]) == ['', caption2]
  72. def test_audio_transform():
  73. audio = np.random.uniform(-1, 1, 44100)
  74. with CliRunner().isolated_filesystem():
  75. meta = wandb.Audio.transform([wandb.Audio(audio, sample_rate=44100)], ".", "test", 0)
  76. assert meta == {'_type': 'audio',
  77. 'count': 1, 'sampleRates': [44100], 'durations': [1.0]}
  78. assert os.path.exists("media/audio/test_0_0.wav")
  79. def test_guess_mode():
  80. image = np.random.randint(255, size=(28, 28, 3))
  81. wbimg = wandb.Image(image)
  82. assert wbimg.image.mode == "RGB"
  83. def test_pil():
  84. pil = PIL.Image.new("L", (28, 28))
  85. img = wandb.Image(pil)
  86. assert img.image == pil
  87. def test_matplotlib_image():
  88. plt.plot([1, 2, 2, 4])
  89. img = wandb.Image(plt)
  90. assert img.image.width == 640
  91. def test_table_default():
  92. table = wandb.Table()
  93. table.add_row("Some awesome text", "Positive", "Negative")
  94. assert wandb.Table.transform(table) == {"_type": "table",
  95. "data": [["Some awesome text", "Positive", "Negative"]],
  96. "columns": ["Input", "Output", "Expected"]}
  97. def test_table_custom():
  98. table = wandb.Table(["Foo", "Bar"])
  99. table.add_row("So", "Cool")
  100. table.add_row("&", "Rad")
  101. assert wandb.Table.transform(table) == {"_type": "table",
  102. "data": [["So", "Cool"], ["&", "Rad"]],
  103. "columns": ["Foo", "Bar"]}
Tip!

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

Comments

Loading...