median filtering a masked array

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

median filtering a masked array

Moroney, Catherine M (388D)
Hello,

I have to perform a median filter on only selected elements in an array and am
wondering if using a masked array will speed things up by preventing the computation
of the median on those elements where it's not needed.

I'm using a Fortran code to fill up a (NX,NY,NZ) array where (NZ) is the number of
points in the median filter window and NX and NY are the array dimensions themselves.

If a filter is not required at a certain (I,J) element, I set all the elements along
the NZ dimension to a fill value, and then create a masked array where the "mask"
argument is set to True for all fill values.  

So, I am hoping that the numpy.median(data, axis=2) call will not even try to compute
the median for those (I,J) elements where all the data in the NZ direction are masked
out, thus saving me valuable computational time.

Is this a valid assumption?  Or does numpy go ahead and calculate the median for all
(I,J) elements irregardless of whether they're masked out or not?

What is the recommended way of doing a fast median filter on an array where only
certain elements of the array need to be calculated?  I'm trying to avoid a
nested loop over all (I,J) elements.

Catherine
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: median filtering a masked array

Daπid
On 5 November 2014 19:11, Moroney, Catherine M (398E)
<[hidden email]> wrote:
> What is the recommended way of doing a fast median filter on an array where only
> certain elements of the array need to be calculated?  I'm trying to avoid a
> nested loop over all (I,J) elements.

Since you are using FORTRAN, I believe the simplest way is to do this
double loop in Cython. While you are at it, if you want to squeeze
cycles, you can implement the median in Cython too. Here is some code:

http://numpy-discussion.10968.n7.nabble.com/A-faster-median-Wirth-s-method-td14267.html


/David.
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: median filtering a masked array

Stéfan van der Walt
On 2014-11-06 11:10:29, Daπid <[hidden email]> wrote:
> On 5 November 2014 19:11, Moroney, Catherine M (398E)
> <[hidden email]> wrote:
>> What is the recommended way of doing a fast median filter on an array where only
>> certain elements of the array need to be calculated?  I'm trying to avoid a
>> nested loop over all (I,J) elements.
>
> Since you are using FORTRAN, I believe the simplest way is to do this
> double loop in Cython.

If you'd prefer to stay on the Python side, have a look at

scipy.ndimage.generic_filter

Stéfan
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion