Ejemplo n.º 1
0
def describe(obj, stats=None, format=pprint.pformat):
    '''Return the object decription depending on its type.

    Usefull with numpy and cdms variables/axes

    :Params:
        - **obj**: The object to describe.
        - **stats**: If True, include numerical information like min, max, mean, count, ...

    :Return:
        - The object's summary string
    '''
    try:
        import cdms2, MV2, numpy
        from cdms2.avariable import AbstractVariable
        from cdms2.axis import AbstractAxis
        if isinstance(obj, configobj.ConfigObj):
            obj = obj.dict()
        if not isinstance(obj, (cdms2.avariable.AbstractVariable,
                                cdms2.axis.AbstractAxis, numpy.ndarray)):
            return format(obj)
        otype = obj.__class__.__name__  # type(obj)
        sh, sz = MV2.shape(obj), MV2.size(obj)
        mi = ma = av = co = None
        if stats and sz:
            try:
                if hasattr(obj, 'typecode') and obj.typecode() not in ('c', ):
                    mi, ma, av, co = MV2.min(obj), MV2.max(obj), MV2.average(
                        obj), MV2.count(obj)
            except:
                logger.exception('Error getting statistics of object %s',
                                 type(obj))
        if isinstance(obj, AbstractVariable):
            return '%s: %s, shape: (%s), order: %s%s' % (
                otype, obj.id, ','.join(
                    '%s=%s' % (a.id, a.shape[0])
                    for a in obj.getAxisList()), obj.getOrder(),
                stats and ', min: %s, max: %s, avg: %s, count: %s' %
                (mi, ma, av, co) or '')
        elif isinstance(obj, AbstractAxis):
            return '%s: %s, size: %s%s' % (
                otype, obj.id, sz,
                stats and ', min: %s, max: %s, avg: %s, count: %s' %
                (mi, ma, av, co) or '')
        else:  #if isinstance(obj, numpy.ndarray):
            return '%s: shape: %s%s' % (
                otype, sh, stats and ', min: %s, max: %s, avg: %s, count: %s' %
                (mi, ma, av, co) or '')
    except Exception as e:
        logger.exception('Error getting description of object %s', type(obj))
        return '%s (error getting description)' % (type(obj))
Ejemplo n.º 2
0
    def rank_nD(self, data, axis=0):
        if not axis in [0, 1]:
            if not isinstance(axis, str):
                raise 'Ranking error, axis can only be 1 or 2 or name'
            else:
                nms = data.getAxisIds()
                for i in range(len(nms)):
                    nm = nms[i]
                    if axis in nm.split('___'):
                        axis = i
                if not axis in [0, 1]:
                    raise 'Ranking error, axis can only be 1 or 2 or name'

        if axis != 0:
            data = data(order=(str(axis) + '...'))
        a0 = MV2.argsort(data.filled(1.E20), axis=0)
        n = a0.shape[0]
        b = MV2.zeros(a0.shape, MV2.float)
        sh = a0[1].shape
        for i in range(n):
            I = MV2.ones(sh) * i
            c = MV2.array(a0[i].filled(n - 1))
            b = genutil.arrayindexing.set(b, c, I)
        m = data.mask
        if not m is None:
            b = MV2.masked_where(m, b)
        else:
            b = MV2.array(b)
        n = MV2.count(b, 0)
        n.setAxisList(b.getAxisList()[1:])
        b, n = genutil.grower(b, n)
        b = 100. * b / (n - 1)
        b.setAxisList(data.getAxisList())
        if axis != 0:
            st = ''
            for i in range(axis):
                st += str(i + 1)
            st += '0...'
            data = data(order=st)
            b = b(order=st)
        return b
Ejemplo n.º 3
0
    def rank(self, data, axis=0):
        if not axis in [0, 1]:
            if not isinstance(axis, str):
                raise 'Ranking error, axis can only be 1 or 2 or name'
            else:
                nms = data.getAxisIds()
                for i in range(len(nms)):
                    nm = nms[i]
                    if axis in nm.split('___'):
                        axis = i
                if not axis in [0, 1]:
                    raise 'Ranking error, axis can only be 1 or 2 or name'
        if data.ndim > 2:
            raise "Ranking error, array can only be 2D"

        if axis == 1:
            data = MV2.transpose(data)
        a0 = MV2.argsort(data.filled(1.E20), axis=0)
        n = a0.shape[0]
        b = MV2.zeros(a0.shape, MV2.float)
        sh = a0[1].shape
        for i in range(n):
            I = MV2.ones(sh) * i
            c = MV2.array(a0[i].filled(n - 1))
            b = genutil.arrayindexing.set(b, c, I)

        m = data.mask
        if not m is None:
            b = MV2.masked_where(m, b)
        else:
            b = MV2.array(b)
        n = MV2.count(b, 0)
        n.setAxis(0, b.getAxis(1))
        b, n = genutil.grower(b, n)
        b = 100. * b / (n - 1)
        b.setAxisList(data.getAxisList())
        if axis == 1:
            b = MV2.transpose(b)
            data = MV2.transpose(data)
        return b
