Skip to content

Commit

Permalink
2380 tof widget (#2427)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc authored Dec 5, 2024
2 parents cdaeafd + deaf03a commit 8a3a310
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 81 deletions.
1 change: 1 addition & 0 deletions docs/release_notes/next/dev-2380-ExperimentSetupFormWidget
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#2380: Rename Spectrum Viewer tofPropertiesGroupBox to experimentSetupGroupBox and place into a custom widget.
50 changes: 2 additions & 48 deletions mantidimaging/gui/ui/spectrum_viewer.ui
Original file line number Diff line number Diff line change
Expand Up @@ -440,54 +440,8 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="tofPropertiesGroupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>84</height>
</size>
</property>
<property name="title">
<string>Time of Flight Properties</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Flight path:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="flightPathSpinBox">
<property name="suffix">
<string/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Time delay: </string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="timeDelaySpinBox">
<property name="suffix">
<string/>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<widget class="QWidget" name="experimentSetupGroupBox"></widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down
87 changes: 87 additions & 0 deletions mantidimaging/gui/widgets/spectrum_widgets/tof_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Copyright (C) 2024 ISIS Rutherford Appleton Laboratory UKRI
# SPDX - License - Identifier: GPL-3.0-or-later

from PyQt5 import QtCore, QtWidgets


class ExperimentSetupFormWidget(QtWidgets.QGroupBox):
"""
A custom Qt widget for setting up experiment properties related to Time of Flight (ToF).
This widget contains input fields for flight path and time delay.
Attributes:
timeDelayLabel: Label for the time delay input field.
flightPathSpinBox: Spin box for inputting the flight path value.
flightPathLabel: Label for the flight path input field.
timeDelaySpinBox: Spin box for inputting the time delay value.
"""

def __init__(self, parent: QtWidgets.QWidget = None):
super().__init__(parent)
self.setupUi()

def setupUi(self):
self.setObjectName("experimentSetupGroupBox")

self.resize(300, 94)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.MinimumExpanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())

target = self.parent() if self.parent() else self
target.setSizePolicy(sizePolicy)
target.setMinimumSize(QtCore.QSize(300, 94))
target.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)

layout = QtWidgets.QGridLayout(self)

self.flightPathLabel = QtWidgets.QLabel(self)
self.flightPathLabel.setObjectName("flightPathLabel")
self.flightPathLabel.setText("Flight path: ")
layout.addWidget(self.flightPathLabel, 0, 0)

self.flightPathSpinBox = QtWidgets.QDoubleSpinBox(self)
self.flightPathSpinBox.setSuffix(" m")
self.flightPathSpinBox.setMinimum(0)
self.flightPathSpinBox.setMaximum(1e10)
self.flightPathSpinBox.setObjectName("flightPathSpinBox")
layout.addWidget(self.flightPathSpinBox, 0, 1)

self.timeDelayLabel = QtWidgets.QLabel(self)
self.timeDelayLabel.setObjectName("timeDelayLabel")
self.timeDelayLabel.setText("Time delay: ")
layout.addWidget(self.timeDelayLabel, 1, 0)

self.timeDelaySpinBox = QtWidgets.QDoubleSpinBox(self)
self.timeDelaySpinBox.setSuffix(" \u03BCs")
self.timeDelaySpinBox.setMaximum(1e10)
self.timeDelaySpinBox.setObjectName("timeDelaySpinBox")
layout.addWidget(self.timeDelaySpinBox, 1, 1)

self.setWindowTitle("Time of Flight Properties")
self.setTitle("Time of Flight Properties")

QtCore.QMetaObject.connectSlotsByName(self)

@property
def flight_path(self) -> float:
return self.flightPathSpinBox.value()

@flight_path.setter
def flight_path(self, value: float):
self.flightPathSpinBox.setValue(value)

@property
def time_delay(self) -> float:
return self.timeDelaySpinBox.value()

@time_delay.setter
def time_delay(self, value: float):
self.timeDelaySpinBox.setValue(value)

