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

Skip to content

BUG: np.sum with keepdims=True fails on masked_array #7312

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

Conversation

astrofrog
Copy link
Contributor

When calling np.sum on a masked array, and using the keepdims option, an error is raised:

In [1]: import numpy as np 

In [2]: np.sum(np.ma.masked_array([1,2,3],mask=[0,0,1]),keepdims=True)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-482ed005c74d> in <module>()
----> 1 np.sum(np.ma.masked_array([1,2,3],mask=[0,0,1]),keepdims=True)

/Users/tom/miniconda3/envs/production27/lib/python2.7/site-packages/numpy-1.12.0.dev0+0dbcd2f-py2.7-macosx-10.5-x86_64.egg/numpy/core/fromnumeric.pyc in sum(a, axis, dtype, out, keepdims)
   1843             pass
   1844         else:
-> 1845             return sum(axis=axis, dtype=dtype, out=out, **kwargs)
   1846     return _methods._sum(a, axis=axis, dtype=dtype,
   1847                          out=out, **kwargs)

TypeError: sum() got an unexpected keyword argument 'keepdims'

@astrofrog
Copy link
Contributor Author

This can be triggered even without the user passing keepdims:

In [6]: x = np.ma.masked_array([1,2,np.nan],mask=[0,0,1])

In [7]: np.nanvar(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-ede4d9457a87> in <module>()
----> 1 np.nanvar(x)

/Users/tom/miniconda3/envs/production27/lib/python2.7/site-packages/numpy-1.12.0.dev0+0dbcd2f-py2.7-macosx-10.5-x86_64.egg/numpy/lib/nanfunctions.py in nanvar(a, axis, dtype, out, ddof, keepdims)
   1182         # the reason that it drops the keepdims kwarg is to force keepdims=True
   1183         # so this used to work by serendipity.
-> 1184         cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims)
   1185         avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims)
   1186         avg = _divide_by_count(avg, cnt)

/Users/tom/miniconda3/envs/production27/lib/python2.7/site-packages/numpy-1.12.0.dev0+0dbcd2f-py2.7-macosx-10.5-x86_64.egg/numpy/core/fromnumeric.pyc in sum(a, axis, dtype, out, keepdims)
   1841         print(type(a))
   1842         sum = a.sum
-> 1843         return sum(axis=axis, dtype=dtype, out=out, **kwargs)
   1844     return _methods._sum(a, axis=axis, dtype=dtype,
   1845                          out=out, **kwargs)

TypeError: sum() got an unexpected keyword argument 'keepdims'

@astrofrog
Copy link
Contributor Author

I'm working on a fix

@astrofrog
Copy link
Contributor Author

I've attached a fix!

@astrofrog astrofrog changed the title np.sum with keepdims=True fails on masked_array BUG: np.sum with keepdims=True fails on masked_array Feb 22, 2016
# not supporting the keepdims option.

# First make sure keepdims works when called explicitly
result = np.sum(np.ma.masked_array([1,2,3],mask=[0,0,1]), keepdims=True)
Copy link
Member

Choose a reason for hiding this comment

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

Lots of PEP8 violations in this function (spaces after commas) -- could you fix that? Otherwise LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, thanks for pointing that out - should be fixed now

…eepdims= keyword argument, and include regression test
@ahaldane
Copy link
Member

Before you work too hard on this, may I suggest looking at my PR #5706, which adds keepdims, and is finished but just waiting for a review.

After a huge discussion, we concluded you can't directly add a keepdims arg, instead you have to catch it through **kwargs, because of the case of masked arrays of ndarray subclasses (eg masked matrices), which want to change the default value of keepdims.

@njsmith
Copy link
Member

njsmith commented Feb 23, 2016

@ahaldane: oh good point

@astrofrog
Copy link
Contributor Author

@njsmith @ahaldane - I'm fine with closing this, as long as the equivalent fix in #5706 is included in 1.11.x - will that be the case?

@astrofrog
Copy link
Contributor Author

Just to clarify, this is causing test failures in Astropy, so it would be good to have this fix included in 1.11.0 final - thanks!

@njsmith
Copy link
Member

njsmith commented Feb 23, 2016

@astrofrog: wait, this is a regression? Can you give an example of code that used to work but now doesn't, and which version it worked in?

@astrofrog
Copy link
Contributor Author

@njsmith - it is a regression but actually after double checking I think it doesn't affect 1.11.0 pre-releases.

This works in Numpy 1.9, 1.10, and 1.11.0rc1 (using the git tag):

>>> import numpy as np
>>> x = np.ma.masked_array([1,2,np.nan],mask=[0,0,1])
>>> np.nanvar(x)
0.25

but fails in master:

>>> import numpy as np

>>> x = np.ma.masked_array([1,2,np.nan],mask=[0,0,1])

>>> np.nanvar(x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-ede4d9457a87> in <module>()
----> 1 np.nanvar(x)

/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/numpy-1.12.0.dev0+da2ad32-py3.5-macosx-10.5-x86_64.egg/numpy/lib/nanfunctions.py in nanvar(a, axis, dtype, out, ddof, keepdims)
   1178         # the reason that it drops the keepdims kwarg is to force keepdims=True
   1179         # so this used to work by serendipity.
-> 1180         cnt = np.sum(~mask, axis=axis, dtype=np.intp, keepdims=_keepdims)
   1181         avg = np.sum(arr, axis=axis, dtype=dtype, keepdims=_keepdims)
   1182         avg = _divide_by_count(avg, cnt)

/Users/tom/miniconda3/envs/production35/lib/python3.5/site-packages/numpy-1.12.0.dev0+da2ad32-py3.5-macosx-10.5-x86_64.egg/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims)
   1843             pass
   1844         else:
-> 1845             return sum(axis=axis, dtype=dtype, out=out, **kwargs)
   1846     return _methods._sum(a, axis=axis, dtype=dtype,
   1847                          out=out, **kwargs)

TypeError: sum() got an unexpected keyword argument 'keepdims'

So maybe we are ok for the 1.11.0 release

@seberg
Copy link
Member

seberg commented Feb 23, 2016

We recently put in a PR fixing keepdims propagation to subclasses for things like sum. That probably is the reason. Which means that nanvar might be broken for weird reduction axis though.

@charris
Copy link
Member

charris commented Mar 7, 2016

@seberg That would probably be #4619. Looks like we need to carefully review that again together with this.

@seberg
Copy link
Member

seberg commented Mar 7, 2016

Ohh, yes, I never looked at this a lot. The issue here is not so much the lack of keepdims in MA as much as the use of keepdims in nanvar, etc.
Not sure whether it is a big issue in practice, but it would be nice to extend the use of _NoValue to those functions like nanvar, etc. where possible at least.

@seberg
Copy link
Member

seberg commented Mar 7, 2016

Tagging with 1.12 milestone, just so we revisit it before a release.

@homu
Copy link
Contributor

homu commented Apr 4, 2016

☔ The latest upstream changes (presumably #5706) made this pull request unmergeable. Please resolve the merge conflicts.

@seberg
Copy link
Member

seberg commented Apr 4, 2016

@ahaldane was this fixed?

@ahaldane
Copy link
Member

ahaldane commented Apr 4, 2016

Yeah this is fixed. Closing.

@ahaldane ahaldane closed this Apr 4, 2016
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.

6 participants