Skip to content

Commit a4dd745

Browse files
committed
hbj
1 parent 0c5ce29 commit a4dd745

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

RATapi/examples/normal_reflectivity/DSPC_custom_layers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ def DSPC_custom_layers():
128128

129129
if __name__ == "__main__":
130130
import RATapi
131-
RATapi.wrappers.get_matlab_engine().editFile(r"C:\Users\steve\Documents\Development\python-RAT\RATapi\examples\normal_reflectivity\customBilayerDSPC.m")
131+
132+
RATapi.wrappers.get_matlab_engine().editFile(
133+
r"C:\Users\steve\Documents\Development\python-RAT\RATapi\examples\normal_reflectivity\customBilayerDSPC.m"
134+
)
132135
import time
136+
133137
time.sleep(20)
134138
problem, results = DSPC_custom_layers()
135-
139+
136140
RAT.plotting.plot_ref_sld(problem, results, True)

RATapi/inputs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ def get_handle(self, index: int):
8080
if custom_file["language"] == Languages.Python:
8181
file_handle = get_python_handle(custom_file["filename"], custom_file["function_name"], custom_file["path"])
8282
elif custom_file["language"] == Languages.Matlab:
83-
file_handle = RATapi.wrappers.MatlabWrapper(full_path).getHandle()
83+
file_handle = RATapi.wrappers.MatlabWrapper(full_path).get_handle()
8484
elif custom_file["language"] == Languages.Cpp:
85-
file_handle = RATapi.wrappers.DylibWrapper(full_path, custom_file["function_name"]).getHandle()
85+
file_handle = RATapi.wrappers.DylibWrapper(full_path, custom_file["function_name"]).get_handle()
8686

8787
return file_handle
8888

RATapi/wrappers.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
"""Wrappers for the interface between RATapi and MATLAB custom files."""
2+
23
import atexit
34
import os
45
import pathlib
56
import platform
67
import shutil
78
import subprocess
89
from contextlib import contextmanager, suppress
9-
from typing import Callable, Tuple
10+
from typing import Callable
1011

1112
from numpy.typing import ArrayLike
1213

@@ -17,6 +18,13 @@
1718

1819

1920
def get_matlab_engine():
21+
"""Return MATLAB engine object if available else None.
22+
23+
Returns
24+
-------
25+
engine : Optional[RATapi.rat_core.MatlabEngine]
26+
A matlab engine object
27+
"""
2028
return __MATLAB_ENGINE
2129

2230

@@ -37,7 +45,7 @@ def cd(new_dir: str):
3745
os.chdir(prev_dir)
3846

3947

40-
def get_matlab_paths(matlab_exe_path: str) -> Tuple[str, str]:
48+
def get_matlab_paths(matlab_exe_path: str) -> tuple[str, str]:
4149
"""Return paths for loading MATLAB engine C interface dynamic libraries.
4250
4351
Parameters
@@ -71,14 +79,14 @@ def get_matlab_paths(matlab_exe_path: str) -> Tuple[str, str]:
7179
return f"{bin_path.as_posix()}/", f"{dll_path.as_posix()}/"
7280

7381

74-
def start_matlab(matlab_exe_path: str =""):
82+
def start_matlab(matlab_exe_path: str = ""):
7583
"""Load MATLAB engine dynamic libraries and creates wrapper object.
76-
84+
7785
Parameters
7886
----------
7987
matlab_exe_path : str, default ""
8088
The full path of the MATLAB executable
81-
89+
8290
Returns
8391
-------
8492
engine : Optional[RATapi.rat_core.MatlabEngine]
@@ -96,7 +104,7 @@ def start_matlab(matlab_exe_path: str =""):
96104
engine = RATapi.rat_core.MatlabEngine()
97105
# Ensure MATLAB is closed when Python shuts down.
98106
atexit.register(engine.close)
99-
107+
100108
return engine
101109

102110

@@ -112,14 +120,13 @@ def set_matlab_path(matlab_exe_path: str) -> None:
112120
"""
113121
if not matlab_exe_path:
114122
return
115-
123+
116124
global __MATLAB_ENGINE
117125
if __MATLAB_ENGINE is not None:
118126
__MATLAB_ENGINE.close()
119127
atexit.unregister(__MATLAB_ENGINE.close)
120128
__MATLAB_ENGINE = start_matlab(matlab_exe_path)
121129

122-
123130
if platform.system() == "Windows":
124131
process = subprocess.Popen(f'"{matlab_exe_path}" -batch "comserver(\'register\')"')
125132
process.wait()
@@ -148,7 +155,7 @@ def __init__(self, filename) -> None:
148155
engine.cd(str(path.parent))
149156
engine.setFunction(path.stem)
150157

151-
def getHandle(self) -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], tuple[ArrayLike, float]]:
158+
def get_handle(self) -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], tuple[ArrayLike, float]]:
152159
"""Return a wrapper for the custom dynamic library function.
153160
154161
Returns
@@ -179,7 +186,7 @@ class DylibWrapper:
179186
def __init__(self, filename, function_name) -> None:
180187
self.engine = RATapi.rat_core.DylibEngine(filename, function_name)
181188

182-
def getHandle(self) -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], tuple[ArrayLike, float]]:
189+
def get_handle(self) -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], tuple[ArrayLike, float]]:
183190
"""Return a wrapper for the custom dynamic library function.
184191
185192
Returns
@@ -196,6 +203,18 @@ def handle(*args):
196203

197204

198205
def find_existing_matlab() -> str:
206+
"""Find existing MATLAB from cache file or checking if the MATLAB command is available.
207+
208+
Parameters
209+
----------
210+
matlab_exe_path : str, default ""
211+
The full path of the MATLAB executable
212+
213+
Returns
214+
-------
215+
engine : Optional[RATapi.rat_core.MatlabEngine]
216+
A matlab engine object
217+
"""
199218
matlab_exe_path = ""
200219

201220
with suppress(FileNotFoundError), open(MATLAB_PATH_FILE) as path_file:
@@ -210,7 +229,8 @@ def find_existing_matlab() -> str:
210229
if temp.is_symlink():
211230
matlab_exe_path = temp.readlink().as_posix()
212231
set_matlab_path(matlab_exe_path)
213-
214-
return matlab_exe_path
232+
233+
return matlab_exe_path
234+
215235

216236
__MATLAB_ENGINE = start_matlab()

tests/test_wrappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_matlab_wrapper() -> None:
2020
assert pathlib.Path(mocked_engine.cd.call_args[0][0]).samefile(".")
2121

2222
wrapper.engine.invoke.return_value = ([2], 5)
23-
handle = wrapper.getHandle()
23+
handle = wrapper.get_handle()
2424
result = handle([1], [2], [3], 0)
2525
assert result == ([2], 5)
2626
assert wrapper.engine.invoke.call_args[0] == ([1], [2], [3], 0)
@@ -40,7 +40,7 @@ def test_dylib_wrapper() -> None:
4040
mocked_engine.assert_called_once_with("demo.dylib", "demo")
4141

4242
wrapper.engine.invoke.return_value = ([2], 5)
43-
handle = wrapper.getHandle()
43+
handle = wrapper.get_handle()
4444
result = handle([1], [2], [3], 0)
4545
assert result == ([2], 5)
4646
assert wrapper.engine.invoke.call_args[0] == ([1], [2], [3], 0)

0 commit comments

Comments
 (0)