本文整理汇总了Python中scipy.interpolate.LinearNDInterpolator方法的典型用法代码示例。如果您正苦于以下问题:Python interpolate.LinearNDInterpolator方法的具体用法?Python interpolate.LinearNDInterpolator怎么用?Python interpolate.LinearNDInterpolator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.interpolate
的用法示例。
在下文中一共展示了interpolate.LinearNDInterpolator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _interpolator
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def _interpolator(points, coord_source, coord_target, method="idw"):
coord_source_i, coord_source_j = coord_source
coord_target_i, coord_target_j = coord_target
# reshape
trg = np.vstack((coord_source_i.ravel(), coord_source_j.ravel())).T
src = np.vstack((coord_target_i.ravel(), coord_target_j.ravel())).T
if method == "nearest":
interpolator = NearestNDInterpolator(src, points.ravel(),
tree_options={"balanced_tree": False})
points_interpolated = interpolator(trg)
elif method == "linear":
interpolator = LinearNDInterpolator(src, points.ravel(), fill_value=0)
points_interpolated = interpolator(trg)
elif method == "idw":
interpolator = ipol.Idw(src, trg)
points_interpolated = interpolator(points.ravel())
# reshape output
points_interpolated = points_interpolated.reshape(points.shape)
return points_interpolated.astype(points.dtype)
示例2: interpolate_look_angles
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def interpolate_look_angles(data):
log.info('Interpolating look angles from radar coordinates...')
log.debug('Radar coordinates extent width %d; length %d',
data.px_width, data.px_length)
log.debug('Radar coordinates data: length %d - %d; width %d - %d',
data.radar_coords[1].min(), data.radar_coords[1].max(),
data.radar_coords[0].min(), data.radar_coords[0].max())
log.debug('Binned radar coordinate ranges: length %d - %d; width %d - %d',
num.nanmin(data.bin_radar_i), num.nanmax(data.bin_radar_i),
num.nanmin(data.bin_radar_j), num.nanmax(data.bin_radar_j))
width_coords = num.linspace(0, data.px_width, 50)
len_coords = num.linspace(0, data.px_length, 50)
coords = num.asarray(num.meshgrid(width_coords, len_coords))\
.reshape(2, 2500)
radar_coords = num.vstack(
[data.bin_radar_j.ravel() - data.radar_coords[0].min(),
data.bin_radar_i.ravel() - data.radar_coords[1].min()])
interp = interpolate.LinearNDInterpolator(coords.T, data.look_angles)
data.bin_look_angles = interp(radar_coords.T).reshape(
*data.bin_ps_mean_v.shape)
return interp
示例3: areas
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def areas(ip):
"""Returns the area per triangle of the triangulation inside
a `LinearNDInterpolator` instance.
Is useful when defining custom loss functions.
Parameters
----------
ip : `scipy.interpolate.LinearNDInterpolator` instance
Returns
-------
areas : numpy.ndarray
The area per triangle in ``ip.tri``.
"""
p = ip.tri.points[ip.tri.vertices]
q = p[:, :-1, :] - p[:, -1, None, :]
areas = abs(q[:, 0, 0] * q[:, 1, 1] - q[:, 0, 1] * q[:, 1, 0]) / 2
return areas
示例4: default_loss
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def default_loss(ip):
"""Loss function that combines `deviations` and `areas` of the triangles.
Works with `~adaptive.Learner2D` only.
Parameters
----------
ip : `scipy.interpolate.LinearNDInterpolator` instance
Returns
-------
losses : numpy.ndarray
Loss per triangle in ``ip.tri``.
"""
dev = np.sum(deviations(ip), axis=0)
A = areas(ip)
losses = dev * np.sqrt(A) + 0.3 * A
return losses
示例5: impute_atmosphere_grid
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def impute_atmosphere_grid(self, grid):
"""
This function imputes the passed atmosphere grid by linear N-D interpolation.
As grid is passed by reference, it is not necessary to re-assign the table to
the return value of this function; the return value is provided for convenience
only, but the grid is changed in place.
"""
valid_mask = ~np.isnan(grid[...,0])
coords = np.array(np.nonzero(valid_mask)).T
values = grid[valid_mask][:,0]
it = interpolate.LinearNDInterpolator(coords, values, fill_value=0)
filled = it(list(np.ndindex(grid[...,0].shape))).reshape(grid[...,0].shape)
filled[filled==0] = np.nan
grid[...,0] = filled
return grid
示例6: _initialize_distance_interpolator_flat
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def _initialize_distance_interpolator_flat(self, layer):
self._layer = layer
self.triangulate_layer_flat(layer=self._layer)
self._interpolator = [None, None]
for side in [0, 1]:
self._interpolator[side] = LinearNDInterpolator(
self.surf_triang[side], self.triangulation_points[side][:, 2])
示例7: lin_interp
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def lin_interp(shape, xyd):
# taken from https://github.com/hunse/kitti
from scipy.interpolate import LinearNDInterpolator
m, n = shape
ij, d = xyd[:, 1::-1], xyd[:, 2]
f = LinearNDInterpolator(ij, d, fill_value=0)
J, I = np.meshgrid(np.arange(n), np.arange(m))
IJ = np.vstack([I.flatten(), J.flatten()]).T
disparity = f(IJ).reshape(shape)
return disparity
示例8: bellman_operator
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def bellman_operator(self, v):
"""
The Bellman operator. Including for comparison. Value function
iteration is not recommended for this problem. See the
reservation wage operator below.
Parameters
----------
v : array_like(float, ndim=1, length=len(π_grid))
An approximate value function represented as a
one-dimensional array.
Returns
-------
new_v : array_like(float, ndim=1, length=len(π_grid))
The updated value function
"""
# == Simplify names == #
f, g, β, c, q = self.f, self.g, self.β, self.c, self.q
vf = LinearNDInterpolator(self.grid_points, v)
N = len(v)
new_v = np.empty(N)
for i in range(N):
w, π = self.grid_points[i, :]
v1 = w / (1 - β)
integrand = lambda m: vf(m, q(m, π)) * (π * f(m)
+ (1 - π) * g(m))
integral, error = fixed_quad(integrand, 0, self.w_max)
v2 = c + β * integral
new_v[i] = max(v1, v2)
return new_v
示例9: get_greedy
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def get_greedy(self, v):
"""
Compute optimal actions taking v as the value function.
Parameters
----------
v : array_like(float, ndim=1, length=len(π_grid))
An approximate value function represented as a
one-dimensional array.
Returns
-------
policy : array_like(float, ndim=1, length=len(π_grid))
The decision to accept or reject an offer where 1 indicates
accept and 0 indicates reject
"""
# == Simplify names == #
f, g, β, c, q = self.f, self.g, self.β, self.c, self.q
vf = LinearNDInterpolator(self.grid_points, v)
N = len(v)
policy = np.zeros(N, dtype=int)
for i in range(N):
w, π = self.grid_points[i, :]
v1 = w / (1 - β)
integrand = lambda m: vf(m, q(m, π)) * (π * f(m) +
(1 - π) * g(m))
integral, error = fixed_quad(integrand, 0, self.w_max)
v2 = c + β * integral
policy[i] = v1 > v2 # Evaluates to 1 or 0
return policy
示例10: make_linear_interpolator_unstructured
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def make_linear_interpolator_unstructured(field, grid=None, fill_value=np.nan):
'''Make a linear interpolator for an unstructured grid.
Parameters
----------
field : Field or array_like
The field to interpolate.
grid : Grid or None
The grid of the field. If it is given, the grid of `field` is replaced by this grid.
fill_value : scalar
The value to use for points outside of the domain of the input field. Extrapolation is not supported.
Returns
-------
Field generator
The interpolator as a Field generator. The grid on which this field generator will be evaluated does
not need to have any structure.
'''
if fill_value is None:
raise ValueError('Extrapolation is not supported for a linear interpolator on an unstructured grid.')
if grid is None:
grid = field.grid
else:
field = Field(field, grid)
interp = LinearNDInterpolator(grid.points, field, fill_value)
def interpolator(evaluated_grid):
res = interp(grid.points)
return Field(res, evaluated_grid)
return interpolator
示例11: build_interpolator
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def build_interpolator(self, dem_proc):
# Build an interpolator
gc = dem_proc.elev.grid_coordinates
# points = np.meshgrid(gc.x_axis, gc.y_axis)
# points = np.column_stack([pts.ravel() for pts in points])
# interp = spinterp.NearestNDInterpolator(points, dem_proc.data.ravel())
# interp = spinterp.LinearNDInterpolator(points, np.ravel(dem_proc.data),
# fill_value=np.nan)
interp = spinterp.interpolate.RegularGridInterpolator(
points=(gc.y_axis[::-1], gc.x_axis),
values=dem_proc.data[::-1, :].astype(float),
method='nearest', fill_value=np.nan, bounds_error=False)
return interp
示例12: __init__
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def __init__(self, filename, verbose=1):
"""
Initialisation of class to load templates from a file and create the interpolation
objects
Parameters
----------
filename: string
Location of Template file
verbose: int
Verbosity level,
0 = no logging
1 = File + interpolation point information
2 = Detailed description of interpolation points
"""
self.verbose = verbose
if self.verbose:
print("Loading lookup tables from", filename)
grid, bins, template = self.parse_fits_table(filename)
x_bins, y_bins = bins
self.interpolator = interpolate.LinearNDInterpolator(
grid, template, fill_value=0
)
self.nearest_interpolator = interpolate.NearestNDInterpolator(grid, template)
self.grid_interp = interpolate.RegularGridInterpolator(
(x_bins, y_bins),
np.zeros([x_bins.shape[0], y_bins.shape[0]]),
method="linear",
bounds_error=False,
fill_value=0,
)
示例13: test_linear_nd
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def test_linear_nd():
"""
In its simplest configuration this code should behave exactly the same as the scipy
LinearNDInterpolator, so lets test that
"""
# First set up 4 grid points and fill them randomly
interpolation_points = {
(0, 0): np.random.rand(2, 2),
(0, 1): np.random.rand(2, 2),
(1, 0): np.random.rand(2, 2),
(1, 1): np.random.rand(2, 2),
}
# Create UnstructuredInterpolator and LinearNDInterpolator with these points
interpolator = UnstructuredInterpolator(interpolation_points)
linear_nd = LinearNDInterpolator(
list(interpolation_points.keys()), list(interpolation_points.values())
)
# Create some random coordinates in this space
points = np.random.rand(10, 2)
# And interpolate...
interpolated_points = interpolator(points)
linear_nd_points = linear_nd(points)
# Check everything agrees to a reasonable precision
assert np.all(np.abs(interpolated_points - linear_nd_points) < 1e-10)
示例14: test_remember_last
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def test_remember_last():
"""
Check we get the same answer when using masked arrays
"""
# First set up 4 grid points and fill them randomly
interpolation_points = {
(0, 0): np.random.rand(2, 2),
(0, 1): np.random.rand(2, 2),
(1, 0): np.random.rand(2, 2),
(1, 1): np.random.rand(2, 2),
}
# Create UnstructuredInterpolator and LinearNDInterpolator with these points
interpolator = UnstructuredInterpolator(interpolation_points, remember_last=True)
# Create some random coordinates in this space
random_nums = np.random.rand(2, 2)
points_mask = ma.masked_array(random_nums, mask=[[True, False], [True, False]])
# And interpolate...
interpolated_points = interpolator(random_nums).T[0]
interpolated_points_mask = interpolator(points_mask).T[0]
# Check everything agrees to a reasonable precision
assert np.all(np.abs(interpolated_points - interpolated_points_mask) < 1e-10)
示例15: test_masked_input
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import LinearNDInterpolator [as 别名]
def test_masked_input():
"""
Now lets test how well this all works if we pass a masked input
"""
# First set up 4 grid points and fill them randomly
interpolation_points = {
(0, 0): np.random.rand(2, 2),
(0, 1): np.random.rand(2, 2),
(1, 0): np.random.rand(2, 2),
(1, 1): np.random.rand(2, 2),
}
# Create UnstructuredInterpolator and LinearNDInterpolator with these points
interpolator = UnstructuredInterpolator(interpolation_points, remember_last=True)
linear_nd = LinearNDInterpolator(
list(interpolation_points.keys()), list(interpolation_points.values())
)
# Create some random coordinates in this space
points = np.random.rand(10, 2)
# And interpolate...
interpolator(points)
interpolated_points = interpolator(points)
linear_nd_points = linear_nd(points)
# Check everything agrees to a reasonable precision
assert np.all(np.abs(interpolated_points - linear_nd_points) < 1e-10)