# -*- coding: utf-8 -*-
#
# Authors: Swolf <swolfforever@gmail.com>
# Date: 2020/12/30
# License: MIT License
from typing import Union
from pathlib import Path
import numpy as np
import scipy.io as sio
import mat73
[docs]def loadmat(mat_file: Union[str, Path]) -> dict:
"""Wrapper of scipy.io loadmat function, works for matv7.3.
Parameters
----------
mat_file : Union[str, Path]
file path
Returns
-------
dict
data
"""
try:
data = _loadmat(mat_file)
except Exception:
data = mat73.loadmat(mat_file)
return data
def _loadmat(filename):
"""
this function should be called instead of direct sio.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
Notes: only works for mat before matlab v7.3
"""
def _check_keys(d):
"""
checks if entries in dictionary are mat-objects. If yes
todict is called to change them to nested dictionaries
"""
for key in d:
if isinstance(d[key], sio.matlab.mio5_params.mat_struct):
d[key] = _todict(d[key])
elif isinstance(d[key], np.ndarray):
d[key] = _tolist(d[key])
return d
def _todict(matobj):
"""
A recursive function which constructs from matobjects nested dictionaries
"""
d = {}
for strg in matobj._fieldnames:
elem = matobj.__dict__[strg]
if isinstance(elem, sio.matlab.mio5_params.mat_struct):
d[strg] = _todict(elem)
elif isinstance(elem, np.ndarray):
d[strg] = _tolist(elem)
else:
d[strg] = elem
return d
def _tolist(ndarray):
"""
A recursive function which constructs lists from cellarrays
(which are loaded as numpy ndarrays), recursing into the elements
if they contain matobjects.
"""
if ndarray.dtype == object():
elem_list = []
for sub_elem in ndarray:
if isinstance(sub_elem, sio.matlab.mio5_params.mat_struct):
elem_list.append(_todict(sub_elem))
elif isinstance(sub_elem, np.ndarray):
elem_list.append(_tolist(sub_elem))
else:
elem_list.append(sub_elem)
return elem_list
else:
return ndarray
data = sio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)