-
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 wrapper for python and dylib custom function (#157)
* Adds wrapper for python and dylib custom function * Adds unit tests and modifies customFileClass to use wrapper instead * Addresses review comments
- Loading branch information
1 parent
ed1416e
commit d171596
Showing
45 changed files
with
575 additions
and
503 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
classdef dyLibWrapper < handle | ||
% A custom function wrapper for c++ dynamic library. The library should be a .dll | ||
% (Windows), .so (Linux), or .dylib (MacOS). | ||
properties (SetAccess = private) | ||
functionName | ||
libPath | ||
end | ||
|
||
properties (Access = private, Hidden = true) | ||
objectHandle % Handle to the underlying C++ class instance | ||
mexHandle % Handle to the mex function | ||
end | ||
|
||
methods | ||
function obj = dyLibWrapper(libPath, functionName) | ||
% Creates a dyLibWrapper object. It creates a callback interface for the | ||
% library via wrapperMex.The arguments should be full or relative path of | ||
% the library with extension and the name of the function to call in the dynamic library | ||
% | ||
% wrapper = dyLibWrapper('customBilayer.so', 'customBilayer'); | ||
% wrapper = dyLibWrapper('D:/MATLAB/customBilayer.dll', 'customDomains'); | ||
obj.mexHandle = @wrapperMex; | ||
obj.objectHandle = obj.mexHandle('new', libPath, functionName); | ||
obj.libPath = libPath; | ||
obj.functionName = functionName; | ||
end | ||
|
||
function delete(obj) | ||
% Destroys the callback class instance in wrapperMex | ||
if ~isempty(obj.objectHandle) | ||
obj.mexHandle('delete', obj.objectHandle); | ||
end | ||
obj.objectHandle = []; | ||
end | ||
|
||
function handle = getHandle(obj) | ||
% Gets the wrapper object handle | ||
handle = obj.objectHandle; | ||
end | ||
|
||
function disp(obj) | ||
% Displays libarary path and function name | ||
fprintf('Dynamic library 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
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
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
Oops, something went wrong.