npy-cpp-patches
Read N-Dimensional patches from .npy
files. This module is built using C++
and has Python3
bindings.
Data Specifications
- Arrays must be saved in
C-contiguous
format, i.e. NOT Fortran-contiguous
. - First dimension is indexed using in a non-contiguous manner. For example, this can be used to extract specific channels within a natural image.
- Next dimensions are specified by a patch shape
C++
vector or Python
tuple. To extract patches of lower dimensionality than that of the data, set the corresponding dimensions to 1
.
Python Usage
Install
pip install npy-patcher
Usage
from npy_patcher import PatcherDouble, PatcherFloat, PatcherInt, PatcherLong
data_fpath = '/my/numpy/file.npy'
patcher = PatcherFloat()
nc_index = [0, 1, 3, 5, 7]
patch_shape = (30, 30)
patch_stride = (30, 30)
patch_num = 2
extra_padding = (0, 0)
patch_num_offset = (0, 0)
patch = patcher.get_patch(
data_fpath, nc_index, patch_shape, patch_stride, patch_num, extra_padding, patch_num_offset
)
patch = patch.reshape((5, 30, 30))
C++ Usage
Below is an example written in C++
, equivalent to the Python
usage above.
#include "src/patcher.hpp"
#include <vector>
#include <string>
int main() {
std::string fpath = "data.npy";
std::vector<size_t> nc_index {0, 1, 3, 5, 7};
std::vector<size_t> patch_shape {30, 30};
std::vector<size_t> patch_stride{30, 30};
size_t patch_num = 2;
std::vector<size_t> extra_padding = {0, 0};
std::vector<size_t> patch_num_offset = {0, 0};
Patcher<float> patcher;
std::vector<float> patch = patcher.get_patch(
fpath, nc_index, patch_shape, patch_stride, patch_num, extra_padding, patch_num_offset
);
return 0;
}
You can then build the package, for example using g++
:
$ cd npy-cpp-patches/
$ g++ -std=c++17 -I ./ -g test.cpp src/npy_header.cpp src/pyparse.cpp -o test