def _dualbroadcast(self, other): s, o = _na.NDArray._dualbroadcast(self, other) if not _na.product(s._strides): s = s.copy() if not _na.product(o._strides): o = o.copy() return s, o
def fromfile(file, itemsize=None, shape=None, padc=" ", kind=CharArray): """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 >>> s=fromfile(testdata.filename, shape=-1, itemsize=80) >>> s[0] 'SIMPLE = T / file does conform to FITS standard' >>> s._shape (108,) >>> s._itemsize 80 """ if isinstance(shape, types.IntType): shape = (shape, ) name = 0 if isString(file): name = 1 file = open(file, 'rb') size = int(os.path.getsize(file.name) - file.tell()) if not shape and not itemsize: shape = (1, ) itemsize = size elif shape is not None: if itemsize is not None: shapesize = _na.product(shape) * itemsize if shapesize < 0: shape = list(shape) shape[shape.index(-1)] = size / -shapesize shape = tuple(shape) else: shapesize = _na.product(shape) if shapesize < 0: raise ValueError("Shape dimension of -1 requires itemsize.") itemsize = size / shapesize elif itemsize: shape = (size / itemsize, ) else: raise ValueError("Must define shape or itemsize.") nbytes = _na.product(shape) * itemsize if nbytes > size: raise ValueError( "Not enough bytes left in file for specified shape and type") # create the array arr = kind(None, shape=shape, itemsize=itemsize, padc=padc) nbytesread = file.readinto(arr._data) if nbytesread != nbytes: raise IOError("Didn't read as many bytes as expected") if name: file.close() return arr
def fromfile(file, itemsize=None, shape=None, padc=" ", kind=CharArray): """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 >>> s=fromfile(testdata.filename, shape=-1, itemsize=80) >>> s[0] 'SIMPLE = T / file does conform to FITS standard' >>> s._shape (108,) >>> s._itemsize 80 """ if isinstance(shape, types.IntType): shape = (shape,) name = 0 if isString(file): name = 1 file = open(file, 'rb') size = int(os.path.getsize(file.name) - file.tell()) if not shape and not itemsize: shape = (1,) itemsize = size elif shape is not None: if itemsize is not None: shapesize = _na.product(shape)*itemsize if shapesize < 0: shape = list(shape) shape[ shape.index(-1) ] = size / -shapesize shape = tuple(shape) else: shapesize=_na.product(shape) if shapesize < 0: raise ValueError("Shape dimension of -1 requires itemsize.") itemsize = size / shapesize elif itemsize: shape = (size/itemsize,) else: raise ValueError("Must define shape or itemsize.") nbytes = _na.product(shape)*itemsize if nbytes > size: raise ValueError( "Not enough bytes left in file for specified shape and type") # create the array arr = kind(None, shape=shape, itemsize=itemsize, padc=padc) nbytesread = file.readinto(arr._data) if nbytesread != nbytes: raise IOError("Didn't read as many bytes as expected") if name: file.close() return arr
def __init__(self, buffer=None, itemsize=None, shape=None, byteoffset=0, bytestride=None, aligned=1, type=None, padc=" "): if isinstance(shape, types.IntType): shape = (shape, ) if type is not None: if itemsize is not None: raise ValueError("Specify type *or* itemsize, not both.") itemsize = type.itemsize if not (padc, str) or len(padc) <> 1: raise ValueError("padc must be a string of length 1.") if buffer is None: if shape is None or itemsize is None: raise ValueError( "Must define both shape & itemsize if buffer is None") else: if shape is None and itemsize is None: raise ValueError("Must specify shape, itemsize, or both.") ni = _bufferItems(buffer, byteoffset, bytestride, itemsize) if shape and itemsize == None: itemsize = ni / _na.product(shape) if itemsize and shape == None: shape = (ni, ) if itemsize == 0: # Another hack for 0 length strings. bytestride = 0 if not _nda.is_buffer(buffer) and buffer is not None: raise TypeError( "buffer must either support the C buffer protocol or return something that does from its __buffer__() method" ) _na.NDArray.__init__(self, shape=shape, itemsize=itemsize, buffer=buffer, byteoffset=byteoffset, bytestride=bytestride, aligned=aligned) self._flags |= _gen._UPDATEDICT if type is None: type = NewCharArrayType(itemsize) self._type = type self._padc = padc if buffer is None: self.fill(" ")
def __init__( self, buffer=None, itemsize=None, shape=None, byteoffset=0, bytestride=None, aligned=1, type=None, padc=" " ): if isinstance(shape, types.IntType): shape = (shape,) if type is not None: if itemsize is not None: raise ValueError("Specify type *or* itemsize, not both.") itemsize = type.itemsize if not (padc, str) or len(padc) <> 1: raise ValueError("padc must be a string of length 1.") if buffer is None: if shape is None or itemsize is None: raise ValueError("Must define both shape & itemsize if buffer is None") else: if shape is None and itemsize is None: raise ValueError("Must specify shape, itemsize, or both.") ni = _bufferItems(buffer, byteoffset, bytestride, itemsize) if shape and itemsize == None: itemsize = ni / _na.product(shape) if itemsize and shape == None: shape = (ni,) if itemsize == 0: # Another hack for 0 length strings. bytestride = 0 if not _nda.is_buffer(buffer) and buffer is not None: raise TypeError( "buffer must either support the C buffer protocol or return something that does from its __buffer__() method" ) _na.NDArray.__init__( self, shape=shape, itemsize=itemsize, buffer=buffer, byteoffset=byteoffset, bytestride=bytestride, aligned=aligned, ) self._flags |= _gen._UPDATEDICT if type is None: type = NewCharArrayType(itemsize) self._type = type self._padc = padc if buffer is None: self.fill(" ")
def copy(self): """Return a new array with the same shape and type, but a copy of the data >>> c = fromlist(["this","that", "another"]) >>> d = c.copy() >>> d CharArray(['this', 'that', 'another']) >>> int(c._data is d._data) 0 """ arr = self.view() arr._data = memory.new_memory(arr._itemsize * arr.nelements()) arr._byteoffset = 0 arr._bytestride = arr._itemsize arr._strides = arr._stridesFromShape() arr._itemsize = self._itemsize if _na.product(self._shape): copyfunction = _bytes.functionDict["copyNbytes"] copyfunction( arr._shape, self._data, self._byteoffset, self._strides, arr._data, 0, arr._strides, arr._itemsize ) return arr
def copy(self): """Return a new array with the same shape and type, but a copy of the data >>> c = fromlist(["this","that", "another"]) >>> d = c.copy() >>> d CharArray(['this', 'that', 'another']) >>> int(c._data is d._data) 0 """ arr = self.view() arr._data = memory.new_memory(arr._itemsize * arr.nelements()) arr._byteoffset = 0 arr._bytestride = arr._itemsize arr._strides = arr._stridesFromShape() arr._itemsize = self._itemsize if _na.product(self._shape): copyfunction = _bytes.functionDict["copyNbytes"] copyfunction(arr._shape, self._data, self._byteoffset, self._strides, arr._data, 0, arr._strides, arr._itemsize) return arr