Skip to content

Commit

Permalink
requested changes made
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSullivan7 committed May 2, 2024
1 parent 746cc34 commit d095920
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 68 deletions.
40 changes: 33 additions & 7 deletions mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import csv
from enum import Enum
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, TypedDict

import numpy as np
from math import ceil
Expand Down Expand Up @@ -52,6 +52,31 @@ def get_by_value(cls, value: str) -> ErrorMode:
raise ValueError(f"Unknown error mode: {value}")


class AllowedModesTypedDict(TypedDict):
mode: ToFUnitMode
label: str


allowed_modes: dict[str, AllowedModesTypedDict] = {
"Image Index": {
"mode": ToFUnitMode.IMAGE_NUMBER,
"label": "Image index"
},
"Wavelength": {
"mode": ToFUnitMode.WAVELENGTH,
"label": "Neutron Wavelength (\u212B)"
},
"Energy": {
"mode": ToFUnitMode.ENERGY,
"label": "Neutron Energy (MeV)"
},
"Time of Flight (\u03BCs)": {
"mode": ToFUnitMode.TOF_US,
"label": "Time of Flight (\u03BCs)"
}
}


class SpectrumViewerWindowModel:
"""
The model for the spectrum viewer window.
Expand All @@ -75,9 +100,6 @@ def __init__(self, presenter: SpectrumViewerWindowPresenter):
self._roi_ranges = {}
self.special_roi_list = [ROI_ALL]

self.tof_data = self.get_stack_time_of_flight()
self.set_tof_unit_mode_for_stack()

self.units = UnitConversion()

def roi_name_generator(self) -> str:
Expand Down Expand Up @@ -479,10 +501,14 @@ def set_relevant_tof_units(self) -> None:
self.tof_plot_range = (self.tof_data.min(), self.tof_data.max())
self.tof_range = (0, self.tof_data.size)

def set_tof_unit_mode_for_stack(self):
def set_tof_unit_mode_for_stack(self) -> None:
if self.get_stack_time_of_flight() is None or self.tof_data is None:
self.tof_mode = ToFUnitMode.IMAGE_NUMBER
elif self.tof_mode is not ToFUnitMode.WAVELENGTH or self.tof_mode is not ToFUnitMode.IMAGE_NUMBER:
pass
self.presenter.change_selected_menu_option("Image Index")
elif self.tof_mode == ToFUnitMode.ENERGY:
self.presenter.change_selected_menu_option("Energy")
elif self.tof_mode == ToFUnitMode.TOF_US:
self.presenter.change_selected_menu_option("Time of Flight (\u03BCs)")
else:
self.tof_mode = ToFUnitMode.WAVELENGTH
self.presenter.change_selected_menu_option("Wavelength")
40 changes: 24 additions & 16 deletions mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from mantidimaging.gui.dialogs.async_task import start_async_task_view, TaskWorkerThread
from mantidimaging.gui.mvp_base import BasePresenter
from mantidimaging.gui.windows.spectrum_viewer.model import SpectrumViewerWindowModel, SpecType, ROI_RITS, ErrorMode, \
ToFUnitMode
ToFUnitMode, allowed_modes

if TYPE_CHECKING:
from mantidimaging.gui.windows.spectrum_viewer.view import SpectrumViewerWindowView # pragma: no cover
Expand Down Expand Up @@ -67,8 +67,10 @@ def handle_stack_changed(self) -> None:
except RuntimeError:
norm_stack = None
self.model.set_normalise_stack(norm_stack)
self.reset_units_menu()

self.model.set_tof_unit_mode_for_stack()
self.reset_units_menu()

self.handle_tof_unit_change()
self.show_new_sample()
self.redraw_all_rois()
Expand All @@ -93,8 +95,9 @@ def handle_sample_change(self, uuid: UUID | None) -> None:
return

self.model.set_stack(self.main_window.get_stack(uuid))
self.reset_units_menu()
self.model.set_tof_unit_mode_for_stack()
self.reset_units_menu()

self.handle_tof_unit_change()
normalise_uuid = self.view.get_normalise_stack()
if normalise_uuid is not None:
Expand All @@ -111,23 +114,19 @@ def handle_sample_change(self, uuid: UUID | None) -> None:
self.view.on_visibility_change()

def reset_units_menu(self):
print(f"self.model.tof_data: {self.model.tof_data}")
if self.model.tof_data is None:
print(f"here 1 {self.model.tof_mode}")
self.view.tof_mode_select_group.setEnabled(False)
self.view.tofPropertiesGroupBox.setEnabled(False)
else:
self.view.tof_mode_select_group.setEnabled(True)
self.view.tofPropertiesGroupBox.setEnabled(True)
self.model.tof_mode = ToFUnitMode.IMAGE_NUMBER
for action in self.view.tof_mode_select_group.actions():
with QSignalBlocker(action):
if action.objectName() == 'Image Index':
action.setChecked(True)
else:
action.setChecked(False)
if self.model.tof_data is None:
self.model.tof_mode = ToFUnitMode.IMAGE_NUMBER
self.change_selected_menu_option("Image Index")
self.view.tof_mode_select_group.setEnabled(False)
else:
print(f"here 2 {self.model.tof_mode}")
self.view.tof_mode_select_group.setEnabled(True)
self.view.tofPropertiesGroupBox.setEnabled(True)
print(f"here 3 {self.model.tof_mode}")

def handle_normalise_stack_change(self, normalise_uuid: UUID | None) -> None:
if normalise_uuid == self.current_norm_stack_uuid:
Expand Down Expand Up @@ -173,6 +172,7 @@ def show_new_sample(self) -> None:
self.view.set_roi_properties()

def handle_range_slide_moved(self, tof_range) -> None:
print(f"self.model.tof_mode: {self.model.tof_mode}")
self.model.tof_plot_range = tof_range
if self.model.tof_mode == ToFUnitMode.IMAGE_NUMBER:
self.model.tof_range = (int(tof_range[0]), int(tof_range[1]))
Expand Down Expand Up @@ -355,12 +355,12 @@ def handle_export_tab_change(self, index: int) -> None:

def handle_tof_unit_change(self) -> None:
self.model.set_relevant_tof_units()
tof_axis_label = self.view.allowed_modes[self.view.tof_units_mode]["label"]
tof_axis_label = allowed_modes[self.view.tof_units_mode]["label"]
self.view.spectrum_widget.spectrum_plot_widget.set_tof_axis_label(tof_axis_label)
self.refresh_spectrum_plot()

def handle_tof_unit_change_via_menu(self) -> None:
self.model.tof_mode = self.view.allowed_modes[self.view.tof_units_mode]["mode"]
self.model.tof_mode = allowed_modes[self.view.tof_units_mode]["mode"]
self.handle_tof_unit_change()

def refresh_spectrum_plot(self) -> None:
Expand All @@ -380,3 +380,11 @@ def handle_time_delay_change(self) -> None:
self.model.units.data_offset = self.view.timeDelaySpinBox.value() * 1e-6
self.model.set_relevant_tof_units()
self.refresh_spectrum_plot()

def change_selected_menu_option(self, opt):
for action in self.view.tof_mode_select_group.actions():
with QSignalBlocker(action):
if action.objectName() == opt:
action.setChecked(True)
else:
action.setChecked(False)
20 changes: 1 addition & 19 deletions mantidimaging/gui/windows/spectrum_viewer/test/presenter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,6 @@ def setUp(self) -> None:
self.view.tofPropertiesGroupBox = mock.create_autospec(QGroupBox)
self.presenter = SpectrumViewerWindowPresenter(self.view, self.main_window)

self.view.allowed_modes = {
"Image Index": {
"mode": ToFUnitMode.IMAGE_NUMBER,
"label": "Image index"
},
"Wavelength": {
"mode": ToFUnitMode.WAVELENGTH,
"label": "Neutron Wavelength (\u212B)"
},
"Energy": {
"mode": ToFUnitMode.ENERGY,
"label": "Neutron Energy (MeV)"
},
"Time of Flight (\u03BCs)": {
"mode": ToFUnitMode.TOF_US,
"label": "Time of Flight (\u03BCs)"
}
}

def test_get_dataset_id_for_stack_no_stack_id(self):
self.assertIsNone(self.presenter.get_dataset_id_for_stack(None))

Expand Down Expand Up @@ -192,6 +173,7 @@ def test_gui_changes_tof_range(self):
image_stack = generate_images([30, 11, 12])
new_tof_range = (10, 20)
self.presenter.model.set_stack(image_stack)
self.presenter.model.tof_mode = ToFUnitMode.IMAGE_NUMBER
self.presenter.handle_range_slide_moved(new_tof_range)

self.assertEqual(self.presenter.model.tof_range, new_tof_range)
Expand Down
29 changes: 3 additions & 26 deletions mantidimaging/gui/windows/spectrum_viewer/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from __future__ import annotations

from pathlib import Path
from typing import TYPE_CHECKING, TypedDict
from typing import TYPE_CHECKING

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QCheckBox, QVBoxLayout, QFileDialog, QPushButton, QLabel, QAbstractItemView, QHeaderView, \
Expand All @@ -13,7 +13,7 @@
from mantidimaging.core.utility import finder
from mantidimaging.gui.mvp_base import BaseMainWindowView
from mantidimaging.gui.widgets.dataset_selector import DatasetSelectorWidgetView
from .model import ROI_RITS, ToFUnitMode
from .model import ROI_RITS, allowed_modes
from .presenter import SpectrumViewerWindowPresenter, ExportMode
from mantidimaging.gui.widgets import RemovableRowTableView
from .spectrum_widget import SpectrumWidget
Expand All @@ -27,11 +27,6 @@
from uuid import UUID


class AllowedModesTypedDict(TypedDict):
mode: ToFUnitMode
label: str


class SpectrumViewerWindowView(BaseMainWindowView):
tableView: RemovableRowTableView
sampleStackSelector: DatasetSelectorWidgetView
Expand Down Expand Up @@ -94,25 +89,7 @@ def __init__(self, main_window: MainWindowView):
self.units_menu = self.spectrum_right_click_menu.addMenu("Units")
self.tof_mode_select_group = QActionGroup(self)

self.allowed_modes: dict[str, AllowedModesTypedDict] = {
"Image Index": {
"mode": ToFUnitMode.IMAGE_NUMBER,
"label": "Image index"
},
"Wavelength": {
"mode": ToFUnitMode.WAVELENGTH,
"label": "Neutron Wavelength (\u212B)"
},
"Energy": {
"mode": ToFUnitMode.ENERGY,
"label": "Neutron Energy (MeV)"
},
"Time of Flight (\u03BCs)": {
"mode": ToFUnitMode.TOF_US,
"label": "Time of Flight (\u03BCs)"
}
}
for mode in self.allowed_modes.keys():
for mode in allowed_modes.keys():
action = QAction(mode, self.tof_mode_select_group)
action.setCheckable(True)
action.setObjectName(mode)
Expand Down

0 comments on commit d095920

Please sign in to comment.