def assert_call_as_expected(exp_xmin, exp_xmax, exp_start_index, exp_end_index, transpose, is_spectra): mock_ws = _create_mock_matrixworkspace(x_axis=[10, 20, 30], y_axis=[1, 2, 3, 4, 5], distribution=False, y_is_spectra=is_spectra) mock_ws.name.return_value = 'mock_ws' model = SliceViewerModel(mock_ws) slicepoint, bin_params, dimension_indices = MagicMock(), MagicMock( ), MagicMock() help_msg = model.export_roi_to_workspace(slicepoint, bin_params, ((xmin, xmax), (ymin, ymax)), transpose, dimension_indices) self.assertEqual('ROI created: mock_ws_roi', help_msg) if is_spectra: self.assertEqual(2, mock_ws.getAxis(1).indexOfValue.call_count) else: mock_ws.getAxis(1).extractValues.assert_called_once() ExtractSpectra.assert_called_once_with( InputWorkspace=mock_ws, OutputWorkspace='mock_ws_roi', XMin=exp_xmin, XMax=exp_xmax, StartWorkspaceIndex=exp_start_index, EndWorkspaceIndex=exp_end_index, EnableLogging=True) ExtractSpectra.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()