Ejemplo n.º 1
0
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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(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 MIArray(r[0]), MIArray(r[1])
Ejemplo n.º 2
0
def pearsonr(x, y):
    '''
    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.
    
    :returns: Pearson’s correlation coefficient and 2-tailed p-value.
    '''
    if isinstance(x, list):
        x = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.pearsonr(x.asarray(), y.asarray())
    return r[0], r[1]
Ejemplo n.º 3
0
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 = MIArray(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 MIArray(r)
        else:
            return r
    else:
        if isinstance(y, list):
            y = MIArray(ArrayUtil.array(y))
        if rowvar == True and y.ndim == 2:
            y = y.T
        r = StatsUtil.cov(m.asarray(), y.asarray(), not bias)
        return MIArray(r)
Ejemplo n.º 4
0
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 = MIArray(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 MIArray(r)
        else:
            return r
    else:
        if isinstance(y, list):
            y = MIArray(ArrayUtil.array(y))
        if rowvar == True and y.ndim == 2:
            y = y.T
        r = StatsUtil.cov(m.asarray(), y.asarray(), not bias)
        return MIArray(r)
Ejemplo n.º 5
0
 def __getitem__(self, key):
     if isinstance(key, (str, unicode)):     
         coldata = self.data.getColumnData(key)
         if coldata.getDataType().isNumeric():
             return MIArray(ArrayUtil.array(coldata.getDataValues()))
         elif coldata.getDataType() == DataTypes.Date:
             vv = coldata.getData()
             r = []
             cal = Calendar.getInstance()
             for v in vv:
                 cal.setTime(v)
                 year = cal.get(Calendar.YEAR)
                 month = cal.get(Calendar.MONTH) + 1
                 day = cal.get(Calendar.DAY_OF_MONTH)
                 hour = cal.get(Calendar.HOUR_OF_DAY)
                 minute = cal.get(Calendar.MINUTE)
                 second = cal.get(Calendar.SECOND)
                 dt = datetime.datetime(year, month, day, hour, minute, second)
                 r.append(dt)
             return r
         else:
             return MIArray(ArrayUtil.array(coldata.getData()))
     else:
         row = key[0]
         col = key[1]
         return self.data.getValue(row, col)
     return None
Ejemplo n.º 6
0
 def astype(self, dtype):
     if dtype == 'int' or dtype is int:
         r = MIArray(ArrayUtil.toInteger(self.array))
     elif dtype == 'float' or dtype is float:
         r = MIArray(ArrayUtil.toFloat(self.array))
     else:
         r = self
     return r
Ejemplo n.º 7
0
 def __init__(self, x, y, z, kind='linear'):
     if isinstance(x, list):
         x = MIArray(ArrayUtil.array(x))
     if isinstance(y, list):
         y = MIArray(ArrayUtil.array(y))
     if isinstance(z, list):
         z = MIArray(ArrayUtil.array(z))
     self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(), z.asarray())
Ejemplo n.º 8
0
 def astype(self, dtype):
     if dtype == 'int' or dtype is int:
         r = MIArray(ArrayUtil.toInteger(self.array))
     elif dtype == 'float' or dtype is float:
         r = MIArray(ArrayUtil.toFloat(self.array))
     else:
         r = self
     return r
Ejemplo n.º 9
0
 def __init__(self, x, y, z):
     if isinstance(x, list):
         x = MIArray(ArrayUtil.array(x))
     if isinstance(y, list):
         y = MIArray(ArrayUtil.array(y))
     if isinstance(z, list):
         z = MIArray(ArrayUtil.array(z))
     self._func = InterpUtil.getBiInterpFunc(x.asarray(), y.asarray(),
                                             z.asarray())
Ejemplo n.º 10
0
def binwrite(fn, data, byteorder='little_endian', append=False):
    """
    Write array data into a binary data file.
    
    :param fn: (*string*) Path needed to locate binary file.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``.
    :param append: (*boolean*) Append to an existing file or not.
    """
    ArrayUtil.saveBinFile(fn, data.asarray(), byteorder, append)
Ejemplo n.º 11
0
def asciiwrite(fn, data, colnum=80, format=None, delimiter=None):
    """
    Write array data into a ASCII data file.
    
    :param fn: (*string*) Path needed to locate ASCII file.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param colnum: (*int*) Column number of each line.
    :param format: (*string*) Number format.
    :param delimiter: (*string*) Delimiter string.    
    """
    ArrayUtil.saveASCIIFile(fn, data.asarray(), colnum, format, delimiter)
Ejemplo n.º 12
0
def binwrite(fn, data, byteorder='little_endian', append=False, sequential=False):
    """
    Create a binary data file from an array variable.
    
    :param fn: (*string*) Path needed to locate binary file.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``.
    :param append: (*boolean*) Append to an existing file or not.
    :param sequential: (*boolean*) If write binary data as sequential - Fortran
    """
    ArrayUtil.saveBinFile(fn, data.asarray(), byteorder, append, sequential)  
Ejemplo n.º 13
0
def asciiwrite(fn, data, colnum=80, format=None, delimiter=None):
    """
    Write array data into a ASCII data file.
    
    :param fn: (*string*) Path needed to locate ASCII file.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param colnum: (*int*) Column number of each line.
    :param format: (*string*) Number format.
    :param delimiter: (*string*) Delimiter string.    
    """
    ArrayUtil.saveASCIIFile(fn, data.asarray(), colnum, format, delimiter)  
Ejemplo n.º 14
0
    def project(self, x=None, y=None, toproj=None, method='bilinear'):
        """
        Project array
        
        :param x: To x coordinates.
        :param y: To y coordinates.
        :param toproj: To projection.
        :param method: Interpolation method: ``bilinear`` or ``neareast`` .
        
        :returns: (*MIArray*) Projected array
        """
        yy = self.dims[self.ndim - 2].getDimValue()
        xx = self.dims[self.ndim - 1].getDimValue()
        if toproj is None:
            toproj = self.proj

        if x is None or y is None:
            pr = ArrayUtil.reproject(self.array.array, xx, yy, self.proj,
                                     toproj)
            r = pr[0]
            x = pr[1]
            y = pr[2]
            dims = []
            ydim = Dimension()
            ydim.setDimValues(MIArray(y).aslist())
            dims.append(ydim)
            xdim = Dimension()
            xdim.setDimValues(MIArray(x).aslist())
            dims.append(xdim)
            rr = DimArray(MIArray(r), dims, self.fill_value, toproj)
            return rr

        if method == 'bilinear':
            method = ResampleMethods.Bilinear
        else:
            method = ResampleMethods.NearestNeighbor
        if isinstance(x, list):
            r = ArrayUtil.reproject(self.array.array, xx, yy, x, y, self.proj,
                                    toproj, self.fill_value, method)
        elif isinstance(x, MIArray):
            if x.rank == 1:
                r = ArrayUtil.reproject(self.array.array, xx, yy, x.aslist(),
                                        y.aslist(), self.proj, toproj,
                                        self.fill_value, method)
            else:
                r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(),
                                        y.asarray(), self.proj, toproj,
                                        self.fill_value, method)
        else:
            r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(),
                                    y.asarray(), self.proj, toproj,
                                    self.fill_value, method)
        #r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(), y.asarray(), self.proj, toproj, self.fill_value, method)
        return MIArray(r)
