Exemplo n.º 1
0
 def write_char(self, arr, name):
     arr = arr_to_chars(arr)
     arr = arr_to_2d(arr, self.oned_as)
     dims = arr.shape
     self.write_header(name, dims, P=miUINT8, T=mxCHAR_CLASS)
     if arr.dtype.kind == "U":
         # Recode unicode to ascii
         n_chars = np.product(dims)
         st_arr = np.ndarray(shape=(), dtype=arr_dtype_number(arr, n_chars), buffer=arr)
         st = st_arr.item().encode("ascii")
         arr = np.ndarray(shape=dims, dtype="S1", buffer=st)
     self.write_bytes(arr)
Exemplo n.º 2
0
 def write_char(self, arr, name):
     arr = arr_to_chars(arr)
     arr = arr_to_2d(arr, self.oned_as)
     dims = arr.shape
     self.write_header(name, dims, P=miUINT8, T=mxCHAR_CLASS)
     if arr.dtype.kind == 'U':
         # Recode unicode to ascii
         n_chars = np.product(dims)
         st_arr = np.ndarray(shape=(),
                             dtype=arr_dtype_number(arr, n_chars),
                             buffer=arr)
         st = st_arr.item().encode('ascii')
         arr = np.ndarray(shape=dims, dtype='S1', buffer=st)
     self.write_bytes(arr)
Exemplo n.º 3
0
 def write_char(self, arr, codec='ascii'):
     ''' Write string array `arr` with given `codec`
     '''
     if arr.size == 0 or np.all(arr == ''):
         # This an empty string array or a string array containing
         # only empty strings.  Matlab cannot distiguish between a
         # string array that is empty, and a string array containing
         # only empty strings, because it stores strings as arrays of
         # char.  There is no way of having an array of char that is
         # not empty, but contains an empty string. We have to
         # special-case the array-with-empty-strings because even
         # empty strings have zero padding, which would otherwise
         # appear in matlab as a string with a space.
         shape = (0,) * np.max([arr.ndim, 2])
         self.write_header(shape, mxCHAR_CLASS)
         self.write_smalldata_element(arr, miUTF8, 0)
         return
     # non-empty string.
     #
     # Convert to char array
     arr = arr_to_chars(arr)
     # We have to write the shape directly, because we are going
     # recode the characters, and the resulting stream of chars
     # may have a different length
     shape = arr.shape
     self.write_header(shape, mxCHAR_CLASS)
     if arr.dtype.kind == 'U' and arr.size:
         # Make one long string from all the characters.  We need to
         # transpose here, because we're flattening the array, before
         # we write the bytes.  The bytes have to be written in
         # Fortran order.
         n_chars = np.product(shape)
         st_arr = np.ndarray(shape=(),
                             dtype=arr_dtype_number(arr, n_chars),
                             buffer=arr.T.copy()) # Fortran order
         # Recode with codec to give byte string
         st = st_arr.item().encode(codec)
         # Reconstruct as one-dimensional byte array
         arr = np.ndarray(shape=(len(st),),
                          dtype='S1',
                          buffer=st)
     self.write_element(arr, mdtype=miUTF8)
Exemplo n.º 4
0
 def write_char(self, arr, codec='ascii'):
     ''' Write string array `arr` with given `codec`
     '''
     if arr.size == 0 or np.all(arr == ''):
         # This an empty string array or a string array containing
         # only empty strings.  Matlab cannot distiguish between a
         # string array that is empty, and a string array containing
         # only empty strings, because it stores strings as arrays of
         # char.  There is no way of having an array of char that is
         # not empty, but contains an empty string. We have to
         # special-case the array-with-empty-strings because even
         # empty strings have zero padding, which would otherwise
         # appear in matlab as a string with a space.
         shape = (0,) * np.max([arr.ndim, 2])
         self.write_header(shape, mxCHAR_CLASS)
         self.write_smalldata_element(arr, miUTF8, 0)
         return
     # non-empty string.
     #
     # Convert to char array
     arr = arr_to_chars(arr)
     # We have to write the shape directly, because we are going
     # recode the characters, and the resulting stream of chars
     # may have a different length
     shape = arr.shape
     self.write_header(shape, mxCHAR_CLASS)
     if arr.dtype.kind == 'U' and arr.size:
         # Make one long string from all the characters.  We need to
         # transpose here, because we're flattening the array, before
         # we write the bytes.  The bytes have to be written in
         # Fortran order.
         n_chars = np.product(shape)
         st_arr = np.ndarray(shape=(),
                             dtype=arr_dtype_number(arr, n_chars),
                             buffer=arr.T.copy()) # Fortran order
         # Recode with codec to give byte string
         st = st_arr.item().encode(codec)
         # Reconstruct as one-dimensional byte array
         arr = np.ndarray(shape=(len(st),),
                          dtype='S1',
                          buffer=st)
     self.write_element(arr, mdtype=miUTF8)
Exemplo n.º 5
0
 def write_char(self, arr, codec='ascii'):
     if arr.size == 0 or np.all(arr == ''):
         # This an empty string array or a string array containing
         # only empty strings.  Matlab cannot distiguish between a
         # string array that is empty, and a string array containing
         # only empty strings, because it stores strings as arrays of
         # char.  There is no way of having an array of char that is
         # not empty, but contains an empty string. We have to
         # special-case the array-with-empty-strings because even
         # empty strings have zero padding, which would otherwise
         # appear in matlab as a string with a space.
         shape = (0,) * np.max([arr.ndim, 2])
         self.write_header(shape, mxCHAR_CLASS)
         self.write_smalldata_element(arr, miUTF8, 0)
         return
     # non-empty string
     arr = arr_to_chars(arr)
     # We have to write the shape directly, because we are going
     # recode the characters, and the resulting stream of chars
     # may have a different length
     shape = arr.shape
     self.write_header(shape, mxCHAR_CLASS)
     # We need to do our own transpose (not using the normal
     # write routines that do this for us), and copy to make a nice C
     # ordered thing
     arr = arr.T.copy()
     if arr.dtype.kind == 'U' and arr.size:
         # Recode unicode using self.codec
         n_chars = np.product(shape)
         st_arr = np.ndarray(shape=(),
                             dtype=arr_dtype_number(arr, n_chars),
                             buffer=arr)
         st = st_arr.item().encode(codec)
         arr = np.ndarray(shape=(len(st),),
                          dtype='u1',
                          buffer=st)
     self.write_element(arr, mdtype=miUTF8)