diff --git a/ratapi/inputs.py b/ratapi/inputs.py index d9b0b72..5e5341d 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/matlab.txt b/ratapi/matlab.txt deleted file mode 100644 index 710371e..0000000 --- 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/utils/plotting.py b/ratapi/utils/plotting.py index fd4480c..38d3d77 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, ), diff --git a/ratapi/wrappers.py b/ratapi/wrappers.py index 6c3d55d..39021e2 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,11 @@ def start_matlab(): """ future = None - with suppress(ImportError): - import matlab.engine + if os.environ.get("DELAY_MATLAB_START", "0") == "0": + with suppress(ImportError): + import matlab.engine - future = matlab.engine.start_matlab(background=True) + future = matlab.engine.start_matlab(background=True) return future @@ -39,10 +41,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 +89,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.