Skip to content

Commit

Permalink
Refactored set_new_roi into the presenter.py (#2451)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc authored Jan 9, 2025
2 parents 6454a32 + 165971f commit 3f099d2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 31 deletions.
9 changes: 0 additions & 9 deletions mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,6 @@ def set_stack(self, stack: ImageStack | None) -> None:
def set_normalise_stack(self, normalise_stack: ImageStack | None) -> None:
self._normalise_stack = normalise_stack

def set_new_roi(self, name: str) -> None:
"""
Sets a new ROI with the given name
@param name: The name of the new ROI
"""
height, width = self.get_image_shape()
self._roi_ranges[name] = SensibleROI.from_list([0, 0, width, height])

def get_averaged_image(self) -> np.ndarray | None:
"""
Get the averaged image from the stack in the model returning as a numpy array
Expand Down
9 changes: 3 additions & 6 deletions mantidimaging/gui/windows/spectrum_viewer/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,8 @@ def do_add_roi(self) -> None:
roi_name = self.model.roi_name_generator()
if roi_name in self.view.spectrum_widget.roi_dict:
raise ValueError(f"ROI name already exists: {roi_name}")
self.model.set_new_roi(roi_name)
roi = self.model._roi_ranges.get(roi_name)
if roi is None:
raise ValueError(f"ROI for {roi_name} is not valid.")
height, width = self.model.get_image_shape()
roi = SensibleROI.from_list([0, 0, width, height])
self.view.spectrum_widget.add_roi(roi, roi_name)
spectrum = self.model.get_spectrum(roi, self.spectrum_mode, self.view.shuttercount_norm_enabled())
self.view.set_spectrum(roi_name, spectrum)
Expand All @@ -351,8 +349,7 @@ def change_roi_colour(self, roi_name: str, new_colour: tuple[int, int, int]) ->
self.view.on_visibility_change()

def add_rits_roi(self) -> None:
self.model.set_new_roi(ROI_RITS)
roi = self.model._roi_ranges[ROI_RITS]
roi = self.model._roi_ranges.setdefault(ROI_RITS, SensibleROI.from_list([0, 0, *self.model.get_image_shape()]))
self.view.spectrum_widget.add_roi(roi, ROI_RITS)
self.view.set_spectrum(ROI_RITS,
self.model.get_spectrum(roi, self.spectrum_mode, self.view.shuttercount_norm_enabled()))
Expand Down
23 changes: 7 additions & 16 deletions mantidimaging/gui/windows/spectrum_viewer/test/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def _set_sample_stack(self, with_tof=False, with_shuttercount=False):
mock_shuttercounts.get_column.return_value = np.arange(5, 15)
stack._shutter_count_file = mock_shuttercounts
self.model.set_stack(stack)
self.model.set_new_roi("roi")
height, width = stack.data.shape[1], stack.data.shape[2]
self.model._roi_ranges["roi"] = SensibleROI.from_list([0, 0, width, height])
return stack, spectrum

def _set_normalise_stack(self, with_shuttercount=False):
Expand Down Expand Up @@ -227,15 +228,14 @@ def test_save_rits_roi_dat(self):
stack, _ = self._set_sample_stack(with_tof=True)
norm = ImageStack(np.full([10, 11, 12], 2))
stack.data[:, :, :5] *= 2
self.model.set_new_roi("rits_roi")

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])
mock_stream, mock_path = self._make_mock_path_stream()

with mock.patch.object(self.model, "save_roi_coords"):
self.model.save_rits_roi(mock_path, ErrorMode.STANDARD_DEVIATION, self.model._roi_ranges["ROI_RITS"])

mock_path.open.assert_called_once_with("w")
self.assertIn("0.0\t0.0\t0.0", mock_stream.captured[0])
self.assertIn("100000.0\t0.75\t0.25", mock_stream.captured[1])
Expand Down Expand Up @@ -378,12 +378,6 @@ def test_WHEN_rois_deleted_THEN_name_generator_is_reset(self):
self.assertEqual(self.model.roi_name_generator(), "roi_1")
self.assertEqual(self.model.roi_name_generator(), "roi_2")

def test_when_new_roi_set_THEN_roi_name_added_to_list_of_roi_names(self):
self.model.set_stack(generate_images())
self.model.set_new_roi("new_roi")
self.assertIn("new_roi", self.model._roi_ranges)
self.assertListEqual(list(self.model._roi_ranges.keys()), ["all", "new_roi"])

@parameterized.expand([
("False", None, False),
("True", ImageStack(np.ones([10, 11, 12])), True),
Expand All @@ -396,7 +390,7 @@ def test_WHEN_remove_all_rois_called_THEN_all_but_default_rois_removed(self):
self.model.set_stack(generate_images())
rois = ["new_roi", "new_roi_2"]
for roi in rois:
self.model.set_new_roi(roi)
self.model._roi_ranges[roi] = SensibleROI.from_list([0, 0, 10, 10])
self.assertListEqual(list(self.model._roi_ranges.keys()), ["all"] + rois)
self.model.remove_all_roi()
self.assertListEqual(list(self.model._roi_ranges.keys()), [])
Expand Down Expand Up @@ -474,22 +468,19 @@ def test_save_single_rits_spectrum(self, mock_save_rits_roi):
stack, _ = self._set_sample_stack(with_tof=True)
norm = ImageStack(np.full([10, 11, 12], 2))
stack.data[:, :, :5] *= 2
self.model.set_new_roi("rits_roi")
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])

_, mock_path = self._make_mock_path_stream()
with mock.patch.object(self.model, "save_roi_coords"):
self.model.save_single_rits_spectrum(mock_path, ErrorMode.STANDARD_DEVIATION)
mock_save_rits_roi.assert_called_once_with(mock_path, mock.ANY, SensibleROI.from_list([0, 0, 5, 5]))

@mock.patch.object(SpectrumViewerWindowModel, "export_spectrum_to_rits")
def test_save_rits_correct_transmision(self, mock_save_rits_roi):
def test_save_rits_correct_transmission(self, mock_save_rits_roi):
stack, spectrum = self._set_sample_stack(with_tof=True)
norm = ImageStack(np.full([10, 11, 12], 2))
for i in range(10):
stack.data[:, :, i] *= i
self.model.set_new_roi("rits_roi")
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

0 comments on commit 3f099d2

Please sign in to comment.