-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_plots.py
More file actions
372 lines (301 loc) · 14.4 KB
/
Copy pathtest_plots.py
File metadata and controls
372 lines (301 loc) · 14.4 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
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
import math
import unittest
from unittest.mock import patch
import numpy as np
import visdom
class TestLine(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _line(self, Y, X=None, **kwargs):
sent = {}
def capture(msg, endpoint="events", **_):
sent["payload"] = msg
sent["endpoint"] = endpoint
return "win1"
with patch.object(self.viz, "_send", side_effect=capture):
self.viz.line(Y, X=X, **kwargs)
return sent
def test_y_1d_no_x(self):
"""Basic 1D Y without X auto-generates x-axis."""
sent = self._line(np.array([1.0, 2.0, 3.0]))
self.assertIn("data", sent["payload"])
def test_y_0d_raises(self):
"""Scalar Y raises before reaching scatter."""
with self.assertRaises(AssertionError):
self.viz.line(np.float64(1.0))
def test_y_3d_raises(self):
"""3D Y raises on the ndim check."""
with self.assertRaises(AssertionError):
self.viz.line(np.ones((2, 3, 4)))
def test_y_empty_last_dim_raises(self):
"""Zero-column Y raises on the empty check."""
with self.assertRaises(AssertionError):
self.viz.line(np.empty((5, 0)))
def test_x_3d_raises(self):
"""3D X raises on the ndim check."""
with self.assertRaises(AssertionError):
self.viz.line(np.array([1.0, 2.0]), X=np.ones((2, 1, 1)))
def test_x_shape_mismatch_raises(self):
"""X and Y with different lengths raise on the shape check."""
with self.assertRaises(AssertionError):
self.viz.line(np.array([1.0, 2.0, 3.0]), X=np.array([0.0, 1.0]))
def test_single_line_one_trace(self):
"""1D Y produces exactly one trace."""
sent = self._line(np.array([1.0, 2.0, 3.0]))
self.assertEqual(len(sent["payload"]["data"]), 1)
def test_multi_line_2d_y(self):
"""2D Y with M columns produces M traces."""
Y = np.array([[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]])
sent = self._line(Y)
self.assertEqual(len(sent["payload"]["data"]), 2)
def test_y_2d_single_col_one_trace(self):
"""(N,1) Y is squeezed to 1D before building linedata."""
sent = self._line(np.array([[1.0], [2.0], [3.0]]))
self.assertEqual(len(sent["payload"]["data"]), 1)
def test_y_2d_x_1d_broadcasts(self):
"""1D X is tiled to match the shape of 2D Y."""
Y = np.array([[1.0, 4.0], [2.0, 5.0], [3.0, 6.0]])
X = np.array([0.0, 1.0, 2.0])
sent = self._line(Y, X=X)
self.assertIn("data", sent["payload"])
def test_update_append_requires_x(self):
"""update='append' without X raises before sending."""
with self.assertRaises(AssertionError):
self.viz.line(np.array([1.0, 2.0]), win="w", update="append")
def test_update_append_sets_append_true(self):
"""Existing window with update='append' sets append=True in payload."""
Y = np.array([1.0, 2.0])
X = np.array([0.0, 1.0])
with patch.object(self.viz, "win_exists", return_value=True):
sent = self._line(Y, X=X, win="w", update="append")
self.assertTrue(sent["payload"]["append"])
def test_update_replace_sets_append_false(self):
"""update='replace' sets append=False in payload."""
Y = np.array([1.0, 2.0])
X = np.array([0.0, 1.0])
sent = self._line(Y, X=X, win="w", update="replace")
self.assertFalse(sent["payload"]["append"])
def test_update_replace_uses_update_endpoint(self):
"""update='replace' routes to the update endpoint."""
Y = np.array([1.0, 2.0])
X = np.array([0.0, 1.0])
sent = self._line(Y, X=X, win="w", update="replace")
self.assertEqual(sent["endpoint"], "update")
def test_update_append_new_window_no_append_key(self):
"""New window with update='append' falls back to creation, no append key."""
Y = np.array([1.0, 2.0])
X = np.array([0.0, 1.0])
with patch.object(self.viz, "win_exists", return_value=False):
sent = self._line(Y, X=X, win="w", update="append")
self.assertNotIn("append", sent["payload"])
def test_update_remove_sends_delete(self):
"""update='remove' sends delete=True without touching Y."""
sent = self._line(None, win="w", name="trace1", update="remove")
self.assertTrue(sent["payload"]["delete"])
def test_nan_y_passes_through(self):
"""All-NaN Y values survive into the payload for use as update mask."""
Y = np.array([np.nan, np.nan, np.nan])
X = np.array([0.0, 1.0, 2.0])
with patch.object(self.viz, "win_exists", return_value=True):
sent = self._line(Y, X=X, win="w", update="append")
y_vals = sent["payload"]["data"][0]["y"]
self.assertTrue(all(np.isnan(v) for v in y_vals))
def test_inf_y_passes_through(self):
"""Inf Y values pass through without raising."""
Y = np.array([np.inf, 1.0, -np.inf])
X = np.array([0.0, 1.0, 2.0])
sent = self._line(Y, X=X)
y_vals = sent["payload"]["data"][0]["y"]
self.assertTrue(np.isinf(y_vals[0]))
self.assertTrue(np.isinf(y_vals[2]))
class TestScatter(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _scatter(self, X, Y=None, **kwargs):
sent = {}
def capture(msg, endpoint="events", **_):
sent["payload"] = msg
sent["endpoint"] = endpoint
return "win1"
with patch.object(self.viz, "_send", side_effect=capture):
self.viz.scatter(X, Y=Y, **kwargs)
return sent
def test_nx2_input(self):
"""Nx2 X produces a scatter trace."""
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
sent = self._scatter(X)
self.assertEqual(sent["payload"]["data"][0]["type"], "scatter")
def test_nx3_input_produces_scatter3d(self):
"""Nx3 X produces a scatter3d trace."""
X = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
sent = self._scatter(X)
self.assertEqual(sent["payload"]["data"][0]["type"], "scatter3d")
def test_x_1d_raises(self):
"""1D X raises on the ndim check."""
with self.assertRaises(AssertionError):
self.viz.scatter(np.array([1.0, 2.0, 3.0]))
def test_x_wrong_cols_raises(self):
"""X with column count other than 2 or 3 raises."""
with self.assertRaises(AssertionError):
self.viz.scatter(np.ones((3, 4)))
def test_y_size_mismatch_raises(self):
"""Y length not matching X row count raises."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
Y = np.array([1, 2, 3])
with self.assertRaises(AssertionError):
self.viz.scatter(X, Y=Y)
def test_nan_label_raises(self):
"""NaN in Y labels raises via the isfinite check in _normalize_labels."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
Y = np.array([1.0, np.nan])
with self.assertRaises(AssertionError):
self.viz.scatter(X, Y=Y)
def test_inf_label_raises(self):
"""Inf in Y labels raises via the same isfinite check as NaN."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
Y = np.array([1.0, np.inf])
with self.assertRaises(AssertionError):
self.viz.scatter(X, Y=Y)
def test_multiple_labels_produce_multiple_traces(self):
"""Distinct Y label values produce one trace each."""
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
Y = np.array([1, 1, 2])
sent = self._scatter(X, Y=Y)
self.assertEqual(len(sent["payload"]["data"]), 2)
def test_name_with_multiple_labels_raises(self):
"""name= combined with multiple label groups raises."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
Y = np.array([1, 2])
with self.assertRaises(AssertionError):
self.viz.scatter(X, Y=Y, name="trace1")
def test_store_history_with_update_raises(self):
"""store_history=True combined with update raises ValueError."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
with self.assertRaises(ValueError):
self.viz.scatter(X, opts={"store_history": True}, update="append", win="w")
def test_update_without_win_raises(self):
"""update without a win raises ValueError."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
with self.assertRaises(ValueError):
self.viz.scatter(X, update="replace")
def test_update_remove_requires_name(self):
"""update='remove' without name raises."""
with self.assertRaises(AssertionError):
self.viz.scatter(None, win="w", update="remove")
def test_update_remove_sends_delete(self):
"""update='remove' sends delete=True in payload."""
sent = self._scatter(None, win="w", name="trace1", update="remove")
self.assertTrue(sent["payload"]["delete"])
def test_update_append_sets_append_true(self):
"""Existing window with update='append' sets append=True."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
with patch.object(self.viz, "win_exists", return_value=True):
sent = self._scatter(X, win="w", update="append")
self.assertTrue(sent["payload"]["append"])
def test_update_replace_sets_append_false(self):
"""update='replace' sets append=False in payload."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._scatter(X, win="w", update="replace")
self.assertFalse(sent["payload"]["append"])
def test_name_based_update_1d_x_1d_y(self):
"""Name-based update stacks 1D X and Y into Nx2 before sending."""
X = np.array([1.0, 2.0, 3.0])
Y = np.array([4.0, 5.0, 6.0])
with patch.object(self.viz, "win_exists", return_value=True):
sent = self._scatter(X, Y=Y, win="w", update="append", name="t1")
data = sent["payload"]["data"][0]
self.assertEqual(data["x"], [1.0, 2.0, 3.0])
self.assertEqual(data["y"], [4.0, 5.0, 6.0])
class TestHeatmap(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _heatmap(self, X, **kwargs):
sent = {}
def capture(msg, endpoint="events", **_):
sent["payload"] = msg
sent["endpoint"] = endpoint
return "win1"
with patch.object(self.viz, "_send", side_effect=capture):
self.viz.heatmap(X, **kwargs)
return sent
def test_nx_m_input(self):
"""NxM X produces a heatmap trace."""
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
sent = self._heatmap(X)
self.assertEqual(sent["payload"]["data"][0]["type"], "heatmap")
def test_x_not_2d_raises(self):
"""1D X raises on the 2D check."""
with self.assertRaises(AssertionError):
self.viz.heatmap(np.array([1.0, 2.0, 3.0]))
def test_invalid_update_raises(self):
"""Unknown update value raises before building data."""
X = np.ones((3, 3))
with self.assertRaises(AssertionError):
self.viz.heatmap(X, update="badvalue")
def test_colormap_defaults_to_viridis(self):
"""colormap defaults to Viridis when not specified."""
X = np.ones((2, 2))
sent = self._heatmap(X)
self.assertEqual(sent["payload"]["opts"]["colormap"], "Viridis")
def test_append_row_sets_update_dir(self):
"""appendRow sets updateDir and append=True on the update endpoint."""
X = np.ones((2, 2))
sent = self._heatmap(X, update="appendRow", win="w")
self.assertEqual(sent["payload"]["updateDir"], "appendRow")
self.assertTrue(sent["payload"]["append"])
self.assertEqual(sent["endpoint"], "update")
def test_append_column_sets_update_dir(self):
"""appendColumn sets updateDir and append=True."""
X = np.ones((2, 2))
sent = self._heatmap(X, update="appendColumn", win="w")
self.assertEqual(sent["payload"]["updateDir"], "appendColumn")
self.assertTrue(sent["payload"]["append"])
def test_replace_sets_append_false(self):
"""replace sets append=False on the update endpoint."""
X = np.ones((2, 2))
sent = self._heatmap(X, update="replace", win="w")
self.assertFalse(sent["payload"]["append"])
def test_nan_values_pass_through(self):
"""NaN values in X pass through to the payload without raising."""
X = np.array([[1.0, np.nan], [np.nan, 4.0]])
sent = self._heatmap(X)
z = sent["payload"]["data"][0]["z"]
self.assertTrue(np.isnan(z[0][1]))
self.assertTrue(np.isnan(z[1][0]))
class _FakePlot:
"""Minimal stand-in for a matplotlib figure.
matplot() only calls plot.savefig(buffer, format="svg"), so we just
write a fixed SVG whose root <svg> carries height/width in points.
"""
def __init__(self, height, width):
self._svg = (
'<?xml version="1.0"?>'
'<svg xmlns="http://www.w3.org/2000/svg" '
'height="{}" width="{}"></svg>'.format(height, width)
)
def savefig(self, buffer, format="svg"):
buffer.write(self._svg)
@unittest.skipUnless(visdom.BS4_AVAILABLE, "requires bs4/lxml")
class TestMatplotResizable(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _matplot(self, plot, **kwargs):
captured = {}
def capture(svgstr=None, opts=None, **_):
captured["opts"] = opts
return "win1"
with patch.object(self.viz, "svg", side_effect=capture):
self.viz.matplot(plot, **kwargs)
return captured["opts"]
def test_whole_number_pt_not_inflated(self):
"""432pt must strip 'pt' -> 432, not become 43200 (the 100x bug)."""
opts = self._matplot(_FakePlot("432pt", "640pt"), opts={"resizable": True})
self.assertEqual(opts["height"], 1.4 * math.ceil(432)) # 604.8
self.assertEqual(opts["width"], 1.35 * math.ceil(640)) # 864.0
def test_decimal_pt_still_correct(self):
"""Decimal dims (which worked before) must keep working."""
opts = self._matplot(_FakePlot("345.6pt", "460.8pt"), opts={"resizable": True})
self.assertEqual(opts["height"], 1.4 * math.ceil(345.6)) # 484.4
self.assertEqual(opts["width"], 1.35 * math.ceil(460.8)) # 622.35
if __name__ == "__main__":
unittest.main()