def view(self, dtype=None, type_=None): if type_ is not None: dtype = type type_type = type( int) # HACK to form an instance of 'type' (very confusing) if isinstance(dtype, type_type) and issubclass(dtype, np.ndarray): return super(ndarray, self).view(dtype) else: # TODO: Endianness changes are not supported here # Consider building byteorder into DataType dtype_bf = DataType(dtype) dtype_np = np.dtype(dtype_bf.as_numpy_dtype()) v = super(ndarray, self).view(dtype_np) v.bf.dtype = dtype_bf v._update_BFarray() return v
def __new__(cls, base=None, space=None, shape=None, dtype=None, buffer=None, offset=0, strides=None, native=None, conjugated=None): if isinstance(shape, int): shape = [shape] ownbuffer = None if base is not None: if (shape is not None or # dtype is not None or buffer is not None or offset != 0 or strides is not None or native is not None): raise ValueError('Invalid combination of arguments when base ' 'is specified') if 'pycuda' in sys.modules: from pycuda.gpuarray import GPUArray as pycuda_GPUArray if isinstance(base, pycuda_GPUArray): return ndarray.__new__(cls, space='cuda', buffer=int(base.gpudata), shape=base.shape, dtype=base.dtype, strides=base.strides, native=np.dtype( base.dtype).isnative) if dtype is not None: dtype = DataType(dtype) if space is None and dtype is None: if not isinstance(base, np.ndarray): base = np.asarray(base) # TODO: This may not be a good idea # Create view of base array obj = base.view(cls) # Note: This calls obj.__array_finalize__ # Allow conjugated to be redefined if conjugated is not None: obj.bf.conjugated = conjugated obj._update_BFarray() else: if not isinstance(base, np.ndarray): # Convert base to np.ndarray if dtype is not None: base = np.array(base, dtype=DataType(dtype).as_numpy_dtype()) else: base = np.array(base) if not isinstance(base, ndarray) and dtype is not None: base = base.astype(dtype.as_numpy_dtype()) base = ndarray(base) # View base as bf.ndarray if dtype is not None and base.bf.dtype != dtype: raise TypeError('Unable to convert type %s to %s during ' 'array construction' % (base.bf.dtype, dtype)) #base = base.view(cls #if dtype is not None: # base = base.astype(DataType(dtype).as_numpy_dtype()) if conjugated is None: conjugated = base.bf.conjugated # Create copy of base array obj = ndarray.__new__(cls, space=space, shape=base.shape, dtype=base.bf.dtype, native=base.bf.native, conjugated=conjugated) copy_array(obj, base) else: # Create new array if dtype is None: dtype = 'f32' # Default dtype dtype = DataType(dtype) if native is None: native = True # Default byteorder if conjugated is None: conjugated = False # Default unconjugated if strides is None: #itemsize = dtype.itemsize itemsize_bits = dtype.itemsize_bits # HACK to support 'packed' arrays, by folding the last # dimension of the shape into the dtype. # TODO: Consider using bit strides when dtype < 8 bits # It's hacky, but it may be worth it if itemsize_bits < 8: pack_factor = 8 // itemsize_bits if shape[-1] % pack_factor != 0 or not len(shape): raise ValueError("Array cannot be packed") shape = list(shape) shape[-1] //= pack_factor itemsize = 1 else: itemsize = itemsize_bits // 8 if len(shape): # This magic came from http://stackoverflow.com/a/32874295 strides = (itemsize * np.r_[ 1, np.cumprod(shape[::-1][:-1], dtype=np.int64)][::-1]) strides = tuple(strides) else: strides = tuple() nbyte = strides[0] * shape[0] if len(shape) else itemsize if buffer is None: # Allocate new buffer if space is None: space = 'system' # Default space if shape is None: raise ValueError('Either buffer or shape must be ' 'specified') ownbuffer = raw_malloc(nbyte, space) buffer = ownbuffer else: if space is None: #space = _get(_bf.bfGetSpace(buffer)) # TODO: raw_get_space should probably return string, and needs a better name space = str(Space(raw_get_space(buffer))) # TODO: Should move np.dtype() into as_numpy_dtype? dtype_np = np.dtype(dtype.as_numpy_dtype()) if not native: dtype_np = dtype_np.newbyteorder() data_buffer = _address_as_buffer(buffer, nbyte) obj = np.ndarray.__new__(cls, shape, dtype_np, data_buffer, offset, strides) obj.bf = BFArrayInfo(space, dtype, native, conjugated, ownbuffer) obj._update_BFarray() return obj