def mxarray_to_ndarray(libmx, pm):
ndims = libmx.mxGetNumberOfDimensions(pm)<EOL>dims = libmx.mxGetDimensions(pm)<EOL>numelems = libmx.mxGetNumberOfElements(pm)<EOL>elem_size = libmx.mxGetElementSize(pm)<EOL>class_name = libmx.mxGetClassName(pm)<EOL>is_numeric = libmx.mxIsNumeric(pm)<EOL>is_complex = libmx.mxIsComplex(pm)<EOL>data = libmx.mxGetData(pm)<EOL>imag_data = libmx.mxGetImagData(pm)<EOL>if is_numeric:<EOL><INDENT>datasize = numelems*elem_size<EOL>real_buffer = ctypes.create_string_buffer(datasize)<EOL>ctypes.memmove(real_buffer, data, datasize)<EOL>pyarray = np.ndarray(<EOL>buffer=real_buffer,<EOL>shape=dims[:ndims],<EOL>dtype=class_name,<EOL>order='<STR_LIT:F>'<EOL>)<EOL>if is_complex:<EOL><INDENT>imag_buffer = ctypes.create_string_buffer(datasize)<EOL>ctypes.memmove(imag_buffer, imag_data, datasize)<EOL>pyarray_imag = np.ndarray(<EOL>buffer=imag_buffer,<EOL>shape=dims[:ndims],<EOL>dtype=class_name,<EOL>order='<STR_LIT:F>'<EOL>)<EOL>pyarray = pyarray + pyarray_imag * <NUM_LIT><EOL><DEDENT>out = pyarray.squeeze()<EOL>if out.ndim == <NUM_LIT:0>:<EOL><INDENT>out, = np.atleast_1d(out)<EOL><DEDENT><DEDENT>elif class_name == '<STR_LIT>':<EOL><INDENT>datasize = numelems + <NUM_LIT:1><EOL>pystring = ctypes.create_string_buffer(datasize+<NUM_LIT:1>)<EOL>libmx.mxGetString(pm, pystring, datasize)<EOL>out = pystring.value<EOL><DEDENT>elif class_name == '<STR_LIT>':<EOL><INDENT>datasize = numelems*elem_size<EOL>buf = ctypes.create_string_buffer(datasize)<EOL>ctypes.memmove(buf, data, datasize)<EOL>pyarray = np.ndarray(<EOL>buffer=buf,<EOL>shape=dims[:ndims],<EOL>dtype='<STR_LIT:bool>',<EOL>order='<STR_LIT:F>'<EOL>)<EOL>out = pyarray.squeeze()<EOL>if out.ndim == <NUM_LIT:0>:<EOL><INDENT>out, = np.atleast_1d(out)<EOL><DEDENT><DEDENT>elif class_name == '<STR_LIT>':<EOL><INDENT>out = np.empty(numelems, dtype='<STR_LIT:O>')<EOL>for i in range(numelems):<EOL><INDENT>cell = libmx.mxGetCell(pm, i)<EOL>if bool(cell):<EOL><INDENT>out[i] = mxarray_to_ndarray(libmx, cell)<EOL><DEDENT>else:<EOL><INDENT>out[i] = None<EOL><DEDENT><DEDENT>out = out.reshape(dims[:ndims], order='<STR_LIT:F>')<EOL>out = out.squeeze()<EOL><DEDENT>elif class_name == '<STR_LIT>':<EOL><INDENT>field_num = libmx.mxGetNumberOfFields(pm)<EOL>field_names = []<EOL>for i in range(field_num):<EOL><INDENT>field_name = libmx.mxGetFieldNameByNumber(pm, i)<EOL>field_names.append(field_name)<EOL><DEDENT>records = []            <EOL>for i in range(numelems):<EOL><INDENT>record = []<EOL>for field_name in field_names:<EOL><INDENT>field = libmx.mxGetField(pm, i, field_name)<EOL>if bool(field):<EOL><INDENT>el = mxarray_to_ndarray(libmx, field)<EOL><DEDENT>else:<EOL><INDENT>el = None<EOL><DEDENT>record.append(el)<EOL><DEDENT>records.append(record)<EOL><DEDENT>arrays = zip(*records)  <EOL>new_arrays = []<EOL>for arr in arrays:<EOL><INDENT>contains_ndarray = np.any([isinstance(el, np.ndarray) for el in arr])<EOL>if contains_ndarray:<EOL><INDENT>newarr = np.empty(len(arr), dtype='<STR_LIT:O>')<EOL>for i,a in enumerate(arr):<EOL><INDENT>newarr[i] = a<EOL><DEDENT><DEDENT>else:<EOL><INDENT>newarr = np.array(arr)<EOL><DEDENT>new_arrays.append(newarr)<EOL><DEDENT>if new_arrays:<EOL><INDENT>out = np.rec.fromarrays(new_arrays, names=field_names)<EOL>out = out.reshape(dims[:ndims], order='<STR_LIT:F>')<EOL>out = out.squeeze()<EOL><DEDENT>else:<EOL><INDENT>out = np.array([])<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise NotImplementedError('<STR_LIT>'.format(class_name))<EOL><DEDENT>return out<EOL>
Convert MATLAB object `pm` to numpy equivalent.