Skip to content

Commit

Permalink
Merge pull request #652 from meeseeksmachine/auto-backport-of-pr-651-…
Browse files Browse the repository at this point in the history
…on-2.1

Backport PR #651 on branch 2.1 (Update error messages for mpl and mpl_animators)
  • Loading branch information
nabobalis authored Nov 16, 2023
2 parents 1c2d897 + f652215 commit b260e27
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions changelog/651.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated the error messages when missing either ``matplotlib`` or ``mpl_animators`` when creating a plot.
24 changes: 16 additions & 8 deletions ndcube/visualization/descriptor.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion ndcube/visualization/mpl_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit b260e27

Please sign in to comment.