コード例 #1
0
ファイル: strings.py プロジェクト: fxia22/ASM_xf
    def fasteval(self, type=_na.Float64):

        """fasteval(self, type=Float64) converts CharArray 'self' into
        a NumArray of the specified type.  fasteval() can't convert
        complex arrays at all, and loses precision when converting
        UInt64 or Int64.

        >>> array([["1","2"],["3","4"]]).fasteval().astype('Long')
        array([[1, 2],
               [3, 4]])
        >>> try:
        ...    array([["1","2"],["3","other"]]).fasteval()
        ... except _chararray.error:
        ...    pass
        """
        n = _na.array(shape=self._shape, type=_na.Float64)
        type = _nt.getType(type)
        _chararray.Eval((), self, n);
        if type != _na.Float64:
	    if ((type is _na.Int64) or 
		(_numinclude.hasUInt64 and type is _na.UInt64)):
                warnings.warn("Loss of precision converting to 64-bit type.  Consider using eval().", PrecisionWarning)
            return n.astype(type)
        else:
            return n
コード例 #2
0
ファイル: strings.py プロジェクト: joshfermin/AI
    def fasteval(self, type=_na.Float64):
        """fasteval(self, type=Float64) converts CharArray 'self' into
        a NumArray of the specified type.  fasteval() can't convert
        complex arrays at all, and loses precision when converting
        UInt64 or Int64.

        >>> array([["1","2"],["3","4"]]).fasteval().astype('Long')
        array([[1, 2],
               [3, 4]])
        >>> try:
        ...    array([["1","2"],["3","other"]]).fasteval()
        ... except _chararray.error:
        ...    pass
        """
        n = _na.array(shape=self._shape, type=_na.Float64)
        type = _nt.getType(type)
        _chararray.Eval((), self, n)
        if type != _na.Float64:
            if ((type is _na.Int64)
                    or (_numinclude.hasUInt64 and type is _na.UInt64)):
                warnings.warn(
                    "Loss of precision converting to 64-bit type.  Consider using eval().",
                    PrecisionWarning)
            return n.astype(type)
        else:
            return n
コード例 #3
0
ファイル: strings.py プロジェクト: joshfermin/AI
 def argsort(self, axis=-1):
     """
     >>> a=fromlist(["other","that","this","another"])
     >>> a.argsort()
     array([3, 0, 1, 2])
     """
     if axis != -1:
         raise TypeError("CharArray.argsort() does not support the axis parameter.")
     ax = range(len(self))
     ax.sort(lambda x, y, z=self: cmp(z[x], z[y]))
     return _na.array(ax)
コード例 #4
0
ファイル: strings.py プロジェクト: joshfermin/AI
 def argsort(self, axis=-1):
     """
     >>> a=fromlist(["other","that","this","another"])
     >>> a.argsort()
     array([3, 0, 1, 2])
     """
     if axis != -1:
         raise TypeError(
             "CharArray.argsort() does not support the axis parameter.")
     ax = range(len(self))
     ax.sort(lambda x, y, z=self: cmp(z[x], z[y]))
     return _na.array(ax)
