Skip to content

Revert "Use atleast_3d instead of ensure_3d" #5241

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

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,21 @@ def _reshape_2D(X):
return X


def ensure_3d(arr):
"""
Return a version of arr with ndim==3, with extra dimensions added
at the end of arr.shape as needed.
"""
arr = np.asanyarray(arr)
if arr.ndim == 1:
arr = arr[:, None, None]
elif arr.ndim == 2:
arr = arr[:, :, None]
elif arr.ndim > 3 or arr.ndim < 1:
raise ValueError("cannot convert arr to 3-dimensional")
return arr


def violin_stats(X, method, points=100):
'''
Returns a list of dictionaries of data which can be used to draw a series
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from numpy import ma

from matplotlib import _path
from matplotlib.cbook import simple_linear_interpolation, maxdict
from matplotlib.cbook import simple_linear_interpolation, maxdict, ensure_3d
from matplotlib import rcParams


Expand Down Expand Up @@ -988,7 +988,7 @@ def get_path_collection_extents(
if len(paths) == 0:
raise ValueError("No paths provided")
return Bbox.from_extents(*_path.get_path_collection_extents(
master_transform, paths, np.atleast_3d(transforms),
master_transform, paths, ensure_3d(transforms),
offsets, offset_transform))


Expand Down
8 changes: 8 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,11 @@ def test_step_fails():
np.arange(12))
assert_raises(ValueError, cbook._step_validation,
np.arange(12), np.arange(3))


def test_ensure_3d():
assert_array_equal([[[1]], [[2]], [[3]]],
cbook.ensure_3d([1, 2, 3]))
assert_array_equal([[[1], [2]], [[3], [4]]],
cbook.ensure_3d([[1, 2], [3, 4]]))
assert_raises(ValueError, cbook.ensure_3d, [[[[1]]]])
3 changes: 2 additions & 1 deletion lib/matplotlib/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from sets import Set as set

from .path import Path
from .cbook import ensure_3d

DEBUG = False
# we need this later, but this is very expensive to set up
Expand Down Expand Up @@ -667,7 +668,7 @@ def count_overlaps(self, bboxes):
bboxes is a sequence of :class:`BboxBase` objects
"""
return count_bboxes_overlapping_bbox(
self, np.atleast_3d([np.array(x) for x in bboxes]))
self, ensure_3d([np.array(x) for x in bboxes]))

def expanded(self, sw, sh):
"""
Expand Down