Vì vậy, để nội suy các đám mây điểm lớn tùy ý, tôi đã viết một đoạn mã để phân vùng dữ liệu thành các khối nhỏ hơn.Đó không phải là đoạn mã tốt nhất nhưng sẽ có sẵn cho những người quá lười biếng để tự viết.
import scipy.interpolate
from scipy.interpolate import griddata
from scipy.spatial.qhull import QhullError
class Interp2P[object]:
"""
Reconstruction of interpolation for 2d applications.
This class is used to avoid any memory errors due to interpolation
of large numbers of points.
Built for use for extremely large point clouds. Interpolation
is partitioned into automatic control parameters px, py, pe, blockpts.
The scipy implementation of interpolation functions has memory problems
for large point clouds. This class divides the problem into several
smaller partitions.
Parameters
----------
points : array shape [a, 2]
table of point coordinates describing z = f[x,y] where
- column 0 = x
- column 1 = y
values : array of shape [a, b]
Corresponding values z = f[x, y]
values may possibly have multiple columns,
depending on the interpolator kind used.
kind : str
Interpolation method. Can be
- 'nearest'
- 'linear'
- 'cubic'
px : int or None
Number of partitions in x-direction. If None, a default is calculated
according to the number of blockpts
py : int or None
Number of partitions in y-direction. If None, a default is calculated
according to the number of blockpts.
pe : scalar
Proportion of block length to overlap on other blocks.
For example, if pe=0.25, the block will be extended 25% on both the
left and right sides of px to overlap on successive blocks.
blockpts : int
Approximate number of interpolation points within each partition block.
Defaults to 300*300. blockpts is used to automatically size either
px or py if these are set to None.
"""
def __init__[self, points, values, kind='linear',
px = None, py = None, pe = 0.5, blockpts = 300*300,
**kwargs]:
points = np.array[points]
self.x = points[:, 0]
self.y = points[:, 1]
self.z = np.array[values]
self.points = points
self.values = np.array[self.z]
self.kind = kind
self.kwargs = kwargs
self.px = px
self.py = py
self.pe = pe
self.blockpts = blockpts
self._set_partitions[]
return
def _set_partitions[self]:
""" Calculate the number of partitions to use in data set"""
ptnum = len[self.x]
blockpts = self.blockpts
blocknum = ptnum / blockpts + 1
if self.px is None:
if self.py is None:
self.px = int[np.sqrt[blocknum]]
self.py = int[blocknum / self.px]
else:
self.px = int[blocknum / self.py]
if self.py is None:
self.py = int[blocknum / self.px]
self.px = max[self.px, 1]
self.py = max[self.py, 1]
self.xmax = np.max[self.x]
self.xmin = np.min[self.x]
self.xlen = self.xmax - self.xmin
self.xp = self.xlen / self.px # block x length
self.xe = self.xp * self.pe # block x overlap length
self.ymax = np.max[self.y]
self.ymin = np.min[self.y]
self.ylen = self.ymax - self.ymin
self.yp = self.ylen / self.py # block y length
self.ye = self.yp * self.pe # block y overlap length
xfudge = [self.xmax - self.xmin] / 1000.
yfudge = [self.ymax - self.ymin] / 1000.
# Construct block upper/lower limits
xl = self.xmin - xfudge
xu = self.xmax + xfudge
yl = self.ymin - yfudge
yu = self.ymax + yfudge
# Construct blocks
self.xblocks = np.linspace[xl, xu, self.px + 1]
self.yblocks = np.linspace[yl, yu, self.py + 1]
return
def _choose_block[self, x, y]:
"""
Calculate which interpolation block to use for the given
coordinates [x, y]
Returns
--------
xindex : int array of shape [N,]
index locations for x-dimension of blocks
yindex : int array of shape [N,]
index locations for y-dimension of blocks
"""
xindex = np.searchsorted[self.xblocks, x] - 1
yindex = np.searchsorted[self.yblocks, y] - 1
return xindex, yindex
@lazy_property
def _template_interp[self]:
"""
Construct template interpolator function based on kind
"""
if self.kind == 'linear':
template = scipy.interpolate.LinearNDInterpolator
elif self.kind == 'cubic':
template = scipy.interpolate.CloughTocher2DInterpolator
elif self.kind == 'nearest':
template = scipy.interpolate.NearestNDInterpolator
elif self.kind == 'rbf':
template = Rbf_wrapper
# def func1[points, values, **kwargs]:
# args = np.column_stack[[points, values]]
# f = scipy.interpolate.Rbf[args, **kwargs]
# return f
# template = func1
return template
@lazy_property
def _interpolators[self]:
"""
Construct interpolators for every block.
- 0 dimension corresponds to x data.
- 1 dimension corresponds to y data.
"""
# Bounds of block interpolation points
xl_arr = self.xblocks[0:-1] - self.xe
xu_arr = self.xblocks[1:] + self.xe
yl_arr = self.yblocks[0:-1] - self.ye
yu_arr = self.yblocks[1:] + self.ye
# Loop through all block boundaries and construct interpolators.
interpolators = []
for [xl, xu] in zip[xl_arr, xu_arr]:
interpx = []
for [yl, yu] in zip[yl_arr, yu_arr]:
#Set original data partition
ix0 = np.logical_and[xl