Skip to content

Commit

Permalink
Refactored set_roi into the presenter.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ashmeigh committed Jan 8, 2025
1 parent 7e9d70a commit 3882445
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
14 changes: 6 additions & 8 deletions mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ def set_stack(self, stack: ImageStack | None) -> None:
self.tof_range = (0, stack.data.shape[0] - 1)
self.tof_range_full = self.tof_range
self.tof_data = self.get_stack_time_of_flight()
self.set_new_roi(ROI_ALL)
height, width = self.get_image_shape()
self._roi_ranges[ROI_ALL] = SensibleROI.from_list([0, 0, width, height])

def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None:
self._normalise_stack = normalise_stack

def set_new_roi(self, name: str) -> None:
"""
Expand All @@ -137,13 +141,7 @@ def set_new_roi(self, name: str) -> None:
@param name: The name of the new ROI
"""
height, width = self.get_image_shape()
self.set_roi(name, SensibleROI.from_list([0, 0, width, height]))

def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None:
self._normalise_stack = normalise_stack

def set_roi(self, roi_name: str, roi: SensibleROI) -> None:
self._roi_ranges[roi_name] = roi
self._roi_ranges[name] = SensibleROI.from_list([0, 0, width, height])

def get_averaged_image(self) -> np.ndarray | None:
"""
Expand Down
4 changes: 2 additions & 2 deletions mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def handle_roi_moved(self, force_new_spectrums: bool = False) -> None:
"""
for name in self.view.spectrum_widget.roi_dict:
current_roi = self.view.spectrum_widget.get_roi(name)
self.model.set_roi(name, current_roi)
self.view.spectrum_widget.set_roi(name, current_roi)
if force_new_spectrums:
spectrum = self.model.get_spectrum(
current_roi,
Expand Down Expand Up @@ -429,7 +429,7 @@ def change_selected_menu_option(self, opt: str) -> None:

def do_adjust_roi(self) -> None:
new_roi = self.convert_spinbox_roi_to_SensibleROI(self.view.roiPropertiesSpinBoxes)
self.model.set_roi(self.view.current_roi_name, new_roi)
self.view.spectrum_widget.set_roi(self.view.current_roi_name, new_roi)
self.view.spectrum_widget.adjust_roi(new_roi, self.view.current_roi_name)

def handle_storing_current_roi_name_on_tab_change(self) -> None:
Expand Down
10 changes: 10 additions & 0 deletions mantidimaging/gui/windows/spectrum_viewer/spectrum_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ def get_roi(self, roi_name: str) -> SensibleROI:
else:
raise KeyError(f"ROI with name {roi_name} does not exist in self.roi_dict or and is not 'all'")

def set_roi(self, roi_name: str, new_roi: SensibleROI) -> None:
if roi_name not in self.roi_dict:
raise KeyError(f"ROI '{roi_name}' does not exist.")
roi = self.roi_dict[roi_name]

roi.blockSignals(True)
roi.setPos((new_roi.left, new_roi.top))
roi.setSize((new_roi.width, new_roi.height))
roi.blockSignals(False)

def remove_roi(self, roi_name: str) -> None:
"""
Remove a given ROI by name unless it is 'roi' or 'all'.
Expand Down
6 changes: 3 additions & 3 deletions mantidimaging/gui/windows/spectrum_viewer/test/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def test_save_rits_roi_dat(self):
norm = ImageStack(np.full([10, 11, 12], 2))
stack.data[:, :, :5] *= 2
self.model.set_new_roi("rits_roi")
self.model.set_roi("rits_roi", SensibleROI.from_list([0, 0, 10, 11]))
self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([0, 0, 10, 11])
self.model.set_normalise_stack(norm)

self.model._roi_ranges["ROI_RITS"] = SensibleROI.from_list([0, 0, 10, 11])
Expand Down Expand Up @@ -475,7 +475,7 @@ def test_save_single_rits_spectrum(self, mock_save_rits_roi):
norm = ImageStack(np.full([10, 11, 12], 2))
stack.data[:, :, :5] *= 2
self.model.set_new_roi("rits_roi")
self.model.set_roi("rits_roi", SensibleROI.from_list([0, 0, 5, 5]))
self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([0, 0, 5, 5])
self.model.set_normalise_stack(norm)
self.model._roi_ranges["ROI_RITS"] = SensibleROI.from_list([0, 0, 5, 5])

Expand All @@ -490,7 +490,7 @@ def test_save_rits_correct_transmision(self, mock_save_rits_roi):
for i in range(10):
stack.data[:, :, i] *= i
self.model.set_new_roi("rits_roi")
self.model.set_roi("rits_roi", SensibleROI.from_list([1, 0, 6, 4]))
self.model._roi_ranges["rits_roi"] = SensibleROI.from_list([1, 0, 6, 4])
self.model.set_normalise_stack(norm)
mock_path = mock.create_autospec(Path, instance=True)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ def test_WHEN_roi_removed_THEN_roi_name_removed_from_list_of_roi_names(self):
self.spectrum_widget.remove_roi("new_roi")
self.assertListEqual(list(self.spectrum_widget.roi_dict.keys()), ["all", "roi"])

def test_set_roi_updates_position_and_size(self):
self.spectrum_widget.roi_dict["test_roi"] = SpectrumROI("test_roi", SensibleROI(10, 20, 30, 40))
new_roi = SensibleROI(50, 60, 120, 140)
self.spectrum_widget.set_roi("test_roi", new_roi)
roi = self.spectrum_widget.roi_dict["test_roi"]
self.assertEqual(roi.pos(), Point(50, 60))
self.assertEqual(roi.size(), Point(70, 80))

def test_WHEN_remove_roi_called_with_default_roi_THEN_raise_runtime_error(self):
self.spectrum_widget.roi_dict = {"all": mock.Mock(), "roi": mock.Mock()}
with self.assertRaises(RuntimeError):
Expand Down

0 comments on commit 3882445

Please sign in to comment.