Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion autofit/model_figure/ep/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ def figure(

Returns
-------
The ``matplotlib`` ``Figure``.
The ``matplotlib`` ``Figure``, or ``None`` when ``format="show"``: the
figure has been shown and closed, and returning it would make a notebook
draw it a second time as the cell's own output.
"""
from .render import draw, save

Expand Down
4 changes: 3 additions & 1 deletion autofit/model_figure/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def figure(

Returns
-------
The ``matplotlib`` ``Figure``.
The ``matplotlib`` ``Figure``, or ``None`` when ``format="show"``: the
figure has been shown and closed, and returning it would make a notebook
draw it a second time as the cell's own output.
"""
from .render import draw, save

Expand Down
8 changes: 7 additions & 1 deletion autofit/model_figure/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ def save(figure, path=None, filename="model", format="show"):
Show or write the figure, mirroring
``autofit/non_linear/plot/plot_util.py:output_figure`` with ``svg`` added.

``format="show"`` returns ``None``, as ``output_figure`` does: the figure has
already been drawn, and handing a ``Figure`` back to a notebook makes IPython
render it a *second* time as the cell's ``execute_result`` beside the one
``plt.show()`` published.

``format=None`` builds the figure and neither shows nor writes it; any other
unrecognised format raises rather than silently doing nothing.

Expand All @@ -434,7 +439,8 @@ def save(figure, path=None, filename="model", format="show"):
return figure
if format == "show":
plt.show()
return figure
plt.close(figure)
return None
if format not in ("png", "pdf", "svg"):
raise ValueError(
f"format must be one of None, 'show', 'png', 'svg', 'pdf', not {format!r}"
Expand Down
24 changes: 24 additions & 0 deletions test_autofit/model_figure/ep/test_acceptance_ep.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,30 @@ def test_the_filename_defaults_by_kind_and_the_formats_are_the_usual_four(
plotter.figure(path=tmp_path, format="gif")


def test_show_returns_nothing_so_a_notebook_draws_it_once(
stalled_plate, tmp_path, monkeypatch
):
"""
``EPPlotter`` shares ``ModelPlotter``'s ``save``, so it shares the fix:
returning the ``Figure`` from the show path made IPython render it a second
time as the cell's ``execute_result``.
"""
import matplotlib.pyplot as plt

shown = []
monkeypatch.setattr(plt, "show", lambda *args, **kwargs: shown.append(True))
open_before = set(plt.get_fignums())

factor_graph, _, _ = stalled_plate

figure = af.EPPlotter(factor_graph).figure(path=tmp_path, format="show")

assert shown == [True]
assert figure is None
assert set(plt.get_fignums()) == open_before
assert list(tmp_path.iterdir()) == []


def test_format_none_builds_the_figure_and_writes_nothing(stalled_plate, tmp_path):
factor_graph, _, _ = stalled_plate

Expand Down
16 changes: 13 additions & 3 deletions test_autofit/model_figure/test_figure_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,26 @@ def test_a_missing_directory_is_created(model, tmp_path):
assert (target / "model.png").exists()


def test_show_returns_a_figure_and_writes_nothing(model, tmp_path, monkeypatch):
def test_show_returns_nothing_so_a_notebook_draws_it_once(model, tmp_path, monkeypatch):
"""
The show path shows the figure and hands nothing back.

A returned ``Figure`` is rendered a second time by IPython's ``image/png``
formatter, as the ``execute_result`` of a cell ending on
``af.ModelPlotter(model).figure()`` -- so the tutorials drew every model
twice. ``output_figure``, the convention this mirrors, returns nothing and
always closes; so does this.
"""
shown = []
monkeypatch.setattr(plt, "show", lambda *args, **kwargs: shown.append(True))
open_before = set(plt.get_fignums())

figure = af.ModelPlotter(model).figure(path=tmp_path, format="show")

assert shown == [True]
assert figure is not None
assert figure is None
assert set(plt.get_fignums()) == open_before
assert list(tmp_path.iterdir()) == []
plt.close(figure)


def test_an_unknown_format_raises_rather_than_silently_doing_nothing(model, tmp_path):
Expand Down
Loading