Skip to content

Commit 928a034

Browse files
committed
Add support for live plot
1 parent de0123b commit 928a034

2 files changed

Lines changed: 42 additions & 18 deletions

File tree

ratapi/project.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -927,16 +927,14 @@ def classlist_script(name, classlist):
927927
+ "\n)"
928928
)
929929

930-
def save(self, filepath: str | Path = "./project.json"):
931-
"""Save a project to a JSON file.
930+
def model_dump(self):
931+
"""Generate a dictionary representation of the model.
932932
933-
Parameters
934-
----------
935-
filepath : str or Path
936-
The path to where the project file will be written.
933+
Returns
934+
-------
935+
json_dict : dict
936+
A dict containing the model information.
937937
"""
938-
filepath = Path(filepath).with_suffix(".json")
939-
940938
json_dict = {}
941939
for field in self.model_fields:
942940
attr = getattr(self, field)
@@ -960,7 +958,7 @@ def make_custom_file_dict(item):
960958
"name": item.name,
961959
"filename": item.filename,
962960
"language": item.language,
963-
"path": try_relative_to(item.path, filepath.parent),
961+
"path": str(item.path),
964962
}
965963
if item.name != item.function_name:
966964
file_dict["function_name"] = item.function_name
@@ -973,7 +971,20 @@ def make_custom_file_dict(item):
973971
json_dict[field] = [item.model_dump() for item in attr]
974972
else:
975973
json_dict[field] = attr
974+
return json_dict
976975

976+
def save(self, filepath: str | Path = "./project.json"):
977+
"""Save a project to a JSON file.
978+
979+
Parameters
980+
----------
981+
filepath : str or Path
982+
The path to where the project file will be written.
983+
"""
984+
filepath = Path(filepath).with_suffix(".json")
985+
json_dict = self.model_dump()
986+
for file in json_dict["custom_files"]:
987+
file["path"] = try_relative_to(file["path"], filepath.parent)
977988
filepath.write_text(json.dumps(json_dict))
978989

979990
@classmethod

ratapi/utils/matlab.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Runs RAT from the MATLAB API."""
22

3+
import json
34
import tempfile
45
import warnings
56
from pathlib import Path
@@ -14,31 +15,40 @@
1415
cd('{rat_path}');
1516
addPaths;
1617
cd(cur_dir);
17-
18+
1819
project = jsonToProject('{project}');
1920
controls = jsonToControls('{control}');
21+
if any(strcmpi(controls.procedure, {{procedures.DE.value, procedures.Simplex.value}}))
22+
disp("hello")
23+
useLivePlot(1);
24+
end
2025
for i=1:project.customFile.rowCount
2126
addpath(project.customFile.varTable{{i, 5}});
2227
end
2328
[project, results] = RAT(project, controls);
2429
2530
projectToJson(project, '{project}');
2631
resultsToJson(results, '{result}');
32+
close all
2733
end
2834
"""
2935

3036

31-
def run_matlab_directly(project, controls, matlab_rat_path):
37+
def run_matlab_directly(project, controls, matlab_rat_path, stdout=None, stderr=None):
3238
"""Run User provided MATLAB RAT for the given project and controls inputs.
3339
3440
Parameters
3541
----------
36-
project : RAT.Project
37-
The project model, which defines the physical system under study.
38-
controls : RAT.Controls
39-
The controls model, which defines algorithmic properties.
42+
project : RAT.Project or dict
43+
The project model (or equivalent json dict), which defines the physical system under study.
44+
controls : RAT.Controls or dict
45+
The controls model (or equivalent json dict), which defines algorithmic properties.
4046
matlab_rat_path : str
4147
The path to MATLAB RAT folder.
48+
stdout : io.TextIOBase, optional
49+
Text stream for MATLAB console output
50+
stderr : io.TextIOBase, optional
51+
Text stream for MATLAB console error output
4252
"""
4353
if MatlabWrapper.loader is None:
4454
raise ImportError(MatlabWrapper.loader_error_message) from None
@@ -56,13 +66,16 @@ def run_matlab_directly(project, controls, matlab_rat_path):
5666
RUNNER.format(project=project_file, control=control_file, result=result_file, rat_path=matlab_rat_path)
5767
)
5868

69+
controls.save(control_file) if not isinstance(controls, dict) else control_file.write_text(json.dumps(controls))
70+
5971
with warnings.catch_warnings(): # Avoid warning about relative paths
6072
warnings.simplefilter("ignore")
61-
project.save(project_file)
62-
controls.save(control_file)
73+
project.save(project_file) if not isinstance(project, dict) else project_file.write_text(
74+
json.dumps(project)
75+
)
6376

6477
engine.addpath(tmp, nargout=0)
65-
engine.executeRAT(nargout=0)
78+
engine.executeRAT(nargout=0, stdout=stdout, stderr=stderr)
6679
engine.rmpath(tmp, nargout=0)
6780

6881
project = Project.load(project_file)

0 commit comments

Comments
 (0)