From b0c34e8c3ab9893ca61b726e29559f72aa85b97d Mon Sep 17 00:00:00 2001 From: Sam Tygier Date: Mon, 4 Dec 2023 14:27:53 +0000 Subject: [PATCH] Add get_transmission_error_propogated() --- .../gui/windows/spectrum_viewer/model.py | 11 +++++++++- .../spectrum_viewer/test/model_test.py | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mantidimaging/gui/windows/spectrum_viewer/model.py b/mantidimaging/gui/windows/spectrum_viewer/model.py index bb6ecd6daf4..54567c29d50 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/model.py +++ b/mantidimaging/gui/windows/spectrum_viewer/model.py @@ -178,6 +178,15 @@ def get_transmission_error_standard_dev(self, roi_name: str) -> np.ndarray: safe_divide = np.divide(sample, normed, out=np.zeros_like(sample), where=normed != 0) return np.std(safe_divide, axis=(1, 2)) + def get_transmission_error_propogated(self, roi_name: str) -> np.ndarray: + if self._stack is None or self._normalise_stack is None: + raise RuntimeError("Sample and open beam must be selected") + roi = self.get_roi(roi_name) + sample = self.get_stack_spectrum_summed(self._stack, roi) + normed = self.get_stack_spectrum_summed(self._normalise_stack, roi) + error = np.sqrt(sample / normed**2 + sample**2 / normed**3) + return error + def get_image_shape(self) -> tuple[int, int]: if self._stack is not None: return self._stack.data.shape[1:] @@ -239,7 +248,7 @@ def save_rits(self, path: Path, normalized: bool, error_mode: ErrorMode) -> None if error_mode == ErrorMode.STANDARD_DEVIATION: transmission_error = self.get_transmission_error_standard_dev(ROI_RITS) elif error_mode == ErrorMode.PROPAGATED: - transmission_error = np.full_like(tof, 0.1) + transmission_error = self.get_transmission_error_propogated(ROI_RITS) else: raise ValueError("Invalid error_mode given") diff --git a/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py b/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py index 2dafdc1b750..6835a0d69b2 100644 --- a/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py +++ b/mantidimaging/gui/windows/spectrum_viewer/test/model_test.py @@ -221,6 +221,27 @@ def test_save_rits_roi_dat(self): self.assertIn("200000.0\t1.5\t0.5", mock_stream.captured[2]) self.assertTrue(mock_stream.is_closed) + @parameterized.expand([ + ("std_dev", ErrorMode.STANDARD_DEVIATION, [0., 0.25, 0.5, 0.75, 1., 1.25, 1.5, 1.75, 2., 2.25]), + ("std_dev", ErrorMode.PROPAGATED, + [0.0000, 0.0772, 0.1306, 0.1823, 0.2335, 0.2845, 0.3354, 0.3862, 0.4369, 0.4876]), + ]) + def test_save_rits_data_errors(self, _, error_mode, expected_error): + 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.set_roi("rits_roi", SensibleROI.from_list([0, 0, 10, 11])) + self.model.set_normalise_stack(norm) + + mock_stream, mock_path = self._make_mock_path_stream() + with mock.patch.object(self.model, "save_roi_coords"): + with mock.patch.object(self.model, "export_spectrum_to_rits") as mock_export: + self.model.save_rits(mock_path, True, error_mode) + + calculated_errors = mock_export.call_args[0][3] + np.testing.assert_allclose(expected_error, calculated_errors, atol=1e-4) + @parameterized.expand([("standard_deviation", ErrorMode.STANDARD_DEVIATION), ("propagated", ErrorMode.PROPAGATED)]) def test_error_mode_rits(self, _, error_mode): stack, _ = self._set_sample_stack(with_tof=True)