import numarray as num import ndarray as mda import memory import chararray import sys, copy, os, re, types, string version = '0.97 (Oct 18, 2001)' class Char: """ data type Char class""" bytes = 1 CharType = Char() # translation table to the num data types numfmt = {'i1':num.Int8, 'u1':num.UInt8, 'i2':num.Int16, 'i4':num.Int32, 'f4':num.Float32, 'f8':num.Float64, 'l':num.Bool, 'b':num.Int8, 'u':num.UInt8, 's':num.Int16, 'i':num.Int32, 'f':num.Float32, 'd':num.Float64, 'r':num.Float32, 'a':CharType, 'Int8':num.Int8, 'Int16':num.Int16, 'Int32':num.Int32, 'UInt8':num.UInt8, 'Float32':num.Float32, 'Float64':num.Float64, 'Bool':num.Bool} # the reverse translation table of the above (for numarray only) revfmt = {num.Int16:'s', num.Int32:'i', num.Float32:'r', num.Float64:'d', num.Bool:'l', num.Int8:'b', num.UInt8:'u', CharType:'a'} # TFORM regular expression format_re = re.compile(r'(?P^[0-9]*)(?P[A-Za-z0-9.]+)') def fromrecords (recList, formats=None, names=None): """ 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=recarray.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']) """ _shape = len(recList) _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, names=names) del arrlist del tmp return _array def fromarrays (arrayList, formats=None, names=None): """ create a Record Array from a list of num/char arrays >>> x1=num.array([1,2,3,4]) >>> x2=chararray.array(['a','dd','xyz','12']) >>> x3=num.array([1.1,2,3,4]) >>> r=fromarrays([x1,x2,x3],names='a,b,c') >>> print r[1] (2, 'dd', 2.0) >>> x1[1]=34 >>> r.field('a') array([1, 2, 3, 4]) """ _shape = len(arrayList[0]) if formats == None: # go through each object in the list to see if it is a numarray or # chararray and determine the formats formats = '' for obj in arrayList: if isinstance(obj, chararray.CharArray): formats += `obj._itemsize` + 'a,' elif isinstance(obj, num.NumArray): if len(obj._shape) == 1: _repeat = '' elif len(obj._shape) == 2: _repeat = `obj._shape[1]` else: raise ValueError, "doesn't support numarray more than 2-D" formats += _repeat + revfmt[obj._type] + ',' else: raise ValueError, "item in the array list must be numarray or chararray" formats=formats[:-1] for obj in arrayList: if len(obj) != _shape: raise ValueError, "array has different lengths" _array = RecArray(None, formats=formats, shape=_shape, names=names) # populate the record array (make a copy) for i in range(len(arrayList)): try: _array.field(_array._names[i])[:] = arrayList[i] except: print "Incorrect CharArray format %s, copy unsuccessful." % _array._formats[i] return _array def fromstring (datastring, formats, shape=0, names=None): """ create a Record Array from binary data contained in a string""" _array = RecArray(chararray._stringToBuffer(datastring), formats, shape, names) if mda.product(_array._shape)*_array._itemsize > len(datastring): raise ValueError("Insufficient input data.") else: return _array def fromfile(file, formats, shape=-1, names=None): """Create an array from binary file data If file is a string then that file is opened, else it is assumed to be a file object. No options at the moment, all file positioning must be done prior to this function call with a file object >>> import testdata >>> fd=open(testdata.filename) >>> fd.seek(2880*2) >>> r=fromfile(fd, formats='d,i,5a', shape=3) >>> r._byteswap = not num.isBigEndian >>> print r[0] (5.1000000000000005, 61, 'abcde') >>> r._shape (3,) """ if isinstance(shape, types.IntType) or isinstance(shape, types.LongType): shape = (shape,) name = 0 if isinstance(file, types.StringType): name = 1 file = open(file, 'rb') size = os.path.getsize(file.name) - file.tell() dummy = array(None, formats=formats, shape=0) itemsize = dummy._itemsize if shape and itemsize: shapesize = mda.product(shape)*itemsize if shapesize < 0: shape = list(shape) shape[ shape.index(-1) ] = size / -shapesize shape = tuple(shape) nbytes = mda.product(shape)*itemsize if nbytes > size: raise ValueError( "Not enough bytes left in file for specified shape and type") # create the array _array = RecArray(None, formats=formats, shape=shape, names=names) nbytesread = memory.file_readinto(file, _array._data) if nbytesread != nbytes: raise IOError("Didn't read as many bytes as expected") if name: file.close() return _array # The test below was factored out of "array" due to platform specific # floating point formatted results: e+020 vs. e+20 if sys.platform == "win32": _fnumber = "2.5984589414244182e+020" else: _fnumber = "2.5984589414244182e+20" __test__ = {} __test__["array_platform_test_workaround"] = """ >>> r=array('a'*200,'r,3i,5a,s',3) >>> print r[0] (%(_fnumber)s, array([1633771873, 1633771873, 1633771873]), 'aaaaa', 24929) >>> print r[1] (%(_fnumber)s, array([1633771873, 1633771873, 1633771873]), 'aaaaa', 24929) """ % globals() del _fnumber def array(buffer=None, formats=None, shape=0, names=None): """This function will creates a new instance of a RecArray. buffer specifies the source of the array's initialization data. buffer can be: RecArray, list of records in text, list of numarray/chararray, None, string, buffer. formats specifies the fromat definitions of the array's records. shape specifies the array dimensions. names specifies the field names. >>> r=array([[456,'dbe',1.2],[2,'de',1.3]],names='col1,col2,col3') >>> print r[0] (456, 'dbe', 1.2) >>> r=array('a'*200,'r,3i,5a,s',3) >>> r._bytestride 23 >>> r._names ['c1', 'c2', 'c3', 'c4'] >>> r._repeats [1, 3, 5, 1] >>> r._shape (3,) """ if (buffer is None) and (formats is None): raise ValueError("Must define formats if buffer=None") elif buffer is None or isinstance(buffer, types.BufferType): return RecArray(buffer, formats=formats, shape=shape, names=names) elif isinstance(buffer, types.StringType): return fromstring(buffer, formats=formats, shape=shape, names=names) elif isinstance(buffer, types.ListType) or isinstance(buffer, types.TupleType): if isinstance(buffer[0], num.NumArray) or isinstance(buffer[0], chararray.CharArray): return fromarrays(buffer, formats=formats, names=names) else: return fromrecords(buffer, formats=formats, names=names) elif isinstance(buffer, RecArray): return buffer.copy() elif isinstance(buffer, types.FileType): return fromfile(buffer, formats=formats, shape=shape, names=names) else: raise ValueError("Unknown input type") class RecArray(mda.NDArray): """Record Array Class""" def __init__(self, buffer, formats, shape=0, names=None, byteoffset=0, bytestride=None, byteswap=0, aligned=1): # names and formats can be either a string with components separated # by commas or a list of string values, e.g. ['i4', 'f4'] and 'i4,f4' # are equivalent formats self._parseFormats(formats) self._fieldNames(names) itemsize = self._stops[-1] + 1 if shape != None: if type(shape) in [types.IntType, types.LongType]: shape = (shape,) elif (type(shape) == types.TupleType and type(shape[0]) in [types.IntType, types.LongType]): pass else: raise NameError, "Illegal shape %s" % `shape` #XXX need to check shape*itemsize == len(buffer)? self._shape = shape mda.NDArray.__init__(self, self._shape, itemsize, buffer=buffer, byteoffset=byteoffset, bytestride=bytestride, aligned=aligned) self._byteswap = byteswap def _parseFormats(self, formats): """ Parse the field formats """ if (type(formats) in [types.ListType, types.TupleType]): _fmt = formats[:] ### make a copy elif (type(formats) == types.StringType): _fmt = string.split(formats, ',') else: raise NameError, "illegal input formats %s" % `formats` self._nfields = len(_fmt) self._repeats = [1] * self._nfields self._sizes = [0] * self._nfields self._stops = [0] * self._nfields # preserve the input for future reference self._formats = [''] * self._nfields sum = 0 for i in range(self._nfields): # parse the formats into repeats and formats try: (_repeat, _dtype) = format_re.match(string.strip(_fmt[i])).groups() except: print 'format %s is not recognized' % _fmt[i] if _repeat == '': _repeat = 1 else: _repeat = eval(_repeat) _fmt[i] = numfmt[_dtype] self._repeats[i] = _repeat self._sizes[i] = _fmt[i].bytes * _repeat sum += self._sizes[i] self._stops[i] = sum - 1 # Unify the appearance of _format, independent of input formats self._formats[i] = `_repeat`+revfmt[_fmt[i]] self._fmt = _fmt def _fieldNames(self, names=None): """convert input field names into a list and assign to the _names attribute """ if (names): if (type(names) in [types.ListType, types.TupleType]): pass elif (type(names) == types.StringType): names = string.split(names, ',') else: raise NameError, "illegal input names %s" % `names` self._names = map(lambda n:string.strip(n), names) else: self._names = [] # if the names are not specified, they will be assigned as "c1, c2,..." # if not enough names are specified, they will be assigned as "c[n+1], # c[n+2],..." etc. where n is the number of specified names..." self._names += map(lambda i: 'c'+`i`, range(len(self._names)+1,self._nfields+1)) def field(self, fieldName): """ get the field data as a numeric array """ # determine the offset within the record indx = index_of(self._names, fieldName) _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[0] _swap = self._byteswap _align = 0 if isinstance(_type, Char): arr = chararray.CharArray(buffer=_buffer, shape=_shape, itemsize=self._repeats[indx], byteoffset=_offset, bytestride=_stride, aligned=_align) else: arr = num.NumArray(shape=_shape, type=_type, buffer=_buffer, byteoffset=_offset, bytestride=_stride, byteswap = _swap, aligned=_align) # modify the _shape and _strides for array elements if (self._repeats[indx] > 1): arr._shape = self._shape + (self._repeats[indx],) arr._strides = (self._strides[0], _type.bytes) return arr def info(self): """display instance's attributes (except _data)""" _attrList = dir(self) _attrList.remove('_data') _attrList.remove('_fmt') for attr in _attrList: print '%s = %s' % (attr, getattr(self,attr)) def __str__(self): outstr = 'RecArray[ \n' for i in self: outstr += Record.__str__(i) + ',\n' return outstr[:-2] + '\n]' ### The followng __getitem__ is not in the requirements ### and is here for experimental purposes def __getitem__(self, key): if type(key) == types.TupleType: if len(key) == 1: return mda.NDArray.__getitem__(self,key[0]) elif len(key) == 2 and type(key[1]) == types.StringType: return mda.NDArray.__getitem__(self,key[0]).field(key[1]) else: raise NameError, "Illegal key %s" % `key` return mda.NDArray.__getitem__(self,key) def _getitem(self, key): byteoffset = self._getByteOffset(key) row = (byteoffset - self._byteoffset) / self._strides[0] return Record(self, row) def _setitem(self, key, value): byteoffset = self._getByteOffset(key) row = (byteoffset - self._byteoffset) / self._strides[0] for i in range(self._nfields): self.field(self._names[i])[row] = value.field(self._names[i]) def reshape(*value): print "Cannot reshape record array." class Record: """Record Class""" def __init__(self, input, row=0): if isinstance(input, types.ListType) or isinstance(input, types.TupleType): input = fromrecords([input]) if isinstance(input, RecArray): self.array = input self.row = row def field(self, fieldName): """ get the field data of the record""" return self.array.field(fieldName)[self.row] def __str__(self): outstr = '(' for i in range(self.array._nfields): ### this is not efficient, need to know how to convert N-bytes to each data type outstr += `self.array.field(i)[self.row]` + ', ' return outstr[:-2] + ')' def index_of(nameList, key): """ Get the index of the key in the name list. The key can be an integer or string. If integer, it is the index in the list. If string, the name matching will be case-insensitive and trailing blank-insensitive. """ if (type(key) in [types.IntType, types.LongType]): indx = key elif (type(key) == types.StringType): _names = nameList[:] for i in range(len(_names)): _names[i] = string.lower(_names[i]) try: indx = _names.index(string.strip(string.lower(key))) except: raise NameError, "Key %s does not exist" % key else: raise NameError, "Illegal key %s" % `key` return indx def find_duplicate (list): """Find duplication in a list, return a list of dupicated elements""" dup = [] for i in range(len(list)): if (list[i] in list[i+1:]): if (list[i] not in dup): dup.append(list[i]) return dup def test(): import doctest, recarray return doctest.testmod(recarray) if __name__ == "__main__": test()