Skip to content

Adds feature to use shared MATLAB for MatlabWrapper #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ratapi/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion ratapi/matlab.txt

This file was deleted.

2 changes: 1 addition & 1 deletion ratapi/utils/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
Expand Down
35 changes: 31 additions & 4 deletions ratapi/wrappers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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.

Expand Down