-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds unit tests and modifies customFileClass to use wrapper instead
- Loading branch information
1 parent
9b24061
commit 501c6fa
Showing
32 changed files
with
255 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
classdef pythonWrapper < handle | ||
% A custom function wrapper for a python file. | ||
|
||
properties (SetAccess = private) | ||
functionName | ||
libPath | ||
end | ||
|
||
properties (SetAccess = private, Hidden = true) | ||
tempFolder | ||
tempName | ||
filename | ||
end | ||
|
||
methods | ||
function obj = pythonWrapper(libPath, functionName) | ||
% Creates a pythonWrapper object. It creates a temporary function which runs | ||
% the python code using the python executable connected to MATLAB pyenv. The | ||
% arguments should be full or relative path of the python file with extension and | ||
% the name of the function to call in the python file | ||
% | ||
% wrapper = pythonWrapper('customBilayer.py', 'customBilayer'); | ||
% wrapper = pythonWrapper('D:/MATLAB/customBilayer.py', 'customDomains'); | ||
obj.tempFolder = tempname(); | ||
obj.libPath = libPath; | ||
obj.functionName = functionName; | ||
|
||
fnstr = ['function [output,sub_rough] = %s(varargin)\n' ... | ||
' [output, sub_rough] = pyRunner("%s", "%s", varargin);\n' ... | ||
'end\n']; | ||
|
||
mkdir(obj.tempFolder); | ||
filename = sprintf('%s.m', tempname(obj.tempFolder)); | ||
fid = fopen(filename, 'w+'); | ||
[~, obj.tempName, ~] = fileparts(filename); | ||
fprintf(fid, fnstr, obj.tempName, obj.libPath, obj.functionName); | ||
fclose(fid); | ||
addpath(obj.tempFolder); | ||
end | ||
|
||
function delete(obj) | ||
% Removes the temporary runner file | ||
rmpath(obj.tempFolder); | ||
if exist(obj.tempFolder, 'dir') | ||
rmdir(obj.tempFolder, 's'); | ||
end | ||
end | ||
|
||
function handle = getHandle(obj) | ||
% Gets the name of the temporary runner file | ||
handle = obj.tempName; | ||
end | ||
|
||
function disp(obj) | ||
% Displays library path and function name | ||
fprintf('Python wrapper for %s function in %s\n', obj.functionName, obj.libPath); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function [output,sub_rough] = pyRunner(filename, functionName, args) | ||
% Calls a Python custom function from the base Matlab | ||
% workspace. The arguments should be full or relative path of the | ||
% python file with extension, the name of the function to call in the | ||
% python file and a cell array of argument for the reflectivity calculation | ||
% | ||
% [o, s] = pythonWrapper.runner('layers.py', 'layer', {params, bulk_in, bulk_out, contrast}); | ||
% [o, s] = pythonWrapper.runner('layers.py', 'layer', {params, bulk_in, bulk_out, contrast, domain}); | ||
|
||
[filepath, name, ~] = fileparts(filename); | ||
insert(py.sys.path, int32(0), filepath) | ||
|
||
module = py.importlib.import_module(name); | ||
py.importlib.reload(module); | ||
|
||
% Call the python function | ||
if (numel(args) == 4) | ||
out = module.(functionName)(args{1}, args{2}, args{3}, int32(args{4} - 1)); | ||
else | ||
out = module.(functionName)(args{1}, args{2}, args{3}, int32(args{4} - 1), int32(args{5} - 1)); | ||
end | ||
|
||
output = out{1}; | ||
if isa(output, "py.numpy.ndarray") | ||
% convert python (Numpy) ndarray to matlab | ||
temp = zeros(double(output.shape)); | ||
list = output.tolist(); | ||
for i=1:size(temp, 1) | ||
temp(i, :) = double(list{i}); | ||
end | ||
output = temp; | ||
end | ||
output = double(output); | ||
sub_rough = double(out{2}); | ||
|
||
% remove the module from sys modules to ensure reload works | ||
py.sys.modules().pop(name); | ||
py.sys.path().remove(filepath); | ||
end |
Oops, something went wrong.