Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions rascal2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class Settings(BaseModel, validate_assignment=True, arbitrary_types_allowed=True
live_recalculate: bool = Field(
default=True, title=SettingsGroups.General, description="Auto-run simulation when parameter values change."
)
show_stop_calculation_warning: bool = Field(
default=True, title=SettingsGroups.General, description="Warn when Bayesian calculation is stopped manually."
)

clear_terminal: bool = Field(
default=True, title=SettingsGroups.Terminal, description="Clear Terminal when Run Starts"
Expand Down
11 changes: 10 additions & 1 deletion rascal2/ui/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,13 @@ def export_fits(self):

def interrupt_terminal(self):
"""Send an interrupt signal to the RAT runner."""
self.runner.interrupt()
if self.model.controls.procedure in [rat.utils.enums.Procedures.Simplex, rat.utils.enums.Procedures.DE]:
self.model.controls.sendStopEvent()
else:
if not self.view.show_confirm_stop_calculation_dialog():
return
if self.runner.process.is_alive():
self.runner.interrupt()

def quick_run(self, project=None):
"""Run rat calculation with calculate procedure on the given project.
Expand Down Expand Up @@ -206,6 +212,7 @@ def run(self):
# hide bayes plots button so users can't open plots during run
self.view.plot_widget.bayes_plots_button.setVisible(False)

self.model.controls.initialise_IPC()
rat_inputs = rat.inputs.make_input(self.model.project, self.model.controls)
display_on = self.model.controls.display != rat.utils.enums.Display.Off

Expand All @@ -226,6 +233,7 @@ def handle_results(self):
)
)
self.view.handle_results(self.runner.results)
self.model.controls.delete_IPC()

def handle_interrupt(self):
"""Handle a RAT run being interrupted."""
Expand All @@ -234,6 +242,7 @@ def handle_interrupt(self):
else:
self.view.logging.error("RAT run failed with exception.\n", exc_info=self.runner.error)
self.view.handle_results()
self.model.controls.delete_IPC()

def handle_event(self):
"""Handle event data produced by the RAT run."""
Expand Down
30 changes: 30 additions & 0 deletions rascal2/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,36 @@ def show_confirm_dialog(self, title: str, message: str) -> bool:

return reply == QtWidgets.QMessageBox.StandardButton.Ok

def show_confirm_stop_calculation_dialog(self) -> bool:
"""Ask the user to confirm stopping the calculation.

Returns
-------
bool
Whether the confirmation was affirmative.
"""
if not self.settings.show_stop_calculation_warning:
return True

message_box = QtWidgets.QMessageBox(self)
message_box.setWindowTitle("Confirm Stop?")
message_box.setText("For calculate and Bayesian procedures, stopping will lose all progress. Confirm Stop?")
message_box.setIcon(QtWidgets.QMessageBox.Icon.Question)

yes_button = message_box.addButton("Stop", QtWidgets.QMessageBox.ButtonRole.YesRole)
no_button = message_box.addButton(QtWidgets.QMessageBox.StandardButton.Cancel)
message_box.setDefaultButton(no_button)

no_show_check_box = QtWidgets.QCheckBox("Do not show this again")
message_box.setCheckBox(no_show_check_box)
no_show_check_box.toggled.connect(lambda val: setattr(self.settings, "show_stop_calculation_warning", not val))

# Make this save to general settings

message_box.exec()

return message_box.clickedButton() == yes_button

def show_unsaved_dialog(self, message: str) -> UnsavedReply:
"""Warn the user of unsaved changes, and ask whether to save those changes.

Expand Down
5 changes: 1 addition & 4 deletions rascal2/widgets/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,6 @@ def plot(self, project: ratapi.Project, results: ratapi.outputs.Results | ratapi
results : Union[ratapi.outputs.Results, ratapi.outputs.BayesResults]
The calculation results.
"""
if project is None or results is None:
self.clear()
return

data = ratapi.events.PlotEventData()

data.modelType = project.model
Expand Down Expand Up @@ -456,6 +452,7 @@ def plot_event(self, data: ratapi.events.PlotEventData | None = None):
return

show_legend = self.show_legend.isChecked() if self.current_plot_data.contrastNames else False
self.figure.clear()
self.update_figure_size()
self.figure.tight_layout()
ratapi.plotting.plot_ref_sld_helper(
Expand Down
25 changes: 22 additions & 3 deletions tests/ui/test_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,31 @@ def test_controls_validation_error(presenter, param, value):
@patch("rascal2.ui.presenter.RATRunner")
def test_run_and_interrupt(mock_runner, mock_inputs, presenter):
"""Test that the runner can be started and interrupted."""
assert presenter.model.controls._IPCFilePath == ""
presenter.run()
presenter.interrupt_terminal()

mock_inputs.assert_called_once()
presenter.runner.start.assert_called_once()
presenter.runner.interrupt.assert_called_once()
assert presenter.model.controls._IPCFilePath != ""

presenter.view.show_confirm_stop_calculation_dialog = MagicMock(return_value=False)
presenter.interrupt_terminal()
presenter.runner.interrupt.assert_not_called()

presenter.view.show_confirm_stop_calculation_dialog = MagicMock(return_value=True)
presenter.runner.process.is_alive.return_value = False
presenter.interrupt_terminal()
presenter.runner.interrupt.assert_not_called()

presenter.runner.process.is_alive.return_value = True
presenter.interrupt_terminal()
presenter.runner.interrupt.assert_called()

with open(presenter.model.controls._IPCFilePath) as f:
assert f.readline() == "\x00"
presenter.model.controls.procedure = "simplex"
presenter.interrupt_terminal()
with open(presenter.model.controls._IPCFilePath) as f:
assert f.readline() == "\x01"


@patch("rascal2.core.commands.SaveCalculationOutputs")
Expand Down
Loading