[Numpy-discussion] rebin (corrected)

Rick White rlw at stsci.edu
Mon Aug 30 10:16:10 EDT 2004

On Mon, 30 Aug 2004, Russell E Owen wrote:

> nd_image.boxcar_filter has an origin argument that *might* be for
> this purpose. Unfortunately, it is not documented and my attempt to
> use it as desired failed. I have no idea if this is a bug in nd_image
> or a misunderstanding on my part:
> >>>  from numarray.nd_image import boxcar_filter
> >>>  b = boxcar_filter(a, (2,), origin=(1,), output_type=num.Float32)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
>    File
> "/usr/local/lib/python2.3/site-packages/numarray/nd_image/filters.py",
> line 339, in boxcar_filter
>      output_type = output_type)
>    File
> "/usr/local/lib/python2.3/site-packages/numarray/nd_image/filters.py",
> line 280, in boxcar_filter1d
>      cval, origin, output_type)
> RuntimeError: shift not within filter extent

Seems like you got close to the answer.  This gives the answer you want:

>>> boxcar_filter(a, (2,), output_type=num.Float32,origin=-1)
array([ 0.5,  1.5,  2.5,  3.5,  4. ], type=Float32)

And so this works for rebin:

>>> boxcar_filter(a, (2,), output_type=num.Float32,origin=-1)[::2].copy()
array([ 0.5,  2.5,  4. ], type=Float32)

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.)

More information about the NumPy-Discussion mailing list