def connect_value_changed(self, handler: QtCore.pyqtSlot):
self.flightPathSpinBox.valueChanged.connect(handler)
self.timeDelaySpinBox.valueChanged.connect(handler)
14 changes: 5 additions & 9 deletions mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ def handle_sample_change(self, uuid: UUID | None) -> None:
def reset_units_menu(self) -> None:
if self.model.tof_data is None:
self.view.tof_mode_select_group.setEnabled(False)
self.view.tofPropertiesGroupBox.setEnabled(False)
self.view.experimentSetupGroupBox.setEnabled(False)
self.model.tof_mode = ToFUnitMode.IMAGE_NUMBER
self.change_selected_menu_option("Image Index")
self.view.tof_mode_select_group.setEnabled(False)
else:
self.view.tof_mode_select_group.setEnabled(True)
self.view.tofPropertiesGroupBox.setEnabled(True)
self.view.experimentSetupGroupBox.setEnabled(True)

def handle_normalise_stack_change(self, normalise_uuid: UUID | None) -> None:
if normalise_uuid == self.current_norm_stack_uuid:
Expand Down Expand Up @@ -429,13 +429,9 @@ def refresh_spectrum_plot(self) -> None:
self.view.spectrum_widget.spectrum_plot_widget.set_image_index_range_label(*self.model.tof_range)
self.view.auto_range_image()

def handle_flight_path_change(self) -> None:
self.model.units.target_to_camera_dist = self.view.flightPathSpinBox.value()
self.model.set_relevant_tof_units()
self.refresh_spectrum_plot()

def handle_time_delay_change(self) -> None:
self.model.units.data_offset = self.view.timeDelaySpinBox.value() * 1e-6
def handle_experiment_setup_properties_change(self) -> None:
self.model.units.target_to_camera_dist = self.view.experimentSetupFormWidget.flight_path
self.model.units.data_offset = self.view.experimentSetupFormWidget.time_delay * 1e-6
self.model.set_relevant_tof_units()
self.refresh_spectrum_plot()

Expand Down
21 changes: 11 additions & 10 deletions mantidimaging/gui/windows/spectrum_viewer/test/presenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from mantidimaging.gui.windows.spectrum_viewer.model import ErrorMode, ToFUnitMode, ROI_RITS, SpecType
from mantidimaging.gui.windows.spectrum_viewer.presenter import ExportMode
from mantidimaging.gui.windows.spectrum_viewer.spectrum_widget import SpectrumWidget, SpectrumPlotWidget, SpectrumROI
from mantidimaging.gui.widgets.spectrum_widgets.tof_properties import ExperimentSetupFormWidget
from mantidimaging.test_helpers import mock_versions, start_qapplication
from mantidimaging.test_helpers.unit_test_helper import generate_images

Expand All @@ -40,7 +41,9 @@ def setUp(self) -> None:
self.view.addBtn = mock.create_autospec(QPushButton)
self.view.exportTabs = mock.create_autospec(QTabWidget)
self.view.tof_mode_select_group = mock.create_autospec(QActionGroup)
self.view.tofPropertiesGroupBox = mock.create_autospec(QGroupBox)
self.view.experimentSetupGroupBox = mock.create_autospec(QGroupBox)
self.view.experimentSetupFormWidget = mock.Mock(spec=ExperimentSetupFormWidget)
self.view.experimentSetupFormWidget.time_delay = 0.0
self.presenter = SpectrumViewerWindowPresenter(self.view, self.main_window)

def test_get_dataset_id_for_stack_no_stack_id(self):
Expand Down Expand Up @@ -360,7 +363,7 @@ def test_WHEN_switch_between_no_spectra_to_spectra_files_THEN_tof_modes_availabi
self.presenter.handle_sample_change(uuid.uuid4())
expected_calls = [mock.call(b) for b in expected_calls]
self.view.tof_mode_select_group.setEnabled.assert_has_calls(expected_calls)
self.view.tofPropertiesGroupBox.setEnabled.assert_has_calls(expected_calls)
self.view.experimentSetupGroupBox.setEnabled.assert_has_calls(expected_calls)
self.assertEqual(self.presenter.model.tof_mode, expected_mode)

def test_WHEN_no_stack_available_THEN_units_menu_disabled(self):
Expand All @@ -369,18 +372,16 @@ def test_WHEN_no_stack_available_THEN_units_menu_disabled(self):
self.view.tof_mode_select_group.setEnabled.assert_called_once_with(False)

