test_mio_funcs.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ''' Jottings to work out format for __function_workspace__ matrix at end
  2. of mat file.
  3. '''
  4. import os.path
  5. import io
  6. from scipy.io.matlab._mio5 import MatFile5Reader
  7. test_data_path = os.path.join(os.path.dirname(__file__), 'data')
  8. def read_minimat_vars(rdr):
  9. rdr.initialize_read()
  10. mdict = {'__globals__': []}
  11. i = 0
  12. while not rdr.end_of_stream():
  13. hdr, next_position = rdr.read_var_header()
  14. name = 'None' if hdr.name is None else hdr.name.decode('latin1')
  15. if name == '':
  16. name = 'var_%d' % i
  17. i += 1
  18. res = rdr.read_var_array(hdr, process=False)
  19. rdr.mat_stream.seek(next_position)
  20. mdict[name] = res
  21. if hdr.is_global:
  22. mdict['__globals__'].append(name)
  23. return mdict
  24. def read_workspace_vars(fname):
  25. fp = open(fname, 'rb')
  26. rdr = MatFile5Reader(fp, struct_as_record=True)
  27. vars = rdr.get_variables()
  28. fws = vars['__function_workspace__']
  29. ws_bs = io.BytesIO(fws.tobytes())
  30. ws_bs.seek(2)
  31. rdr.mat_stream = ws_bs
  32. # Guess byte order.
  33. mi = rdr.mat_stream.read(2)
  34. rdr.byte_order = mi == b'IM' and '<' or '>'
  35. rdr.mat_stream.read(4) # presumably byte padding
  36. mdict = read_minimat_vars(rdr)
  37. fp.close()
  38. return mdict
  39. def test_jottings():
  40. # example
  41. fname = os.path.join(test_data_path, 'parabola.mat')
  42. read_workspace_vars(fname)