-
Notifications
You must be signed in to change notification settings - Fork 0
/
matlabtools.py
72 lines (58 loc) · 2.14 KB
/
matlabtools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import scipy.io as spio
import datetime as dt
import pytz
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
from: `StackOverflow <http://stackoverflow.com/questions/7008608/scipy-io-loadmat-nested-structures-i-e-dictionaries>`_
"""
data = spio.loadmat(filename, struct_as_record=False, squeeze_me=True)
return _check_keys(data)
def savemat(filename, mdict):
spio.savemat(filename, mdict)
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], spio.matlab.mio5_params.mat_struct):
d[key] = _todict(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, spio.matlab.mio5_params.mat_struct):
d[strg] = _todict(elem)
else:
d[strg] = elem
return d
def matlab2datetime(matlab_datenum, tz=True):
"""
Convert matlab datenum to python datetime, optionally with UTC timezone applied
https://stackoverflow.com/q/13965740
"""
utc = pytz.timezone("UTC")
day = dt.datetime.fromordinal(int(matlab_datenum))
dayfrac = dt.timedelta(days=matlab_datenum % 1) - dt.timedelta(days=366)
if not tz:
return day + dayfrac
else:
return utc.localize(day + dayfrac)
def datetime2matlab(dtime):
dtime = dtime.replace(
tzinfo=None
) # need to get rid of tzinfo since not supported by matlab
mdn = dtime + dt.timedelta(days=366)
frac_seconds = (
dtime - dt.datetime(dtime.year, dtime.month, dtime.day, 0, 0, 0)
).seconds / (24.0 * 60.0 * 60.0)
frac_microseconds = dtime.microsecond / (24.0 * 60.0 * 60.0 * 1000000.0)
return mdn.toordinal() + frac_seconds + frac_microseconds