def to_value(cls, instance): """Convert to a value to send to Octave.""" if not isinstance(instance, OctaveUserClass) or not instance._attrs: return dict() # Bootstrap a MatlabObject from scipy.io # From https://github.com/scipy/scipy/blob/93a0ea9e5d4aba1f661b6bb0e18f9c2d1fce436a/scipy/io/matlab/mio5.py#L435-L443 # and https://github.com/scipy/scipy/blob/93a0ea9e5d4aba1f661b6bb0e18f9c2d1fce436a/scipy/io/matlab/mio5_params.py#L224 dtype = [] values = [] for attr in instance._attrs: dtype.append((str(attr), object)) values.append(getattr(instance, attr)) struct = np.array([tuple(values)], dtype) return MatlabObject(struct, instance._name)
'expected': {'teststructnest': st2} }) a = np.empty((1,2), dtype=[(n, object) for n in ['one', 'two']]) a[0,0]['one'] = mlarr(1) a[0,0]['two'] = mlarr(2) a[0,1]['one'] = array([u'number 1']) a[0,1]['two'] = array([u'number 2']) case_table5.append( {'name': 'structarr', 'classes': {'teststructarr': 'struct'}, 'expected': {'teststructarr': a} }) ODT = np.dtype([(n, object) for n in ['expr', 'inputExpr', 'args', 'isEmpty', 'numArgs', 'version']]) MO = MatlabObject(np.zeros((1,1), dtype=ODT), 'inline') m0 = MO[0,0] m0['expr'] = array([u'x']) m0['inputExpr'] = array([u' x = INLINE_INPUTS_{1};']) m0['args'] = array([u'x']) m0['isEmpty'] = mlarr(0) m0['numArgs'] = mlarr(1) m0['version'] = mlarr(1) case_table5.append( {'name': 'object', 'classes': {'testobject': 'object'}, 'expected': {'testobject': MO} }) fp_u_str = open(pjoin(test_data_path, 'japanese_utf8.txt'), 'rb') u_str = fp_u_str.read().decode('utf-8') fp_u_str.close()
def _encode(data, convert_to_float): """Convert the Python values to values suitable to send to Octave. """ ctf = convert_to_float # Handle variable pointer. if isinstance(data, (OctaveVariablePtr)): return _encode(data.value, ctf) # Handle a user defined object. if isinstance(data, OctaveUserClass): return _encode(OctaveUserClass.to_value(data), ctf) # Handle a function pointer. if isinstance(data, (OctaveFunctionPtr, MatlabFunction)): raise Oct2PyError('Cannot write Octave functions') # Handle matlab objects. if isinstance(data, MatlabObject): view = data.view(np.ndarray) out = MatlabObject(data, data.classname) for name in out.dtype.names: out[name] = _encode(view[name], ctf) return out # Integer objects should be converted to floats if isinstance(data, int): return float(data) # Handle pandas series and dataframes if isinstance(data, (DataFrame, Series)): return _encode(data.values, ctf) # Extract and encode values from dict-like objects. if isinstance(data, dict): out = dict() for (key, value) in data.items(): out[key] = _encode(value, ctf) return out # Send None as nan. if data is None: return np.NaN # Sets are treated like lists. if isinstance(data, set): return _encode(list(data), ctf) # Lists can be interpreted as numeric arrays or cell arrays. if isinstance(data, list): if _is_simple_numeric(data): return _encode(np.array(data), ctf) return _encode(tuple(data), ctf) # Tuples are handled as cells. if isinstance(data, tuple): obj = np.empty(len(data), dtype=object) for (i, item) in enumerate(data): obj[i] = _encode(item, ctf) return obj # Sparse data must be floating type. if isinstance(data, spmatrix): return data.astype(np.float64) # Return other data types unchanged. if not isinstance(data, np.ndarray): return data # Extract and encode data from object-like arrays. if data.dtype.kind in 'OV': out = np.empty(data.size, dtype=data.dtype) for (i, item) in enumerate(data.ravel()): if data.dtype.names: for name in data.dtype.names: out[i][name] = _encode(item[name], ctf) else: out[i] = _encode(item, ctf) return out.reshape(data.shape) # Complex 128 is the highest supported by savemat. if data.dtype.name == 'complex256': return data.astype(np.complex128) # Convert to float if applicable. if ctf and data.dtype.kind in 'ui': return data.astype(np.float64) # Return standard array. return data