Ejemplo n.º 15
0
def binwrite(fn, data, byteorder='little_endian', append=False, sequential=False):
    """
    Create a binary data file from an array variable.
    
    :param fn: (*string*) Path needed to locate binary file.
    :param data: (*array_like*) A numeric array variable of any dimensionality.
    :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``.
    :param append: (*boolean*) Append to an existing file or not.
    :param sequential: (*boolean*) If write binary data as sequential - Fortran
    """
    ArrayUtil.saveBinFile(fn, data.asarray(), byteorder, append, sequential)  
Ejemplo n.º 16
0
 def tostation(self, x, y):
     gdata = self.asgriddata()
     if isinstance(x, MIArray) or isinstance(x, DimArray):
         r = gdata.data.toStation(x.aslist(), y.aslist())
         return MIArray(ArrayUtil.array(r))
     else:
         return gdata.data.toStation(x, y)
Ejemplo n.º 17
0
 def tostation(self, x, y):
     gdata = self.asgriddata()
     if isinstance(x, MIArray) or isinstance(x, DimArray):
         r = gdata.data.toStation(x.aslist(), y.aslist())
         return MIArray(ArrayUtil.array(r))
     else:
         return gdata.data.toStation(x, y)
Ejemplo n.º 18
0
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(fromproj)
    if isinstance(toproj, str):
        toproj = ProjectionInfo(toproj)
    if isinstance(x, (tuple, list)):
        x = array(x)
    if isinstance(y, (tuple, list)):
        y = array(y)
    if isinstance(x, MIArray):
        outxy = ArrayUtil.reproject(x.asarray(), y.asarray(), fromproj, toproj)
        return MIArray(outxy[0]), MIArray(outxy[1])
    else:
        inpt = PointD(x, y)
        outpt = Reproject.reprojectPoint(inpt, fromproj, toproj)
        return outpt.X, outpt.Y
Ejemplo n.º 19
0
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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.multipleLineRegress_OLS(y.asarray(), x.asarray())
    return MIArray(r[0]), MIArray(r[1])
Ejemplo n.º 20
0
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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.multipleLineRegress_OLS(y.asarray(), x.asarray())
    return MIArray(r[0]), MIArray(r[1])
Ejemplo n.º 21
0
 def astype(self, dtype):
     '''
     Convert to another data type.
     
     :param dtype: (*string*) Data type.
     
     :returns: (*array*) Converted array.
     '''
     if dtype == 'int' or dtype is int:
         r = MIArray(ArrayUtil.toInteger(self.array))
     elif dtype == 'float' or dtype is float:
         r = MIArray(ArrayUtil.toFloat(self.array))
     elif dtype == 'boolean' or dtype is bool:
         r = MIArray(ArrayUtil.toBoolean(self.array))
     else:
         r = self
     return r
Ejemplo n.º 22
0
 def project(self, x=None, y=None, toproj=None, method='bilinear'):
     """
     Project array
     
     :param x: To x coordinates.
     :param y: To y coordinates.
     :param toproj: To projection.
     :param method: Interpolation method: ``bilinear`` or ``neareast`` .
     
     :returns: (*MIArray*) Projected array
     """
     yy = self.dims[self.ndim - 2].getDimValue()
     xx = self.dims[self.ndim - 1].getDimValue()
     if toproj is None:
         toproj = self.proj
     
     if x is None or y is None:
         pr = ArrayUtil.reproject(self.array.array, xx, yy, self.proj, toproj)
         r = pr[0]
         x = pr[1]
         y = pr[2]
         dims = []
         ydim = Dimension()
         ydim.setDimValues(MIArray(y).aslist())
         dims.append(ydim)
         xdim = Dimension()
         xdim.setDimValues(MIArray(x).aslist())    
         dims.append(xdim)
         rr = DimArray(MIArray(r), dims, self.fill_value, toproj)
         return rr
     
     if method == 'bilinear':
         method = ResampleMethods.Bilinear
     else:
         method = ResampleMethods.NearestNeighbor
     if isinstance(x, list):
         r = ArrayUtil.reproject(self.array.array, xx, yy, x, y, self.proj, toproj, self.fill_value, method)
     elif isinstance(x, MIArray):
         if x.rank == 1:
             r = ArrayUtil.reproject(self.array.array, xx, yy, x.aslist(), y.aslist(), self.proj, toproj, self.fill_value, method)
         else:
             r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(), y.asarray(), self.proj, toproj, self.fill_value, method)
     else:
         r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(), y.asarray(), self.proj, toproj, self.fill_value, method)
     #r = ArrayUtil.reproject(self.array.array, xx, yy, x.asarray(), y.asarray(), self.proj, toproj, self.fill_value, method)
     return MIArray(r)
Ejemplo n.º 23
0
 def astype(self, dtype):
     '''
     Convert to another data type.
     
     :param dtype: (*string*) Data type.
     
     :returns: (*array*) Converted array.
     '''
     if dtype == 'int' or dtype is int:
         r = MIArray(ArrayUtil.toInteger(self.array))
     elif dtype == 'float' or dtype is float:
         r = MIArray(ArrayUtil.toFloat(self.array))
     elif dtype == 'boolean' or dtype == 'bool' or dtype is bool:
         r = MIArray(ArrayUtil.toBoolean(self.array))
     else:
         r = self
     return r
Ejemplo n.º 24
0
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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, (list, tuple)):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.covariance(x.asarray(), y.asarray(), bias)
    return r
Ejemplo n.º 25
0
 def repeat(self, repeats, axis=None):
     '''
     Repeat elements of an array.
     
     :param repeats: (*int or list of ints*) The number of repetitions for each 
         element. repeats is broadcasted to fit the shape of the given axis.
     :param axis: (*int*) The axis along which to repeat values. By default, use 
         the flattened input array, and return a flat output array.
     
     :returns: (*array_like*) Repeated array.
     '''
     if isinstance(repeats, int):
         repeats = [repeats]
     if axis is None:
         r = ArrayUtil.repeat(self.array, repeats)
     else:
         r = ArrayUtil.repeat(self.array, repeats, axis)
     return MIArray(r)
Ejemplo n.º 26
0
 def repeat(self, repeats, axis=None):
     '''
     Repeat elements of an array.
     
     :param repeats: (*int or list of ints*) The number of repetitions for each 
         element. repeats is broadcasted to fit the shape of the given axis.
     :param axis: (*int*) The axis along which to repeat values. By default, use 
         the flattened input array, and return a flat output array.
     
     :returns: (*array_like*) Repeated array.
     '''
     if isinstance(repeats, int):
         repeats = [repeats]
     if axis is None:
         r = ArrayUtil.repeat(self.array, repeats)
     else:
         r = ArrayUtil.repeat(self.array, repeats, axis)
     return MIArray(r)
Ejemplo n.º 27
0
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 = MIArray(ArrayUtil.array(a))
    if isinstance(b, list):
        b = MIArray(ArrayUtil.array(b))
    r = StatsUtil.pairedTTest(a.asarray(), b.asarray())
    return r[0], r[1]
Ejemplo n.º 28
0
 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 MIArray(ArrayUtil.array(self.dims[idx].getDimValue()))
     else:
         return MIArray(ArrayUtil.array(self.dims[idx].getDimValue()))
Ejemplo n.º 29
0
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 = MIArray(ArrayUtil.array(a))
    if isinstance(b, list):
        b = MIArray(ArrayUtil.array(b))
    r = StatsUtil.pairedTTest(a.asarray(), b.asarray())
    return r[0], r[1]
Ejemplo n.º 30
0
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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, (list, tuple)):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.covariance(x.asarray(), y.asarray(), bias)
    return r
Ejemplo n.º 31
0
 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()))
Ejemplo n.º 32
0
    def tojarray(self, dtype=None):
        '''
        Convert to java array.

        :param dtype: (*string*) Data type ['double','long',None]. 
        
        :returns: (*java array*) Java array.
        '''
        r = ArrayUtil.copyToNDJavaArray(self.array, dtype)
        return r
Ejemplo n.º 33
0
def numasciirow(filename):
    '''
    Returns the number of rows in an ASCII file.
    
    :param filename: (*string*) The ASCII file name.
    
    :returns: The number of rows in the file.
    '''
    nrow = ArrayUtil.numASCIIRow(filename)
    return nrow