Ejemplo n.º 4
0
 def _get(self):
     if 'relative' in self.portrait_types.keys():
         d = self.portrait_types['relative']
         vals = d[1]
         real_value = getattr(self, d[0])
         real = self.__get()
         setattr(self, d[0], vals[0])
         a0 = self.__get()
         sh = list(a0.shape)
         sh.insert(0, 1)
         a0 = MV2.reshape(a0, sh)
         for v in vals[1:]:
             setattr(self, d[0], v)
             tmp = self.__get()
             tmp = MV2.reshape(tmp, sh)
             a0 = MV2.concatenate((a0, tmp))
         a0 = MV2.sort(a0, 0).filled()
         real2 = real.filled()
         a0 = MV2.reshape(a0, (a0.shape[0], sh[1] * sh[2]))
         real2 = MV2.reshape(real2, (sh[1] * sh[2], ))
         a0 = MV2.transpose(a0)
         indices = []
         for i in range(len(real2)):
             indices.append(MV2.searchsorted(a0[i], real2[i]))
         indices = MV2.array(indices)
         indices = MV2.reshape(indices, (sh[1], sh[2]))
         if not ((real.mask is None) or (real.mask is MV2.nomask)):
             indices = MV2.masked_where(real.mask, indices)
         a = MV2.masked_equal(a0, 1.e20)
         a = MV2.count(a, 1)
         a = MV2.reshape(a, indices.shape)
         indices = indices / a * 100
         setattr(self, d[0], real_value)
         indices.setAxisList(real.getAxisList())
         ##             print indices.shape
         return indices
     else:
         return self.__get()
Ejemplo n.º 5
0
##   masked_less(x, value) = x masked where x < value
xl = MV2.masked_less(xouter, 160)

## masked_less_equal(x, value)
##   masked_less_equal(x, value) = x masked where x <= value

## masked_not_equal(x, value)
##   masked_not_equal(x, value) = x masked where x != value

## masked_outside(x, v1, v2)
##   x with mask of all values of x that are outside [v1,v2]
xmo = MV2.masked_outside(xouter, 120, 160)

## count(a, axis=None)
##   Count of the non-masked elements in a, or along a certain axis.
xcount = MV2.count(xmo, 0)
xcount2 = MV2.count(xmo, 1)

## masked_where(condition, x, copy=1)
##   Return x as an array masked where condition is true.
##   Also masked where x or condition masked.
xmwhere = MV2.masked_where(
    MV2.logical_and(MV2.greater(xouter, 120), MV2.less(xouter, 160)), xouter)

## maximum(a, b=None)
##   returns maximum element of a single array, or elementwise
maxval = MV2.maximum(xouter)
xmax = MV2.maximum(ud, vd)
xmax = MV2.maximum.reduce(ud)
xmax = MV2.maximum.reduce(vd)
xmax2 = numpy.ma.maximum.reduce(vd.subSlice(), axis=0)
Ejemplo n.º 6
0
b137 = MV2.zeros(137 + 1, typecode=MV2.float32)

f = open('L137.dta')
lines = f.readlines()
f.close()

for i, line in enumerate(lines):
    tmp = line.split()
    a137[i] = float(tmp[0])
    b137[i] = float(tmp[1])

ph137 = a137 + b137 * ps

tmp = MV2.where(ph137 > ph[nlev], 1.e20, ph137)
tmp = MV2.masked_values(tmp, 1.e20)
nlev0 = MV2.count(tmp)

tmp = ph137[0:nlev0]
tmp = tmp[::-1]

nlev2 = nlev + 1 + len(tmp)

ph2 = MV2.zeros(nlev2, typecode=MV2.float)
ph2[0:nlev + 1] = ph[:]
ph2[nlev + 1:] = tmp[:]

a = MV2.zeros(nlev2, typecode=MV2.float)
b = MV2.zeros(nlev2, typecode=MV2.float)

tmp = a137[0:nlev0]
a[nlev + 1:] = tmp[::-1]
Ejemplo n.º 7
0
 def testCount(self):
     xouter = MV2.outerproduct(MV2.arange(5.), [1] * 10)
     masked = MV2.masked_outside(xouter, 1, 3)
     self.assertEqual(MV2.count(masked), 30)
     self.assertTrue(MV2.allequal(MV2.count(masked, 0), 3))
     self.assertTrue((MV2.count(masked, 1) == [0, 10, 10, 10, 0]).all())