Skip to main content

Extract arbitrary n-dimensional regions from ndarrays.

Project description


NDPatch is the package for extracting arbitrary regions from an N-dimensional numpy array assuming it mirrored infinitely.


The easiest way to install the latest version is by using pip:

$ pip install ndpatch

You may also use Git to clone the repository and install it manually:

$ git clone
$ cd ndpatch
$ python install


To take a patch from the array:

import numpy as np
import ndpatch
array = np.arange(25).reshape((5,5))
index = (1, 2)
shape = (3, 3)
patch = ndpatch.get_ndpatch(array, shape, index)
# patch =
# [[ 7,  8,  9],
#  [12, 13, 14],
#  [17, 18, 19]]

To take get a random patch index:

import numpy as np
import ndpatch
array_shape = (5, 5)
index = ndpatch.get_random_patch_index(array_shape)

To extract random patches from the array:

import numpy as np
import ndpatch
npatches = 10
patch_shape = (3, 3)
array = np.arange(100).reshape((10,10))
patches = [ndpatch.get_random_ndpatch(array, patch_shape) for _ in range(npatches)]

To split the 3D array on set of overlapping 3D patches and rebuild it back:

import numpy as np
import ndpatch
array = np.arange(0, 125).reshape((5,5,5))
patch_shape = (4, 3, 3)
overlap = 2
indices = ndpatch.get_patches_indices(array.shape, patch_shape, overlap)
patches = [ndpatch.get_ndpatch(array, patch_shape, index) for index in indices]
reconstructed = ndpatch.reconstruct_from_patches(patches, indices, array.shape, default_value=0)
# Validate
equal = (reconstructed == array)
assert (np.all(equal))

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ndpatch, version 0.0.2
Filename, size File type Python version Upload date Hashes
Filename, size ndpatch-0.0.2.tar.gz (7.9 kB) File type Source Python version None Upload date Hashes View
Filename, size ndpatch-0.0.2-py3-none-any.whl (8.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ndpatch-0.0.2-py2-none-any.whl (8.6 kB) File type Wheel Python version py2 Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page