Ejemplo n.º 34
0
def polyfit(x, y, degree, func=False):
    '''
    Polynomail 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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = FittingUtil.polyFit(x.asarray(), y.asarray(), degree)
    if func:
        return r[0], r[1], r[2]
    else:
        return r[0], r[1]
Ejemplo n.º 35
0
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 = MIArray(ArrayUtil.array(a))
    if isinstance(b, list):
        b = MIArray(ArrayUtil.array(b))
    r = StatsUtil.tTest(a.asarray(), b.asarray())
    return r[0], r[1]
Ejemplo n.º 36
0
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 = MIArray(ArrayUtil.array(a))
    if isinstance(b, list):
        b = MIArray(ArrayUtil.array(b))
    r = StatsUtil.tTest(a.asarray(), b.asarray())
    return r[0], r[1]
Ejemplo n.º 37
0
    def tojarray(self, dtype=None):
        '''
        Convert to java array.

        :param dtype: (*string*) Data type ['double','long',None]. 
        
        :returns: (*java array*) Java array.
        '''
        r = ArrayUtil.copyToNDJavaArray(self.array, dtype)
        return r
Ejemplo n.º 38
0
def numasciirow(filename):
    '''
    Returns the number of rows in an ASCII file.
    
    :param filename: (*string*) The ASCII file name.
    
    :returns: The number of rows in the file.
    '''
    nrow = ArrayUtil.numASCIIRow(filename)
    return nrow
Ejemplo n.º 39
0
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]
Ejemplo n.º 40
0
 def griddata(self, xi=None, **kwargs):
     method = kwargs.pop('method', 'idw')
     fill_value = self.data.missingValue
     x_s = MIArray(ArrayUtil.array(self.data.getXList()))
     y_s = MIArray(ArrayUtil.array(self.data.getYList()))
     if xi is None:
         xn = int(math.sqrt(len(x_s)))
         yn = xn
         x_g = MIArray(ArrayUtil.lineSpace(x_s.min(), x_s.max(), xn, True))
         y_g = MIArray(ArrayUtil.lineSpace(y_s.min(), y_s.max(), yn, True))
     else:
         x_g = xi[0]
         y_g = xi[1]
     if isinstance(x_s, MIArray):
         x_s = x_s.aslist()
     if isinstance(y_s, MIArray):
         y_s = y_s.aslist()
     if isinstance(x_g, MIArray):
         x_g = x_g.aslist()
     if isinstance(y_g, MIArray):
         y_g = y_g.aslist()
     if method == 'idw':
         pnum = kwargs.pop('pointnum', 2)
         radius = kwargs.pop('radius', None)
         if radius is None:
             r = self.data.interpolate_Neighbor(x_g, y_g, pnum, fill_value)
             return PyGridData(r)
         else:
             r = self.data.interpolate_Radius(x_g, y_g, pnum, radius,
                                              fill_value)
             return PyGridData(r)
     elif method == 'cressman':
         radius = kwargs.pop('radius', [10, 7, 4, 2, 1])
         if isinstance(radius, MIArray):
             radius = radius.aslist()
         r = self.data.interpolate_Cressman(x_g, y_g, radius, fill_value)
         return PyGridData(r)
     elif method == 'neareast':
         r = self.data.interpolate_Assign(x_g, y_g, fill_value)
         return PyGridData(r)
     else:
         return None
Ejemplo n.º 41
0
 def __call__(self, x, y):
     '''
     Evaluate the interpolate vlaues.
     
     :param x: (*array_like*) X to evaluate the interpolant at.
     :param y: (*array_like*) Y to evaluate the interpolant at.
     '''
     if isinstance(x, list):
         x = MIArray(ArrayUtil.array(x))
     if isinstance(x, (MIArray, DimArray)):
         x = x.asarray()
     if isinstance(y, list):
         y = MIArray(ArrayUtil.array(y))
     if isinstance(y, (MIArray, DimArray)):
         y = y.asarray()
     r = InterpUtil.evaluate(self._func, x, y)
     if isinstance(r, float):
         return r
     else:
         return MIArray(r)
Ejemplo n.º 42
0
 def __call__(self, x, y):
     '''
     Evaluate the interpolate vlaues.
     
     :param x: (*array_like*) X to evaluate the interpolant at.
     :param y: (*array_like*) Y to evaluate the interpolant at.
     '''
     if isinstance(x, list):
         x = MIArray(ArrayUtil.array(x))
     if isinstance(x, (MIArray, DimArray)):
         x = x.asarray()
     if isinstance(y, list):
         y = MIArray(ArrayUtil.array(y))
     if isinstance(y, (MIArray, DimArray)):
         y = y.asarray()
     r = InterpUtil.evaluate(self._func, x, y)
     if isinstance(r, float):
         return r
     else:
         return MIArray(r)
Ejemplo n.º 43
0
def linregress(x, y, outvdn=False):
    '''
    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.
    :param outvdn: (*boolean*) Output validate data number or not. Default is False.
    
    :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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = ArrayMath.lineRegress(x.asarray(), y.asarray())
    if outvdn:
        return r[0], r[1], r[2], r[3], r[4], r[5]
    else:
        return r[0], r[1], r[2], r[3], r[4]
Ejemplo n.º 44
0
def convexhull(*args):
    if len(args) == 1:
        a = args[0]
        ap = asshape(a)
        r = ap.convexHull()
        return r
    else:
        x = args[0]
        y = args[1]
        r = ArrayUtil.convexHull(x.asarray(), y.asarray())
        return r
