Skip to content

Commit

Permalink
Speed up py2mat and mat2py by avoiding repeated Python package imports
Browse files Browse the repository at this point in the history
  • Loading branch information
hcommin committed Sep 14, 2023
1 parent 869ed9f commit 2f53222
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
23 changes: 9 additions & 14 deletions bittrue/models/matlab/helpers/mat2py.m
Expand Up @@ -36,13 +36,8 @@
% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
% DEALINGS IN THE SOFTWARE.
% }}}

Im = @py.importlib.import_module;
np = Im('numpy');
sp = Im('scipy.sparse');
dt = Im('datetime');
tz = Im('dateutil.tz');
x_py = np.array({});

x_py = py.numpy.array({});
switch class(x_mat)
case 'char'
if strcmp(char_to,'bytes')
Expand All @@ -57,11 +52,11 @@
frac_sec = x_mat.Second - double(int_sec);
micro_sec = int64(round(1e6 * frac_sec));
if ~isempty(x_mat.TimeZone)
tzinfo = tz.gettz(x_mat.TimeZone);
tzinfo = py.dateutil.tz.gettz(x_mat.TimeZone);
else
tzinfo = py.None;
end
x_py = dt.datetime(int64(x_mat.Year), int64(x_mat.Month), ...
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);
Expand All @@ -76,20 +71,20 @@
[nR,nC] = size(x_mat);
[i,j,vals] = find(x_mat);
% subtract 1 to go from 1-based to 0-based indices
py_I = np.array(int64(i)-1);
py_J = np.array(int64(j)-1);
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 = sp.coo_matrix(V_IJ,py_dims);
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 = np.array(x_mat);
x_py = py.numpy.array(x_mat);
else
x_py = np.array(real(x_mat)) + 1j*np.array(imag(x_mat));
x_py = py.numpy.array(real(x_mat)) + 1j*py.numpy.array(imag(x_mat));
end
end
case 'logical'
Expand Down
20 changes: 9 additions & 11 deletions bittrue/models/matlab/helpers/py2mat.m
Expand Up @@ -29,8 +29,6 @@
% DEALINGS IN THE SOFTWARE.
% }}}
function [x_mat] = py2mat(x_py)
Im = @py.importlib.import_module;
np = Im('numpy');
switch class(x_py)
% Python dictionaries
case 'py.dict'
Expand Down Expand Up @@ -77,25 +75,25 @@
x_mat = x_py.single;
case "float16"
% doesn't exist in matlab, upcast to float32
x_mat = single(x_py.astype(np.float32));
x_mat = single(x_py.astype('float32'));
% only reals and logicals can be cast to arrays so
% have to cast the rest to either single or double
case "uint8"
x_mat = uint8(x_py.astype(np.float32));
x_mat = uint8(x_py.astype('float32'));
case "int8"
x_mat = int8(x_py.astype(np.float32));
x_mat = int8(x_py.astype('float32'));
case "uint16"
x_mat = uint16(x_py.astype(np.float32));
x_mat = uint16(x_py.astype('float32'));
case "int16"
x_mat = int16(x_py.astype(np.float32));
x_mat = int16(x_py.astype('float32'));
case "uint32"
x_mat = uint32(x_py.astype(np.float32));
x_mat = uint32(x_py.astype('float32'));
case "int32"
x_mat = int32(x_py.astype(np.float32));
x_mat = int32(x_py.astype('float32'));
case "uint64"
x_mat = uint64(x_py.astype(np.float64));
x_mat = uint64(x_py.astype('float64'));
case "int64"
x_mat = int64(x_py.astype(np.float64));
x_mat = int64(x_py.astype('float64'));
case "bool"
x_mat = logical(x_py);
% Complex types require a math operation to coerce the
Expand Down

0 comments on commit 2f53222

Please sign in to comment.