Skip to content
Open
48 changes: 48 additions & 0 deletions py/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# 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
Expand Down Expand Up @@ -631,5 +632,52 @@ def test_layout_is_flat(self):
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):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Maya-Mohamed Please make it compatible with pytest .

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()
4 changes: 2 additions & 2 deletions py/visdom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,14 +1520,14 @@ def matplot(self, plot, opts=None, env=None, win=None):
if height is not None:
if not isstr(height):
height = height.group(1)
height = height.replace("pt", "00")
height = height.replace("pt", "")
opts["height"] = 1.4 * int(math.ceil(float(height)))
if "width" not in opts:
width = width or re.search(r'width\="([0-9\.]*)pt"', svg)
if width is not None:
if not isstr(width):
width = width.group(1)
width = width.replace("pt", "00")
width = width.replace("pt", "")
opts["width"] = 1.35 * int(math.ceil(float(width)))
return self.svg(svgstr=svg, opts=opts, env=env, win=win)

Expand Down
2 changes: 2 additions & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ numpy
av
--extra-index-url https://download.pytorch.org/whl/cpu
torch
beautifulsoup4
lxml