コード例 #5
0
ファイル: records.py プロジェクト: joshfermin/AI
def fromrecords(recList, formats=None, names=None, shape=0, byteorder=sys.byteorder, aligned=0):
    """ create a Record Array from a list of records in text form

        The data in the same field can be heterogeneous, they will be promoted
        to the highest data type.  This method is intended for creating
        smaller record arrays.  If used to create large array e.g.

        r=fromrecords([[2,3.,'abc']]*100000)

        it is slow.

    >>> r=fromrecords([[456,'dbe',1.2],[2,'de',1.3]],names='col1,col2,col3')
    >>> print r[0]
    (456, 'dbe', 1.2)
    >>> r.field('col1')
    array([456,   2])
    >>> r.field('col2')
    CharArray(['dbe', 'de'])
    >>> import cPickle
    >>> print cPickle.loads(cPickle.dumps(r))
    RecArray[ 
    (456, 'dbe', 1.2),
    (2, 'de', 1.3)
    ]
    """

    if shape == 0:
        _shape = len(recList)
    else:
        _shape = shape

    _nfields = len(recList[0])
    for _rec in recList:
        if len(_rec) != _nfields:
            raise ValueError, "inconsistent number of objects in each record"
    arrlist = [0]*_nfields
    for col in range(_nfields):
        tmp = [0]*_shape
        for row in range(_shape):
            tmp[row] = recList[row][col]
        try:
            arrlist[col] = num.array(tmp)
        except:
            try:
                arrlist[col] = chararray.array(tmp)
            except:
                raise ValueError, "inconsistent data at row %d,field %d" % (row, col)
    _array = fromarrays(arrlist, formats=formats, shape=_shape, names=names,
                byteorder=byteorder, aligned=aligned)
    del arrlist
    del tmp
    return _array
コード例 #6
0
ファイル: records.py プロジェクト: joshfermin/AI
    def _get_fields(self):
        """Get a dictionary of fields as numeric arrays."""

        # Iterate over all the fields
        fields = {}
        for indx in range(self._nfields):

            # determine the offset within the record
            _start = self._stops[indx] - self._sizes[indx] + 1
            _shape = self._shape
            _type = self._fmt[indx]
            _buffer = self._data
            _offset = self._byteoffset + _start

            # don't use self._itemsize due to possible slicing
            _stride = self._strides[-1]

            _order = self._byteorder

            if isinstance(_type, Char):
                arr = chararray.CharArray(buffer=_buffer,
                                          shape=_shape,
                                          itemsize=self._itemsizes[indx],
                                          byteoffset=_offset,
                                          bytestride=_stride)
            else:
                arr = num.NumArray(shape=_shape,
                                   type=_type,
                                   buffer=_buffer,
                                   byteoffset=_offset,
                                   bytestride=_stride,
                                   byteorder=_order)

            # modify the _shape and _strides for array elements
            if (self._repeats[indx] != 1):
                if type(self._repeats[indx]) == types.TupleType:
                    arr._shape = self._shape + self._repeats[indx]
                else:
                    arr._shape = self._shape + (self._repeats[indx], )
                nd_stride = num.array(arr._shape[2:] + (1, ))[::-1]
                nd_stride = num.cumproduct(nd_stride) * self._itemsizes[indx]
                nd_stride = nd_stride[::-1]
                arr._strides = (self._strides[0], ) + tuple(nd_stride)

            # Put this array as a value in dictionary
            # Do both name and index
            fields[indx] = arr
            fields[self._names[indx]] = arr

        return fields
コード例 #7
0
ファイル: strings.py プロジェクト: joshfermin/AI
    def eval(self):
        """eval(self) converts CharArray 'self' into a NumArray.
        This is the original slow implementation based on a Python loop
        and the eval() function.

        >>> array([["1","2"],["3","4"]]).eval()
        array([[1, 2],
               [3, 4]])
        >>> try:
        ...    array([["1","2"],["3","other"]]).eval()
        ... except NameError:
        ...    pass
        """
        n = _na.array([eval(x, {}, {}) for x in _na.ravel(self)])
        n.setshape(self._shape)
        return n
コード例 #8
0
ファイル: strings.py プロジェクト: joshfermin/AI
    def eval(self):
        """eval(self) converts CharArray 'self' into a NumArray.
        This is the original slow implementation based on a Python loop
        and the eval() function.

        >>> array([["1","2"],["3","4"]]).eval()
        array([[1, 2],
               [3, 4]])
        >>> try:
        ...    array([["1","2"],["3","other"]]).eval()
        ... except NameError:
        ...    pass
        """
        n = _na.array([eval(x, {}, {}) for x in _na.ravel(self)])
        n.setshape(self._shape)
        return n
