8000 [WIP] Masking invalid x and/or weights in hist (#6483) by TrishGillett · Pull Request #7133 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

[WIP] Masking invalid x and/or weights in hist (#6483) #7133

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 3 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Make _normalize_input always output a list of arrays.
  • Loading branch information
TrishGillett committed Sep 18, 2016
commit 1d9783b0a522267156e6683a7353c06771b4ca3c
31 changes: 18 additions & 13 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5871,7 +5871,7 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,
Parameters
----------
x : (n,) array or sequence of (n,) arrays
Input values, this takes either a single array or a sequency of
Input values, this takes either a single array or a sequence of
arrays which are not required to be of the same length

bins : integer or array_like or 'auto', optional
Expand Down Expand Up @@ -6042,8 +6042,8 @@ def hist(self, x, bins=None, range=None, normed=False, weights=None,

"""
def _normalize_input(inp, ename='input'):
"""Normalize 1 or 2d input into list of np.ndarray or
a single 2D np.ndarray.
"""Normalize 1 or 2d input into a list of one or more np.ndarrays
which have potentially different lengths.

Parameters
----------
Expand All @@ -6054,25 +6054,30 @@ def _normalize_input(inp, ename='input'):
"""
if (isinstance(x, np.ndarray) or
not iterable(cbook.safe_first_element(inp))):
# TODO: support masked arrays;

inp = np.asarray(inp)
if inp.ndim == 2:
# 2-D input with columns as datasets; switch to rows
inp = inp.T

if inp.shape[1] < inp.shape[0]:
Copy link
Member

Choose a reason for hiding this comment

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

I am 👎 on this warning, I think it is assuming too much about how users will be using this method.

Copy link
Member

Choose a reason for hiding this comment

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

I now see that this is just moving this warning around. I would be in favor of removing it, but if it is already in the code base, mostly ignore my previous comment.

Copy link
Contributor Author
@TrishGillett TrishGillett Sep 19, 2016

Choose a reason for hiding this comment

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

This is actually just migrated up from where it was before (old line 6068). I don't care for it either but left it alone thinking it probably got debated when it was added. I'm more than happy to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. I'm good either way.

Copy link
Member

Choose a reason for hiding this comment

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

Absent a compelling reason I err on the side of not removing things (which may not be a fully defend able position, but it is a safe conservative one)

Chasing this back through git blame, the warning came into the code base in
214976f in 2008.

Copy link
Member

Choose a reason for hiding this comment

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

I am also in favor of removing it (I am biased as I am in a field where nsamples << nvariables )

Copy link
Member

Choose a reason for hiding this comment

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

Oh, wow, that I can see being a problem.
Maybe throw an error (or raise a warning) if x.shape[0] == 1?

warnings.warn(
'2D hist input should be nsamples x nvariables;\n '
'this looks transposed '
'(shape is %d x %d)' % inp.shape[::-1])

# Change to a list of arrays
inp = [inp[i, :] for i in range(inp.shape[0])]

elif inp.ndim == 1:
# new view, single row
inp = inp.reshape(1, inp.shape[0])
# Change to a list with a single array
inp = [inp.reshape(1, inp.shape[0])]
else:
raise ValueError(
"{ename} must be 1D or 2D".format(ename=ename))
if inp.shape[1] < inp.shape[0]:
warnings.warn(
'2D hist input should be nsamples x nvariables;\n '
'this looks transposed '
'(shape is %d x %d)' % inp.shape[::-1])
else:
# multiple hist with data of different length
inp = [np.asarray(xi) for xi in inp]
# Change to a list of arrays
inp = [np.asarray(arr) for arr in inp]

return inp

Expand Down
0