def write_object(self, arr): """Same as writing structs, except different mx class, and extra classname element after header """ self.write_header(matdims(arr, self.oned_as), mxOBJECT_CLASS) self.write_element(np.array(arr.classname, dtype="S"), mdtype=miINT8) self._write_items(arr)
def write_header(self, mclass=None, is_global=False, is_complex=False, is_logical=False, nzmax=0, shape=None): ''' Write header for given data options mclass - mat5 matrix class is_global - True if matrix is global is_complex - True if matrix is complex is_logical - True if matrix is logical nzmax - max non zero elements for sparse arrays shape : {None, tuple} optional directly specify shape if this is not the same as for self.arr ''' if mclass is None: mclass = self.default_mclass if shape is None: shape = matdims(self.arr, self.oned_as) self._mat_tag_pos = self.file_stream.tell() self.write_dtype(self.mat_tag) # write array flags (complex, global, logical, class, nzmax) af = np.zeros((), mdtypes_template['array_flags']) af['data_type'] = miUINT32 af['byte_count'] = 8 flags = is_complex << 3 | is_global << 2 | is_logical << 1 af['flags_class'] = mclass | flags << 8 af['nzmax'] = nzmax self.write_dtype(af) self.write_element(np.array(shape, dtype='i4')) # write name self.write_element(np.array([ord(c) for c in self.name], 'i1'))
def arr_to_2d(arr, oned_as='row'): ''' Make ``arr`` exactly two dimensional If `arr` has more than 2 dimensions, then, for the sake of compatibility with previous versions of scipy, we reshape to 2D preserving the last dimension and increasing the first dimension. In future versions we will raise an error, as this is at best a very counterinituitive thing to do. Parameters ---------- arr : array oned_as : {'row', 'column'} Whether to reshape 1D vectors as row vectors or column vectors. See documentation for ``matdims`` for more detail Returns ------- arr2d : array 2D version of the array ''' dims = matdims(arr, oned_as) if len(dims) > 2: warnings.warn('Matlab 4 files only support <=2 ' 'dimensions; the next version of scipy will ' 'raise an error when trying to write >2D arrays ' 'to matlab 4 format files', DeprecationWarning, ) return arr.reshape((-1,dims[-1])) return arr.reshape(dims)
def write_object(self, arr): '''Same as writing structs, except different mx class, and extra classname element after header ''' self.write_header(matdims(arr, self.oned_as), mxOBJECT_CLASS) self.write_element(np.array(arr.classname, dtype='S'), mdtype=miINT8) self._write_items(arr)
def arr_to_2d(arr, oned_as='row'): ''' Make ``arr`` exactly two dimensional If `arr` has more than 2 dimensions, then, for the sake of compatibility with previous versions of scipy, we reshape to 2D preserving the last dimension and increasing the first dimension. In future versions we will raise an error, as this is at best a very counterinituitive thing to do. Parameters ---------- arr : array oned_as : {'row', 'column'} Whether to reshape 1D vectors as row vectors or column vectors. See documentation for ``matdims`` for more detail Returns ------- arr2d : array 2D version of the array ''' dims = matdims(arr, oned_as) if len(dims) > 2: warnings.warn( 'Matlab 4 files only support <=2 ' 'dimensions; the next version of scipy will ' 'raise an error when trying to write >2D arrays ' 'to matlab 4 format files', DeprecationWarning, ) return arr.reshape((-1, dims[-1])) return arr.reshape(dims)
def write_cells(self, arr): self.write_header(matdims(arr, self.oned_as), mxCELL_CLASS) # loop over data, column major A = np.atleast_2d(arr).flatten('F') for el in A: self.write(el)
def write_sparse(self, arr): """ Sparse matrices are 2D """ A = arr.tocsc() # convert to sparse CSC format A.sort_indices() # MATLAB expects sorted row indices is_complex = A.dtype.kind == "c" nz = A.nnz self.write_header(matdims(arr, self.oned_as), mxSPARSE_CLASS, is_complex=is_complex, nzmax=nz) self.write_element(A.indices.astype("i4")) self.write_element(A.indptr.astype("i4")) self.write_element(A.data.real) if is_complex: self.write_element(A.data.imag)
def write_sparse(self, arr): ''' Sparse matrices are 2D ''' A = arr.tocsc() # convert to sparse CSC format A.sort_indices() # MATLAB expects sorted row indices is_complex = (A.dtype.kind == 'c') nz = A.nnz self.write_header(matdims(arr, self.oned_as), mxSPARSE_CLASS, is_complex=is_complex, nzmax=nz) self.write_element(A.indices.astype('i4')) self.write_element(A.indptr.astype('i4')) self.write_element(A.data.real) if is_complex: self.write_element(A.data.imag)
def write_numeric(self, arr): imagf = arr.dtype.kind == 'c' try: mclass = np_to_mxtypes[arr.dtype.str[1:]] except KeyError: if imagf: arr = arr.astype('c128') else: arr = arr.astype('f8') mclass = mxDOUBLE_CLASS self.write_header(matdims(arr, self.oned_as), mclass, is_complex=imagf) if imagf: self.write_element(arr.real) self.write_element(arr.imag) else: self.write_element(arr)
def write_numeric(self, arr): imagf = arr.dtype.kind == "c" try: mclass = NP_TO_MXTYPES[arr.dtype.str[1:]] except KeyError: # No matching matlab type, probably complex256 / float128 / float96 # Cast data to complex128 / float64. if imagf: arr = arr.astype("c128") else: arr = arr.astype("f8") mclass = mxDOUBLE_CLASS self.write_header(matdims(arr, self.oned_as), mclass, is_complex=imagf) if imagf: self.write_element(arr.real) self.write_element(arr.imag) else: self.write_element(arr)
def write_numeric(self, arr): imagf = arr.dtype.kind == 'c' try: mclass = NP_TO_MXTYPES[arr.dtype.str[1:]] except KeyError: # No matching matlab type, probably complex256 / float128 / float96 # Cast data to complex128 / float64. if imagf: arr = arr.astype('c128') else: arr = arr.astype('f8') mclass = mxDOUBLE_CLASS self.write_header(matdims(arr, self.oned_as), mclass, is_complex=imagf) if imagf: self.write_element(arr.real) self.write_element(arr.imag) else: self.write_element(arr)
def write_struct(self, arr): self.write_header(matdims(arr, self.oned_as), mxSTRUCT_CLASS) self._write_items(arr)
def arr_to_2d(self): dims = matdims(self.arr, self.oned_as) self.arr.shape = dims if len(dims) > 2: self.arr = self.arr.reshape(-1, dims[-1])
def arr_to_2d(self): dims = matdims(self.arr, self.oned_as) self.arr.shape = dims if len(dims) > 2: self.arr = self.arr.reshape(-1,dims[-1])