def cov(m, y=None, rowvar=True, bias=False): ''' Estimate a covariance matrix. :param m: (*array_like*) A 1-D or 2-D array containing multiple variables and observations. :param y: (*array_like*) Optional. An additional set of variables and observations. y has the same form as that of m. :param rowvar: (*boolean*) If ``rowvar`` is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. :param bias: (*boolean*) Default normalization (False) is by (N - 1), where N is the number of observations given (unbiased estimate). If bias is True, then normalization is by N. :returns: Covariance. ''' if isinstance(m, list): m = NDArray(ArrayUtil.array(m)) if rowvar == True and m.ndim == 2: m = m.T if y is None: r = StatsUtil.cov(m.asarray(), not bias) if isinstance(r, Array): return NDArray(r) else: return r else: if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) if rowvar == True and y.ndim == 2: y = y.T r = StatsUtil.cov(m.asarray(), y.asarray(), not bias) return NDArray(r)
def inpolygon(x, y, polygon): ''' Check if x/y points are inside a polygon or not. :param x: (*array_like*) X coordinate of the points. :param y: (*array_like*) Y coordinate of the points. :param polygon: (*PolygonShape list*) The polygon list. :returns: (*boolean array*) Inside or not. ''' if isinstance(x, numbers.Number): return GeoComputation.pointInPolygon(polygon, x, y) if isinstance(x, (list, tuple)): x = minum.array(x) if isinstance(y, (list, tuple)): y = minum.array(y) if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, (list, tuple)): x_p = minum.array(x_p) if isinstance(y_p, (list, tuple)): y_p = minum.array(y_p) return NDArray(ArrayMath.inPolygon(x.array, y.array, x_p.array, y_p.array)) else: if isinstance(polygon, MILayer): polygon = polygon.shapes() elif isinstance(polygon, PolygonShape): polygon = [polygon] return NDArray(ArrayMath.inPolygon(x.array, y.array, polygon))
def qr(a): ''' Compute QR decomposition of a matrix. Calculate the decomposition ``A = Q R`` where Q is unitary/orthogonal and R upper triangular. Parameters ---------- a : (M, N) array_like Matrix to be decomposed Returns ------- Q : float or complex ndarray Of shape (M, M), or (M, K) for ``mode='economic'``. Not returned if ``mode='r'``. R : float or complex ndarray Of shape (M, N), or (K, N) for ``mode='economic'``. ``K = min(M, N)``. ''' r = LinalgUtil.qr(a.asarray()) q = NDArray(r[0]) r = NDArray(r[1]) return q, r
def arrayinpolygon(a, polygon, x=None, y=None): ''' Set array element value as 1 if inside a polygon or set value as -1. :param a: (*array_like*) The array. :param polygon: (*PolygonShape*) The polygon. :param x: (*float*) X coordinate of the point. Default is ``None``, for DimArray :param y: (*float*) Y coordinate of the point. Default is ``None``, for DimArray :returns: (*array_like*) Result array. ''' if isinstance(a, DimArray): if x is None or y is None: x = self.dimvalue(1) y = self.dimvalue(0) if not x is None and not y is None: if isinstance(polygon, tuple): x_p = polygon[0] y_p = polygon[1] if isinstance(x_p, NDArray): x_p = x_p.aslist() if isinstance(y_p, NDArray): y_p = y_p.aslist() return NDArray(ArrayMath.inPolygon(a.asarray(), x.aslist(), y.aslist(), x_p, y_p)) else: if isinstance(polygon, MILayer): polygon = polygon.layer return NDArray(ArrayMath.inPolygon(a.asarray(), x.aslist(), y.aslist(), polygon)) else: return None
def svd(a): ''' Singular Value Decomposition. Factorizes the matrix a into two unitary matrices U and Vh, and a 1-D array s of singular values (real, non-negative) such that ``a == U*S*Vh``, where S is a suitably shaped matrix of zeros with main diagonal s. Parameters ---------- a : (M, N) array_like Matrix to decompose. Returns ------- U : ndarray Unitary matrix having left singular vectors as columns. Of shape ``(M,K)``. s : ndarray The singular values, sorted in non-increasing order. Of shape (K,), with ``K = min(M, N)``. Vh : ndarray Unitary matrix having right singular vectors as rows. Of shape ``(N,N)``. ''' #r = LinalgUtil.svd(a.asarray()) r = LinalgUtil.svd_EJML(a.asarray()) U = NDArray(r[0]) s = NDArray(r[1]) Vh = NDArray(r[2]) return U, s, Vh
def eig(a): ''' Compute the eigenvalues and right eigenvectors of a square array. Parameters ---------- a : (M, M) array Matrices for which the eigenvalues and right eigenvectors will be computed Returns ------- w : (M) array The eigenvalues, each repeated according to its multiplicity. The eigenvalues are not necessarily ordered. The resulting array will be of complex type, unless the imaginary part is zero in which case it will be cast to a real type. When `a` is real the resulting eigenvalues will be real (0 imaginary part) or occur in conjugate pairs v : (M, M) array The normalized (unit "length") eigenvectors, such that the column ``v[:,i]`` is the eigenvector corresponding to the eigenvalue ``w[i]``. ''' r = LinalgUtil.eigen(a.asarray()) #r = LinalgUtil.eigen_EJML(a.asarray()) w = NDArray(r[0]) v = NDArray(r[1]) return w, v
def maskin(data, mask, x=None, y=None): """ Maskin data by polygons - NaN values of elements inside polygons. :param data: (*array_like*) Array data for maskout. :param mask: (*list*) Polygon list as maskin borders. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :returns: (*array_like*) Maskined data array. """ if mask is None: return data elif isinstance(mask, NDArray): r = ArrayMath.maskin(data.array, mask.array) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return NDArray(r) if x is None or y is None: if isinstance(data, DimArray): x = data.dimvalue(data.ndim - 1) y = data.dimvalue(data.ndim - 2) else: return None if not isinstance(mask, (list, ArrayList)): mask = [mask] r = ArrayMath.maskin(data.array, x.array, y.array, mask) if isinstance(data, DimArray): return DimArray(r, data.dims, data.fill_value, data.proj) else: return NDArray(r)
def project(x, y, fromproj=KnownCoordinateSystems.geographic.world.WGS1984, toproj=KnownCoordinateSystems.geographic.world.WGS1984): """ Project geographic coordinates from one projection to another. :param x: (*array_like*) X coordinate values for projection. :param y: (*array_like*) Y coordinate values for projection. :param fromproj: (*ProjectionInfo*) From projection. Default is longlat projection. :param toproj: (*ProjectionInfo*) To projection. Default is longlat projection. :returns: (*array_like*, *array_like*) Projected geographic coordinates. """ if isinstance(fromproj, str): fromproj = ProjectionInfo.factory(fromproj) if isinstance(toproj, str): toproj = ProjectionInfo.factory(toproj) if isinstance(x, (tuple, list)): x = array(x) if isinstance(y, (tuple, list)): y = array(y) if isinstance(x, NDArray): outxy = ArrayUtil.reproject(x.asarray(), y.asarray(), fromproj, toproj) return NDArray(outxy[0]), NDArray(outxy[1]) else: inpt = PointD(x, y) outpt = Reproject.reprojectPoint(inpt, fromproj, toproj) return outpt.X, outpt.Y
def pearsonr(x, y, axis=None): ''' Calculates a Pearson correlation coefficient and the p-value for testing non-correlation. The Pearson correlation coefficient measures the linear relationship between two datasets. Strictly speaking, Pearson’s correlation requires that each dataset be normally distributed, and not necessarily zero-mean. Like other correlation coefficients, this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 imply an exact linear relationship. Positive correlations imply that as x increases, so does y. Negative correlations imply that as x increases, y decreases. The p-value roughly indicates the probability of an uncorrelated system producing datasets that have a Pearson correlation at least as extreme as the one computed from these datasets. The p-values are not entirely reliable but are probably reasonable for datasets larger than 500 or so. :param x: (*array_like*) x data array. :param y: (*array_like*) y data array. :param axis: (*int*) By default, the index is into the flattened array, otherwise along the specified axis. :returns: Pearson’s correlation coefficient and 2-tailed p-value. ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) if axis is None: r = StatsUtil.pearsonr(x.asarray(), y.asarray()) return r[0], r[1] else: r = StatsUtil.pearsonr(x.array, y.array, axis) return NDArray(r[0]), NDArray(r[1])
def __init__(self, x, y, z, kind='linear'): if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) if isinstance(z, list): z = NDArray(ArrayUtil.array(z)) self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.asarray())
def get_data(self): r = self._dataframe.getData() if isinstance(r, Array): r = NDArray(r) else: rr = [] for d in r: rr.append(NDArray(d)) r = rr return r
def rmaskin(data, x, y, mask): """ Maskin data by polygons - the elements inside polygons will be removed :param data: (*array_like*) Array data for maskin. :param x: (*array_like*) X coordinate array. :param y: (*array_like*) Y coordinate array. :param mask: (*list*) Polygon list as mask borders. :returns: (*list*) Masked data, x and y array list. """ if not isinstance(mask, (list, ArrayList)): mask = [mask] r = ArrayMath.maskin_Remove(data.array, x.array, y.array, mask) return NDArray(r[0]), NDArray(r[1]), NDArray(r[2])
def __call__(self, x): ''' Evaluate the interpolate vlaues. :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 = x.asarray() r = InterpUtil.evaluate(self._func, x) if isinstance(r, float): return r else: return NDArray(r)
def predict(func, x): ''' Predict y value using fitting function and x value. :param func: (*Fitting function object*) Fitting function. :param x: (*float*) x value. :returns: (*float*) y value. ''' if isinstance(x, (int, float, long)): return func.predict(x) if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) return NDArray(FittingUtil.predict(x.asarray(), func))
def __init__(self, data=None, index=None, name=None, series=None): ''' One-dimensional array with axis labels (including time series). :param data: (*array_like*) One-dimensional array data. :param index: (*list*) Data index list. Values must be unique and hashable, same length as data. :param name: (*string*) Series name. ''' if series is None: if isinstance(data, (list, tuple)): data = minum.array(data) if index is None: index = range(0, len(data)) else: if len(data) != len(index): raise ValueError('Wrong length of index!') if isinstance(index, (NDArray, DimArray)): index = index.tolist() if isinstance(index, Index): self._index = index else: self._index = Index.factory(index) self._data = data self._series = MISeries(data.array, self._index._index, name) else: self._series = series self._data = NDArray(self._series.getData()) self._index = Index.factory(index=self._series.getIndex())
def randint(low, high=None, size=None): """ Return random integers from low (inclusive) to high (exclusive). Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low). :param low: (*int*) Lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer). :param high: (*int*) If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None). :param size: (*int or tuple*) Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned. :returns: (*int or array*) Random integer array. """ if high is None: high = low low = 0 else: high = high - low if size is None: r = RandomUtil.randint(high) r += low else: r = NDArray(RandomUtil.randint(high, size)) if low != 0: r += low return r
def qair2rh(qair, temp, press=1013.25): """ Specific humidity to relative humidity qair: DimArray or NDArray or number Specific humidity - dimensionless (e.g. kg/kg) ratio of water mass / total air mass temp: DimArray or NDArray or number Temperature - degree c press: DimArray or NDArray or number Pressure - hPa (mb) return: DimArray or NDArray or number Relative humidity - % """ if isinstance(press, NDArray) or isinstance(press, DimArray): p = press.asarray() else: p = press if isinstance(qair, NDArray): r = NDArray(ArrayMath.qair2rh(qair.asarray(), temp.asarray(), p)) if isinstance(qair, DimArray): r = DimArray(r, qair.dims, qair.fill_value, qair.proj) return r else: return MeteoMath.qair2rh(qair, temp, press)
def mlinregress(y, x): ''' Implements ordinary least squares (OLS) to estimate the parameters of a multiple linear regression model. :param y: (*array_like*) Y sample data - one dimension array. :param x: (*array_like*) X sample data - two dimension array. :returns: Estimated regression parameters and residuals. ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) r = StatsUtil.multipleLineRegress_OLS(y.asarray(), x.asarray()) return NDArray(r[0]), NDArray(r[1])
def covariance(x, y, bias=False): ''' Calculate covariance of two array. :param x: (*array_like*) A 1-D array containing multiple variables and observations. :param y: (*array_like*) An additional set of variables and observations. y has the same form as that of x. :param bias: (*boolean*) Default normalization (False) is by (N - 1), where N is the number of observations given (unbiased estimate). If bias is True, then normalization is by N. returns: Covariance ''' if isinstance(x, (list, tuple)): x = NDArray(ArrayUtil.array(x)) if isinstance(y, (list, tuple)): y = NDArray(ArrayUtil.array(y)) r = StatsUtil.covariance(x.asarray(), y.asarray(), bias) return r
def ttest_rel(a, b): ''' Calculates the T-test on TWO RELATED samples of scores, a and b. This is a two-sided test for the null hypothesis that 2 related or repeated samples have identical average (expected) values. :param a: (*array_like*) Sample data a. :param b: (*array_like*) Sample data b. :returns: t-statistic and p-value ''' if isinstance(a, list): a = NDArray(ArrayUtil.array(a)) if isinstance(b, list): b = NDArray(ArrayUtil.array(b)) r = StatsUtil.pairedTTest(a.asarray(), b.asarray()) return r[0], r[1]
def attrvalue(self, key): ''' Get a global attribute value by key. ''' attr = self.dataset.getDataInfo().findGlobalAttribute(key) if attr is None: return None v = NDArray(attr.getValues()) return v
def dimvalue(self, idx, convert=False): ''' Get dimension values. :param idx: (*int*) Dimension index. :param convert: (*boolean*) If convert to real values (i.e. datetime). Default is ``False``. :returns: (*array_like*) Dimension values ''' dim = self.dims[idx] if convert: if dim.getDimType() == DimensionType.T: return miutil.nums2dates(dim.getDimValue()) else: return NDArray(ArrayUtil.array(self.dims[idx].getDimValue())) else: return NDArray(ArrayUtil.array(self.dims[idx].getDimValue()))
def randn(*args): """ Return a sample (or samples) from the “standard normal” distribution. Create an array of the given shape and propagate it with random samples from a "normal" (Gaussian) distribution of mean 0 and variance 1. :param d0, d1, ..., dn: (*int*) optional. The dimensions of the returned array, should all be positive. If no argument is given a single Python float is returned. :returns: Random values array. """ if len(args) == 0: return RandomUtil.randn() elif len(args) == 1: return NDArray(RandomUtil.randn(args[0])) else: return NDArray(RandomUtil.randn(args))
def rand(*args): """ Random values in a given shape. Create an array of the given shape and propagate it with random samples from a uniform distribution over [0, 1). :param d0, d1, ..., dn: (*int*) optional. The dimensions of the returned array, should all be positive. If no argument is given a single Python float is returned. :returns: Random values array. """ if len(args) == 0: return RandomUtil.rand() elif len(args) == 1: return NDArray(RandomUtil.rand(args[0])) else: return NDArray(RandomUtil.rand(args))
def inv(a): ''' Compute the (multiplicative) inverse of a matrix. :param a: (*array_like*) Input array. :returns: Inverse matrix. ''' r = LinalgUtil.inv(a.asarray()) return NDArray(r)
def expfit(x, y, func=False): ''' Exponent fitting. :param x: (*array_like*) x data array. :param y: (*array_like*) y data array. :param func: (*boolean*) Return fit function (for predict function) or not. Default is ``False``. :returns: Fitting parameters and function (optional). ''' if isinstance(x, list): x = NDArray(ArrayUtil.array(x)) if isinstance(y, list): y = NDArray(ArrayUtil.array(y)) r = FittingUtil.expFit(x.asarray(), y.asarray()) if func: return r[0], r[1], r[2], r[3] else: return r[0], r[1], r[2]
def ttest_ind(a, b): ''' Calculates the T-test for the means of TWO INDEPENDENT samples of scores. This is a two-sided test for the null hypothesis that 2 independent samples have identical average (expected) values. This test assumes that the populations have identical variances. :param a: (*array_like*) Sample data a. :param b: (*array_like*) Sample data b. :returns: t-statistic and p-value ''' if isinstance(a, list): a = NDArray(ArrayUtil.array(a)) if isinstance(b, list): b = NDArray(ArrayUtil.array(b)) r = StatsUtil.tTest(a.asarray(), b.asarray()) return r[0], r[1]
def percentile(a, q, axis=None): ''' Compute the qth percentile of the data along the specified axis. :param a: (*array_like*) Input array. :param q: (*float*) float in range of [0,100]. Percentile to compute, which must be between 0 and 100 inclusive. :param axis: (*int*) Axis or axes along which the percentiles are computed. The default is to compute the percentile along a flattened version of the array. :returns: (*float*) qth percentile value. ''' if isinstance(a, list): a = NDArray(ArrayUtil.array(x)) if axis is None: r = StatsUtil.percentile(a.asarray(), q) else: r = StatsUtil.percentile(a.asarray(), q, axis) r = NDArray(r) return r
def uv2ds(u, v): ''' Calculate wind direction and wind speed from U/V. :param u: (*array_like*) U component of wind field. :param v: (*array_like*) V component of wind field. :returns: Wind direction and wind speed. ''' if isinstance(u, NDArray): r = ArrayMath.uv2ds(u.asarray(), v.asarray()) d = NDArray(r[0]) s = NDArray(r[1]) if isinstance(u, DimArray) and isinstance(v, DimArray): d = DimArray(d, u.dims, u.fill_value, u.proj) s = DimArray(s, u.dims, u.fill_value, u.proj) return d, s else: r = ArrayMath.uv2ds(u, v) return r[0], r[1]
def ds2uv(d, s): ''' Calculate U/V from wind direction and wind speed. :param d: (*array_like*) Wind direction. :param s: (*array_like*) Wind speed. :returns: Wind U/V. ''' if isinstance(d, NDArray): r = ArrayMath.ds2uv(d.asarray(), s.asarray()) u = NDArray(r[0]) v = NDArray(r[1]) if isinstance(d, DimArray) and isinstance(s, DimArray): u = DimArray(u, d.dims, d.fill_value, d.proj) v = DimArray(v, d.dims, d.fill_value, d.proj) return u, v else: r = ArrayMath.ds2uv(d, s) return r[0], r[1]