[Numpy-discussion] rebin (corrected)

Tim Hochberg tim.hochberg at cox.net
Mon Aug 30 10:58:05 EDT 2004


>But I still agree with Perry that we ought to provide a built-in rebin
>function.  It is particularly useful for large multi-dimensional arrays
>where it is wasteful (in both CPU and memory) to create a full-size
>copy of the array before resampling it down to the desired rebinned
>size.  I appended the .copy() so that at least the big array is not
>still hanging around in memory (remember that the slice creates a
>view rather than a copy.)
>				Rick
A reasonable facsimile of this should  be doable without dropping into 
C. Something like:

def rebin_sum(a, (m, n)):
    M, N = a.shape
    a = na.reshape(a, (M/m,m,N/n,n))
    return na.sum(na.sum(a, 3), 1) / float(m*n)

This does create some temps, but they're smaller than in the boxcar case 
and it doesn't do all the extra calculation. This doesn't handle the 
case where a.shape isn't an exact multiple of (m,n). However, I don't 
think that would be all that hard to implement, if there is a consensus 
on what should happen then.
    I can think of at least two different ways this might be done: 
tacking on values that match the last value as already proposed and 
tacking on zeros. There may be others as well. It should probably get a 
boundary condition argument like convolve and friends.
    Personally, I'd be find rebin a little suprising if it resulted in 
an average, as all the implementations thus far have done, rather than a 
simple sum over the stencil.  When I think of rebinning I'm thinking of 
number of occurences per bin, and rebinning should keep the totals 
occurences the same, not change them by the inverse of the stencil size.

My 2.3 cents anyway


>This SF.Net email is sponsored by BEA Weblogic Workshop
>FREE Java Enterprise J2EE developer tools!
>Get your free copy of BEA WebLogic Workshop 8.1 today.
>Numpy-discussion mailing list
>Numpy-discussion at lists.sourceforge.net

More information about the NumPy-Discussion mailing list