本文整理汇总了Python中scipy.interpolate.RegularGridInterpolator方法的典型用法代码示例。如果您正苦于以下问题:Python interpolate.RegularGridInterpolator方法的具体用法?Python interpolate.RegularGridInterpolator怎么用?Python interpolate.RegularGridInterpolator使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.interpolate
的用法示例。
在下文中一共展示了interpolate.RegularGridInterpolator方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def __init__(self, fuel_map, full_load_curve):
from scipy.interpolate import UnivariateSpline as Spl
from scipy.interpolate import RegularGridInterpolator as Interpolator
self.full_load_curve = full_load_curve
self.fc = Interpolator(
(fuel_map['speed'], fuel_map['power']), fuel_map['fuel'],
bounds_error=False, fill_value=0
)
(s, p), fc = self.fc.grid, self.fc.values
with np.errstate(divide='ignore', invalid='ignore'):
e = np.maximum(0, p / fc)
e[(p > full_load_curve(s)[:, None]) | (p < 0)] = np.nan
b = ~np.isnan(e).all(1)
(s, i), e = np.unique(s[b], return_index=True), e[b]
b = ~np.isnan(e).all(0)
p = p[b][np.nanargmax(e[:, b], 1)][i]
func = Spl(s, p, w=1 / np.clip(p * .01, dfl.EPS, 1))
s = np.unique(np.append(s, np.linspace(s.min(), s.max(), 1000)))
p = func(s)
self.max_power = p.max()
self.speed_power = Spl(s, p, s=0)
self.power_speed = Spl(*_invert(np.unique(p), s, p)[::-1], s=0, ext=3)
self.idle_fc = self.fc((self.power_speed(0), 0))
示例2: nearest_2D_interpolator
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def nearest_2D_interpolator(lats_o, lons_o, values):
'''
Produces a 2D interpolator function using the nearest value interpolation
method. If the grids are regular grids, uses the
scipy.interpolate.RegularGridInterpolator,
otherwise, scipy.intepolate.griddata
Values can be interpolated from the returned function as follows:
f = nearest_2D_interpolator(lat_origin, lon_origin, values_origin)
interp_values = f(lat_interp, lon_interp)
Parameters
-----------
lats_o: numpy.ndarray
Latitude coordinates of the values usde by the interpolator
lons_o: numpy.ndarray
Longitude coordinates of the values usde by the interpolator
values: numpy.ndarray
Values usde by the interpolator
Returns
--------
interpolator: function
Nearest neighbour interpolator function
'''
# Determine if we are dealing with a regular grid
if is_regular_grid(lats_o[2:-2, 2:-2], lons_o[2:-2, 2:-2]):
return _nearest_2D_interpolator_reg(lats_o, lons_o, values)
else:
return _nearest_2D_interpolator_arb(lats_o, lons_o, values)
示例3: _nearest_2D_interpolator_reg
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def _nearest_2D_interpolator_reg(lats_o, lons_o, values, lats_d, lons_d):
f = RegularGridInterpolator((np.flipud(lats_o[:, 0]), lons_o[0, :]),
np.flipud(values), method='nearest',
bounds_error=False)
return f
示例4: bilinear_2D_interpolator
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def bilinear_2D_interpolator(lats_o, lons_o, values):
'''
Produces a 2D interpolator function using the bilinear interpolation
method. If the grids are regular grids, uses the
scipy.interpolate.RegularGridInterpolator,
otherwise, scipy.intepolate.griddata
Values can be interpolated from the returned function as follows:
f = nearest_2D_interpolator(lat_origin, lon_origin, values_origin)
interp_values = f(lat_interp, lon_interp)
Parameters
-----------
lats_o: numpy.ndarray
Latitude coordinates of the values usde by the interpolator
lons_o: numpy.ndarray
Longitude coordinates of the values usde by the interpolator
values: numpy.ndarray
Values usde by the interpolator
Returns
--------
interpolator: function
Bilinear interpolator function
'''
if is_regular_grid(lats_o[2:-2, 2:-2], lons_o[2:-2, 2:-2]):
return _bilinear_2D_interpolator_reg(lats_o, lons_o, values)
else:
return _bilinear_2D_interpolator_arb(lats_o, lons_o, values)
示例5: _bilinear_2D_interpolator_reg
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def _bilinear_2D_interpolator_reg(lats_o, lons_o, values):
f = RegularGridInterpolator((np.flipud(lats_o[:, 0]), lons_o[0, :]),
np.flipud(values), method='linear',
bounds_error=False)
return f
示例6: interpolate_from_raster
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def interpolate_from_raster(input_raster, xy_array_to_interpolate,
method='nearest'):
'''
Interpolate input_raster to xy array.
Parameters
----------
input_raster : str
filepath to raster
xy_array : ndarray, shape (num_x, num_y)
Node values to interpolate z at
Returns
-------
interp_z : ndarray, shape (len(xy_array))
Interpolated z values as xy_array (x, y)
See Also
--------
See scipy.interpolate.RegularGridInterpolator documentation for further
details.
'''
x_raster, y_raster, raster_grid = raster_XYZ(input_raster)
interp_f = RegularGridInterpolator((y_raster, x_raster),
raster_grid[2],
bounds_error=False,
fill_value=np.nan)
# Need to flip
xy_flipped = np.fliplr(xy_array_to_interpolate)
interp_z = interp_f(xy_flipped, method=method)
return interp_z
示例7: __init__
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def __init__(self, points, values, delta_list, extrapolate=False):
# type: (Sequence[np.multiarray.ndarray], np.multiarray.ndarray, List[float], bool) -> None
input_range = [(pvec[0], pvec[-1]) for pvec in points]
DiffFunction.__init__(self, input_range, delta_list=delta_list)
self._points = points
self._extrapolate = extrapolate
self.fun = interp.RegularGridInterpolator(points, values, method='linear',
bounds_error=not extrapolate,
fill_value=None)
示例8: make_linear_interpolator_separated
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def make_linear_interpolator_separated(field, grid=None, fill_value=np.nan):
'''Make a linear interpolators for a separated grid.
Parameters
----------
field : Field or ndarray
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. If this is None, the values
outside the domain are extrapolated.
Returns
-------
Field generator
The interpolator, as a Field generator. The grid on which this field generator will evaluated, does
not have to be separated.
'''
if grid is None:
grid = field.grid
else:
field = Field(field, grid)
axes_reversed = np.array(grid.separated_coords)
interp = RegularGridInterpolator(axes_reversed, field.shaped, 'linear', False, fill_value)
def interpolator(evaluated_grid):
evaluated_coords = np.flip(np.array(evaluated_grid.coords), 0)
res = interp(evaluated_coords.T)
return Field(res.ravel(), evaluated_grid)
return interpolator
示例9: make_nearest_interpolator_separated
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def make_nearest_interpolator_separated(field, grid=None):
'''Make a nearest interpolator for a field on a separated grid.
Parameters
----------
field : Field or ndarray
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.
Returns
-------
Field generator
The interpolator, as a Field generator. The grid on which this field generator will evaluated, does
not have to be separated.
'''
if grid is None:
grid = field.grid
else:
field = Field(field, grid)
axes_reversed = np.array(grid.separated_coords)
interp = RegularGridInterpolator(axes_reversed, field.shaped, 'nearest', False)
def interpolator(evaluated_grid):
evaluated_coords = np.flip(np.array(evaluated_grid.coords), 0)
res = interp(evaluated_coords.T)
return Field(res.ravel(), evaluated_grid)
return interpolator
示例10: expected_MonteCarlos
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def expected_MonteCarlos(data, airFrame):
# data = data[data[:,0].argsort()]
alpha = data[:,0]
velocity = data[:,1]
cl = data[:,2]
lift_to_drag = data[:,3]
expected_value = 0
# V = 12*45
V = 1
pdfs = []
f_interpolation = RegularGridInterpolator((np.unique(alpha.ravel()).T, np.unique(velocity.ravel()).T), np.reshape(lift_to_drag, [200,200]))
for i in range(len(airFrame.samples)):
pdf = airFrame.pdf.score_samples(airFrame.samples[i,:])
pdf = np.exp(pdf)
# print(alpha.ravel().shape)
# print(velocity.ravel().shape)
# print(lift_to_drag.ravel().shape)
# print(pdf)
# print(airFrame.samples[i,:][0])
data_i = C172.denormalize(np.array(airFrame.samples[i,:][0])).T
try:
LD_interpolated = f_interpolation(data_i)
except(ValueError):
print(data_i)
# print(pdf, LD_interpolated)
expected_value += (V/1)*pdf[0]*LD_interpolated[0]
pdfs.append(pdf)
total_pdf = sum(pdfs)
expected_value /= total_pdf
return(expected_value)
示例11: expected_LD
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def expected_LD(alpha, velocity, cl, lift_to_drag, airFrame):
# data = data[data[:,0].argsort()]
expected_value = 0
# V = 12*45
V = 1
pdfs = []
alpha = np.sort(np.unique(alpha.ravel()).T)
velocity = np.sort(np.unique(velocity.ravel()).T)
lift_to_drag = np.reshape(lift_to_drag, [len(alpha), len(velocity)])
f_interpolation = RegularGridInterpolator((alpha, velocity), lift_to_drag)
for i in range(len(airFrame.samples)):
pdf = airFrame.pdf.score_samples(airFrame.samples[i,:])
pdf = np.exp(pdf)
# print(alpha.ravel().shape)
# print(velocity.ravel().shape)
# print(lift_to_drag.ravel().shape)
# print(pdf)
# print(airFrame.samples[i,:][0])
data_i = C172.denormalize(np.array(airFrame.samples[i,:][0])).T
try:
LD_interpolated = f_interpolation(data_i)
except(ValueError):
print(data_i)
# print(pdf, LD_interpolated)
expected_value += LD_interpolated[0]
pdfs.append(pdf)
total_pdf = sum(pdfs)
# print(total_pdf, expected_value, expected_value/len(airFrame.samples))
expected_value = expected_value/len(airFrame.samples)
return(expected_value)
示例12: interpolate_var_to_points
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def interpolate_var_to_points(self,
points,
variable,
method='linear',
indices=None,
slices=None,
mask=None,
**kwargs):
try:
from scipy.interpolate import RegularGridInterpolator
except ImportError:
raise ImportError("The scipy package is required to use "
"Grid_R.interpolate_var_to_points\n"
" -- interpolating a regular grid")
points = np.asarray(points, dtype=np.float64)
just_one = (points.ndim == 1)
points = points.reshape(-1, 2)
if slices is not None:
variable = variable[slices]
if np.ma.isMA(variable):
variable = variable.filled(0) #eventually should use Variable fill value
x = self.node_lon if variable.shape[0] == len(self.node_lon) else self.node_lat
y = self.node_lat if x is self.node_lon else self.node_lon
interp_func = RegularGridInterpolator((x, y),
variable,
method=method,
bounds_error=False,
fill_value=0)
if x is self.node_lon:
vals = interp_func(points, method=method)
else:
vals = interp_func(points[:, ::-1], method=method)
if just_one:
return vals[0]
else:
return vals
示例13: polychromatic
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def polychromatic(psfs, spectral_weights=None, interp_method='linear'):
"""Create a new PSF instance from an ensemble of monochromatic PSFs given spectral weights.
The new PSF is the polychromatic PSF, assuming the wavelengths are
sufficiently different that they do not interfere and the mode of
imaging is incoherent.
"""
if spectral_weights is None:
spectral_weights = [1] * len(psfs)
# find the most densely sampled PSF
min_spacing = 1e99
ref_idx = None
ref_x = None
ref_y = None
ref_samples_x = None
ref_samples_y = None
for idx, psf in enumerate(psfs):
if psf.sample_spacing < min_spacing:
min_spacing = psf.sample_spacing
ref_idx = idx
ref_x = psf.x
ref_y = psf.y
ref_samples_x = psf.samples_x
ref_samples_y = psf.samples_y
merge_data = e.zeros((ref_samples_x, ref_samples_y, len(psfs)))
for idx, psf in enumerate(psfs):
# don't do anything to the reference PSF besides spectral scaling
if idx is ref_idx:
merge_data[:, :, idx] = psf.data * spectral_weights[idx]
else:
xv, yv = e.meshgrid(ref_x, ref_y)
interpf = interpolate.RegularGridInterpolator((psf.y, psf.x), psf.data)
merge_data[:, :, idx] = interpf((yv, xv), method=interp_method) * spectral_weights[idx]
psf = PSF(data=merge_data.sum(axis=2), x=ref_x, y=ref_y)
psf.spectral_weights = spectral_weights
psf._renorm()
return psf
示例14: uniform_cart_to_polar
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def uniform_cart_to_polar(x, y, data):
"""Interpolate data uniformly sampled in cartesian coordinates to polar coordinates.
Parameters
----------
x : `numpy.ndarray`
sorted 1D array of x sample pts
y : `numpy.ndarray`
sorted 1D array of y sample pts
data : `numpy.ndarray`
data sampled over the (x,y) coordinates
Returns
-------
rho : `numpy.ndarray`
samples for interpolated values
phi : `numpy.ndarray`
samples for interpolated values
f(rho,phi) : `numpy.ndarray`
data uniformly sampled in (rho,phi)
"""
# create a set of polar coordinates to interpolate onto
xmin, xmax = x.min(), x.max()
ymin, ymax = y.min(), y.max()
_max = max(abs(e.asarray([xmin, xmax, ymin, ymax])))
rho = e.linspace(0, _max, len(x))
phi = e.linspace(0, 2 * e.pi, len(y))
rv, pv = e.meshgrid(rho, phi)
# map points to x, y and make a grid for the original samples
xv, yv = polar_to_cart(rv, pv)
# interpolate the function onto the new points
f = interpolate.RegularGridInterpolator((y, x), data, bounds_error=False, fill_value=0)
return rho, phi, f((yv, xv), method='linear')
示例15: _make_interp_function_2d
# 需要导入模块: from scipy import interpolate [as 别名]
# 或者: from scipy.interpolate import RegularGridInterpolator [as 别名]
def _make_interp_function_2d(self):
"""Generate a 2D interpolation function for this instance, used in sampling with exact_xy.
Returns
-------
`scipy.interpolate.RegularGridInterpolator`
interpolator instance.
"""
if self.interpf_2d is None:
self.interpf_2d = interpolate.RegularGridInterpolator((self.y, self.x), self.data)
return self.interpf_2d