diff --git a/pymatviz/utils/testing.py b/pymatviz/utils/testing.py index d34f02b4..d02d0e96 100644 --- a/pymatviz/utils/testing.py +++ b/pymatviz/utils/testing.py @@ -3,6 +3,7 @@ from __future__ import annotations import os +import tempfile import matplotlib.pyplot as plt import pytest @@ -21,13 +22,6 @@ def interactive_check( ) -> bool | None: """Ask a human to visually inspect a plot. - Note: - You would need to pass `-s` to `pytest` to release output. - - Todo: - - `pytest` would capture output by default unless `pytest -s`, possible to - modify it within? - Args: plot (plt.Figure | plt.Axes): Plot to inspect. elem_to_check (str): Prompt for what element in the plot to check. @@ -47,27 +41,34 @@ def interactive_check( elif not isinstance(plot, plt.Figure): raise TypeError(f"plot type {type(plot).__name__} is not supported.") - # TODO: scale the figure by screen resolution - # TODO: display at the top middle of the screen - plt.show(block=False) + # Save the figure to as a temporary file (ensure it matches the savefig style) + with tempfile.NamedTemporaryFile(suffix=".png") as tmpfile: + temp_filepath = tmpfile.name + + plot.savefig(temp_filepath, dpi=300) - # print() # TODO: print the fully qualified name of the test + # Open and show the saved figure + plt.close(plot) # Close the original plot before opening the saved one + img = plt.imread(temp_filepath) + plt.imshow(img) # Display the saved image in the plot window + plt.axis("off") # Hide axes for cleaner inspection - # KeyboardInterrupt (ctrl + C) would be treated as a quick "y" + plt.show(block=False) + + # Ask the user to check (treat KeyboardInterrupt as "yes") try: - goodenough: bool = ( + good_enough: bool = ( input(f"Please check the {elem_to_check}. Is it looking good? (y/n): ") .strip() .lower() ) == "y" - except KeyboardInterrupt: - goodenough = True + good_enough = True finally: plt.close() - if not goodenough: + if not good_enough: pytest.fail(reason=f"{elem_to_check} would need attention.") - return goodenough + return good_enough diff --git a/tests/utils/test_testing.py b/tests/utils/test_testing.py index 6d1104c5..3925d81c 100644 --- a/tests/utils/test_testing.py +++ b/tests/utils/test_testing.py @@ -20,8 +20,6 @@ def test_good_figure(self) -> None: # Test Axes interactive_check(ax, elem_to_check="line") - # TODO: after closing the first figure, the following don't show up - # Test Figure interactive_check(fig, elem_to_check="line")