|
| 1 | +import pathlib |
| 2 | +from typing import Callable, Tuple |
| 3 | +import numpy as np |
| 4 | +from numpy.typing import ArrayLike |
| 5 | +import RAT.rat_core |
| 6 | + |
| 7 | + |
| 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: |
| 29 | + self.engine.quit() |
| 30 | + |
| 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 |
| 53 | + |
| 54 | + |
| 55 | +class DylibWrapper: |
| 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: |
| 66 | + self.engine = RAT.rat_core.DylibEngine(filename, function_name) |
| 67 | + |
| 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 | + """ |
| 78 | + def handle(params, bulk_in, bulk_out, contrast, domain=-1): |
| 79 | + if domain == -1: |
| 80 | + output, sub_rough = self.engine.invoke(params, bulk_in, bulk_out, contrast) |
| 81 | + else: |
| 82 | + output, sub_rough = self.engine.invoke(params, bulk_in, bulk_out, contrast, domain) |
| 83 | + return output, sub_rough |
| 84 | + return handle |
0 commit comments