[SciPy-User] Focal Majority

Aronne Merrelli aronne.merrelli@gmail....
Wed Feb 22 12:06:04 CST 2012


On Wed, Feb 22, 2012 at 10:55 AM, Bjorn Nyberg
<bjorn.burr.nyberg@gmail.com>wrote:

> Thanks Aronne,
>
> I only had half an hour or so to play around with it but it certainty
> looks promising.  Im going to spend some more time on that over the weekend
> when im free... especially to understand how the ranking is being
> calculated.
>


It looks like it just sorts the elements within the window, and then pulls
out the requested item, by the rank you specify. So, this is not terribly
efficient since you will be sorting each window 10 times, while if you
wrote it yourself you could sort each window once. But I'm guessing this
would be faster than writing a pure NumPy/python loop, and the code is much
simpler.

BTW I think there is a typo in what I sent before, it should be:

for n in range(5):
    zmask = np.logical_or(zmask, rank_filter(z,n,3)==rank_filter(z,(n-5),3))

Because you need to check 5 pairs of the sorted elements - [0, 4], [1, 5],
... [4, 8], not 4 pairs as I wrote before.

It might be clearer to write
for n in range(5):
    zmask = np.logical_or(zmask, rank_filter(z,n,3)==rank_filter(z,n+4,3))

You'd probably want to derive those constants (5,n+4, etc) in terms of the
window size and the number of elements that need to be equal. I'm sure if I
did, the result I would be off by one, as I was before, so I will let you
figure that part out =)


> I have to ask though, as I am using this for GIS purposes is there an easy
> way to convert the Bool format into a 0,1 integer - i.e. needed to convert
> the array to raster format (arcpy.ArrayToRaster....).
>
>
>
 zmask.astype() would change it to whatever you need:

In [47]: array([True, False, True])
Out[47]: array([ True, False,  True], dtype=bool)

In [48]: array([True, False, True]).astype(int)
Out[48]: array([1, 0, 1])

Obviously that will make the memory footprint a lot bigger - I guess try
out whichever smallest integer works with arcpy? (I have never used package)


Aronne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120222/3e20648a/attachment.html 


More information about the SciPy-User mailing list