mirror of
https://github.com/meteoinfo/MeteoInfo.git
synced 2026-02-01 17:20:51 +00:00
update interpolate package
This commit is contained in:
parent
12fc6e66a5
commit
34341dcdd9
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<MeteoInfo File="milconfig.xml" Type="configurefile">
|
||||
<Path OpenPath="D:\Working\MIScript\Jython\mis\common_math\random">
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
|
||||
<Path OpenPath="D:\Working\MIScript\Jython\mis\common_math\interpolate">
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\contour"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
|
||||
@ -14,19 +13,28 @@
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\toolbox\miml"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\random"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
|
||||
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\interpolate"/>
|
||||
</Path>
|
||||
<File>
|
||||
<OpenedFiles>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\deep_learning\mnist_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\cluster\specc_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\random\random_seed_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp1d_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_1.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\nearest_3d.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\linint2.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_3.py"/>
|
||||
<OpenedFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_2.py"/>
|
||||
</OpenedFiles>
|
||||
<RecentFiles>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\deep_learning\mnist_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\toolbox\miml\cluster\specc_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\random\random_seed_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp1d_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_1.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\nearest_3d.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\linint2.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_3.py"/>
|
||||
<RecentFile File="D:\Working\MIScript\Jython\mis\common_math\interpolate\interp2d_2.py"/>
|
||||
</RecentFiles>
|
||||
</File>
|
||||
<Font>
|
||||
|
||||
Binary file not shown.
@ -43,9 +43,9 @@ __all__ = [
|
||||
'broadcast_to','cdiff','ceil','concatenate','corrcoef','cos','cumsum','degrees','delete','delnan','diag',
|
||||
'diff','dim_array','datatable','dot','empty','empty_like','exp','eye','floor','fmax','fmin','full',
|
||||
'griddata','hcurl','hdivg','hstack','identity','interp2d',
|
||||
'interpn','isarray','isfinite','isinf','isnan','linint2','linregress','linspace','log','log10',
|
||||
'interpn','isarray','isfinite','isinf','isnan','linspace','log','log10',
|
||||
'logical_not','logspace','magnitude','max','maximum','mean','median','meshgrid','min','minimum',
|
||||
'monthname','moveaxis','newaxis','nonzero','ones','ones_like','pol2cart','polyval','power','radians','ravel',
|
||||
'monthname','moveaxis','newaxis','nonzero','ones','ones_like','pol2cart','power','radians','ravel',
|
||||
'reshape','repeat','roll','rolling_mean','rot90','sin','shape','smooth5','smooth9','sort','squeeze','argsort',
|
||||
'split','sqrt','square','std','sum','swapaxes','tan','tile','transpose','trapz','vdot','unique',
|
||||
'unravel_index','var','vstack','where','zeros','zeros_like'
|
||||
@ -2034,44 +2034,7 @@ def corrcoef(x, y):
|
||||
b = ArrayMath.getR(y.asarray(), x.asarray())
|
||||
r = array([[1, a], [b, 1]])
|
||||
return r
|
||||
|
||||
def linregress(x, y):
|
||||
'''
|
||||
Calculate a linear least-squares regression for two sets of measurements.
|
||||
|
||||
:param x, y: (*array_like*) Two sets of measurements. Both arrays should have the same length.
|
||||
|
||||
:returns: Result slope, intercept, relative coefficient, two-sided p-value for a hypothesis test
|
||||
whose null hypothesis is that the slope is zero, standard error of the estimated gradient,
|
||||
validate data number (remove NaN values).
|
||||
'''
|
||||
if isinstance(x, list):
|
||||
x = array(x)
|
||||
if isinstance(y, list):
|
||||
y = array(y)
|
||||
r = ArrayMath.lineRegress(x.asarray(), y.asarray())
|
||||
return r[0], r[1], r[2], r[3], r[4], r[5]
|
||||
|
||||
def polyval(p, x):
|
||||
"""
|
||||
Evaluate a polynomial at specific values.
|
||||
|
||||
If p is of length N, this function returns the value:
|
||||
|
||||
p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]
|
||||
|
||||
If x is a sequence, then p(x) is returned for each element of x. If x is another polynomial then the
|
||||
composite polynomial p(x(t)) is returned.
|
||||
|
||||
:param p: (*array_like*) 1D array of polynomial coefficients (including coefficients equal to zero)
|
||||
from highest degree to the constant term.
|
||||
:param x: (*array_like*) A number, an array of numbers, or an instance of poly1d, at which to evaluate
|
||||
p.
|
||||
|
||||
:returns: Polynomial value
|
||||
"""
|
||||
return NDArray(ArrayMath.polyVal(p, x.asarray()))
|
||||
|
||||
|
||||
def transpose(a, axes=None):
|
||||
'''
|
||||
Transpose 2-D array.
|
||||
@ -2611,38 +2574,6 @@ def asgridarray(data, x=None, y=None, fill_value=-9999.0):
|
||||
def asstationdata(data, x, y, fill_value=-9999.0):
|
||||
stdata = StationData(data.asarray(), x.asarray(), y.asarray(), fill_value)
|
||||
return PyStationData(stdata)
|
||||
|
||||
def linint2(*args, **kwargs):
|
||||
"""
|
||||
Interpolates from a rectilinear grid to another rectilinear grid using bilinear interpolation.
|
||||
|
||||
:param x: (*array_like*) X coordinate array of the sample data (one dimension).
|
||||
:param y: (*array_like*) Y coordinate array of the sample data (one dimension).
|
||||
:param z: (*array_like*) Value array of the sample data (muti-dimension, last two dimensions are y and x).
|
||||
:param xq: (*array_like*) X coordinate array of the query data (one dimension).
|
||||
:param yq: (*array_like*) Y coordinate array of the query data (one dimension).
|
||||
|
||||
:returns: (*array_like*) Interpolated array.
|
||||
"""
|
||||
if len(args) == 3:
|
||||
z = args[0]
|
||||
x = z.dimvalue(z.ndim - 1)
|
||||
y = z.dimvalue(z.ndim - 2)
|
||||
xq = args[1]
|
||||
yq = args[2]
|
||||
else:
|
||||
x = args[0]
|
||||
y = args[1]
|
||||
z = args[2]
|
||||
xq = args[3]
|
||||
yq = args[4]
|
||||
x = array(x)._array
|
||||
y = array(y)._array
|
||||
z = array(z)._array
|
||||
xq = array(xq)._array
|
||||
yq = array(yq)._array
|
||||
r = ArrayUtil.linint2(z, x, y, xq, yq)
|
||||
return NDArray(r)
|
||||
|
||||
def interp2d(*args, **kwargs):
|
||||
"""
|
||||
|
||||
Binary file not shown.
@ -7,13 +7,13 @@
|
||||
#-----------------------------------------------------
|
||||
|
||||
from org.meteoinfo.math.interpolate import InterpUtil
|
||||
from org.meteoinfo.math import ArrayMath, ArrayUtil
|
||||
from org.meteoinfo.ndarray import Array
|
||||
from org.meteoinfo.math import ArrayUtil
|
||||
|
||||
from ..core import NDArray, DimArray
|
||||
from ..core import NDArray
|
||||
from ..core import numeric as np
|
||||
|
||||
__all__ = [
|
||||
'interp1d','RectBivariateSpline'
|
||||
'interp1d','interp2d','linint2','RectBivariateSpline'
|
||||
]
|
||||
|
||||
class interp1d(object):
|
||||
@ -27,9 +27,9 @@ class interp1d(object):
|
||||
'''
|
||||
def __init__(self, x, y, kind='linear'):
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
x = np.array(x)
|
||||
if isinstance(y, list):
|
||||
y = NDArray(ArrayUtil.array(y))
|
||||
y = np.array(y)
|
||||
self._func = InterpUtil.getInterpFunc(x.asarray(), y.asarray(), kind)
|
||||
|
||||
def __call__(self, x):
|
||||
@ -39,8 +39,8 @@ class interp1d(object):
|
||||
:param x: (*array_like*) Points to evaluate the interpolant at.
|
||||
'''
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
if isinstance(x, (NDArray, DimArray)):
|
||||
x = np.array(x)
|
||||
if isinstance(x, NDArray):
|
||||
x = x.asarray()
|
||||
r = InterpUtil.evaluate(self._func, x)
|
||||
if isinstance(r, float):
|
||||
@ -66,12 +66,12 @@ class interp2d(object):
|
||||
'''
|
||||
def __init__(self, x, y, z, kind='linear'):
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
x = np.array(x)
|
||||
if isinstance(y, list):
|
||||
y = NDArray(ArrayUtil.array(y))
|
||||
y = np.array(y)
|
||||
if isinstance(z, list):
|
||||
z = NDArray(ArrayUtil.array(z))
|
||||
self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.asarray())
|
||||
z = np.array(z)
|
||||
self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.T.asarray(), kind)
|
||||
|
||||
def __call__(self, x, y):
|
||||
'''
|
||||
@ -81,12 +81,12 @@ class interp2d(object):
|
||||
:param y: (*array_like*) Y to evaluate the interpolant at.
|
||||
'''
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
if isinstance(x, (NDArray, DimArray)):
|
||||
x = np.array(x)
|
||||
if isinstance(x, NDArray):
|
||||
x = x.asarray()
|
||||
if isinstance(y, list):
|
||||
y = NDArray(ArrayUtil.array(y))
|
||||
if isinstance(y, (NDArray, DimArray)):
|
||||
y = np.array(y)
|
||||
if isinstance(y, NDArray):
|
||||
y = y.asarray()
|
||||
r = InterpUtil.evaluate(self._func, x, y)
|
||||
if isinstance(r, float):
|
||||
@ -106,12 +106,12 @@ class RectBivariateSpline(object):
|
||||
'''
|
||||
def __init__(self, x, y, z):
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
x = np.array(x)
|
||||
if isinstance(y, list):
|
||||
y = NDArray(ArrayUtil.array(y))
|
||||
y = np.array(y)
|
||||
if isinstance(z, list):
|
||||
z = NDArray(ArrayUtil.array(z))
|
||||
self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.asarray())
|
||||
z = np.array(z)
|
||||
self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.asarray(), 'linear')
|
||||
|
||||
def __call__(self, x, y):
|
||||
'''
|
||||
@ -121,17 +121,47 @@ class RectBivariateSpline(object):
|
||||
:param y: (*array_like*) Y to evaluate the interpolant at.
|
||||
'''
|
||||
if isinstance(x, list):
|
||||
x = NDArray(ArrayUtil.array(x))
|
||||
if isinstance(x, (NDArray, DimArray)):
|
||||
x = np.array(x)
|
||||
if isinstance(x, NDArray):
|
||||
x = x.asarray()
|
||||
if isinstance(y, list):
|
||||
y = NDArray(ArrayUtil.array(y))
|
||||
if isinstance(y, (NDArray, DimArray)):
|
||||
y = np.array(y)
|
||||
if isinstance(y, NDArray):
|
||||
y = y.asarray()
|
||||
r = InterpUtil.evaluate(self._func, x, y)
|
||||
if isinstance(r, float):
|
||||
return r
|
||||
else:
|
||||
return NDArray(r)
|
||||
|
||||
############################################################
|
||||
|
||||
def linint2(*args, **kwargs):
|
||||
"""
|
||||
Interpolates from a rectilinear grid to another rectilinear grid using bilinear interpolation.
|
||||
|
||||
:param x: (*array_like*) X coordinate array of the sample data (one dimension).
|
||||
:param y: (*array_like*) Y coordinate array of the sample data (one dimension).
|
||||
:param z: (*array_like*) Value array of the sample data (muti-dimension, last two dimensions are y and x).
|
||||
:param xq: (*array_like*) X coordinate array of the query data (one dimension).
|
||||
:param yq: (*array_like*) Y coordinate array of the query data (one dimension).
|
||||
|
||||
:returns: (*array_like*) Interpolated array.
|
||||
"""
|
||||
if len(args) == 3:
|
||||
z = args[0]
|
||||
x = z.dimvalue(z.ndim - 1)
|
||||
y = z.dimvalue(z.ndim - 2)
|
||||
xq = args[1]
|
||||
yq = args[2]
|
||||
else:
|
||||
x = args[0]
|
||||
y = args[1]
|
||||
z = args[2]
|
||||
xq = args[3]
|
||||
yq = args[4]
|
||||
x = np.array(x)._array
|
||||
y = np.array(y)._array
|
||||
z = np.array(z)._array
|
||||
xq = np.array(xq)._array
|
||||
yq = np.array(yq)._array
|
||||
r = ArrayUtil.linint2(z, x, y, xq, yq)
|
||||
return NDArray(r)
|
||||
@ -9,15 +9,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.apache.commons.math3.analysis.BivariateFunction;
|
||||
import org.apache.commons.math3.analysis.UnivariateFunction;
|
||||
import org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.BicubicInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.BivariateGridInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.LinearInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.LoessInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.NevilleInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.UnivariateInterpolator;
|
||||
import org.apache.commons.math3.analysis.interpolation.*;
|
||||
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
|
||||
import org.meteoinfo.geoprocess.GeoComputation;
|
||||
import org.meteoinfo.global.PointD;
|
||||
@ -95,13 +87,22 @@ public class InterpUtil {
|
||||
* @param x X data
|
||||
* @param y Y data
|
||||
* @param z Z data
|
||||
* @param kind Specifies the kind of interpolation as a string.
|
||||
* @return Interpolation function
|
||||
*/
|
||||
public static BivariateFunction getBiInterpFunc(Array x, Array y, Array z) {
|
||||
public static BivariateFunction getBiInterpFunc(Array x, Array y, Array z, String kind) {
|
||||
double[] xd = (double[]) ArrayUtil.copyToNDJavaArray_Double(x);
|
||||
double[] yd = (double[]) ArrayUtil.copyToNDJavaArray_Double(y);
|
||||
double[][] zd = (double[][]) ArrayUtil.copyToNDJavaArray_Double(z);
|
||||
BivariateGridInterpolator li = new BicubicInterpolator();
|
||||
BivariateGridInterpolator li;
|
||||
switch (kind) {
|
||||
case "spline":
|
||||
li = new PiecewiseBicubicSplineInterpolator();
|
||||
break;
|
||||
default:
|
||||
li = new BicubicInterpolator();
|
||||
break;
|
||||
}
|
||||
BivariateFunction func = li.interpolate(xd, yd, zd);
|
||||
|
||||
return func;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user