# [SciPy-User] Focal Majority

Kevin Gullikson kevin.gullikson@gmail....
Wed Feb 22 11:07:21 CST 2012

```I'm not positive it is the best way, but you can just cast your array as an
integer:

>>> int(True)
1
>>> int(False)
0

Kevin Gullikson

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.
>
> 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....).
>
> Regards,
> Nyberg
>
> On Feb 21, 2012, at 19:58 PM, Aronne Merrelli wrote:
>
> >
> >
> >
> > I don't think a convolution would work. A convolution is really just a
> weighted sum, so I can't see a way to mimic a sort or conditional that way.
> >
> > But, I think you can do this with scipy.ndimage.rank_filter. If you want
> 5 cells with the same value, it should be equivalent to checking if the
> first and fifth ranked elements are the same (or second and sixth, etc...).
> So a loop through the window size, combining rank_filter calls, should do
> this. Definitely double check me on this - I'm not 100% sure it is doing
> the the correct thing, and it probably isn't doing what you want at the
> edges. If this is not fast enough, then I would consider writing a "brute
> force" loop in Cython to make it fast.
> >
> > In [90]: z
> > Out[90]:
> > array([[1, 1, 0, 8],
> >        [8, 1, 3, 1],
> >        [3, 1, 1, 2],
> >        [3, 1, 4, 5]])
> >
> > In [91]: zmask = np.zeros(z.shape, bool)
> >
> > In [92]: for n in range(4):
> rank_filter(z,n,3)==rank_filter(z,(n-5),3))
> >
> > Out[93]:
> > array([[ True,  True, False, False],
> >        [ True,  True,  True, False],
> >        [False, False,  True, False],
> >        [ True, False, False, False]], dtype=bool)
> >
> >
> > HTH,
> > Aronne
> > _______________________________________________
> > SciPy-User mailing list
> > SciPy-User@scipy.org
> > http://mail.scipy.org/mailman/listinfo/scipy-user
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20120222/a7a0eeaa/attachment.html
```