-
Notifications
You must be signed in to change notification settings - Fork 5
Add function to run directly from MATLAB API #205
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
Open
StephenNneji
wants to merge
4
commits into
RascalSoftware:main
Choose a base branch
from
StephenNneji:run_matlab
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−15
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| """Runs RAT from the MATLAB API.""" | ||
|
|
||
| import json | ||
| import tempfile | ||
| import warnings | ||
| from pathlib import Path | ||
|
|
||
| from ..outputs import Results | ||
| from ..project import Project | ||
| from ..wrappers import MatlabWrapper | ||
|
|
||
| RUNNER = """function executeRAT() | ||
|
|
||
| cur_dir = pwd; | ||
| cd('{rat_path}'); | ||
| addPaths; | ||
| cd(cur_dir); | ||
|
|
||
| project = jsonToProject('{project}'); | ||
| controls = jsonToControls('{control}'); | ||
| customControls = customControl(); | ||
| customControls.update(controls); | ||
| customControls.filePath = '{ipc_path}'; | ||
| if any(strcmpi(controls.procedure, {{procedures.DE.value, procedures.Simplex.value}})) | ||
| useLivePlot(1); | ||
| end | ||
| for i=1:project.customFile.rowCount | ||
| addpath(project.customFile.varTable{{i, 5}}); | ||
| end | ||
| [project, results] = RAT(project, customControls); | ||
|
|
||
| projectToJson(project, '{project}'); | ||
| resultsToJson(results, '{result}'); | ||
| close all | ||
| end | ||
| """ | ||
|
|
||
|
|
||
| CONTROL = """classdef customControl < controlsClass | ||
| properties(Hidden = true) | ||
| filePath = '' | ||
| end | ||
| methods | ||
| function update(obj, controls) | ||
| propNames = properties(controls); | ||
| for i = 1:length(propNames) | ||
| obj.(propNames{i}) = controls.(propNames{i}); | ||
| end | ||
| end | ||
| function path = getIPCFilePath(obj) | ||
| path = obj.filePath; | ||
| end | ||
| end | ||
| end | ||
| """ | ||
|
|
||
|
|
||
| def run_matlab_directly(project, controls, matlab_rat_path, ipc_path="", stdout=None, stderr=None): | ||
| """Run User provided MATLAB RAT for the given project and controls inputs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| project : RAT.Project or dict | ||
| The project model (or equivalent json dict), which defines the physical system under study. | ||
| controls : RAT.Controls or dict | ||
| The controls model (or equivalent json dict), which defines algorithmic properties. | ||
| matlab_rat_path : str | ||
| The path to MATLAB RAT folder. | ||
| ipc_path : str, optional | ||
| IPC path for MATLAB to use | ||
| stdout : io.TextIOBase, optional | ||
| Text stream for MATLAB console output | ||
| stderr : io.TextIOBase, optional | ||
| Text stream for MATLAB console error output | ||
| """ | ||
| if MatlabWrapper.loader is None: | ||
| raise ImportError(MatlabWrapper.loader_error_message) from None | ||
|
|
||
| engine = MatlabWrapper.loader.result() | ||
|
|
||
| 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") | ||
| custom_controls_file = Path(tmp, "customControl.m") | ||
| with open(custom_controls_file, "w") as f: | ||
| f.write(CONTROL) | ||
|
|
||
| with open(runner_file, "w") as f: | ||
| f.write( | ||
| RUNNER.format( | ||
| project=project_file, | ||
| control=control_file, | ||
| result=result_file, | ||
| rat_path=matlab_rat_path, | ||
| ipc_path=ipc_path, | ||
| ) | ||
| ) | ||
|
|
||
| controls.save(control_file) if not isinstance(controls, dict) else control_file.write_text(json.dumps(controls)) | ||
|
|
||
| with warnings.catch_warnings(): # Avoid warning about relative paths | ||
| warnings.simplefilter("ignore") | ||
| project.save(project_file) if not isinstance(project, dict) else project_file.write_text( | ||
| json.dumps(project) | ||
| ) | ||
|
|
||
| engine.addpath(tmp, nargout=0) | ||
| engine.executeRAT(nargout=0, stdout=stdout, stderr=stderr) | ||
| engine.rmpath(tmp, nargout=0) | ||
|
|
||
| project = Project.load(project_file) | ||
| results = Results.load(result_file) | ||
| return project, results |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.