Skip to content

Commit

Permalink
fix figure display style, also fix second figure doesn't show
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Dec 18, 2024
1 parent d1c9e6b commit 8144fbe
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
35 changes: 18 additions & 17 deletions pymatviz/utils/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
import tempfile

import matplotlib.pyplot as plt
import pytest
Expand All @@ -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.
Expand All @@ -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
2 changes: 0 additions & 2 deletions tests/utils/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 8144fbe

Please sign in to comment.