From 610e377228a458679bf7723fc690b488a2e4a33a Mon Sep 17 00:00:00 2001 From: joseph-fourier Date: Mon, 14 Apr 2014 11:49:42 +0100 Subject: [PATCH] Proposed fix for the string heuristics issue. --- mat73_to_pickle.py | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/mat73_to_pickle.py b/mat73_to_pickle.py index e82c0cc..62874f8 100644 --- a/mat73_to_pickle.py +++ b/mat73_to_pickle.py @@ -71,16 +71,10 @@ def recursive_dict(f, root=None, name='root'): return np.array(f, dtype=dtype).squeeze() elif f.dtype.name in ['uint16']: # this may be a string for Matlab add_dtype_name(f, name) - try: + if 'MATLAB_int_decode' in f.attrs.keys(): # this key is only present if f is a string return string(f) - except ValueError: # it wasn't... - print "WARNING:", name, ":" - print "\t", f - print "\t CONVERSION TO STRING FAILED, USING ARRAY!" - tmp = np.array(f).squeeze() - print "\t", tmp - return tmp - pass + else: + return np.array(f, dtype=np.uint16).squeeze() # otherwise it's a uint16 elif f.dtype.name=='object': # this is a 2D array of HDF5 object references or just objects add_dtype_name(f, name) container = [] @@ -155,17 +149,10 @@ def __recursive(self, f, root): raise MemoryError return np.array(f, dtype=dtype).squeeze() elif f.dtype.name in ['uint16']: # this may be a string for Matlab - # print "STRING!" - try: - return string(f) - except ValueError: # it wasn't... - print "WARNING:", self.__name, ":" - print "\t", f - print "\t CONVERSION TO STRING FAILED, USING ARRAY!" - tmp = np.array(f).squeeze() - print "\t", tmp - return tmp - pass + if 'MATLAB_int_decode' in f.attrs.keys(): + return string(f) + else: + return np.array(f, dtype=np.uint16).squeeze() elif f.dtype.name=='object': # this is a 2D array of HDF5 object references or just objects # print "OBJECT!" container = []