From b2406b83c8dc144fa84f324ca8de6a2068213022 Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Thu, 3 Jul 2025 15:34:05 +0100 Subject: [PATCH 1/3] Adds feature to use shared matlab for MatlabWrapper --- ratapi/inputs.py | 1 - ratapi/wrappers.py | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ratapi/inputs.py b/ratapi/inputs.py index d9b0b729..5e5341d0 100644 --- a/ratapi/inputs.py +++ b/ratapi/inputs.py @@ -8,7 +8,6 @@ import numpy as np import ratapi -import ratapi.controls import ratapi.wrappers from ratapi.rat_core import Checks, Control, NameStore, ProblemDefinition from ratapi.utils.enums import Calculations, Languages, LayerModels, TypeOptions diff --git a/ratapi/wrappers.py b/ratapi/wrappers.py index 6c3d55db..62321c69 100644 --- a/ratapi/wrappers.py +++ b/ratapi/wrappers.py @@ -1,5 +1,6 @@ """Wrappers for the interface between ratapi and MATLAB custom files.""" +import os import pathlib from contextlib import suppress from typing import Callable @@ -20,10 +21,10 @@ def start_matlab(): """ future = None - with suppress(ImportError): - import matlab.engine - - future = matlab.engine.start_matlab(background=True) + if os.environ.get("DELAY_MATLAB_START", '0') == '0': + with suppress(ImportError): + import matlab.engine + future = matlab.engine.start_matlab(background=True) return future @@ -39,10 +40,11 @@ class MatlabWrapper: """ loader = start_matlab() + loader_error_message = "matlabengine is required to use MatlabWrapper" def __init__(self, filename: str) -> None: if self.loader is None: - raise ImportError("matlabengine is required to use MatlabWrapper") from None + raise ImportError(self.loader_error_message) from None self.engine = self.loader.result() path = pathlib.Path(filename) @@ -86,6 +88,30 @@ def handle(*args): return handle +def use_shared_matlab(name, custom_error_message): + """Connect asynchronously to shared MATLAB engine instance with the given name. + + Parameters + ---------- + name : str + The name of shared MATLAB engine instance + custom_error_message : str + The custom error message in case of failed connection + + Returns + ------- + future : matlab.engine.futureresult.FutureResult + A future used to get the actual matlab engine. + + """ + with suppress(ImportError): + import matlab.engine + + MatlabWrapper.loader = matlab.engine.connect_matlab(name, background=True) + MatlabWrapper.loader_error_message = custom_error_message + return MatlabWrapper.loader + + class DylibWrapper: """Creates a python callback for a function in dynamic library. From 63aada7fb5dfb16f08a8ae190dbb2d199dac9a2c Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Thu, 3 Jul 2025 15:43:18 +0100 Subject: [PATCH 2/3] removes matlab.txt --- ratapi/matlab.txt | 1 - ratapi/wrappers.py | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 ratapi/matlab.txt diff --git a/ratapi/matlab.txt b/ratapi/matlab.txt deleted file mode 100644 index 710371e7..00000000 --- a/ratapi/matlab.txt +++ /dev/null @@ -1 +0,0 @@ -C:\Program Files\MATLAB\R2024b\bin\matlab.EXE \ No newline at end of file diff --git a/ratapi/wrappers.py b/ratapi/wrappers.py index 62321c69..39021e29 100644 --- a/ratapi/wrappers.py +++ b/ratapi/wrappers.py @@ -21,9 +21,10 @@ def start_matlab(): """ future = None - if os.environ.get("DELAY_MATLAB_START", '0') == '0': + if os.environ.get("DELAY_MATLAB_START", "0") == "0": with suppress(ImportError): import matlab.engine + future = matlab.engine.start_matlab(background=True) return future @@ -97,7 +98,7 @@ def use_shared_matlab(name, custom_error_message): The name of shared MATLAB engine instance custom_error_message : str The custom error message in case of failed connection - + Returns ------- future : matlab.engine.futureresult.FutureResult From df985a06306880ae3bcbb79fc4d7d87e8938fa5a Mon Sep 17 00:00:00 2001 From: Stephen Nneji Date: Thu, 3 Jul 2025 16:36:56 +0100 Subject: [PATCH 3/3] fixup --- ratapi/utils/plotting.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ratapi/utils/plotting.py b/ratapi/utils/plotting.py index fd4480cd..38d3d777 100644 --- a/ratapi/utils/plotting.py +++ b/ratapi/utils/plotting.py @@ -1058,7 +1058,7 @@ def validate_dens_type(dens_type: Union[str, None], param: str): i, smooth=smooth, sigma=sigma, - estimated_density=estimated_density.get(i, None), + estimated_density=estimated_density.get(i), axes=ax, **hist_settings, ),