8000 Taking median (np.ma.median) of a list of masked arrays. · Issue #10757 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Taking median (np.ma.median) of a list of masked arrays. #10757

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
lokhorst opened this issue Mar 16, 2018 · 1 comment · Fixed by #21999
Closed

Taking median (np.ma.median) of a list of masked arrays. #10757

lokhorst opened this issue Mar 16, 2018 · 1 comment · Fixed by #21999

Comments

@lokhorst
Copy link
lokhorst commented Mar 16, 2018

When trying to take a median of a list of masked arrays (using np.ma.median),
the masks are ignored and the median of the unmasked arrays is returned. An example of this is below:

In [1]: import numpy as np
In [2]: data1 = np.array([[1,2,3,4],[5,6,7,8]])
In [3]: masked1 = np.ma.masked_where(data1 == 4, data1)
In [4]: data2 = np.array([[8,7,6,5],[4,3,2,1]])
In [5]: masked2 = np.ma.masked_where(data2==4,data2)
In [8]: list = [masked1,masked2]
In [9]: list
Out[9]: 
[masked_array(data =
  [[1 2 3 --]
  [5 6 7 8]],
              mask =
  [[False False False  True]
  [False False False False]],
        fill_value = 999999), masked_array(data =
  [[8 7 6 5]
  [-- 3 2 1]],
              mask =
  [[False False False False]
  [ True False False False]],
        fill_value = 999999)]
In [10]: np.ma.median(list,axis=0).data
Out[10]: 
array([[ 4.5,  4.5,  4.5,  4.5],
       [ 4.5,  4.5,  4.5,  4.5]])

but the output of np.ma.median(list,axis=0).data should actually be:

array([[ 4.5,  4.5,  4.5,  5],
       [ 5,  4.5,  4.5,  4.5]])

because the np.ma.median function should be ignoring the data values that are masked.

I have since found a workaround for this that works for me, which is to create the mask after making an array of arrays, e.g.:

In [16]: dataarray = np.array([data1,data2])
In [19]: maskedarray = np.ma.masked_where(dataarray==4,dataarray)
In [20]: maskedarray
Out[20]: 
masked_array(data =
 [[[1 2 3 --]
  [5 6 7 8]]

 [[8 7 6 5]
  [-- 3 2 1]]],
             mask =
 [[[False False False  True]
  [False False False False]]

 [[False False False False]
  [ True False False False]]],
       fill_value = 999999)
In [21]: np.ma.median(maskedarray,axis=0).data
Out[21]: 
array([[ 4.5,  4.5,  4.5,  5. ],
       [ 5. ,  4.5,  4.5,  4.5]])

But this was a bug in my code for years (ah!) and because it did not return an error, I never knew that it was not considering the masks when it was taking the medians of the data.

Is there a way to change the np.ma.median code so that if one tries to take the median of a list of masked arrays it at least will return an error so that future coders know that it is not working the way they assume it will work?

Thanks all!

Versions:
numpy: 1.11.2/1.11.3
python: 2.7.12/2.7.13
on MacOSX

@eric-wieser eric-wieser added the component: numpy.ma masked arrays label Mar 19, 2018
@jsclose
Copy link
Contributor
jsclose commented Mar 30, 2018

I will try and work on this!

jsclose added a commit to jsclose/numpy that referenced this issue Apr 15, 2018
Fixed issue that occurs when trying to take the median of a list of masked arrays.
Added a check to see if the input is a list then converts to a masked array.
See issue numpy#10757 for more information.
bsipocz pushed a commit to bsipocz/numpy that referenced this issue Jul 16, 2022
Fixed issue that occurs when trying to take the median of a list of masked arrays.
Added a check to see if the input is a list then converts to a masked array.
See issue numpy#10757 for more information.
rossbar pushed a commit that referenced this issue Jul 17, 2022
Fixed issue that occurs when trying to take the median of a list of masked arrays.
Added a check to see if the input is a list then converts to a masked array.
See issue #10757 for more information.

Co-authored-by: jsclose <jsclose@umich.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
0