Ejemplo n.º 45
0
def linregress(x, y, outvdn=False):
    '''
    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.
    :param outvdn: (*boolean*) Output validate data number or not. Default is False.
    
    :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 = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = ArrayMath.lineRegress(x.asarray(), y.asarray())
    if outvdn:
        return r[0], r[1], r[2], r[3], r[4], r[5]
    else:
        return r[0], r[1], r[2], r[3], r[4]
Ejemplo n.º 46
0
def kendalltau(x, y):
    '''
    Calculates Kendall's tau, a correlation measure for ordinal data.
    
    Kendall's tau is a measure of the correspondence between two rankings.
    Values close to 1 indicate strong agreement, values close to -1 indicate
    strong disagreement.  This is the 1945 "tau-b" version of Kendall's
    tau [2]_, which can account for ties and which reduces to the 1938 "tau-a"
    version [1]_ in absence of ties.
    
    :param x: (*array_like*) x data array.
    :param y: (*array_like*) y data array.
    
    :returns: Correlation.
    
    Notes
    -----
    The definition of Kendall's tau that is used is [2]_::
      tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U))
    where P is the number of concordant pairs, Q the number of discordant
    pairs, T the number of ties only in `x`, and U the number of ties only in
    `y`.  If a tie occurs for the same pair in both `x` and `y`, it is not
    added to either T or U.
    References
    ----------
    .. [1] Maurice G. Kendall, "A New Measure of Rank Correlation", Biometrika
           Vol. 30, No. 1/2, pp. 81-93, 1938.
    .. [2] Maurice G. Kendall, "The treatment of ties in ranking problems",
           Biometrika Vol. 33, No. 3, pp. 239-251. 1945.
    .. [3] Gottfried E. Noether, "Elements of Nonparametric Statistics", John
           Wiley & Sons, 1967.
    .. [4] Peter M. Fenwick, "A new data structure for cumulative frequency
           tables", Software: Practice and Experience, Vol. 24, No. 3,
           pp. 327-336, 1994.
    '''
    if isinstance(x, list):
        x = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.kendalltau(x.asarray(), y.asarray())
    return r
Ejemplo n.º 47
0
def kendalltau(x, y):
    '''
    Calculates Kendall's tau, a correlation measure for ordinal data.
    
    Kendall's tau is a measure of the correspondence between two rankings.
    Values close to 1 indicate strong agreement, values close to -1 indicate
    strong disagreement.  This is the 1945 "tau-b" version of Kendall's
    tau [2]_, which can account for ties and which reduces to the 1938 "tau-a"
    version [1]_ in absence of ties.
    
    :param x: (*array_like*) x data array.
    :param y: (*array_like*) y data array.
    
    :returns: Correlation.
    
    Notes
    -----
    The definition of Kendall's tau that is used is [2]_::
      tau = (P - Q) / sqrt((P + Q + T) * (P + Q + U))
    where P is the number of concordant pairs, Q the number of discordant
    pairs, T the number of ties only in `x`, and U the number of ties only in
    `y`.  If a tie occurs for the same pair in both `x` and `y`, it is not
    added to either T or U.
    References
    ----------
    .. [1] Maurice G. Kendall, "A New Measure of Rank Correlation", Biometrika
           Vol. 30, No. 1/2, pp. 81-93, 1938.
    .. [2] Maurice G. Kendall, "The treatment of ties in ranking problems",
           Biometrika Vol. 33, No. 3, pp. 239-251. 1945.
    .. [3] Gottfried E. Noether, "Elements of Nonparametric Statistics", John
           Wiley & Sons, 1967.
    .. [4] Peter M. Fenwick, "A new data structure for cumulative frequency
           tables", Software: Practice and Experience, Vol. 24, No. 3,
           pp. 327-336, 1994.
    '''
    if isinstance(x, list):
        x = MIArray(ArrayUtil.array(x))
    if isinstance(y, list):
        y = MIArray(ArrayUtil.array(y))
    r = StatsUtil.kendalltau(x.asarray(), y.asarray())
    return r
Ejemplo n.º 48
0
 def griddata(self, xi=None, **kwargs):
     method = kwargs.pop('method', 'idw')
     fill_value = self.data.missingValue
     x_s = MIArray(ArrayUtil.array(self.data.getXList()))
     y_s = MIArray(ArrayUtil.array(self.data.getYList()))
     if xi is None:            
         xn = int(math.sqrt(len(x_s)))
         yn = xn
         x_g = MIArray(ArrayUtil.lineSpace(x_s.min(), x_s.max(), xn, True))
         y_g = MIArray(ArrayUtil.lineSpace(y_s.min(), y_s.max(), yn, True))     
     else:
         x_g = xi[0]
         y_g = xi[1]
     if isinstance(x_s, MIArray):
         x_s = x_s.aslist()
     if isinstance(y_s, MIArray):
         y_s = y_s.aslist()    
     if isinstance(x_g, MIArray):
         x_g = x_g.aslist()
     if isinstance(y_g, MIArray):
         y_g = y_g.aslist()
     if method == 'idw':
         pnum = kwargs.pop('pointnum', 2)
         radius = kwargs.pop('radius', None)
         if radius is None:
             r = self.data.interpolate_Neighbor(x_g, y_g, pnum, fill_value)
             return PyGridData(r)
         else:
             r = self.data.interpolate_Radius(x_g, y_g, pnum, radius, fill_value)
             return PyGridData(r)
     elif method == 'cressman':
         radius = kwargs.pop('radius', [10, 7, 4, 2, 1])
         if isinstance(radius, MIArray):
             radius = radius.aslist()
         r = self.data.interpolate_Cressman(x_g, y_g, radius, fill_value)
         return PyGridData(r)
     elif method == 'neareast':
         r = self.data.interpolate_Assign(x_g, y_g, fill_value)
         return PyGridData(r)
     else:
         return None
Ejemplo n.º 49
0
def convexhull(*args):
    if len(args) == 1:
        a = args[0]
        ap = asshape(a)
        cp = ap.convexHull()
        c = Graphic(cp, a.getLegend())
        return c
    else:
        x = args[0]
        y = args[1]
        r = ArrayUtil.convexHull(x.asarray(), y.asarray())
        return r
Ejemplo n.º 50
0
def convexhull(*args):
    if len(args) == 1:
        a = args[0]
        ap = asshape(a)
        cp = ap.convexHull()
        c = Graphic(cp, a.getLegend())
        return c
    else:
        x = args[0]
        y = args[1]
        r = ArrayUtil.convexHull(x.asarray(), y.asarray())
        return r
Ejemplo n.º 51
0
def reproject(a, x=None, y=None, fromproj=None, xp=None, yp=None, toproj=None, method='bilinear'):
    """
    Project array
    
    :param a: (*array_like*) Input array.
    :param x: (*array_like*) Input x coordinates.
    :param y: (*array_like*) Input y coordinates.
    :param fromproj: (*ProjectionInfo*) Input projection.
    :param xp: (*array_like*) Projected x coordinates.
    :param yp: (*array_like*) Projected y coordinates.
    :param toproj: (*ProjectionInfo*) Output projection.
    :param method: Interpolation method: ``bilinear`` or ``neareast`` .
    
    :returns: (*NDArray*) Projected array
    """
    if isinstance(a, DimArray):
        y = a.dimvalue(a.ndim - 2)
        x = a.dimvalue(a.ndim - 1)
        if fromproj is None:
            fromproj = a.proj
            
    if toproj is None:
        toproj = KnownCoordinateSystems.geographic.world.WGS1984
    
    if xp is None or yp is None:
        pr = ArrayUtil.reproject(a.asarray(), x.aslist(), y.aslist(), fromproj, toproj)
        r = pr[0]        
        return NDArray(r)
    
    if method == 'bilinear':
        method = ResampleMethods.Bilinear
    else:
        method = ResampleMethods.NearestNeighbor
    if isinstance(xp, (list, tuple)):
        xp = NDArray(xp)
    if isinstance(yp, (list, tuple)):
        yp = NDArray(yp)
    xp, yp = ArrayUtil.meshgrid(xp.asarray(), yp.asarray())
    r = ArrayUtil.reproject(a.asarray(), x.aslist(), y.aslist(), xp, yp, fromproj, toproj, method)
    return NDArray(r)    
Ejemplo n.º 52
0
 def coldata(self, key):
     '''
     Return column data as one dimension array.
     
     :param key: (*string*) Column name.
     
     :returns: (*MIArray*) Colomn data.
     '''
     if isinstance(key, str):
         print key     
         values = self.data.getColumnData(key).getDataValues()
         return MIArray(ArrayUtil.array(values))
     return None
Ejemplo n.º 53
0
def numasciicol(filename, delimiter=None, headerlines=0):
    '''
    Returns the number of columns in an ASCII file.
    
    :param filename: (*string*) The ASCII file name.
    :param delimiter: (*string*) Field delimiter character. Default is ``None``, means space or tab 
        delimiter.
    :param headerlines: (*int*) Lines to skip at beginning of the file. Default is ``0``.
    
    :returns: The number of columns in the file.
    '''
    ncol = ArrayUtil.numASCIICol(filename, delimiter, headerlines)
    return ncol        
Ejemplo n.º 54
0
 def coldata(self, key):
     '''
     Return column data as one dimension array.
     
     :param key: (*string*) Column name.
     
     :returns: (*MIArray*) Colomn data.
     '''
     if isinstance(key, str):
         print key
         values = self.data.getColumnData(key).getDataValues()
         return MIArray(ArrayUtil.array(values))
     return None
Ejemplo n.º 55
0
def chisquare(f_obs, f_exp=None):
    '''
    Calculates a one-way chi square test.

    The chi square test tests the null hypothesis that the categorical data has the 
    given frequencies.
    
    :param f_obs: (*array_like*) Observed frequencies in each category.
    :param f_exp: (*array_like*) Expected frequencies in each category. By default the categories 
        are assumed to be equally likely.
    
    :returns: Chi-square statistic and p-value
    '''
    if isinstance(f_obs, list):
        f_obs = MIArray(ArrayUtil.array(f_obs))
    if f_exp is None:
        n = len(f_obs)
        f_exp = minum.ones(n) / n * f_obs.sum()
    elif isinstance(f_exp, list):
        f_exp = MIArray(ArrayUtil.array(f_exp))
    r = StatsUtil.chiSquareTest(f_exp.asarray(), f_obs.asarray())
    return r[0], r[1]
Ejemplo n.º 56
0
def convexhull(*args):
    '''
    Computes the smallest convex Polygon that contains all the points in the Geometry.
    '''
    if len(args) == 1:
        a = args[0]
        ap = asshape(a)
        r = ap.convexHull()
        return r
    else:
        x = args[0]
        y = args[1]
        r = ArrayUtil.convexHull(x.asarray(), y.asarray())
        return r    
Ejemplo n.º 57
0
def spearmanr(m, y=None, axis=0):
    '''
    Calculates a Spearman rank-order correlation coefficient.
    
    The Spearman correlation is a nonparametric measure of the monotonicity of the relationship 
    between two datasets. Unlike the Pearson correlation, the Spearman correlation does not 
    assume that both datasets are normally distributed. Like other correlation coefficients, 
    this one varies between -1 and +1 with 0 implying no correlation. Correlations of -1 or +1 
    imply an exact monotonic relationship. Positive correlations imply that as x increases, so 
    does y. Negative correlations imply that as x increases, y decreases.
    
    :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 axis: (*int*) If axis=0 (default), then each column represents a variable, with 
        observations in the rows. If axis=1, the relationship is transposed: each row represents 
        a variable, while the columns contain observations..
    
    :returns: Spearman correlation matrix.
    '''
    if isinstance(m, list):
        m = MIArray(ArrayUtil.array(m))
    if axis == 1 and m.ndim == 2:
        m = m.T
    if y is None:        
        r = StatsUtil.spearmanr(m.asarray())
        if isinstance(r, Array):
            return MIArray(r)
        else:
            return r
    else:
        if isinstance(y, list):
            y = MIArray(ArrayUtil.array(y))
        if axis == 1 and y.ndim == 2:
            y = y.T
        r = StatsUtil.spearmanr(m.asarray(), y.asarray())
        return MIArray(r)
Ejemplo n.º 58
0
def binread(fn, dim, datatype=None, skip=0, byteorder='little_endian'):
    """
    Read data array from a binary file.
    
    :param fn: (*string*) The binary file name for data reading. 
    :param dim: (*list*) Dimensions.
    :param datatype: (*string*) Data type string [byte | short | int | float | double].
    :param skip: (*int*) Skip bytes number.
    :param byteorder: (*string*) Byte order. ``little_endian`` or ``big_endian``.
    
    :returns: (*MIArray*) Data array
    """
    if not os.path.exists(fn):
        raise IOError('No such file: ' + fn)
    r = ArrayUtil.readBinFile(fn, dim, datatype, skip, byteorder);
    return MIArray(r)
Ejemplo n.º 59
0
def ttest_1samp(a, popmean):
    '''
    Calculate the T-test for the mean of ONE group of scores.

    This is a two-sided test for the null hypothesis that the expected value (mean) of 
    a sample of independent observations a is equal to the given population mean, popmean.
    
    :param a: (*array_like*) Sample observation.
    :param popmean: (*float*) Expected value in null hypothesis.
    
    :returns: t-statistic and p-value
    '''
    if isinstance(a, list):
        a = MIArray(ArrayUtil.array(x))
    r = StatsUtil.tTest(a.asarray(), popmean)
    return r[0], r[1]
Ejemplo n.º 60
0
def chi2_contingency(observed):
    '''
    Chi-square test of independence of variables in a contingency table.

    This function computes the chi-square statistic and p-value for the hypothesis test of 
    independence of the observed frequencies in the contingency table observed.
    
    :param observed: (*array_like*) The contingency table. The table contains the observed 
        frequencies (i.e. number of occurrences) in each category. In the two-dimensional case, 
        the table is often described as an `R x C table`.
    
    :returns: Chi-square statistic and p-value
    '''
    if isinstance(observed, list):
        observed = MIArray(ArrayUtil.array(observed))
    r = StatsUtil.chiSquareTest(observed.asarray())
    return r[0], r[1]