Skip to content

Instantly share code, notes, and snippets.

@zonca
Created November 8, 2011 19:12
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zonca/1348792 to your computer and use it in GitHub Desktop.
IDL rebin in python
import numpy as np
def rebin(a, new_shape):
"""
Resizes a 2d array by averaging or repeating elements,
new dimensions must be integral factors of original dimensions
Parameters
----------
a : array_like
Input array.
new_shape : tuple of int
Shape of the output array
Returns
-------
rebinned_array : ndarray
If the new shape is smaller of the input array, the data are averaged,
if the new shape is bigger array elements are repeated
See Also
--------
resize : Return a new array with the specified shape.
Examples
--------
>>> a = np.array([[0, 1], [2, 3]])
>>> b = rebin(a, (4, 6)) #upsize
>>> b
array([[0, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 1, 1],
[2, 2, 2, 3, 3, 3],
[2, 2, 2, 3, 3, 3]])
>>> c = rebin(b, (2, 3)) #downsize
>>> c
array([[ 0. , 0.5, 1. ],
[ 2. , 2.5, 3. ]])
"""
M, N = a.shape
m, n = new_shape
if m<M:
return a.reshape((m,M/m,n,N/n)).mean(3).mean(1)
else:
return np.repeat(np.repeat(a, m/M, axis=0), n/N, axis=1)
if __name__ == '__main__':
import doctest
doctest.testmod()
@mjigmond
Copy link

Came across your method from stackoverflow and wanted to let you know it only works for regular numpy arrays. This method produces wrong results for masked arrays.
-marius

@derricw
Copy link

derricw commented Apr 9, 2015

Hey I have written a version of this function for ndarrays with arbitrary dimensions:
https://gist.github.com/derricw/95eab740e1b08b78c03f
It also lets you sum or average.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment