Skip to content

Commit d8aa862

Browse files
committed
Use the IPC file generated in python in MATLAB
1 parent 928a034 commit d8aa862

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

ratapi/project.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,10 @@ def model_dump(self):
932932
933933
Returns
934934
-------
935-
json_dict : dict
935+
model_dict : dict
936936
A dict containing the model information.
937937
"""
938-
json_dict = {}
938+
model_dict = {}
939939
for field in self.model_fields:
940940
attr = getattr(self, field)
941941

@@ -949,7 +949,7 @@ def make_data_dict(item):
949949
"simulation_range": item.simulation_range,
950950
}
951951

952-
json_dict["data"] = [make_data_dict(data) for data in attr]
952+
model_dict["data"] = [make_data_dict(data) for data in attr]
953953

954954
elif field == "custom_files":
955955

@@ -965,13 +965,13 @@ def make_custom_file_dict(item):
965965

966966
return file_dict
967967

968-
json_dict["custom_files"] = [make_custom_file_dict(file) for file in attr]
968+
model_dict["custom_files"] = [make_custom_file_dict(file) for file in attr]
969969

970970
elif isinstance(attr, ClassList):
971-
json_dict[field] = [item.model_dump() for item in attr]
971+
model_dict[field] = [item.model_dump() for item in attr]
972972
else:
973-
json_dict[field] = attr
974-
return json_dict
973+
model_dict[field] = attr
974+
return model_dict
975975

976976
def save(self, filepath: str | Path = "./project.json"):
977977
"""Save a project to a JSON file.

ratapi/utils/matlab.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
1919
project = jsonToProject('{project}');
2020
controls = jsonToControls('{control}');
21+
customControls = customControl();
22+
customControls.update(controls);
23+
customControls.filePath = '{ipc_path}';
2124
if any(strcmpi(controls.procedure, {{procedures.DE.value, procedures.Simplex.value}}))
22-
disp("hello")
2325
useLivePlot(1);
2426
end
2527
for i=1:project.customFile.rowCount
2628
addpath(project.customFile.varTable{{i, 5}});
2729
end
28-
[project, results] = RAT(project, controls);
30+
[project, results] = RAT(project, customControls);
2931
3032
projectToJson(project, '{project}');
3133
resultsToJson(results, '{result}');
@@ -34,7 +36,26 @@
3436
"""
3537

3638

37-
def run_matlab_directly(project, controls, matlab_rat_path, stdout=None, stderr=None):
39+
CONTROL = """classdef customControl < controlsClass
40+
properties(Hidden = true)
41+
filePath = ''
42+
end
43+
methods
44+
function update(obj, controls)
45+
propNames = properties(controls);
46+
for i = 1:length(propNames)
47+
obj.(propNames{i}) = controls.(propNames{i});
48+
end
49+
end
50+
function path = getIPCFilePath(obj)
51+
path = obj.filePath;
52+
end
53+
end
54+
end
55+
"""
56+
57+
58+
def run_matlab_directly(project, controls, matlab_rat_path, ipc_path="", stdout=None, stderr=None):
3859
"""Run User provided MATLAB RAT for the given project and controls inputs.
3960
4061
Parameters
@@ -45,6 +66,8 @@ def run_matlab_directly(project, controls, matlab_rat_path, stdout=None, stderr=
4566
The controls model (or equivalent json dict), which defines algorithmic properties.
4667
matlab_rat_path : str
4768
The path to MATLAB RAT folder.
69+
ipc_path : str, optional
70+
IPC path for MATLAB to use
4871
stdout : io.TextIOBase, optional
4972
Text stream for MATLAB console output
5073
stderr : io.TextIOBase, optional
@@ -60,10 +83,19 @@ def run_matlab_directly(project, controls, matlab_rat_path, stdout=None, stderr=
6083
control_file = Path(tmp, "controls.json")
6184
result_file = Path(tmp, "results.json")
6285
runner_file = Path(tmp, "executeRAT.m")
86+
custom_controls_file = Path(tmp, "customControl.m")
87+
with open(custom_controls_file, "w") as f:
88+
f.write(CONTROL)
6389

6490
with open(runner_file, "w") as f:
6591
f.write(
66-
RUNNER.format(project=project_file, control=control_file, result=result_file, rat_path=matlab_rat_path)
92+
RUNNER.format(
93+
project=project_file,
94+
control=control_file,
95+
result=result_file,
96+
rat_path=matlab_rat_path,
97+
ipc_path=ipc_path,
98+
)
6799
)
68100

69101
controls.save(control_file) if not isinstance(controls, dict) else control_file.write_text(json.dumps(controls))

0 commit comments

Comments
 (0)