Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenNneji committed Nov 7, 2024
1 parent bac34ff commit 8b2f386
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 34 deletions.
60 changes: 44 additions & 16 deletions rascal2/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import copy
from enum import IntEnum, unique
from typing import Callable
from typing import Callable, Union

import RATapi
from PyQt6 import QtGui
Expand Down Expand Up @@ -100,20 +100,28 @@ def id(self):
return CommandID.EditProject


class SaveResults(QtGui.QUndoCommand):
"""Command for saving the Results object.
class SaveCalculationOutputs(QtGui.QUndoCommand):
"""Command for saving the updated problem, results, and log text from a calculation run.
Parameters
----------
problem : RATapi.rat_core.ProblemDefinition
The problem
The updated parameter values from a RAT run
results : Union[RATapi.outputs.Results, RATapi.outputs.BayesResults]
The calculation results.
log : str
log text from the given calculation.
presenter : MainWindowPresenter
The RasCAL main window presenter
"""

def __init__(self, problem, results, log: str, presenter):
def __init__(
self,
problem: RATapi.rat_core.ProblemDefinition,
results: Union[RATapi.outputs.Results, RATapi.outputs.BayesResults],
log: str,
presenter,
):
super().__init__()
self.presenter = presenter
self.results = results
Expand All @@ -124,8 +132,19 @@ def __init__(self, problem, results, log: str, presenter):
self.old_log = self.presenter.model.result_log
self.setText("Save calculation results")

def get_parameter_values(self, problem_definition: RATapi.rat_core.ProblemDefinition):
"""Get parameter values from problem definition."""
def get_parameter_values(self, problem: RATapi.rat_core.ProblemDefinition):
"""Gets updated parameter values from problem definition.
Parameters
----------
problem : RATapi.rat_core.ProblemDefinition
The updated parameter values from a RAT run.
Returns
-------
values : dict
A dict with updated parameter values from a RAT run.
"""
parameter_field = {
"parameters": "params",
"bulk_in": "bulkIn",
Expand All @@ -139,30 +158,39 @@ def get_parameter_values(self, problem_definition: RATapi.rat_core.ProblemDefini
values = {}
for class_list in RATapi.project.parameter_class_lists:
entry = values.setdefault(class_list, [])
entry.extend(getattr(problem_definition, parameter_field[class_list]))
entry.extend(getattr(problem, parameter_field[class_list]))
return values

def set_parameter_values(self, values):
"""Update the project given a set of results."""
def set_parameter_values(self, values: dict):
"""Updates the parameter values of the project in the main window model.
Parameters
----------
values : dict
A dict with updated parameter values from a RAT run
"""
for key, value in values.items():
for index in range(len(value)):
getattr(self.presenter.model.project, key)[index].value = value[index]
return values

def undo(self):
self.swap_results(self.old_problem, self.old_results, self.old_log)
self.update_calculation_outputs(self.old_problem, self.old_results, self.old_log)

def redo(self):
self.swap_results(self.problem, self.results, self.log)
self.update_calculation_outputs(self.problem, self.results, self.log)

def swap_results(self, problem, results, log):
"""Swap problem, result and log in model with given one
def update_calculation_outputs(
self,
problem: RATapi.rat_core.ProblemDefinition,
results: Union[RATapi.outputs.Results, RATapi.outputs.BayesResults],
log: str,
):
"""Updates the project, results and log in the main window model
Parameters
----------
problem : RATapi.rat_core.ProblemDefinition
The problem definition
The updated parameter values from a RAT run
results : Union[RATapi.outputs.Results, RATapi.outputs.BayesResults]
The calculation results.
log : str
Expand Down
15 changes: 13 additions & 2 deletions rascal2/ui/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@


class MainWindowModel(QtCore.QObject):
"""Manages project data and communicates to view via signals"""
"""Manages project data and communicates to view via signals
Emits
-----
project_updated
A signal that indicates the project has been updated.
controls_updated
A signal that indicates the control has been updated.
results_updated
A signal that indicates the project and results have been updated.
"""

project_updated = QtCore.pyqtSignal()
controls_updated = QtCore.pyqtSignal()
Expand Down Expand Up @@ -111,7 +122,7 @@ def load_r1_project(self, load_path: str):
self.save_path = str(Path(load_path).parent)

def update_controls(self, new_values: dict):
"""Updates the control attributes.
"""Update the control attributes.
Parameters
----------
Expand Down
25 changes: 13 additions & 12 deletions rascal2/ui/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def run(self):
def handle_results(self):
"""Handle a RAT run being finished."""
self.view.undo_stack.push(
commands.SaveResults(
commands.SaveCalculationOutputs(
self.runner.updated_problem,
self.runner.results,
self.view.terminal_widget.text_area.toPlainText(),
Expand All @@ -171,17 +171,18 @@ def handle_interrupt(self):
def handle_event(self):
"""Handle event data produced by the RAT run."""
event = self.runner.events.pop(0)
if isinstance(event, str):
self.view.terminal_widget.write(event)
chi_squared = get_live_chi_squared(event, str(self.model.controls.procedure))
if chi_squared is not None:
self.view.controls_widget.chi_squared.setText(chi_squared)
elif isinstance(event, RAT.events.ProgressEventData):
self.view.terminal_widget.update_progress(event)
elif isinstance(event, RAT.events.PlotEventData):
self.view.plotting_widget.plot_event(event)
elif isinstance(event, LogData):
self.view.logging.log(event.level, event.msg)
match event:
case str():
self.view.terminal_widget.write(event)
chi_squared = get_live_chi_squared(event, str(self.model.controls.procedure))
if chi_squared is not None:
self.view.controls_widget.chi_squared.setText(chi_squared)
case RAT.events.ProgressEventData():
self.view.terminal_widget.update_progress(event)
case RAT.events.PlotEventData():
self.view.plot_widget.plot_event(event)
case LogData():
self.view.logging.log(event.level, event.msg)

def edit_project(self, updated_project: dict) -> None:
"""Edit the Project with a dictionary of attributes.
Expand Down
4 changes: 2 additions & 2 deletions rascal2/ui/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self):
self.presenter = MainWindowPresenter(self)
self.mdi = QtWidgets.QMdiArea()

self.plotting_widget = PlotWidget(self)
self.plot_widget = PlotWidget(self)
self.terminal_widget = TerminalWidget()
self.controls_widget = ControlsWidget(self)
self.project_widget = ProjectWidget(self)
Expand Down Expand Up @@ -247,7 +247,7 @@ def setup_mdi(self):
return

widgets = {
"Plots": self.plotting_widget,
"Plots": self.plot_widget,
"Project": self.project_widget,
"Terminal": self.terminal_widget,
"Fitting Controls": self.controls_widget,
Expand Down
2 changes: 1 addition & 1 deletion rascal2/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rascal2.widgets.controls import ControlsWidget
from rascal2.widgets.inputs import AdaptiveDoubleSpinBox, get_validated_input
from rascal2.widgets.plotter import PlotWidget
from rascal2.widgets.plot import PlotWidget
from rascal2.widgets.terminal import TerminalWidget

__all__ = ["ControlsWidget", "AdaptiveDoubleSpinBox", "get_validated_input", "PlotWidget", "TerminalWidget"]
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/test_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import RATapi
from PyQt6 import QtWidgets

from rascal2.widgets.plotter import PlotWidget
from rascal2.widgets.plot import PlotWidget


class MockWindowView(QtWidgets.QMainWindow):
Expand Down

0 comments on commit 8b2f386

Please sign in to comment.