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
2 changes: 1 addition & 1 deletion src/aspire/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def show(self, columns=5, figsize=(20, 10), colorbar=True):
)

plt.figure(figsize=figsize)
for i, im in enumerate(self):
for i, im in enumerate(self.asnumpy()):
plt.subplot(self.n_images // columns + 1, columns, i + 1)
plt.imshow(im, cmap="gray")
if colorbar:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from aspire.image import Image
from aspire.utils import powerset, utest_tolerance

from .test_utils import matplotlib_dry_run

DATA_DIR = os.path.join(os.path.dirname(__file__), "saved_test_data")

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -281,3 +283,12 @@ def testMultiDimBroadcast():
mdim_ims_np, mdim_ims = get_mdim_images()
X = mdim_ims + ims
assert np.allclose(X[0], 2 * ims.asnumpy())


@matplotlib_dry_run
def testShow():
"""
Test show doesn't crash.
"""
im = Image(np.random.random((3, 8, 8)))
im.show()
14 changes: 14 additions & 0 deletions tests/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from aspire.utils.types import utest_tolerance
from aspire.volume import LegacyVolume, Volume

from .test_utils import matplotlib_dry_run

DATA_DIR = os.path.join(os.path.dirname(__file__), "saved_test_data")


Expand All @@ -28,6 +30,18 @@ def testImage(self):
"""Test we can get an Image from a length 1 Sim."""
_ = self.sim.images[0]

@matplotlib_dry_run
def testImageShow(self):
self.sim.images[:].show()

@matplotlib_dry_run
def testCleanImagesShow(self):
self.sim.clean_images[:].show()

@matplotlib_dry_run
def testProjectionsShow(self):
self.sim.projections[:].show()


class SimVolTestCase(TestCase):
"""Test Simulation with Volume provided."""
Expand Down
38 changes: 38 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import warnings
from contextlib import contextmanager
from unittest import TestCase
from unittest.mock import patch

import matplotlib
import numpy as np
from parameterized import parameterized
from pytest import raises
Expand Down Expand Up @@ -312,3 +315,38 @@ def testVrtSuggestion(self):

def testGetNumMultiProcs(self):
self.assertTrue(isinstance(num_procs_suggestion(), int))


@contextmanager
def matplotlib_no_gui():
"""
Context manager for disabling and restoring matplotlib plots, and
ignoring associated warnings.
"""

# Save current backend
backend = matplotlib.get_backend()

# Use non GUI backend.
matplotlib.use("Agg")

# Save and restore current warnings list.
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Matplotlib is currently using agg")

yield

# Restore backend
matplotlib.use(backend)


def matplotlib_dry_run(func):
"""
Decorator that wraps function in `matplotlib_no_gui` context.
"""

def wrapper(*args, **kwargs):
with matplotlib_no_gui():
return func(*args, **kwargs)

return wrapper