resize_array

odl.util.numerics.resize_array(arr, newshp, offset=None, pad_mode='constant', pad_const=0, direction='forward', out=None)[source]

Return the resized version of arr with shape newshp.

In axes where newshp > arr.shape, padding is applied according to the supplied options. Where newshp < arr.shape, the array is cropped to the new size.

See the online documentation on resizing operators for mathematical details.

Parameters
arrarray-like

Array to be resized.

newshpsequence of ints

Desired shape of the output array.

offsetsequence of ints, optional

Specifies how many entries are added to/removed from the “left” side (corresponding to low indices) of arr.

pad_modestring, optional

Method to be used to fill in missing values in an enlarged array.

'constant': Fill with pad_const.

'symmetric': Reflect at the boundaries, not doubling the outmost values. This requires left and right padding sizes to be strictly smaller than the original array shape.

'periodic': Fill in values from the other side, keeping the order. This requires left and right padding sizes to be at most as large as the original array shape.

'order0': Extend constantly with the outmost values (ensures continuity).

'order1': Extend with constant slope (ensures continuity of the first derivative). This requires at least 2 values along each axis where padding is applied.

pad_constscalar, optional

Value to be used in the 'constant' padding mode.

direction{‘forward’, ‘adjoint’}

Determines which variant of the resizing is applied.

‘forward’ : in axes where out is larger than arr, apply padding. Otherwise, restrict to the smaller size.

‘adjoint’ : in axes where out is larger than arr, apply zero-padding. Otherwise, restrict to the smaller size and add the outside contributions according to pad_mode.

outnumpy.ndarray, optional

Array to write the result to. Must have shape newshp and be able to hold the data type of the input array.

Returns
resizednumpy.ndarray

Resized array created according to the above rules. If out was given, the returned object is a reference to it.

Examples

The input can be shrunk by simply providing a smaller size. By default, values are removed from the right. When enlarging, zero-padding is applied by default, and the zeros are added to the right side. That behavior can be changed with the offset parameter:

>>> from odl.util.numerics import resize_array
>>> resize_array([1, 2, 3], (1,))
array([1])
>>> resize_array([1, 2, 3], (1,), offset=2)
array([3])
>>> resize_array([1, 2, 3], (6,))
array([1, 2, 3, 0, 0, 0])
>>> resize_array([1, 2, 3], (7,), offset=2)
array([0, 0, 1, 2, 3, 0, 0])

The padding constant can be changed, as well as the padding mode:

>>> resize_array([1, 2, 3], (7,), pad_const=-1, offset=2)
array([-1, -1,  1,  2,  3, -1, -1])
>>> resize_array([1, 2, 3], (7,), pad_mode='periodic', offset=2)
array([2, 3, 1, 2, 3, 1, 2])
>>> resize_array([1, 2, 3], (7,), pad_mode='symmetric', offset=2)
array([3, 2, 1, 2, 3, 2, 1])
>>> resize_array([1, 2, 3], (7,), pad_mode='order0', offset=2)
array([1, 1, 1, 2, 3, 3, 3])
>>> resize_array([1, 2, 3], (7,), pad_mode='order1', offset=2)
array([-1,  0,  1,  2,  3,  4,  5])

Everything works for arbitrary number of dimensions:

>>> # Take the middle two columns and extend rows symmetrically
>>> resize_array([[1, 2, 3, 4],
...               [5, 6, 7, 8],
...               [9, 10, 11, 12]],
...               (5, 2), pad_mode='symmetric', offset=[1, 1])
array([[ 6,  7],
       [ 2,  3],
       [ 6,  7],
       [10, 11],
       [ 6,  7]])
>>> # Take the rightmost two columns and extend rows symmetrically
>>> # downwards
>>> resize_array([[1, 2, 3, 4],
...               [5, 6, 7, 8],
...               [9, 10, 11, 12]], (5, 2), pad_mode='symmetric',
...              offset=[0, 2])
array([[ 3,  4],
       [ 7,  8],
       [11, 12],
       [ 7,  8],
       [ 3,  4]])