Skip to content

Commit

Permalink
Add get_transmission_error_propogated()
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed Dec 4, 2023
1 parent 50f5980 commit b0c34e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
11 changes: 10 additions & 1 deletion mantidimaging/gui/windows/spectrum_viewer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
Expand Down Expand Up @@ -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")

Expand Down
21 changes: 21 additions & 0 deletions mantidimaging/gui/windows/spectrum_viewer/test/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b0c34e8

Please sign in to comment.