コード例 #9
0
ファイル: records.py プロジェクト: joshfermin/AI
    def _get_fields(self):
        """Get a dictionary of fields as numeric arrays."""

        # Iterate over all the fields
        fields = {}
        for indx in range(self._nfields):

            # determine the offset within the record
            _start = self._stops[indx] - self._sizes[indx] + 1
            _shape = self._shape
            _type = self._fmt[indx]
            _buffer = self._data
            _offset = self._byteoffset + _start

            # don't use self._itemsize due to possible slicing
            _stride = self._strides[-1]

            _order = self._byteorder

            if isinstance(_type, Char):
                arr = chararray.CharArray(buffer=_buffer, shape=_shape,
                          itemsize=self._itemsizes[indx], byteoffset=_offset,
                          bytestride=_stride)
            else:
                arr = num.NumArray(shape=_shape, type=_type,
                              buffer=_buffer, byteoffset=_offset, 
                              bytestride=_stride, byteorder = _order)

            # modify the _shape and _strides for array elements
            if (self._repeats[indx] != 1):
                if type(self._repeats[indx]) == types.TupleType:
                    arr._shape = self._shape + self._repeats[indx]
                else:
                    arr._shape = self._shape + (self._repeats[indx],)
                nd_stride = num.array(arr._shape[2:]+(1,))[::-1]
                nd_stride = num.cumproduct(nd_stride) * self._itemsizes[indx]
                nd_stride = nd_stride[::-1]
                arr._strides = (self._strides[0],) + tuple(nd_stride)

            # Put this array as a value in dictionary
            # Do both name and index
            fields[indx] = arr
            fields[self._names[indx]] = arr 

        return fields
コード例 #10
0
ファイル: strings.py プロジェクト: joshfermin/AI
 def search(self, pattern, flags=0):
     """
     >>> a=fromlist([["wo","what"],["wen","erewh"]])
     >>> a.search("wh")
     (array([0, 1]), array([1, 1]))
     >>> a.search("1")
     (array([], type=Long), array([], type=Long))
     >>> b=array(["",""])
     >>> b.search("1")
     (array([], type=Long),)
     >>> b.search("")
     (array([0, 1]),)
     """
     searcher = re.compile(pattern, flags).search
     l = lambda x, f=searcher: int(f(x) is not None)
     matches = _na.array(self.amap(l), type=_na.Bool)
     if len(matches):
         return _na.nonzero(matches)
     else:
         return ()
コード例 #11
0
ファイル: strings.py プロジェクト: joshfermin/AI
 def search(self, pattern, flags=0):
     """
     >>> a=fromlist([["wo","what"],["wen","erewh"]])
     >>> a.search("wh")
     (array([0, 1]), array([1, 1]))
     >>> a.search("1")
     (array([], type=Long), array([], type=Long))
     >>> b=array(["",""])
     >>> b.search("1")
     (array([], type=Long),)
     >>> b.search("")
     (array([0, 1]),)
     """
     searcher = re.compile(pattern, flags).search
     l = lambda x, f=searcher: int(f(x) is not None)
     matches = _na.array(self.amap(l), type=_na.Bool)
     if len(matches):
         return _na.nonzero(matches)
     else:
         return ()
コード例 #12
0
ファイル: ieeespecial.py プロジェクト: joshfermin/AI
def _BIT(x):
    a = _na.array((1 << x), type=_na.Int32)
    a.__class__ = _IeeeMaskBit
    return a
コード例 #13
0
ファイル: ieeespecial.py プロジェクト: joshfermin/AI
Warning: Encountered divide by zero(s)  in divide
>>> negzero = array([1], type=Float64)/neginf
>>> ieeemask(negzero, POS_ZERO)
array([0], type=Bool)
>>> ieeemask(negzero, NEG_ZERO)
array([1], type=Bool)
>>> ieeemask(array([-0], type=Float64), ZERO)
array([1], type=Bool)
"""

import numarrayall as _na
from numarray.ufunc import isnan
# Define *ieee special values*
_na.Error.pushMode(all="ignore")

plus_inf = inf = (_na.array(1.0) / _na.array(0.0))[()]
minus_inf = (_na.array(-1.0) / _na.array(0.0))[()]
nan = (_na.array(0.0) / _na.array(0.0))[()]
plus_zero = zero = 0.0
minus_zero = (_na.array(-1.0) * 0.0)[()]

_na.Error.popMode()


# Define *mask condition bits*
class _IeeeMaskBit(_na.NumArray):
    pass


def _BIT(x):
    a = _na.array((1 << x), type=_na.Int32)
コード例 #14
0
ファイル: ieeespecial.py プロジェクト: fxia22/ASM_xf
def _BIT(x):
    a = _na.array((1 << x), type=_na.Int32)
    a.__class__ = _IeeeMaskBit
    return a
コード例 #15
0
ファイル: ieeespecial.py プロジェクト: fxia22/ASM_xf
Warning: Encountered divide by zero(s)  in divide
>>> negzero = array([1], type=Float64)/neginf
>>> ieeemask(negzero, POS_ZERO)
array([0], type=Bool)
>>> ieeemask(negzero, NEG_ZERO)
array([1], type=Bool)
>>> ieeemask(array([-0], type=Float64), ZERO)
array([1], type=Bool)
"""

import numarrayall as _na
from numarray.ufunc import isnan
# Define *ieee special values*
_na.Error.pushMode(all="ignore")

plus_inf = inf = (_na.array(1.0)/_na.array(0.0))[()]
minus_inf = (_na.array(-1.0)/_na.array(0.0))[()]
nan = (_na.array(0.0)/_na.array(0.0))[()]
plus_zero = zero = 0.0
minus_zero = (_na.array(-1.0)*0.0)[()]

_na.Error.popMode()

# Define *mask condition bits*
class _IeeeMaskBit(_na.NumArray):
    pass

def _BIT(x):
    a = _na.array((1 << x), type=_na.Int32)
    a.__class__ = _IeeeMaskBit
    return a
コード例 #16
0
ファイル: records.py プロジェクト: joshfermin/AI
def fromrecords(recList,
                formats=None,
                names=None,
                shape=0,
                byteorder=sys.byteorder,
                aligned=0):
    """ create a Record Array from a list of records in text form

        The data in the same field can be heterogeneous, they will be promoted
        to the highest data type.  This method is intended for creating
        smaller record arrays.  If used to create large array e.g.

        r=fromrecords([[2,3.,'abc']]*100000)

        it is slow.

    >>> r=fromrecords([[456,'dbe',1.2],[2,'de',1.3]],names='col1,col2,col3')
    >>> print r[0]
    (456, 'dbe', 1.2)
    >>> r.field('col1')
    array([456,   2])
    >>> r.field('col2')
    CharArray(['dbe', 'de'])
    >>> import cPickle
    >>> print cPickle.loads(cPickle.dumps(r))
    RecArray[ 
    (456, 'dbe', 1.2),
    (2, 'de', 1.3)
    ]
    """

    if shape == 0:
        _shape = len(recList)
    else:
        _shape = shape

    _nfields = len(recList[0])
    for _rec in recList:
        if len(_rec) != _nfields:
            raise ValueError, "inconsistent number of objects in each record"
    arrlist = [0] * _nfields
    for col in range(_nfields):
        tmp = [0] * _shape
        for row in range(_shape):
            tmp[row] = recList[row][col]
        try:
            arrlist[col] = num.array(tmp)
        except:
            try:
                arrlist[col] = chararray.array(tmp)
            except:
                raise ValueError, "inconsistent data at row %d,field %d" % (
                    row, col)
    _array = fromarrays(arrlist,
                        formats=formats,
                        shape=_shape,
                        names=names,
                        byteorder=byteorder,
                        aligned=aligned)
    del arrlist
    del tmp
    return _array