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

BUG: fixed failure of np.ma.median for 1-D even arrays. #8414

Merged
merged 2 commits into from
Dec 25, 2016

Conversation

mhvk
Copy link
Contributor

@mhvk mhvk commented Dec 23, 2016

@charris: I went ahead and made changes that should solve the problem with even-numbered 1-d arrays. It turned out to be slightly trickier than we thought (although the missing out is still the culprit, the division by 2 should obviously not be done for odd number of elements; also, we cannot use s.data since it is a scalar).

@juliantaylor - if you could have a look, it would be much appreciated.

fixes #8409

For such arrays, the sum of the two entries closest to the middle was
not divided by 2.  Now fixed, with test cases adapted to ensure this
stays OK.
@juliantaylor
Copy link
Contributor

hm returned = true_divide(out=out) does not fulfill returned is out for masked arrays

@juliantaylor
Copy link
Contributor

added a commit which fixes the small inconsistency and added more tests

s = np.true_divide(s, 2., casting='safe', out=out)
# masked ufuncs do not fullfill returned is out
# fix this to return the same in the nd path
if out is not None:
Copy link
Member

@charris charris Dec 24, 2016

Choose a reason for hiding this comment

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

If out is not None, I believe it is returned by true_divide so that s has already been set.

Copy link
Contributor

Choose a reason for hiding this comment

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

it is not which is why I have added that code
maybe a bug of masked arrays or a feature of ndarray subtypes

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so the comment says this is not the case for masked?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem only seems to occur when out is a MaskedArray.

np.true_divide(s, 2., casting='unsafe')
if not odd:
s = np.true_divide(s, 2., casting='safe', out=out)
# masked ufuncs do not fullfill returned is out
Copy link
Member

Choose a reason for hiding this comment

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

This need clarification. np.true_divide is not a masked function, but you are saying that s is not out in some circumstances? When precisely does that happen?

Copy link
Contributor

Choose a reason for hiding this comment

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

masked array __array_wrap__ creates a view of the object so it still references the same data but it is not the same object

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does mean __array_wrap__ is not implemented correctly (which at least for Quantity I found not all that trivial to do...). But anyway, that deserves a separate issue: see #8416.

else:
assert_equal(out, 15.)
assert_(r is out)
assert_(type(r) == MaskedArray)
Copy link
Member

Choose a reason for hiding this comment

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

Should be is ? Of course, singletons can be a bit hazardous...

Copy link
Contributor

Choose a reason for hiding this comment

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

due to the is check earlier this is actually unnecessary but it does serve as extra documentation on the output type

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 think @charris just meant that it should be type(r) is MaskedArray (which I think is correct).

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that is what I meant.

np.true_divide(s, 2., casting='unsafe')
if not odd:
s = np.true_divide(s, 2., casting='safe', out=out)
# masked ufuncs do not fullfill returned is out
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does mean __array_wrap__ is not implemented correctly (which at least for Quantity I found not all that trivial to do...). But anyway, that deserves a separate issue: see #8416.

s = np.true_divide(s, 2., casting='safe', out=out)
# masked ufuncs do not fullfill returned is out
# fix this to return the same in the nd path
if out is not None:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem only seems to occur when out is a MaskedArray.

else:
assert_equal(out, 15.)
assert_(r is out)
assert_(type(r) == MaskedArray)
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 think @charris just meant that it should be type(r) is MaskedArray (which I think is correct).

mask=[True]*3 + [False]*4 + [True]*3)
assert_equal(r, e)
assert_(r is out)
assert_(type(r) == MaskedArray)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here.

@juliantaylor juliantaylor force-pushed the ma/median-even-number-of-elements-fix branch from 9179a86 to 6d52633 Compare December 25, 2016 14:38
@juliantaylor juliantaylor merged commit 2498b74 into numpy:master Dec 25, 2016
@mhvk mhvk deleted the ma/median-even-number-of-elements-fix branch February 19, 2017 22:10
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.

BUG: np.ma.median returns wrong values for even number of elements
3 participants