从nDarrays中提取任意n维区域。
ndpatch的Python项目详细描述
ndpatch是一个用于从n维numpy数组中提取任意区域的包,假设它无限镜像。
安装
安装最新版本的最简单方法是使用pip:
$ pip install ndpatch
您还可以使用git克隆存储库并手动安装:
$ git clone https://github.com/ashkarin/ndpatch.git $ cd ndpatch $ python setup.py install
用法
从阵列中获取修补程序:
importnumpyasnpimportndpatcharray=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]]
获取随机补丁索引:
importnumpyasnpimportndpatcharray_shape=(5,5)index=ndpatch.get_random_patch_index(array_shape)
从阵列中提取随机补丁:
importnumpyasnpimportndpatchnpatches=10patch_shape=(3,3)array=np.arange(100).reshape((10,10))patches=[ndpatch.get_random_ndpatch(array,patch_shape)for_inrange(npatches)]
要在一组重叠的三维面片上拆分三维阵列并将其重新生成,请执行以下操作:
importnumpyasnpimportndpatcharray=np.arange(0,125).reshape((5,5,5))patch_shape=(4,3,3)overlap=2indices=ndpatch.get_patches_indices(array.shape,patch_shape,overlap)patches=[ndpatch.get_ndpatch(array,patch_shape,index)forindexinindices]reconstructed=ndpatch.reconstruct_from_patches(patches,indices,array.shape,default_value=0)# Validateequal=(reconstructed==array)assert(np.all(equal))