def py_conv(img, kern, mode, subsample):
 if isinstance(mode, int):
  mode = (mode, mode)
 if isinstance(mode, tuple):
  (pad_h, pad_w) = map(int, mode)
  img = py_conv_pad_img(img, pad_h, pad_w)
  mode = 'valid'
 if imported_scipy_convolve2d:
  return py_conv_scipy(img, kern, mode, subsample)
 elif (mode == 'valid'):
  return py_conv_valid_numpy(img, kern)[:, :, ::subsample[0], ::subsample[1]]
 elif (mode == 'full'):
  return py_conv_full_numpy(img, kern)[:, :, ::subsample[0], ::subsample[1]]
 else:
  raise Exception("Can't execute this kernel.")

def py_conv ( img , kern , mode , subsample ) : if isinstance ( mode , int ) : mode = ( mode , mode ) if isinstance ( mode , tuple ) : ( pad_h , pad_w ) = map ( int , mode ) img = py_conv_pad_img ( img , pad_h , pad_w ) mode = 'valid' if imported_scipy_convolve2d : return py_conv_scipy ( img , kern , mode , subsample ) elif ( mode == 'valid' ) : return py_conv_valid_numpy ( img , kern ) [ : , : , : : subsample [ 0 ] , : : subsample [ 1 ] ] elif ( mode == 'full' ) : return py_conv_full_numpy ( img , kern ) [ : , : , : : subsample [ 0 ] , : : subsample [ 1 ] ] else : raise Exception ( "Can't execute this kernel." )
use a scipy or numpy implementation depending is scipy is available .