Skip to content

Commit b721e93

Browse files
committed
Add unit tests for exporting pixel cuts from different workspaces types
1 parent cf3f37c commit b721e93

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Sliceviwer no longer raises an error when exporting a pixel cut from a MDHistoWorkspace

qt/python/mantidqt/mantidqt/widgets/sliceviewer/test/test_sliceviewer_model.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,176 @@ def assert_call_as_expected(mock_ws, transpose, export_type, is_spectra, is_ragg
677677
mock_ws.name.return_value = "mock_ws"
678678
assert_call_as_expected(mock_ws, transpose=False, export_type=export_type, is_spectra=is_spectra, is_ragged=is_ragged)
679679

680+
@patch("mantidqt.widgets.sliceviewer.models.model.IntegrateMDHistoWorkspace")
681+
@patch("mantidqt.widgets.sliceviewer.models.model.TransposeMD")
682+
def test_export_pixel_cut_to_workspace_mdhisto(self, mock_transpose, mock_intMD):
683+
slicepoint = [None, None, 0.5]
684+
bin_params = [100, 100, 0.1]
685+
dimension_indices = [0, 1, None]
686+
xpos, ypos = 0.0, 2.0
687+
688+
dim0_delta = self.ws_MD_3D.getDimension(0).getBinWidth()
689+
dim1_delta = self.ws_MD_3D.getDimension(1).getBinWidth()
690+
691+
xmin, xmax = xpos - 0.5 * dim0_delta, xpos + 0.5 * dim0_delta
692+
ymin, ymax = ypos - 0.5 * dim1_delta, ypos + 0.5 * dim1_delta
693+
zmin, zmax = 0.45, 0.55
694+
695+
def assert_call_as_expected(transpose, cut_type):
696+
model = SliceViewerModel(self.ws_MD_3D)
697+
698+
help_msg = model.export_pixel_cut_to_workspace(slicepoint, bin_params, (xpos, ypos), transpose, dimension_indices, cut_type)
699+
700+
if not transpose:
701+
if cut_type == "x":
702+
# X-cut: integrate along X, keep Y dimension
703+
xcut_params = dict(
704+
InputWorkspace=self.ws_MD_3D,
705+
P1Bin=[xmin, 0, xmax],
706+
P2Bin=[ymin, ymax],
707+
P3Bin=[zmin, zmax],
708+
OutputWorkspace="ws_MD_3D_cut_x",
709+
)
710+
mock_intMD.assert_called_once_with(**xcut_params)
711+
self.assertEqual("Cut along X created: ws_MD_3D_cut_x", help_msg)
712+
elif cut_type == "y":
713+
# Y-cut: integrate along Y, keep X dimension
714+
ycut_params = dict(
715+
InputWorkspace=self.ws_MD_3D,
716+
P1Bin=[xmin, xmax],
717+
P2Bin=[ymin, 0, ymax],
718+
P3Bin=[zmin, zmax],
719+
OutputWorkspace="ws_MD_3D_cut_y",
720+
)
721+
mock_intMD.assert_called_once_with(**ycut_params)
722+
self.assertEqual("Cut along Y created: ws_MD_3D_cut_y", help_msg)
723+
else:
724+
# In transposed mode, dimensions are swapped
725+
if cut_type == "x":
726+
# X-cut: integrate along Y, keep X dimension (transposed)
727+
xcut_params = dict(
728+
InputWorkspace=self.ws_MD_3D,
729+
P1Bin=[ymin, ymax],
730+
P2Bin=[xmin, 0, xmax],
731+
P3Bin=[zmin, zmax],
732+
OutputWorkspace="ws_MD_3D_cut_x",
733+
)
734+
mock_intMD.assert_called_once_with(**xcut_params)
735+
self.assertEqual("Cut along X created: ws_MD_3D_cut_x", help_msg)
736+
elif cut_type == "y":
737+
# Y-cut: integrate along X, keep Y dimension (transposed)
738+
ycut_params = dict(
739+
InputWorkspace=self.ws_MD_3D,
740+
P1Bin=[ymin, 0, ymax],
741+
P2Bin=[xmin, xmax],
742+
P3Bin=[zmin, zmax],
743+
OutputWorkspace="ws_MD_3D_cut_y",
744+
)
745+
mock_intMD.assert_called_once_with(**ycut_params)
746+
self.assertEqual("Cut along Y created: ws_MD_3D_cut_y", help_msg)
747+
748+
mock_intMD.reset_mock()
749+
750+
for cut_type in ("x", "y"):
751+
assert_call_as_expected(transpose=False, cut_type=cut_type)
752+
assert_call_as_expected(transpose=True, cut_type=cut_type)
753+
754+
@patch("mantidqt.widgets.sliceviewer.models.model.BinMD")
755+
@patch("mantidqt.widgets.sliceviewer.models.model.TransposeMD")
756+
def test_export_pixel_cut_to_workspace_mdevent(self, mock_transpose, mock_binmd):
757+
slicepoint = [None, None, 0.5]
758+
bin_params = [100, 100, 0.1]
759+
dimension_indices = [0, 1, None]
760+
xpos, ypos = 0.0, 2.0
761+
762+
dim0_delta = self.ws_MDE_3D.getDimension(0).getBinWidth()
763+
dim1_delta = self.ws_MDE_3D.getDimension(1).getBinWidth()
764+
765+
xmin, xmax = xpos - 0.5 * dim0_delta, xpos + 0.5 * dim0_delta
766+
ymin, ymax = ypos - 0.5 * dim1_delta, ypos + 0.5 * dim1_delta
767+
zmin, zmax = 0.45, 0.55
768+
769+
def assert_call_as_expected(transpose, cut_type):
770+
model = SliceViewerModel(self.ws_MDE_3D)
771+
772+
help_msg = model.export_pixel_cut_to_workspace_mdevent(
773+
slicepoint, bin_params, (xpos, ypos), transpose, dimension_indices, cut_type
774+
)
775+
776+
common_params = dict(
777+
InputWorkspace=self.ws_MDE_3D,
778+
AxisAligned=False,
779+
BasisVector0="h,rlu,1.0,0.0,0.0",
780+
BasisVector1="k,rlu,0.0,1.0,0.0",
781+
BasisVector2="l,rlu,0.0,0.0,1.0",
782+
)
783+
784+
if not transpose:
785+
extents = [xmin, xmax, ymin, ymax, zmin, zmax]
786+
787+
if cut_type == "x":
788+
expected_bins = [100, 1, 1]
789+
expected_ws = "ws_MDE_3D_cut_x"
790+
expected_msg = "Cut along X created: ws_MDE_3D_cut_x"
791+
else:
792+
expected_bins = [1, 100, 1]
793+
expected_ws = "ws_MDE_3D_cut_y"
794+
expected_msg = "Cut along Y created: ws_MDE_3D_cut_y"
795+
else:
796+
extents = [ymin, ymax, xmin, xmax, zmin, zmax]
797+
798+
if cut_type == "x":
799+
expected_bins = [1, 100, 1]
800+
expected_ws = "ws_MDE_3D_cut_x"
801+
expected_msg = "Cut along X created: ws_MDE_3D_cut_x"
802+
else:
803+
expected_bins = [100, 1, 1]
804+
expected_ws = "ws_MDE_3D_cut_y"
805+
expected_msg = "Cut along Y created: ws_MDE_3D_cut_y"
806+
807+
mock_binmd.assert_called_once_with(
808+
**common_params, OutputExtents=extents, OutputBins=expected_bins, OutputWorkspace=expected_ws
809+
)
810+
811+
self.assertEqual(expected_msg, help_msg)
812+
mock_binmd.reset_mock()
813+
814+
for cut_type in ("x", "y"):
815+
assert_call_as_expected(transpose=False, cut_type=cut_type)
816+
assert_call_as_expected(transpose=True, cut_type=cut_type)
817+
818+
@patch("mantidqt.widgets.sliceviewer.models.roi.extract_roi_matrix")
819+
def test_export_pixel_cut_to_workspace_matrix(self, mock_extract_roi):
820+
slicepoint = [None, None]
821+
bin_params = [100, 100]
822+
dimension_indices = [0, 1]
823+
xpos, ypos = 1.5, 3.0
824+
825+
def assert_call_as_expected(transpose, cut_type):
826+
model = SliceViewerModel(self.ws2d_histo)
827+
mock_extract_roi.reset_mock()
828+
829+
help_msg = model.export_pixel_cut_to_workspace(slicepoint, bin_params, (xpos, ypos), transpose, dimension_indices, cut_type)
830+
831+
if not transpose:
832+
if cut_type == "x":
833+
mock_extract_roi.assert_called_once_with(self.ws2d_histo, None, None, ypos, ypos, False, "ws2d_histo_cut_x")
834+
self.assertEqual("Cut along X created: ws2d_histo_cut_x", help_msg)
835+
elif cut_type == "y":
836+
mock_extract_roi.assert_called_once_with(self.ws2d_histo, xpos, xpos, None, None, True, "ws2d_histo_cut_y")
837+
self.assertEqual("Cut along Y created: ws2d_histo_cut_y", help_msg)
838+
else:
839+
if cut_type == "x":
840+
mock_extract_roi.assert_called_once_with(self.ws2d_histo, ypos, ypos, None, None, True, "ws2d_histo_cut_y")
841+
self.assertEqual("Cut along X created: ws2d_histo_cut_y", help_msg)
842+
elif cut_type == "y":
843+
mock_extract_roi.assert_called_once_with(self.ws2d_histo, None, None, xpos, xpos, False, "ws2d_histo_cut_x")
844+
self.assertEqual("Cut along Y created: ws2d_histo_cut_x", help_msg)
845+
846+
for cut_type in ("x", "y"):
847+
assert_call_as_expected(transpose=False, cut_type=cut_type)
848+
assert_call_as_expected(transpose=False, cut_type=cut_type)
849+
680850
@patch("mantidqt.widgets.sliceviewer.models.model.TransposeMD")
681851
@patch("mantidqt.widgets.sliceviewer.models.model.IntegrateMDHistoWorkspace")
682852
def test_export_region_for_mdhisto_workspace(self, mock_intMD, mock_transposemd):

0 commit comments

Comments
 (0)