Skip to content
Open
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
27 changes: 22 additions & 5 deletions rascal2/core/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from dataclasses import dataclass
from io import StringIO
from logging import INFO
from multiprocessing import Event, Process, Queue

Expand Down Expand Up @@ -194,7 +195,12 @@ def init_matlab_engine(problem_definition, engine_ready, engine_output, msg_queu
MATLAB engine future or Exception from MatlabHelper.
"""
engine_future = rat.wrappers.MatlabWrapper.loader
if engine_future is None and any([file["language"] == "matlab" for file in problem_definition.customFiles.files]):
files = (
problem_definition["custom_files"]
if isinstance(problem_definition, dict)
else problem_definition.customFiles.files
)
if engine_future is None and any([file["language"] == "matlab" for file in files]):
if not engine_output:
msg_queue.put(LogData(INFO, "Attempting to start Matlab..."))

Expand Down Expand Up @@ -227,7 +233,7 @@ def is_empty_bayes_result(result):
result : Union[ratapi.outputs.Results, ratapi.outputs.BayesResults]
The calculation results.
"""
return isinstance(result, rat.BayesResults) and result.chain.shape == (1, 2)
return isinstance(result, rat.BayesResults) and result.chain.size == 2


def run(
Expand Down Expand Up @@ -272,9 +278,20 @@ def run(

try:
sys.path.append(working_dir)
engine_future = init_matlab_engine(problem_definition, engine_ready, engine_output, msg_queue)
problem_definition, output_results, bayes_results = rat.rat_core.RATMain(problem_definition, cpp_controls)
results = rat.outputs.make_results(procedure, output_results, bayes_results)
engine_future = init_matlab_engine(problem_definition, engine_ready, engine_output, queue)

if isinstance(cpp_controls, dict):
ipc_path = cpp_controls.pop("ipc_path")
matlab_rat_path = cpp_controls.pop("matlab_rat_path")
problem, results = rat.matlab.run_matlab_directly(
problem_definition, cpp_controls, matlab_rat_path, ipc_path, stderr=StringIO(), stdout=StringIO()
)
problem_definition = rat.inputs.make_problem(problem)
else:
problem_definition, output_results, bayes_results = rat.rat_core.RATMain(
problem_definition, cpp_controls
)
results = rat.outputs.make_results(procedure, output_results, bayes_results)
except Exception as err:
queue.put(err)
go_event.clear()
Expand Down
66 changes: 45 additions & 21 deletions rascal2/dialogs/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from rascal2.config import LOGGER, SETTINGS, MatlabHelper
from rascal2.paths import MATLAB_ARCH_FILE
from rascal2.settings import SettingsGroups, change_ui_style
from rascal2.settings import SettingsGroups, change_ui_style, get_global_settings
from rascal2.theme import IconEngine
from rascal2.widgets.inputs import get_validated_input

Expand Down Expand Up @@ -38,8 +38,6 @@ def __init__(self, parent):
self.tab_widget.addTab(SettingsTab(self, SettingsGroups.General), SettingsGroups.General)
self.tab_widget.addTab(SettingsTab(self, SettingsGroups.Plotting), SettingsGroups.Plotting)
self.tab_widget.addTab(self.matlab_tab, "Matlab")
self.tab_widget.setTabVisible(0, parent.presenter.model.save_path != "")
self.tab_widget.setTabVisible(1, parent.presenter.model.save_path != "")

self.reset_button = QtWidgets.QPushButton("Reset to Defaults", self)
self.reset_button.clicked.connect(self.reset_default_settings)
Expand Down Expand Up @@ -144,9 +142,8 @@ def __init__(self):
form_layout.setVerticalSpacing(10)
form_layout.setHorizontalSpacing(0)

label_layout = QtWidgets.QHBoxLayout()
label_layout.addWidget(QtWidgets.QLabel("Current Matlab Directory:"))
label_layout.addStretch(1)
matlab_dir_label = QtWidgets.QLabel("Current Matlab Directory:")
form_layout.addWidget(matlab_dir_label, 0, 0, 1, 6)
self.matlab_path = QtWidgets.QLineEdit(self)
self.matlab_path.setText(MatlabHelper().matlab_dir)
self.matlab_path.setReadOnly(True)
Expand All @@ -155,24 +152,39 @@ def __init__(self):

browse_button = QtWidgets.QPushButton(QtGui.QIcon(IconEngine("browse-light.png")), "Browse")
browse_button.clicked.connect(self.open_folder_selector)
form_layout.addWidget(self.matlab_path, 0, 0, 1, 4)
form_layout.addWidget(browse_button, 0, 4, 1, 1)
form_layout.addWidget(self.matlab_path, 1, 0, 1, 5)
form_layout.addWidget(browse_button, 1, 5, 1, 1)

main_layout = QtWidgets.QVBoxLayout()
if not getattr(sys, "frozen", False):
self.setEnabled(False)
main_layout.addWidget(
QtWidgets.QLabel(
"<b>The current matlab path can only be changed when running in bundle.<br/>"
"For non-bundle, You can change which Matlab to use by pip installing a <br/>"
"different version of matlabengine."
)
browse_button.setEnabled(False)
desc_text = (
"<b>The current matlab path can only be changed when running in bundle.<br/>"
"For non-bundle, You can change which Matlab to use by pip installing a "
"different version <br/>of matlabengine.</b>"
)
main_layout.addLayout(label_layout)
main_layout.addLayout(form_layout)
main_layout.addStretch(1)

self.setLayout(main_layout)
matlab_dir_label.setText(f"{matlab_dir_label.text()}<br/>{desc_text}")

desc_label = QtWidgets.QLabel(
"MATLAB RAT Directory (Optional):<br/>"
"<i>Running fully in MATLAB can provide more performance for custom files.</i>"
)
form_layout.addWidget(desc_label, 3, 0, 1, 6)
self.rat_path = QtWidgets.QLineEdit(self)
self.rat_path.setText(get_global_settings().value("matlab_rat_path", ""))
self.rat_path.setReadOnly(True)
self.rat_path.setPlaceholderText("Select MATLAB RAT directory")
self.rat_path.setFocusPolicy(QtCore.Qt.FocusPolicy.NoFocus)

browse_button = QtWidgets.QPushButton("Browse")
browse_button.clicked.connect(lambda: self.set_matlab_rat_dir(clear=False))
clear_button = QtWidgets.QPushButton("Clear")
clear_button.clicked.connect(lambda: self.set_matlab_rat_dir(clear=True))
form_layout.addWidget(self.rat_path, 4, 0, 1, 4)
form_layout.addWidget(browse_button, 4, 4, 1, 1)
form_layout.addWidget(clear_button, 4, 5, 1, 1)
form_layout.setRowStretch(5, 1)

self.setLayout(form_layout)
self.changed = False

def open_folder_selector(self) -> None:
Expand All @@ -187,6 +199,18 @@ def open_folder_selector(self) -> None:
self.matlab_path.setText(folder_name)
self.changed = True

def set_matlab_rat_dir(self, clear=False):
if clear:
self.rat_path.setText("")
get_global_settings().remove("matlab_rat_path")
else:
folder_name = QtWidgets.QFileDialog.getExistingDirectory(
self, "Select MATLAB Directory", self.rat_path.text()
)
if folder_name:
self.rat_path.setText(folder_name)
get_global_settings().setValue("matlab_rat_path", folder_name)

def set_matlab_paths(self):
"""Update MATLAB paths in arch file."""
if not self.changed:
Expand Down
17 changes: 14 additions & 3 deletions rascal2/ui/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rascal2.core.enums import UnsavedReply
from rascal2.core.runner import LogData, RATRunner
from rascal2.core.writer import write_result_to_zipped_csvs
from rascal2.settings import update_recent_projects
from rascal2.settings import get_global_settings, update_recent_projects

from .model import InvalidResultWarning, MainWindowModel, validate_plot_data

Expand Down Expand Up @@ -243,9 +243,20 @@ def run(self):

self.model.controls.initialise_IPC()
working_dir = os.getcwd()
rat_inputs = rat.inputs.make_input(self.model.project, self.model.controls)
display_on = self.model.controls.display != rat.utils.enums.Display.Off
self.runner.set_runner_args(rat_inputs, self.model.controls.procedure, display_on, working_dir)
rat_inputs = rat.inputs.make_input(self.model.project, self.model.controls)
procedure = self.model.controls.procedure

matlab_rat_path = get_global_settings().value("matlab_rat_path", "")
if (
procedure != rat.utils.enums.Procedures.Calculate
and any([file.language == "matlab" for file in self.model.project.custom_files])
and matlab_rat_path
):
# Run in MATLAB RAT
rat_inputs = self.model.project.to_dict(), self.model.controls.model_dump()
rat_inputs[1].update({"ipc_path": self.model.controls._IPCFilePath, "matlab_rat_path": matlab_rat_path})
self.runner.set_runner_args(rat_inputs, procedure, display_on, working_dir)
self.view.terminal_widget.write("Initializing RAT Process...")
self.runner.start()

Expand Down
Loading