Source code for physoce.io

import scipy.io as spio
import numpy as np

[docs]def loadmat(filename): ''' This function is an alternative to directly calling scipy.io.loadmat and cures the problem of not properly recovering python dictionaries from mat files. It calls the function check_keys() to cure all entries which are still mat-objects. Useful for loading comlicated structures with multiple nested levels. Source: Stack Overflow http://stackoverflow.com/questions/7008608/scipy-io-loadmat-nested-structures-i-e-dictionaries Author: mergen http://stackoverflow.com/users/887597/mergen License: Creative Commons Attribution-ShareAlike 3.0 http://creativecommons.org/licenses/by-sa/3.0/ ''' data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True) return _check_keys(data)
def _check_keys(dict): ''' Checks if entries in dictionary are mat-objects. If yes, _todict is called to change them to nested dictionaries Source: Stack Overflow http://stackoverflow.com/questions/7008608/scipy-io-loadmat-nested-structures-i-e-dictionaries Author: mergen http://stackoverflow.com/users/887597/mergen License: Creative Commons Attribution-ShareAlike 3.0 http://creativecommons.org/licenses/by-sa/3.0/ ''' for key in dict: if isinstance(dict[key], spio.matlab.mio5_params.mat_struct): dict[key] = _todict(dict[key]) return dict def _todict(matobj): ''' A recursive function which constructs from matobjects nested dictionaries. Source: Stack Overflow http://stackoverflow.com/questions/7008608/scipy-io-loadmat-nested-structures-i-e-dictionaries Author: mergen http://stackoverflow.com/users/887597/mergen License: Creative Commons Attribution-ShareAlike 3.0 http://creativecommons.org/licenses/by-sa/3.0/ ''' dict = {} for strg in matobj._fieldnames: elem = matobj.__dict__[strg] if isinstance(elem, spio.matlab.mio5_params.mat_struct): dict[strg] = _todict(elem) else: dict[strg] = elem return dict