def shift(input, shift, output=None, order=3, mode='constant', cval=0.0, prefilter=True): """Shift an array. The array is shifted using spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode. Args: input (cupy.ndarray): The input array. shift (float or sequence): The shift along the axes. If a float, ``shift`` is the same for each axis. If a sequence, ``shift`` should contain one value for each axis. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation, default is 3. Must be in the range 0-5. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'``, ``'reflect'``, ``'wrap'``, ``'grid-mirror'``, ``'grid-wrap'``, ``'grid-constant'`` or ``'opencv'``). cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray or None: The shifted input. .. seealso:: :func:`scipy.ndimage.shift` """ _check_parameter('shift', order, mode) shift = _util._fix_sequence_arg(shift, input.ndim, 'shift', float) if mode == 'opencv': mode = '_opencv_edge' output = affine_transform( input, cupy.ones(input.ndim, input.dtype), cupy.negative(cupy.asarray(shift)), None, output, order, mode, cval, prefilter, ) else: output = _util._get_output(output, input) if input.dtype.kind in 'iu': input = input.astype(cupy.float32) filtered, nprepad = _filter_input(input, prefilter, mode, cval, order) integer_output = output.dtype.kind in 'iu' _util._check_cval(mode, cval, integer_output) large_int = _prod(input.shape) > 1 << 31 kern = _interp_kernels._get_shift_kernel(input.ndim, large_int, input.shape, mode, cval=cval, order=order, integer_output=integer_output, nprepad=nprepad) shift = cupy.asarray(shift, dtype=cupy.float64, order='C') if shift.ndim != 1: raise ValueError('shift must be 1d') if shift.size != filtered.ndim: raise ValueError('len(shift) must equal input.ndim') kern(filtered, shift, output) return output
def shift(input, shift, output=None, order=None, mode='constant', cval=0.0, prefilter=True): """Shift an array. The array is shifted using spline interpolation of the requested order. Points outside the boundaries of the input are filled according to the given mode. Args: input (cupy.ndarray): The input array. shift (float or sequence): The shift along the axes. If a float, ``shift`` is the same for each axis. If a sequence, ``shift`` should contain one value for each axis. output (cupy.ndarray or ~cupy.dtype): The array in which to place the output, or the dtype of the returned array. order (int): The order of the spline interpolation. If it is not given, order 1 is used. It is different from :mod:`scipy.ndimage` and can change in the future. Currently it supports only order 0 and 1. mode (str): Points outside the boundaries of the input are filled according to the given mode (``'constant'``, ``'nearest'``, ``'mirror'`` or ``'opencv'``). Default is ``'constant'``. cval (scalar): Value used for points outside the boundaries of the input if ``mode='constant'`` or ``mode='opencv'``. Default is 0.0 prefilter (bool): It is not used yet. It just exists for compatibility with :mod:`scipy.ndimage`. Returns: cupy.ndarray or None: The shifted input. .. seealso:: :func:`scipy.ndimage.shift` """ _check_parameter('shift', order, mode) if not hasattr(shift, '__iter__') and type(shift) is not cupy.ndarray: shift = [shift] * input.ndim if mode == 'opencv': mode = '_opencv_edge' output = affine_transform( input, cupy.ones(input.ndim, input.dtype), cupy.negative(cupy.asarray(shift)), None, output, order, mode, cval, prefilter, ) else: if order is None: order = 1 output = _get_output(output, input) if input.dtype.kind in 'iu': input = input.astype(cupy.float32) integer_output = output.dtype.kind in 'iu' large_int = _prod(input.shape) > 1 << 31 kern = _interp_kernels._get_shift_kernel(input.ndim, large_int, input.shape, mode, cval=cval, order=order, integer_output=integer_output) shift = cupy.asarray(shift, dtype=cupy.float64) kern(input, shift, output) return output