[Numpy-discussion] dedicated function for resize with averaging or rebin 2d arrays?

Craig Yoshioka craigyk@me....
Fri Nov 11 22:28:15 CST 2011


I once wrote a generic n-dimensional binning routine in C that I could find if anyone is interested in integrating it into numpy... it didn't do size increases though... and I think I implemented it so that binning by a non-divisible factor trimmed the extras.  It was very-very fast though.  

On Nov 11, 2011, at 8:22 PM, Andrea Zonca wrote:

> Hi,
> I think the usefulness of the rebin function is to be simple and fast,
> and the best would be to implement it in the core numpy, as a simple
> method for smoothing and reshaping, without the need of using scipy.
> For anything more sophisticated, the ndimage module as you suggest
> should be used.
> cheers,
> Andrea
> 
> On Fri, Nov 11, 2011 at 07:07, Zachary Pincus <zachary.pincus@yale.edu> wrote:
>> Hi Andrea,
>> 
>> scipy.ndimage.zoom will do this nicely for magnification. (Just set the spline order to 0 to get nearest-neighbor interpolation; otherwise you can use higher orders for better smoothing.)
>> 
>> For decimation (zooming out) scipy.ndimage.zoom also works, but it's not as nice as a dedicated decimation filter that would average properly over the area that's being squeezed into a single output pixel. (You'd have to choose the spline order manually to approximate that.) I'm afraid I don't have enough signal-processing background to know how to write a proper general-purpose decimation filter -- basically, you convolve with whatever bandlimiting filter (e.g. a gaussian, or do it in the Fourier domain), then just do nearest-neighbor downsampling, but I'm never sure how to properly choose the filter parameters!
>> 
>> Between this and ndimage.zoom for magnifying, one could get together a much better "rebin" function that in the edge cases of integer magnification/minification should work the same as the IDL one. But the participants in the old discussion you highlighted seemed unhappy with the time/space used for proper decimation, so I'm not sure what really would be best.
>> 
>> Zach
>> 
>> 
>> On Nov 11, 2011, at 1:41 AM, Andrea Zonca wrote:
>> 
>>> hi,
>>> I work in astrophysics where the most common programming language is
>>> currently IDL.
>>> A common request of people switching from IDL to python is the
>>> implementation of the REBIN function, which either downsizes a 2d
>>> array by averaging or increases its dimension by repeating its
>>> elements. In both cases the new shape must be an integer factor of the
>>> old shape.
>>> 
>>> I believe it is a very handy function for quick smoothing of 2 dimensional data.
>>> 
>>> I found a discussion about this topic in the archives:
>>> http://thread.gmane.org/gmane.comp.python.numeric.general/885/focus=894
>>> 
>>> Do you think it would be useful to add such function to numpy?
>>> 
>>> I created a simple implementation to help in the discussion:
>>> https://gist.github.com/1348792
>>> 
>>> thanks,
>>> Andrea Zonca
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> NumPy-Discussion@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> 
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>> 
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion



More information about the NumPy-Discussion mailing list