diff --git a/py/tests/test_plots.py b/py/tests/test_plots.py index 0b6d1aa50..7b855a151 100644 --- a/py/tests/test_plots.py +++ b/py/tests/test_plots.py @@ -6,9 +6,11 @@ # 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 pytest import visdom @@ -631,5 +633,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 carries height/width in points. + """ + + def __init__(self, height, width): + self._svg = ( + '' + ''.format(height, width) + ) + + def savefig(self, buffer, format="svg"): + buffer.write(self._svg) + + +@pytest.mark.skipif(not visdom.BS4_AVAILABLE, reason="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() diff --git a/py/visdom/__init__.py b/py/visdom/__init__.py index ee1daeb93..56d188394 100644 --- a/py/visdom/__init__.py +++ b/py/visdom/__init__.py @@ -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) diff --git a/test-requirements.txt b/test-requirements.txt index d14930b15..0ad2cc751 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -5,3 +5,5 @@ numpy av --extra-index-url https://download.pytorch.org/whl/cpu torch +beautifulsoup4 +lxml \ No newline at end of file