Skip to content

Simplify _reshape_2D. #8116

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

Merged
merged 1 commit into from
Mar 23, 2017
Merged
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
34 changes: 9 additions & 25 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1983,32 +1983,16 @@ def _reshape_2D(X):

v is iterable and can be used to instantiate a 1D array.
"""
if hasattr(X, 'shape'):
# one item
if len(X.shape) == 1:
if hasattr(X[0], 'shape'):
X = list(X)
else:
X = [X, ]

# several items
elif len(X.shape) == 2:
nrows, ncols = X.shape
if nrows == 1:
X = [X]
elif ncols == 1:
X = [X.ravel()]
else:
X = [X[:, i] for i in xrange(ncols)]
else:
raise ValueError("input `X` must have 2 or fewer dimensions")

if not hasattr(X[0], '__len__'):
X = [X]
# Iterate over columns for ndarrays, over rows otherwise.
X = X.T if isinstance(X, np.ndarray) else np.asarray(X)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you behave differently for ndarrays than for any other 2D types? (such as matrices, 2D lists etc.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel very uncomfortable with having such a behavior in something that could be easily called in functions where we wouldn't want this behaviour. Is it possible to rename this function to something like _fundamentally_broken_reshape_2D and have a huge warning in the documentation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even go as far as raising a warning when a list is provided and deprecate the behavior we have with list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you, but I think these should be a separate issue. The only point of this PR is to deobfuscate the implementation of this function.

I think it is important to keep the ability to pass lists in -- it is natural to plot boxplots with unequal sized datasets, and you can't do that with a 2D array.

if X.ndim == 1 and X.dtype.type != np.object_:
# 1D array of scalars: directly return it.
return [X]
elif X.ndim in [1, 2]:
# 2D array, or 1D array of iterables: flatten them first.
return [np.reshape(x, -1) for x in X]
else:
X = [np.ravel(x) for x in X]

return X
raise ValueError("input `X` must have 2 or fewer dimensions")


def violin_stats(X, method, points=100):
Expand Down