def test_WHEN_tof_flight_path_changed_THEN_unit_conversion_flight_path_set(self):
self.view.flightPathSpinBox = mock.Mock()
self.view.flightPathSpinBox.value.return_value = 10
self.view.experimentSetupFormWidget.flight_path = 10.0
self.presenter.refresh_spectrum_plot = mock.Mock()
self.presenter.handle_flight_path_change()
self.assertEqual(self.presenter.model.units.target_to_camera_dist, 10)
self.presenter.handle_experiment_setup_properties_change()
self.assertEqual(self.presenter.model.units.target_to_camera_dist, 10.0)

def test_WHEN_tof_delay_changed_THEN_unit_conversion_delay_set(self):
self.view.timeDelaySpinBox = mock.Mock()
self.view.timeDelaySpinBox.value.return_value = 400
self.view.experimentSetupFormWidget.time_delay = 400.00
self.presenter.refresh_spectrum_plot = mock.Mock()
self.presenter.handle_time_delay_change()
self.assertEqual(self.presenter.model.units.data_offset, 400 * 1e-6)
self.presenter.handle_experiment_setup_properties_change()
self.assertEqual(float(self.presenter.model.units.data_offset), 400.00 * 1e-6)

def test_WHEN_menu_option_selected_THEN_menu_option_changed(self):
menu_options = [QAction("opt1"), QAction("opt2"), QAction("opt3"), QAction("opt4")]
Expand Down
21 changes: 7 additions & 14 deletions mantidimaging/gui/windows/spectrum_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QCheckBox, QVBoxLayout, QFileDialog, QPushButton, QLabel, QAbstractItemView, QHeaderView, \
QTabWidget, QComboBox, QSpinBox, QTableWidget, QTableWidgetItem, QGroupBox, QActionGroup, QAction, QDoubleSpinBox
QTabWidget, QComboBox, QSpinBox, QTableWidget, QTableWidgetItem, QGroupBox, QActionGroup, QAction
from PyQt5.QtCore import QSignalBlocker, Qt, QModelIndex

from mantidimaging.core.utility import finder
Expand All @@ -18,7 +18,7 @@
from mantidimaging.gui.widgets import RemovableRowTableView
from .spectrum_widget import SpectrumWidget
from mantidimaging.gui.windows.spectrum_viewer.roi_table_model import TableModel

from mantidimaging.gui.widgets.spectrum_widgets.tof_properties import ExperimentSetupFormWidget
import numpy as np

if TYPE_CHECKING:
Expand Down Expand Up @@ -55,9 +55,8 @@ class SpectrumViewerWindowView(BaseMainWindowView):

number_roi_properties_procced: int = 0

tofPropertiesGroupBox: QGroupBox
flightPathSpinBox: QDoubleSpinBox
timeDelaySpinBox: QDoubleSpinBox
experimentSetupGroupBox: QGroupBox
experimentSetupFormWidget: ExperimentSetupFormWidget

def __init__(self, main_window: MainWindowView):
super().__init__(None, 'gui/ui/spectrum_viewer.ui')
Expand Down Expand Up @@ -174,15 +173,9 @@ def __init__(self, main_window: MainWindowView):
self.current_roi_name = self.last_clicked_roi = self.roi_table_model.roi_names()[0]
self.set_roi_properties()

self.flightPathSpinBox.setMinimum(0)
self.flightPathSpinBox.setMaximum(1e10)
self.flightPathSpinBox.setValue(56.4)
self.flightPathSpinBox.setSuffix(" m")
self.timeDelaySpinBox.setMaximum(1e10)
self.timeDelaySpinBox.setSuffix(" \u03BCs")

self.flightPathSpinBox.valueChanged.connect(self.presenter.handle_flight_path_change)
self.timeDelaySpinBox.valueChanged.connect(self.presenter.handle_time_delay_change)
self.experimentSetupFormWidget = ExperimentSetupFormWidget(self.experimentSetupGroupBox)
self.experimentSetupFormWidget.flight_path = 56.4
self.experimentSetupFormWidget.connect_value_changed(self.presenter.handle_experiment_setup_properties_change)

def on_row_change(item: QModelIndex, _: Any) -> None:
"""
Expand Down

0 comments on commit 8a3a310

Please sign in to comment.