Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: multifield-view of MaskedArray gets bad fill_value #12408

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
Nov 25, 2018

Conversation

ahaldane
Copy link
Member

Fixes #10483

The problem was the fill_value was not being updated correctly for multifield-views of masked-arrays. Also, dtype.descr was used incorrectly. This eliminates the last use of .descr in np.ma.

@ahaldane ahaldane force-pushed the multifield_ma branch 2 times, most recently from 5dcdd1b to 204bd5d Compare November 18, 2018 06:56
@eric-wieser
Copy link
Member

So, removing that .descr changes behavior on subarrays, which were previously mapped to a scalar fill value

@ahaldane
Copy link
Member Author

Very possible, but I'm not seeing it yet. Could you give an example?

I see that numpy currently does (old behavior):

>>> x = np.ma.ones(3, dtype=[('a','u2'), ('b','u4',(2,))])
>>> x['b'].fill_value
999999
>>> x[['b']].fill_value
(16959, [999999, 999999])

This PR (new behavior):

>>> x = np.ma.ones(3, dtype=[('a','u2'), ('b','u4',(2,))])
>>> x['b'].fill_value
999999
>>> x[['b']].fill_value
([999999, 999999],)

@charris
Copy link
Member

charris commented Nov 18, 2018

Seems reasonable.

numpy/ma/core.py Outdated
@@ -3245,7 +3244,8 @@ def _scalar_heuristic(arr, elem):
# Inherit attributes from self
dout._update_from(self)
# Check the fill_value
if isinstance(indx, basestring):
if isinstance(indx, basestring) or (isinstance(indx, list) and
builtins.all(isinstance(s, basestring) for s in indx)):
Copy link
Member

Choose a reason for hiding this comment

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

Possible candidate for a helper function, _is_field_index(idx)

Copy link
Member Author

Choose a reason for hiding this comment

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

I originally did that, but later deleted it since it was only used in one place.

Copy link
Member

Choose a reason for hiding this comment

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

Helper functions are still useful even if they get called only once - if you pick a good enough name, I can tell what it does without looking at the implementation - and if you don't, at least they provide a good place to put a docstring.

@eric-wieser
Copy link
Member

@ahaldane: here's an example when the behavior changes:

In [63]: np.ma.array([], dtype=dt, fill_value=np.array(2)).fill_value
Out[63]: (2, 2)
In [64]: np.ma.array([], dtype=dt, fill_value=2).fill_value
Out[64]: (2, [2, 2])

My prediction is that this patch changes the second case to match the first, which is probably the right thing to do here.

numpy/ma/core.py Outdated
elif fields:
fdtype = [(_[0], _[1]) for _ in ndtype.descr]
elif ndtype.names is not None:
fdtype = [(name, ndtype.fields[name][0]) for name in ndtype.names]
Copy link
Member

Choose a reason for hiding this comment

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

At this point, it's not clear to me why this line isn't just fdtype - ndtype

Copy link
Member Author

Choose a reason for hiding this comment

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

True.. I just tried it, it passes all tests. So let's do that.

@@ -2029,6 +2029,16 @@ def test_fillvalue(self):
assert_equal(x.fill_value, 999.)
assert_equal(x._fill_value, np.array(999.))

# gh-10483 test multi-field index fill value
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth having a separate test here

@ahaldane
Copy link
Member Author

Fixed up following your suggestions, thanks!

@charris charris merged commit 11a316e into numpy:master Nov 25, 2018
@charris
Copy link
Member

charris commented Nov 25, 2018

Thanks Allan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants