def assert_call_as_expected(mock_ws, transpose, export_type, is_spectra, is_ragged): model = SliceViewerModel(mock_ws) slicepoint, bin_params, dimension_indices = MagicMock(), MagicMock( ), MagicMock() help_msg = model.export_cuts_to_workspace( slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices, export_type) if export_type == 'c': if is_spectra: mock_extract_spectra.assert_called_once() if is_ragged: mock_rebin.assert_called_once() mock_sum_spectra.assert_called_once() else: if is_ragged: self.assertEqual(2, mock_rebin.call_count) else: mock_rebin.assert_called_once() self.assertEqual(1, mock_transpose.call_count) self.assertEqual(1, mock_extract_spectra.call_count) self.assertEqual( 'Cuts along X/Y created: mock_ws_cut_x & mock_ws_cut_y', help_msg) elif export_type == 'x': mock_extract_spectra.assert_called_once() if is_ragged: self.assertEqual(1, mock_rebin.call_count) if is_spectra: mock_sum_spectra.assert_called_once() else: mock_transpose.assert_not_called() self.assertEqual('Cut along X created: mock_ws_cut_x', help_msg) elif export_type == 'y': mock_extract_spectra.assert_called_once() mock_transpose.assert_called_once() if is_ragged: self.assertEqual(2, mock_rebin.call_count) else: self.assertEqual(1, mock_rebin.call_count) self.assertEqual('Cut along Y created: mock_ws_cut_y', help_msg) mock_transpose.reset_mock() mock_rebin.reset_mock() mock_extract_spectra.reset_mock() mock_sum_spectra.reset_mock()
def assert_error_returned_in_help(workspace, export_type, mock_alg, err_msg): model = SliceViewerModel(workspace) slicepoint, bin_params, dimension_indices = (None, None, None), MagicMock(), [ 0, 1, None ] mock_alg.side_effect = RuntimeError(err_msg) try: if export_type == 'r': help_msg = model.export_roi_to_workspace( slicepoint, bin_params, ((1.0, 2.0), (-1, 2.0)), True, dimension_indices) else: help_msg = model.export_cuts_to_workspace( slicepoint, bin_params, ((1.0, 2.0), (-1, 2.0)), True, dimension_indices, export_type) except Exception as exc: help_msg = str(exc) mock_alg.reset_mock() self.assertTrue(err_msg in help_msg)
def assert_call_as_expected(transpose, dimension_indices, export_type): model = SliceViewerModel(self.ws_MDE_3D) if export_type == 'r': help_msg = model.export_roi_to_workspace( slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices) else: help_msg = model.export_cuts_to_workspace( slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices, export_type) if transpose: extents = [ymin, ymax, xmin, xmax, zmin, zmax] else: extents = [xmin, xmax, ymin, ymax, zmin, zmax] common_call_params = dict(InputWorkspace=self.ws_MDE_3D, AxisAligned=False, BasisVector0='h,rlu,1.0,0.0,0.0', BasisVector1='k,rlu,0.0,1.0,0.0', BasisVector2='l,rlu,0.0,0.0,1.0', OutputExtents=extents) xcut_name, ycut_name = 'ws_MDE_3D_cut_x', 'ws_MDE_3D_cut_y' if export_type == 'r': expected_help_msg = 'ROI created: ws_MDE_3D_roi' expected_calls = [ call(**common_call_params, OutputBins=[100, 100, 1], OutputWorkspace='ws_MDE_3D_roi') ] elif export_type == 'x': expected_help_msg = f'Cut along X created: {xcut_name}' expected_bins = [1, 100, 1] if transpose else [100, 1, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=xcut_name) ] elif export_type == 'y': expected_help_msg = f'Cut along Y created: {ycut_name}' expected_bins = [100, 1, 1] if transpose else [1, 100, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=ycut_name) ] elif export_type == 'c': expected_help_msg = f'Cuts along X/Y created: {xcut_name} & {ycut_name}' expected_bins = [100, 1, 1] if transpose else [1, 100, 1] expected_calls = [ call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=xcut_name), call(**common_call_params, OutputBins=expected_bins, OutputWorkspace=ycut_name) ] mock_binmd.assert_has_calls(expected_calls, any_order=True) if export_type == 'r': if transpose: mock_transposemd.assert_called_once() else: mock_transposemd.assert_not_called() else: if export_type == 'x': index = 1 if transpose else 0 expected_calls = [ call(InputWorkspace=xcut_name, OutputWorkspace=xcut_name, Axes=[index]) ] elif export_type == 'y': index = 0 if transpose else 1 expected_calls = [ call(InputWorkspace=ycut_name, OutputWorkspace=ycut_name, Axes=[index]) ] elif export_type == 'c': xindex = 1 if transpose else 0 yindex = 0 if transpose else 1 expected_calls = [ call(InputWorkspace=xcut_name, OutputWorkspace=xcut_name, Axes=[xindex]), call(InputWorkspace=ycut_name, OutputWorkspace=ycut_name, Axes=[yindex]) ] mock_transposemd.assert_has_calls(expected_calls, any_order=True) self.assertEqual(expected_help_msg, help_msg) mock_binmd.reset_mock() mock_transposemd.reset_mock()
def assert_call_as_expected(transpose, dimension_indices, export_type): model = SliceViewerModel(self.ws_MD_3D) if export_type == 'r': model.export_roi_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices) else: model.export_cuts_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices, export_type) if export_type == 'c': export_type = 'xy' # will loop over this string as 'c' performs both 'x' and 'y' for export in export_type: xbin, ybin = [xmin, xmax], [ ymin, ymax ] # create in loop as these are altered in case of both cuts # perform transpose on limits - i.e map x/y on plot to basis of MD workspace p1/p2 if not transpose: p1_bin, p2_bin = xbin, ybin else: p2_bin, p1_bin = xbin, ybin # determine which axis was binnned if export == 'x': xbin.insert( 1, 0.0 ) # insert 0 between min,max - this means preserve binning along this dim out_name = 'ws_MD_3D_cut_x' transpose_axes = [1 if transpose else 0 ] # Axes argument of TransposeMD elif export == 'y': ybin.insert(1, 0.0) out_name = 'ws_MD_3D_cut_y' # check call to transposeMD transpose_axes = [0 if transpose else 1] else: # export == 'r' xbin.insert(1, 0.0) ybin.insert(1, 0.0) out_name = 'ws_MD_3D_roi' transpose_axes = [1, 0] if transpose else None # check call to IntegrateMDHistoWorkspace mock_intMD.assert_has_calls([ call(InputWorkspace=self.ws_MD_3D, P1Bin=p1_bin, P2Bin=p2_bin, P3Bin=[ slicepoint[2] - bin_params[2] / 2, slicepoint[2] + bin_params[2] / 2 ], OutputWorkspace=out_name) ], any_order=False) if transpose_axes is not None: mock_transposemd.assert_has_calls([ call(InputWorkspace=out_name, OutputWorkspace=out_name, Axes=transpose_axes) ], any_order=False) else: mock_transposemd.assert_not_called( ) # ROI with Transpose == False mock_intMD.reset_mock() mock_transposemd.reset_mock()