New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Speed up execution of mat2py #5
Comments
@AlDanial Thank you for this highly useful code. Coming from a MATLAB background (with only ~3 years of intermittent exposure to Python), I have been wrestling with the MATLAB-Python interface for a long time. Your functions just behave exactly how I want them to. I don't need commit credit. But it may be helpful if I share my code so you can diff and modify. I will paste % Convert a MATLAB variable to an equivalent Python-native variable.
% py_var = mat2py(mat_var);
% py_var = mat2py(mat_var, 'bytes'); % char mapped to Python bytes
% py_var = mat2py(mat_var, 'string'); % char mapped to Python string
function [x_py] = mat2py(x_mat, char_to)
arguments
x_mat
char_to = 'string';
end
% {{{ code/matlab_py/mat2py.m
% This code accompanies the book _Python for MATLAB Development:
% Extend MATLAB with 300,000+ Modules from the Python Package Index_
% ISBN 978-1-4842-7222-0 | ISBN 978-1-4842-7223-7 (eBook)
% DOI 10.1007/978-1-4842-7223-7
% https://github.com/Apress/python-for-matlab-development
%
% Copyright © 2022 Albert Danial
%
% MIT License:
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
% THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
% DEALINGS IN THE SOFTWARE.
% }}}
x_py = py.numpy.array({});
switch class(x_mat)
case 'char'
if strcmp(char_to,'bytes')
x_py = py.bytes(x_mat,'ASCII');
else
x_py = py.str(x_mat);
end
case 'string'
x_py = py.str(x_mat);
case 'datetime'
int_sec = int64(floor(x_mat.Second));
frac_sec = x_mat.Second - double(int_sec);
micro_sec = int64(round(1e6 * frac_sec));
if ~isempty(x_mat.TimeZone)
tzinfo = py.dateutil.tz.gettz(x_mat.TimeZone);
else
tzinfo = py.None;
end
x_py = py.datetime.datetime(int64(x_mat.Year), int64(x_mat.Month), ...
int64(x_mat.Day) , int64(x_mat.Hour) , ...
int64(x_mat.Minute), int64(x_mat.Second), ...
micro_sec, tzinfo);
case {'double', 'single', ...
'uint8', 'uint16', 'uint32', 'uint64', ...
'int8', 'int16', 'int32', 'int64'}
if issparse(x_mat)
if ndims(x_mat) ~= 2
fprintf('mat2py: can only convert 2D sparse matrices\n')
return
end
[nR,nC] = size(x_mat);
[i,j,vals] = find(x_mat);
% subtract 1 to go from 1-based to 0-based indices
py_I = py.numpy.array(int64(i)-1);
py_J = py.numpy.array(int64(j)-1);
py_vals = mat2py(vals);
py_dims = py.tuple({int64(nR), int64(nC)});
py_IJ = py.tuple({py_I, py_J});
V_IJ = py.tuple({py_vals, py_IJ});
x_py = py.scipy.sparse.coo_matrix(V_IJ,py_dims);
elseif ismatrix(x_mat)
if numel(x_mat) == 1
x_py = x_mat; % scalar numeric value
elseif isreal(x_mat)
x_py = py.numpy.array(x_mat);
else
x_py = py.numpy.array(real(x_mat)) + 1j*py.numpy.array(imag(x_mat));
end
end
case 'logical'
if x_mat
x_py = py.True;
else
x_py = py.False;
end
case 'cell'
x_py = py.list();
dims = size(x_mat);
if prod(dims) == max(size(x_mat))
% 1D cell array
for i = 1:numel(x_mat)
x_py.append(mat2py(x_mat{i}, char_to));
end
else
if length(dims) > 2
fprintf('mat2py: %d-dimensional cell array conversion ' + ...
'is not implemented\n', length(dims));
return
end
nR = dims(1,1); nC = dims(1,2);
for r = 1:nR
this_row = py.list();
for c = 1:nC
this_row.append(mat2py(x_mat{r,c}, char_to));
end
x_py.append(this_row);
end
end
case 'struct'
x_py = py.dict();
F = fieldnames(x_mat);
if (length(x_mat) > 1) && ...
(class(x_mat) == "struct")
% struct array
x_py = py.list();
for j = 1:length(x_mat)
x_py.append( mat2py(x_mat(j)) );
end
else
for i = 1:length(F)
if (length(x_mat.(F{i})) > 1) && ...
(class(x_mat.(F{i})) == "struct")
% struct of struct array
List = py.list();
for j = 1:length(x_mat.(F{i}))
List.append( mat2py(x_mat.(F{i})(j)) );
end
x_py.update(pyargs(F{i}, List));
else
x_py.update(pyargs(F{i}, mat2py(x_mat.(F{i}))));
end
end
end
otherwise
fprintf('mat2py: %s conversion is not implemented\n', class(x_mat))
end % switch
end |
support bool type in py2mat, #3 fix microsecond/millisecond conversion error in datetime add conversion and performance tests for mat2py and py2mat
@hcommin your contributions were fantastic, thank you! I plan to post about the improvements on "Matlab Central", that is https://www.mathworks.com/matlabcentral/answers/?category=matlab%2Findex&sort=updated+desc&term=matlab+python |
Similar to #4, the execution time of mat2py is dominated by Python library imports. For example, I run this simple test script:
And execution takes roughly 8.5 seconds:
Similar to #4, it seems like we can delete these lines:
Then we need to:
np
withpy.numpy
.sp
withpy.scipy.sparse
.dt
withpy.datetime
.tz
withpy.dateutil.tz
.After that, my simple (NumPy-only) example executes about 200x faster:
I quickly tested the
sp
,dt
andtz
changes like this:And they appear to be working correctly.
The text was updated successfully, but these errors were encountered: