-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_python_obj.m
More file actions
61 lines (57 loc) · 1.93 KB
/
convert_python_obj.m
File metadata and controls
61 lines (57 loc) · 1.93 KB
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
function matobj = convert_python_obj(pyobj)
% Recursively convert a python object to MATLAB equivalent (somewhat opinionated)
switch class(pyobj)
case 'py.pandas.core.frame.DataFrame'
pyobj_table = table(pyobj);
matobj = varfun(@convert_python_obj, pyobj_table);
% undo change to variable names from varfun
matobj.Properties.VariableNames = pyobj_table.Properties.VariableNames;
case 'py.int'
matobj = double(pyobj);
case 'py.str'
matobj = string(pyobj);
case 'py.datetime.date'
% assume zero time
matobj = py.datetime.datetime.combine(pyobj, py.datetime.time());
case 'py.dict'
pyobj_struct = struct(pyobj);
matobj = structfun(@convert_python_obj, pyobj_struct, 'uni', false);
case {'py.list', 'py.tuple', 'cell'}
pyobj_cell = cell(pyobj);
matobj = cellfun(@convert_python_obj, pyobj_cell, 'uni', false);
if all(cellfun(@isscalar, matobj))
try
matobj = reshape(vertcat(matobj{:}), size(matobj));
catch % ignore any error
end
end
case 'py.numpy.ndarray'
switch char(pyobj.dtype.kind)
case {'i', 'f', 'c'}
matobj = double(pyobj);
case 'b'
matobj = logical(pyobj);
case 'u'
switch double(pyobj.itemsize)
case 1
matobj = uint8(pyobj);
case 2
matobj = uint16(pyobj);
otherwise
matobj = uint64(pyobj);
end
case 'M'
matobj = datetime(pyobj);
case 'm'
matobj = duration(pyobj);
otherwise
matobj = pyobj;
end
otherwise
matobj = pyobj;
end
% for residual integer types (when recursing)
if isinteger(matobj)
matobj = double(matobj);
end
end