rebin#

# who when what # ——- ———- ——————————— # apuglisi 2022-02-30 Fixed bug in detecting downsampling # on just one axis # apuglisi 2020-04-15 Added sample option, added exceptions, # allow any kind of sequence for new_shape # 2012-11-20 Created from git://gist.github.com/1348792.git

arte.utils.rebin.rebin(a, new_shape, sample=False)#

Replacement of IDL’s rebin() function for 2d arrays.

Resizes a 2d array by averaging or repeating elements. New dimensions must be integral factors of original dimensions, otherwise a ValueError exception will be raised.

Parameters:
  • a (ndarray) – Input array.

  • new_shape (2-elements sequence) – Shape of the output array

  • sample (bool) – if True, when reducing the array side elements are set using a nearest-neighbor algorithm instead of averaging. This parameter has no effect when enlarging the array.

Returns:

rebinned_array – If the new shape is smaller of the input array the data are averaged, unless the sample parameter is set. If the new shape is bigger array elements are repeated.

Return type:

ndarray

Raises:
  • ValueError – in the following cases: - new_shape is not a sequence of 2 values that can be converted to int - new dimensions are not an integral factor of original dimensions

  • NotImplementedError

    • one dimension requires an upsampling while the other requires

    a downsampling

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]])
>>> rebin(b, (2, 3)) #downsize
array([[0. , 0.5, 1. ],
       [2. , 2.5, 3. ]])
>>> rebin(b, (2, 3), sample=True) #downsize
array([[0, 0, 1],
       [2, 2, 3]])