parameters: (source, target_shape, trg2src, interp_method='linear', fill=0, smooth_sigma=0)
docstring: Resamples a volume array from one space to another given
      a target-to-source transformation matrix.
  
      Parameters:
          source: Source array to sample from. Must be 3D or 4D.
          target_shape: Shape of the returned target array.
          trg2src: 4x4 affine matrix that transforms target coords to source coords.
          interp_method: Interpolation method. Must be 'linear' or 'nearest'. Default is 'linear'.
          smooth_sigma: Apply gaussian smoothing before resampling (smoothing kernel is
              in voxel space). Default is 0.
docstring_summary: Resamples a volume array from one space to another given
      a target-to-source transformation matrix.
docstring_tokens: Resamples a volume array from one space to another given a target - to - source transformation matrix .
function: def resample(source, target_shape, trg2src, interp_method='linear', fill=0, smooth_sigma=0):
      '''
      Resamples a volume array from one space to another given
      a target-to-source transformation matrix.
  
      Parameters:
          source: Source array to sample from. Must be 3D or 4D.
          target_shape: Shape of the returned target array.
          trg2src: 4x4 affine matrix that transforms target coords to source coords.
          interp_method: Interpolation method. Must be 'linear' or 'nearest'. Default is 'linear'.
          smooth_sigma: Apply gaussian smoothing before resampling (smoothing kernel is
              in voxel space). Default is 0.
      '''
      if source.ndim > 4:
          raise ValueError('resampling can not be done on arrays with more than 4 dimensions')
      elif source.ndim < 3:
          raise NotImplementedError('%dD resampling is not yet supported (must be 3D or 4D)' % source.ndim)
   
      # the resample binding function only works with 4D inputs for easier maintenance, so let's add an axis to any 3D input
      target_shape = target_shape[:3]
      if source.ndim == 4:
          target_shape = (*target_shape, source.shape[-1])
  
      orig_target_shape = target_shape
      if source.ndim == 3:
          source = source[..., np.newaxis]
          target_shape = (*target_shape, 1)
  
      if len(target_shape) != source.ndim:
          raise ValueError('resampled target shape (%sD) must match source dims (%sD)' % (len(target_shape), source.ndim))
  
      if target_shape[-1] != source.shape[-1]:
          raise ValueError('resampled target must have the same number of frames as the source')
  
      # apply optional gaussian smoothing
      if smooth_sigma > 0:
          sigmas = (smooth_sigma, smooth_sigma, smooth_sigma, 0)
          source = scipy.ndimage.gaussian_filter(source, sigma=sigmas, order=0)  # TODO order?
  
      trg2src = LinearTransform.ensure(trg2src).matrix
  
      if interp_method == 'linear':
          return bindings.vol.resample_volume_linear(source, target_shape, trg2src, fill).reshape(orig_target_shape)
      elif interp_method == 'nearest':
          return bindings.vol.resample_volume_nearest(source, target_shape, trg2src, fill).reshape(orig_target_shape)
      else:
          raise ValueError('invalid resample interpolation method "%s"' % interp_method)
function_tokens: def resample ( source , target_shape , trg2src , interp_method = 'linear' , fill = 0 , smooth_sigma = 0 ) : if source . ndim > 4 : raise ValueError ( 'resampling can not be done on arrays with more than 4 dimensions' ) elif source . ndim < 3 : raise NotImplementedError ( '%dD resampling is not yet supported (must be 3D or 4D)' % source . ndim ) # the resample binding function only works with 4D inputs for easier maintenance, so let's add an axis to any 3D input target_shape = target_shape [ : 3 ] if source . ndim == 4 : target_shape = ( * target_shape , source . shape [ - 1 ] ) orig_target_shape = target_shape if source . ndim == 3 : source = source [ ... , np . newaxis ] target_shape = ( * target_shape , 1 ) if len ( target_shape ) != source . ndim : raise ValueError ( 'resampled target shape (%sD) must match source dims (%sD)' % ( len ( target_shape ) , source . ndim ) ) if target_shape [ - 1 ] != source . shape [ - 1 ] : raise ValueError ( 'resampled target must have the same number of frames as the source' ) # apply optional gaussian smoothing if smooth_sigma > 0 : sigmas = ( smooth_sigma , smooth_sigma , smooth_sigma , 0 ) source = scipy . ndimage . gaussian_filter ( source , sigma = sigmas , order = 0 ) # TODO order? trg2src = LinearTransform . ensure ( trg2src ) . matrix if interp_method == 'linear' : return bindings . vol . resample_volume_linear ( source , target_shape , trg2src , fill ) . reshape ( orig_target_shape ) elif interp_method == 'nearest' : return bindings . vol . resample_volume_nearest ( source , target_shape , trg2src , fill ) . reshape ( orig_target_shape ) else : raise ValueError ( 'invalid resample interpolation method "%s"' % interp_method )