Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport PR #651 on branch 2.1 (Update error messages for mpl and mpl_animators) #652

Merged
merged 2 commits into from
Nov 16, 2023
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
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