From a7da5575646afd98f7d1316574cab7ef7afc5b53 Mon Sep 17 00:00:00 2001 From: Nabil Freij Date: Wed, 15 Nov 2023 22:27:44 -0800 Subject: [PATCH] Backport PR #651: Update error messages for mpl and mpl_animators --- changelog/651.trivial.rst | 1 + ndcube/visualization/descriptor.py | 24 ++++++++++++++++-------- ndcube/visualization/mpl_plotter.py | 8 +++++++- 3 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 changelog/651.trivial.rst diff --git a/changelog/651.trivial.rst b/changelog/651.trivial.rst new file mode 100644 index 000000000..f9ae4ac9e --- /dev/null +++ b/changelog/651.trivial.rst @@ -0,0 +1 @@ +Updated the error messages when missing either ``matplotlib`` or ``mpl_animators`` when creating a plot. diff --git a/ndcube/visualization/descriptor.py b/ndcube/visualization/descriptor.py index b4e58ce0d..06926fe80 100644 --- a/ndcube/visualization/descriptor.py +++ b/ndcube/visualization/descriptor.py @@ -1,7 +1,11 @@ import functools -MISSING_MATPLOTLIB_ERROR_MSG = ("Matplotlib can not be imported, so the default plotting " - "functionality is disabled. Please install matplotlib.") +MISSING_MATPLOTLIB_ERROR_MSG = ("matplotlib cannot be imported, so the default plotting " + "functionality is disabled. Please install matplotlib") +MISSING_ANIMATORS_ERROR_MSG = ("mpl_animators cannot be imported, so the default plotting " + "functionality is disabled. Please install mpl_animators") + +__all__ = ['PlotterDescriptor', 'MISSING_MATPLOTLIB_ERROR_MSG', 'MISSING_ANIMATORS_ERROR_MSG'] class PlotterDescriptor: @@ -29,16 +33,20 @@ def _resolve_default_type(self, raise_error=True): # delay the import of matplotlib until the plotter is first # accessed. if self._default_type in ("mpl_plotter", "mpl_sequence_plotter"): - try: - if self._default_type == "mpl_plotter": + if self._default_type == "mpl_plotter": + try: from ndcube.visualization.mpl_plotter import MatplotlibPlotter return MatplotlibPlotter - elif self._default_type == "mpl_sequence_plotter": + except ImportError as e: + if raise_error: + raise ImportError(MISSING_MATPLOTLIB_ERROR_MSG) from e + elif self._default_type == "mpl_sequence_plotter": + try: from ndcube.visualization.mpl_sequence_plotter import MatplotlibSequencePlotter return MatplotlibSequencePlotter - except ImportError as e: - if raise_error: - raise ImportError(MISSING_MATPLOTLIB_ERROR_MSG) from e + except ImportError as e: + if raise_error: + raise ImportError(MISSING_ANIMATORS_ERROR_MSG) from e elif self._default_type is not None: return self._default_type diff --git a/ndcube/visualization/mpl_plotter.py b/ndcube/visualization/mpl_plotter.py index a3dfe1234..d95d27564 100644 --- a/ndcube/visualization/mpl_plotter.py +++ b/ndcube/visualization/mpl_plotter.py @@ -5,10 +5,10 @@ import numpy as np from astropy.utils.exceptions import AstropyUserWarning from astropy.visualization.wcsaxes import WCSAxes -from mpl_animators import ArrayAnimatorWCS from . import plotting_utils as utils from .base import BasePlotter +from .descriptor import MISSING_ANIMATORS_ERROR_MSG __all__ = ['MatplotlibPlotter'] @@ -189,9 +189,15 @@ def _plot_2D_cube(self, wcs, axes=None, plot_axes=None, axes_coordinates=None, def _animate_cube(self, wcs, plot_axes=None, axes_coordinates=None, axes_units=None, data_unit=None, **kwargs): + try: + from mpl_animators import ArrayAnimatorWCS + except ImportError as e: + raise ImportError(MISSING_ANIMATORS_ERROR_MSG) from e + # Derive inputs for animation object and instantiate. data, wcs, plot_axes, coord_params = self._prep_animate_args(wcs, plot_axes, axes_units, data_unit) + ax = ArrayAnimatorWCS(data, wcs, plot_axes, coord_params=coord_params, **kwargs) # We need to modify the visible axes after the axes object has been created.