-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmatlab.py
More file actions
67 lines (52 loc) · 2.02 KB
/
matlab.py
File metadata and controls
67 lines (52 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Runs RAT from the MATLAB API."""
import os
import tempfile
import warnings
from pathlib import Path
from ..outputs import Results
from ..project import Project
from ..wrappers import MatlabWrapper
RUNNER = """function executeRAT()
project = jsonToProject('{project}');
controls = jsonToControls('{control}');
[project, results] = RAT(project, controls);
projectToJson(project, '{project}');
resultsToJson(results, '{result}');
end
"""
def run_matlab_directly(project, controls, matlab_rat_path):
"""Run User provided MATLAB RAT for the given project and controls inputs.
Parameters
----------
project : RAT.Project
The project model, which defines the physical system under study.
controls : RAT.Controls
The controls model, which defines algorithmic properties.
matlab_rat_path : str
The path to MATLAB RAT folder.
"""
if MatlabWrapper.loader is None:
raise ImportError(MatlabWrapper.loader_error_message) from None
engine = MatlabWrapper.loader.result()
cur_dir = os.getcwd()
with tempfile.TemporaryDirectory() as tmp:
project_file = Path(tmp, "project.json")
control_file = Path(tmp, "controls.json")
result_file = Path(tmp, "results.json")
runner_file = Path(tmp, "executeRAT.m")
with open(runner_file, "w") as f:
f.write(RUNNER.format(project=project_file, control=control_file, result=result_file))
with warnings.catch_warnings(): # Avoid warning about relative paths
warnings.simplefilter("ignore")
project.save(project_file)
controls.save(control_file)
engine.cd(matlab_rat_path, nargout=0)
engine.eval("addPaths", nargout=0)
engine.cd(cur_dir, nargout=0)
engine.addpath(tmp, nargout=0)
for file in project.custom_files:
engine.addpath(str(file.path), nargout=0)
engine.executeRAT(nargout=0)
project = Project.load(project_file)
results = Results.load(result_file)
return project, results