|
1 | 1 | import pathlib |
| 2 | +from typing import Callable, Tuple |
2 | 3 | import numpy as np |
| 4 | +from numpy.typing import ArrayLike |
3 | 5 | from rat import rat_core |
4 | 6 |
|
5 | | -try: |
6 | | - import matlab.engine |
7 | | - class MatlabWrapper: |
8 | | - def __init__(self, filename): |
9 | | - self.engine = matlab.engine.start_matlab() |
10 | | - path = pathlib.Path(filename) |
11 | | - self.engine.cd(str(path.parent), nargout=0) |
12 | | - self.function_name = path.stem |
13 | 7 |
|
14 | | - def __del__(self): |
| 8 | +class MatlabWrapper: |
| 9 | + """Creates a python callback for a MATLAB function. |
| 10 | +
|
| 11 | + Parameters |
| 12 | + ---------- |
| 13 | + filename : string |
| 14 | + The path of the file containing MATLAB function |
| 15 | + """ |
| 16 | + def __init__(self, filename: str) -> None : |
| 17 | + self.engine = None |
| 18 | + try: |
| 19 | + import matlab.engine |
| 20 | + except ImportError: |
| 21 | + raise ImportError('matlabengine is required to use MatlabWrapper') |
| 22 | + self.engine = matlab.engine.start_matlab() |
| 23 | + path = pathlib.Path(filename) |
| 24 | + self.engine.cd(str(path.parent), nargout=0) |
| 25 | + self.function_name = path.stem |
| 26 | + |
| 27 | + def __del__(self): |
| 28 | + if self.engine is not None: |
15 | 29 | self.engine.quit() |
16 | 30 |
|
17 | | - def getHandle(self): |
18 | | - def handle(params, bulk_in, bulk_out, contrast, domain=-1): |
19 | | - if domain == -1: |
20 | | - output, sub_rough = getattr(self.engine, self.function_name)(np.array(params, 'float'), |
21 | | - np.array(bulk_in, 'float'), |
22 | | - np.array(bulk_out, 'float'), |
23 | | - float(contrast + 1), nargout=2) |
24 | | - else: |
25 | | - output, sub_rough = getattr(self.engine, self.function_name)(np.array(params, 'float'), |
26 | | - np.array(bulk_in, 'float'), |
27 | | - np.array(bulk_out, 'float'), |
28 | | - float(contrast + 1), float(domain + 1), nargout=2) |
29 | | - return output, sub_rough |
30 | | - return handle |
31 | | -except ImportError: |
32 | | - pass # maybe should log matlab engine is not installed |
| 31 | + def getHandle(self)\ |
| 32 | + -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], Tuple[ArrayLike, float]]: |
| 33 | + """Returns a wrapper for the custom MATLAB function |
| 34 | +
|
| 35 | + Returns |
| 36 | + ------- |
| 37 | + wrapper : Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], Tuple[ArrayLike, float]] |
| 38 | + The wrapper function for the MATLAB callback |
| 39 | + """ |
| 40 | + def handle(params, bulk_in, bulk_out, contrast, domain=-1): |
| 41 | + if domain == -1: |
| 42 | + output, sub_rough = getattr(self.engine, self.function_name)(np.array(params, 'float'), |
| 43 | + np.array(bulk_in, 'float'), |
| 44 | + np.array(bulk_out, 'float'), |
| 45 | + float(contrast + 1), nargout=2) |
| 46 | + else: |
| 47 | + output, sub_rough = getattr(self.engine, self.function_name)(np.array(params, 'float'), |
| 48 | + np.array(bulk_in, 'float'), |
| 49 | + np.array(bulk_out, 'float'), |
| 50 | + float(contrast + 1), float(domain + 1), nargout=2) |
| 51 | + return output, sub_rough |
| 52 | + return handle |
33 | 53 |
|
34 | 54 |
|
35 | 55 | class DylibWrapper: |
36 | | - def __init__(self, filename, function_name): |
| 56 | + """Creates a python callback for a function in dynamic library. |
| 57 | +
|
| 58 | + Parameters |
| 59 | + ---------- |
| 60 | + filename : str |
| 61 | + The path of the dyanamic library |
| 62 | + function_name : str |
| 63 | + The name of the function to call |
| 64 | + """ |
| 65 | + def __init__(self, filename, function_name) -> None: |
37 | 66 | self.engine = rat_core.DylibEngine(filename, function_name) |
38 | 67 |
|
39 | | - def getHandle(self): |
| 68 | + def getHandle(self)\ |
| 69 | + -> Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], Tuple[ArrayLike, float]]: |
| 70 | + |
| 71 | + """Returns a wrapper for the custom dynamic library function |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + wrapper : Callable[[ArrayLike, ArrayLike, ArrayLike, int, int], Tuple[ArrayLike, float]] |
| 76 | + The wrapper function for the dynamic library callback |
| 77 | + """ |
40 | 78 | def handle(params, bulk_in, bulk_out, contrast, domain=-1): |
41 | 79 | if domain == -1: |
42 | 80 | output, sub_rough = self.engine.invoke(params, bulk_in, bulk_out, contrast) |
|
0 commit comments