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_summary.py 5.3 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
  1. import matplotlib
  2. # this needs to be called before importing wandb Graph
  3. matplotlib.use("Agg")
  4. from wandb import wandb_run
  5. from wandb.summary import FileSummary
  6. import pandas
  7. import tensorflow as tf
  8. import torch
  9. import json
  10. import glob
  11. import os
  12. import numpy as np
  13. import tempfile
  14. import plotly.graph_objs as go
  15. import matplotlib.pyplot as plt
  16. from wandb import Histogram, Image, Graph, Table
  17. from click.testing import CliRunner
  18. import pytest
  19. @pytest.fixture
  20. def summary():
  21. with CliRunner().isolated_filesystem():
  22. run = wandb_run.Run()
  23. run.summary.update({"foo": "init"})
  24. yield run.summary
  25. def disk_summary(summary):
  26. return json.load(open(summary._fname))
  27. def test_set_attrs(summary):
  28. summary.foo = "bar"
  29. assert disk_summary(summary) == {"foo": "bar"}
  30. def test_get_attr(summary):
  31. assert summary.foo == "init"
  32. def test_update(summary):
  33. summary.update({"foo": "bar"})
  34. assert disk_summary(summary) == {"foo": "bar"}
  35. def test_update_histogram(summary):
  36. summary.update({"hist": Histogram(np_histogram=([1, 2, 3], [1, 2, 3, 4]))})
  37. assert disk_summary(summary) == {
  38. 'foo': 'init',
  39. "hist": {"_type": "histogram", "values": [1, 2, 3], "bins": [1, 2, 3, 4]}}
  40. def test_set_histogram(summary):
  41. summary["hist"] = Histogram(np_histogram=([1, 2, 3], [1, 2, 3, 4]))
  42. assert disk_summary(summary) == {
  43. 'foo': 'init',
  44. "hist": {"_type": "histogram", "values": [1, 2, 3], "bins": [1, 2, 3, 4]}}
  45. def test_set_item(summary):
  46. summary["foo"] = "bar"
  47. assert disk_summary(summary) == {"foo": "bar"}
  48. def test_get_item(summary):
  49. assert summary["foo"] == "init"
  50. def test_delete(summary):
  51. summary.update({"foo": "bar", "bad": True})
  52. assert summary['foo'] == 'bar'
  53. assert summary['bad'] is True
  54. del summary["bad"]
  55. assert disk_summary(summary) == {"foo": "bar"}
  56. def test_image(summary):
  57. summary["image"] = Image(np.zeros((28, 28)))
  58. ds = disk_summary(summary)
  59. assert os.path.exists(os.path.join(summary._run.dir, ds['image']['path']))
  60. expected = {
  61. '_type': 'image-file',
  62. 'height': 28,
  63. 'width': 28,
  64. 'size': 73,
  65. }
  66. assert set(ds['image'].items()) >= set(expected.items())
  67. def test_matplot_image(summary):
  68. img = plt.imshow(np.zeros((28, 28)), cmap='gray')
  69. summary["fig"] = img
  70. plt.close()
  71. ds = disk_summary(summary)
  72. assert os.path.exists(os.path.join(summary._run.dir, ds['fig']['path']))
  73. assert set(ds["fig"].items()) >= set({
  74. "_type": "image-file",
  75. "height": 480,
  76. "width": 640,
  77. }.items())
  78. def test_matplot_plotly(summary):
  79. plt.cla()
  80. plt.plot([1, 2, 3])
  81. summary["plot"] = plt
  82. plt.close()
  83. plot = disk_summary(summary)["plot"]
  84. assert plot["_type"] == "plotly"
  85. def test_plotly_plot(summary):
  86. summary["plot"] = go.Scatter(x=[0, 1, 2])
  87. plot = disk_summary(summary)["plot"]
  88. assert plot["_type"] == "plotly"
  89. assert plot["plot"]['type'] == 'scatter'
  90. def test_newline(summary):
  91. summary["rad \n"] = 1
  92. summary.update({"bad \n ": 2})
  93. summ = disk_summary(summary)
  94. assert summ["rad"] == 1
  95. assert summ["bad"] == 2
  96. def test_big_numpy(summary):
  97. summary.update({"rad": np.random.rand(1000)})
  98. assert disk_summary(summary)["rad"]["max"] > 0
  99. assert os.path.exists(os.path.join(summary._run.dir, "wandb.h5"))
  100. def test_big_nested_numpy(summary):
  101. summary.update({"rad": {"deep": np.random.rand(1000)}})
  102. assert disk_summary(summary)["rad"]["deep"]["max"] > 0
  103. summary["rad"]["deep2"] = np.random.rand(1000)
  104. assert disk_summary(summary)["rad"]["deep2"]["max"] > 0
  105. assert os.path.exists(os.path.join(summary._run.dir, "wandb.h5"))
  106. def test_torch_tensor(summary):
  107. summary.update({"pytorch": torch.rand(1000, 1000)})
  108. assert os.path.exists(os.path.join(summary._run.dir, "wandb.h5"))
  109. assert disk_summary(summary)["pytorch"]["_type"] == "torch.Tensor"
  110. def test_tensorflow_tensor(summary):
  111. with tf.Session().as_default():
  112. summary.update({"tensorflow": tf.random_normal([1000])})
  113. assert os.path.exists(os.path.join(summary._run.dir, "wandb.h5"))
  114. assert disk_summary(summary)["tensorflow"]["_type"] == "tensorflow.Tensor"
  115. def test_pandas(summary):
  116. summary.update({"pandas": pandas.DataFrame(
  117. data=np.random.rand(1000), columns=['col'])})
  118. def test_read_numpy(summary):
  119. summary.update({"rad": np.random.rand(1000)})
  120. s = FileSummary(summary._run)
  121. assert len(s["rad"]) == 1000
  122. def test_read_nested_numpy(summary):
  123. summary.update({"rad": {"deep": np.random.rand(1000)}})
  124. s = FileSummary(summary._run)
  125. assert len(s["rad"]["deep"]) == 1000
  126. def test_read_nested_array(summary):
  127. summary["rad"] = {"deep": "dish"}
  128. s = FileSummary(summary._run)
  129. assert summary["rad"]["deep"] == "dish"
  130. def test_read_very_nested_numpy(summary):
  131. # Test that even deeply nested writes are written to disk.
  132. summary.update(
  133. {"a": {"b": {"c": {"d": {"e": {"f": {"g": {"h": {"i": {}}}}}}}}}})
  134. summary['a']['b']['c']['d']['e']['f']['g']['h']['i']['j'] = True
  135. assert disk_summary(summary)[
  136. 'a']['b']['c']['d']['e']['f']['g']['h']['i']['j'] is True
  137. def test_key_locking(summary):
  138. summary.update({'a': 'a'})
  139. assert summary['a'] == 'a'
  140. summary.update({'a': 'b'})
  141. assert summary['a'] == 'b'
  142. summary.update({'a': 'c'}, overwrite=False)
  143. assert summary['a'] == 'b'
Tip!

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

Comments

Loading...