forked from wandb/wandb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_util.py
More file actions
273 lines (199 loc) · 8 KB
/
test_util.py
File metadata and controls
273 lines (199 loc) · 8 KB
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
import random
import sys
import os
import pytest
import numpy
import platform
import tensorflow
import plotly
import matplotlib.pyplot as plt
from . import utils
from wandb import util
try:
import torch
except ImportError:
pass
def pt_variable(nested_list, requires_grad=True):
v = torch.autograd.Variable(torch.Tensor(nested_list))
v.requires_grad = requires_grad
return v
def r():
return random.random()
def nested_list(*shape):
"""Makes a nested list of lists with a "shape" argument like numpy,
TensorFlow, etc.
"""
if not shape:
# reduce precision so we can use == for comparison regardless
# of conversions between other libraries
return [float(numpy.float16(random.random()))]
else:
return [nested_list(*shape[1:]) for _ in range(shape[0])]
def json_friendly_test(orig_data, obj):
data, converted = util.json_friendly(obj)
utils.assert_deep_lists_equal(orig_data, data)
assert converted
def tensorflow_json_friendly_test(orig_data):
json_friendly_test(orig_data, tensorflow.convert_to_tensor(orig_data))
v = tensorflow.Variable(tensorflow.convert_to_tensor(orig_data))
json_friendly_test(orig_data, v)
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_0d():
a = nested_list()
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_1d_1x1():
a = nested_list(1)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_1d():
a = nested_list(3)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_1d_large():
a = nested_list(300)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_2d():
a = nested_list(3, 3)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_2d_large():
a = nested_list(300, 300)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_3d():
a = nested_list(3, 3, 3)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_4d():
a = nested_list(1, 1, 1, 1)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_nd():
a = nested_list(1, 1, 1, 1, 1, 1, 1, 1)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="PyTorch no longer supports py2")
def test_pytorch_json_nd_large():
a = nested_list(3, 3, 3, 3, 3, 3, 3, 3)
json_friendly_test(a, torch.Tensor(a))
json_friendly_test(a, pt_variable(a))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_0d():
tensorflow_json_friendly_test(nested_list())
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_1d_1x1():
tensorflow_json_friendly_test(nested_list(1))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_1d():
tensorflow_json_friendly_test(nested_list(3))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_1d_large():
tensorflow_json_friendly_test(nested_list(300))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_2d():
tensorflow_json_friendly_test(nested_list(3, 3))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_2d_large():
tensorflow_json_friendly_test(nested_list(300, 300))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_nd():
tensorflow_json_friendly_test(nested_list(1, 1, 1, 1, 1, 1, 1, 1))
@pytest.mark.skipif(sys.version_info < (3, 5), reason="TF has sketchy support for py2")
def test_tensorflow_json_nd_large():
tensorflow_json_friendly_test(nested_list(3, 3, 3, 3, 3, 3, 3, 3))
def test_image_from_docker_args_simple():
image = util.image_from_docker_args(
["run", "-v", "/foo:/bar", "-e", "NICE=foo", "-it", "wandb/deepo", "/bin/bash"]
)
assert image == "wandb/deepo"
def test_image_from_docker_args_simple_no_namespace():
image = util.image_from_docker_args(["run", "-e", "NICE=foo", "nginx", "/bin/bash"])
assert image == "nginx"
def test_image_from_docker_args_simple_no_equals():
image = util.image_from_docker_args(
["run", "--runtime=runc", "ufoym/deepo:cpu-all"]
)
assert image == "ufoym/deepo:cpu-all"
def test_image_from_docker_args_bash_simple():
image = util.image_from_docker_args(
["run", "ufoym/deepo:cpu-all", "/bin/bash", "-c", "python train.py"]
)
assert image == "ufoym/deepo:cpu-all"
def test_image_from_docker_args_sha():
dsha = (
"wandb/deepo@sha256:"
"3ddd2547d83a056804cac6aac48d46c5394a76df76b672539c4d2476eba38177"
)
image = util.image_from_docker_args([dsha])
assert image == dsha
def test_safe_for_json():
res = util.make_safe_for_json(
{
"nan": float("nan"),
"inf": float("+inf"),
"ninf": float("-inf"),
"str": "str",
"seq": [float("nan"), 1],
"map": {"foo": 1, "nan": float("nan")},
}
)
assert res == {
"inf": "Infinity",
"map": {"foo": 1, "nan": "NaN"},
"nan": "NaN",
"ninf": "-Infinity",
"seq": ["NaN", 1],
"str": "str",
}
@pytest.mark.skipif(
platform.system() == "Windows", reason="find_runner is broken on Windows"
)
def test_find_runner():
res = util.find_runner(__file__)
assert "python" in res[0]
def test_parse_sweep_id():
parts = {"name": "test/test/test"}
util.parse_sweep_id(parts)
assert parts == {"name": "test", "entity": "test", "project": "test"}
def test_sizeof_fmt():
assert util.sizeof_fmt(1000) == "1000.0B"
assert util.sizeof_fmt(1000000) == "976.6KiB"
assert util.sizeof_fmt(5000000) == "4.8MiB"
def test_matplotlib_contains_images():
"""Ensures that the utility function can properly detect if immages are in a
matplotlib figure"""
# fig true
fig = utils.matplotlib_with_image()
assert util.matplotlib_contains_images(fig)
plt.close()
# plt true
fig = utils.matplotlib_with_image()
assert util.matplotlib_contains_images(plt)
plt.close()
# fig false
fig = utils.matplotlib_without_image()
assert not util.matplotlib_contains_images(fig)
plt.close()
# plt false
fig = utils.matplotlib_without_image()
assert not util.matplotlib_contains_images(plt)
plt.close()
def test_matplotlib_to_plotly():
"""Ensures that the utility function can properly transform a pyplot object to a
plotly object (not the wandb.* versions"""
fig = utils.matplotlib_without_image()
assert type(util.matplotlib_to_plotly(fig)) == plotly.graph_objs._figure.Figure
plt.close()
fig = utils.matplotlib_without_image()
assert type(util.matplotlib_to_plotly(plt)) == plotly.graph_objs._figure.Figure
plt.close()