Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
bos-density-integration-package/loadmat_functions.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
35 lines (32 sloc)
1.12 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The following functions convert an object to a struct so that it can be saved to a mat file | |
import scipy.io as sio | |
def loadmat(filename): | |
''' | |
this function should be called instead of direct spio.loadmat | |
as it 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 | |
''' | |
data = sio.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 | |
''' | |
for key in dict: | |
if isinstance(dict[key], sio.matlab.mio5_params.mat_struct): | |
dict[key] = _todict(dict[key]) | |
return dict | |
def _todict(matobj): | |
''' | |
A recursive function which constructs from matobjects nested dictionaries | |
''' | |
dict = {} | |
for strg in matobj._fieldnames: | |
elem = matobj.__dict__[strg] | |
if isinstance(elem, sio.matlab.mio5_params.mat_struct): | |
dict[strg] = _todict(elem) | |
else: | |
dict[strg] = elem | |
return dict |