-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtest_plots.py
More file actions
683 lines (547 loc) · 26.4 KB
/
Copy pathtest_plots.py
File metadata and controls
683 lines (547 loc) · 26.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
#!/usr/bin/env python3
# Copyright 2017-present, The Visdom Authors
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
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 TestBar(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _bar(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.bar(X, Y=Y, **kwargs)
return sent
def test_1d_x_one_trace(self):
"""1D X produces a single bar trace."""
sent = self._bar(np.array([1.0, 2.0, 3.0]))
self.assertEqual(len(sent["payload"]["data"]), 1)
self.assertEqual(sent["payload"]["data"][0]["type"], "bar")
def test_2d_x_one_trace_per_column(self):
"""NxM X produces M bar traces."""
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
sent = self._bar(X)
self.assertEqual(len(sent["payload"]["data"]), 2)
def test_3d_x_raises(self):
"""3D X raises on the ndim check."""
with self.assertRaises(AssertionError):
self.viz.bar(np.ones((2, 3, 4)))
def test_x_y_length_mismatch_raises(self):
"""Y with a different length than X raises."""
with self.assertRaises(AssertionError):
self.viz.bar(np.array([1.0, 2.0, 3.0]), Y=np.array([1.0, 2.0]))
def test_default_x_axis_is_one_based(self):
"""Without Y the x-axis is 1..N."""
sent = self._bar(np.array([1.0, 2.0, 3.0]))
self.assertEqual(sent["payload"]["data"][0]["x"], [1, 2, 3])
def test_y_sets_x_axis_values(self):
"""Y supplies the x-axis values."""
sent = self._bar(np.array([1.0, 2.0]), Y=np.array([10.0, 20.0]))
self.assertEqual(sent["payload"]["data"][0]["x"], [10.0, 20.0])
def test_column_values_go_to_y(self):
"""Each trace carries its own column of X as bar heights."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._bar(X)
self.assertEqual(sent["payload"]["data"][0]["y"], [1.0, 3.0])
self.assertEqual(sent["payload"]["data"][1]["y"], [2.0, 4.0])
def test_rownames_replace_x_axis(self):
"""rownames are used as x-axis labels instead of Y."""
opts = {"rownames": ["a", "b", "c"]}
sent = self._bar(np.array([1.0, 2.0, 3.0]), opts=opts)
self.assertEqual(sent["payload"]["data"][0]["x"], ["a", "b", "c"])
def test_rownames_length_mismatch_raises(self):
"""rownames shorter than the number of rows raises."""
with self.assertRaises(AssertionError):
self.viz.bar(np.array([1.0, 2.0, 3.0]), opts={"rownames": ["a", "b"]})
def test_legend_sets_trace_names(self):
"""legend labels name each column trace."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._bar(X, opts={"legend": ["first", "second"]})
names = [trace["name"] for trace in sent["payload"]["data"]]
self.assertEqual(names, ["first", "second"])
def test_legend_length_mismatch_raises(self):
"""legend length not matching the column count raises."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
with self.assertRaises(AssertionError):
self.viz.bar(X, opts={"legend": ["only_one"]})
def test_legend_on_1d_x_transposes(self):
"""1D X with a legend is treated as one row of grouped bars."""
sent = self._bar(np.array([1.0, 2.0, 3.0]), opts={"legend": ["a", "b", "c"]})
self.assertEqual(len(sent["payload"]["data"]), 3)
def test_legend_with_rownames_on_1d_raises(self):
"""legend and rownames together on 1D X raise."""
opts = {"legend": ["a", "b", "c"], "rownames": ["x", "y", "z"]}
with self.assertRaises(AssertionError):
self.viz.bar(np.array([1.0, 2.0, 3.0]), opts=opts)
def test_stacked_sets_barmode(self):
"""stacked=True stacks the columns instead of grouping them."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._bar(X, opts={"stacked": True})
self.assertEqual(sent["payload"]["layout"]["barmode"], "stack")
class TestHistogram(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _histogram(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.histogram(X, **kwargs)
return sent
def test_2d_x_raises(self):
"""2D X raises on the one-dimensional check."""
with self.assertRaises(AssertionError):
self.viz.histogram(np.ones((3, 3)))
def test_default_bin_count_capped_at_30(self):
"""A large sample is drawn with 30 bars by default."""
sent = self._histogram(np.arange(100.0))
self.assertEqual(len(sent["payload"]["data"][0]["y"]), 30)
def test_default_bin_count_is_sample_count_when_small(self):
"""A sample smaller than 30 gets one bar per value by default."""
sent = self._histogram(np.arange(5.0))
self.assertEqual(len(sent["payload"]["data"][0]["y"]), 5)
def test_numbins_opt_sets_bin_count(self):
"""numbins controls how many bars are produced."""
sent = self._histogram(np.arange(100.0), opts={"numbins": 10})
self.assertEqual(len(sent["payload"]["data"][0]["y"]), 10)
def test_counts_sum_to_sample_count(self):
"""Every sample lands in exactly one bin."""
sent = self._histogram(np.arange(100.0), opts={"numbins": 10})
self.assertEqual(sum(sent["payload"]["data"][0]["y"]), 100)
def test_bin_edges_span_data_range(self):
"""The x-axis runs from the minimum to the maximum of X."""
sent = self._histogram(np.arange(100.0), opts={"numbins": 10})
x = sent["payload"]["data"][0]["x"]
self.assertEqual(x[0], 0.0)
self.assertEqual(x[-1], 99.0)
class TestBoxplot(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _boxplot(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.boxplot(X, **kwargs)
return sent
def test_1d_x_one_box(self):
"""1D X produces a single box trace."""
sent = self._boxplot(np.array([1.0, 2.0, 3.0]))
self.assertEqual(len(sent["payload"]["data"]), 1)
self.assertEqual(sent["payload"]["data"][0]["type"], "box")
def test_2d_x_one_box_per_column(self):
"""NxM X produces M box traces."""
X = np.array([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
sent = self._boxplot(X)
self.assertEqual(len(sent["payload"]["data"]), 2)
def test_3d_x_raises(self):
"""3D X raises on the ndim check."""
with self.assertRaises(AssertionError):
self.viz.boxplot(np.ones((2, 3, 4)))
def test_column_values_go_to_y(self):
"""Each box carries the values of its own column."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._boxplot(X)
self.assertEqual(sent["payload"]["data"][0]["y"], [1.0, 3.0])
self.assertEqual(sent["payload"]["data"][1]["y"], [2.0, 4.0])
def test_default_names_are_column_indexed(self):
"""Without a legend each box is named after its column index."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._boxplot(X)
names = [trace["name"] for trace in sent["payload"]["data"]]
self.assertEqual(names, ["column 0", "column 1"])
def test_legend_sets_box_names(self):
"""legend labels replace the default column names."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._boxplot(X, opts={"legend": ["train", "test"]})
names = [trace["name"] for trace in sent["payload"]["data"]]
self.assertEqual(names, ["train", "test"])
def test_legend_length_mismatch_raises(self):
"""legend length not matching the column count raises."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
with self.assertRaises(AssertionError):
self.viz.boxplot(X, opts={"legend": ["only_one"]})
class TestSurf(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _surf(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.surf(X, **kwargs)
return sent
def test_1d_x_raises(self):
"""1D X raises on the two-dimensional check."""
with self.assertRaises(AssertionError):
self.viz.surf(np.array([1.0, 2.0, 3.0]))
def test_type_is_surface(self):
"""surf produces a surface trace."""
sent = self._surf(np.ones((3, 3)))
self.assertEqual(sent["payload"]["data"][0]["type"], "surface")
def test_z_matches_input(self):
"""X is sent verbatim as the z values."""
X = np.array([[1.0, 2.0], [3.0, 4.0]])
sent = self._surf(X)
self.assertEqual(sent["payload"]["data"][0]["z"], [[1.0, 2.0], [3.0, 4.0]])
def test_colormap_defaults_to_viridis(self):
"""colormap defaults to Viridis when not specified."""
sent = self._surf(np.ones((2, 2)))
self.assertEqual(sent["payload"]["data"][0]["colorscale"], "Viridis")
def test_colormap_opt_forwarded(self):
"""An explicit colormap reaches the trace."""
sent = self._surf(np.ones((2, 2)), opts={"colormap": "Hot"})
self.assertEqual(sent["payload"]["data"][0]["colorscale"], "Hot")
def test_range_defaults_to_data_range(self):
"""xmin and xmax default to the minimum and maximum of X."""
X = np.array([[1.0, 5.0], [3.0, 9.0]])
sent = self._surf(X)
self.assertEqual(sent["payload"]["data"][0]["cmin"], 1.0)
self.assertEqual(sent["payload"]["data"][0]["cmax"], 9.0)
def test_range_opts_override_data_range(self):
"""xmin and xmax opts clip the color range."""
X = np.array([[1.0, 5.0], [3.0, 9.0]])
sent = self._surf(X, opts={"xmin": 2.0, "xmax": 4.0})
self.assertEqual(sent["payload"]["data"][0]["cmin"], 2.0)
self.assertEqual(sent["payload"]["data"][0]["cmax"], 4.0)
def test_nan_ignored_in_range(self):
"""NaN values do not leak into the default color range."""
X = np.array([[1.0, np.nan], [3.0, 9.0]])
sent = self._surf(X)
self.assertEqual(sent["payload"]["data"][0]["cmin"], 1.0)
self.assertEqual(sent["payload"]["data"][0]["cmax"], 9.0)
def test_layout_is_3d(self):
"""surf builds a 3D scene layout."""
sent = self._surf(np.ones((2, 2)))
self.assertIn("scene", sent["payload"]["layout"])
class TestContour(unittest.TestCase):
def setUp(self):
self.viz = visdom.Visdom(send=False, use_incoming_socket=False)
def _contour(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.contour(X, **kwargs)
return sent
def test_type_is_contour(self):
"""contour produces a contour trace."""
sent = self._contour(np.ones((3, 3)))
self.assertEqual(sent["payload"]["data"][0]["type"], "contour")
def test_layout_is_flat(self):
"""contour renders flat, without the 3D scene surf builds."""
sent = self._contour(np.ones((2, 2)))
self.assertNotIn("scene", sent["payload"]["layout"])
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()