def setUpClass(cls): cls.sim_scattering_data = np.arange(0, 1.5, 0.002).reshape(30, 25).transpose() cls.scattering_rotated = np.rot90(cls.sim_scattering_data, k=3) cls.scattering_rotated = np.flipud(cls.scattering_rotated) cls.e_axis = Axis('DeltaE', -10, 15, 1) cls.q_axis = Axis('|Q|', 0.1, 3.1, 0.1) cls.q_axis_degrees = Axis('Degrees', 3, 33, 1) cls.test_ws = CreateSampleWorkspace(OutputWorkspace='test_ws', NumBanks=1, BankPixelWidth=5, XMin=0.1, XMax=3.1, BinWidth=0.1, XUnit='DeltaE') for i in range(cls.test_ws.raw_ws.getNumberHistograms()): cls.test_ws.raw_ws.setY(i, cls.sim_scattering_data[i]) AddSampleLog(workspace=cls.test_ws.raw_ws, LogName='Ei', LogText='3.', LogType='Number', StoreInADS=False) cls.test_ws.e_mode = 'Direct' cls.test_ws.e_fixed = 3
def test_plot_slice_error_handling(self): slice_widget_presenter = SliceWidgetPresenter(self.slice_view) slice_widget_presenter.register_master(self.main_presenter) slice_widget_presenter.set_slice_plotter_presenter( self.slice_plotter_presenter) x = Axis('x', '0', '10', '1') y = Axis('y', '2', '8', '3') intensity_start = '7' intensity_end = '8' norm_to_one = False smoothing = '10' colourmap = 'colormap' selected_workspace = 'workspace1' self.slice_view.get_slice_x_axis.return_value = x.units self.slice_view.get_slice_x_start.return_value = x.start self.slice_view.get_slice_x_end.return_value = x.end self.slice_view.get_slice_x_step.return_value = x.step self.slice_view.get_slice_y_axis.return_value = y.units self.slice_view.get_slice_y_start.return_value = y.start self.slice_view.get_slice_y_end.return_value = y.end self.slice_view.get_slice_y_step.return_value = y.step self.slice_view.get_slice_intensity_start.return_value = intensity_start self.slice_view.get_slice_intensity_end.return_value = intensity_end self.slice_view.get_slice_is_norm_to_one.return_value = norm_to_one self.slice_view.get_slice_smoothing.return_value = smoothing self.slice_view.get_slice_colourmap.return_value = colourmap plot_info = ("plot_data", "boundaries", "colormap", "norm") self.slice_plotter_presenter.plot_slice = mock.Mock( return_value=plot_info) self.slice_plotter_presenter.validate_intensity = mock.Mock( return_value=(7.0, 8.0)) # Test empty workspace, multiple workspaces self.main_presenter.get_selected_workspaces.return_value = [] slice_widget_presenter.notify(Command.DisplaySlice) assert self.slice_view.error_select_one_workspace.called self.main_presenter.get_selected_workspaces.return_value = [ selected_workspace, selected_workspace ] self.slice_view.error_select_one_workspace.reset_mock() slice_widget_presenter.notify(Command.DisplaySlice) assert self.slice_view.error_select_one_workspace.called # Test invalid axes self.main_presenter.get_selected_workspaces.return_value = [ selected_workspace ] self.slice_view.get_slice_y_axis.return_value = x.units slice_widget_presenter.notify(Command.DisplaySlice) assert self.slice_view.error_invalid_plot_parameters.called # Simulate matplotlib error self.slice_plotter_presenter.plot_slice = mock.Mock( side_effect=ValueError( 'minvalue must be less than or equal to maxvalue')) self.slice_view.get_slice_y_axis.return_value = y.units slice_widget_presenter.notify(Command.DisplaySlice) assert self.slice_view.error_invalid_intensity_params.called self.slice_plotter_presenter.plot_slice = mock.Mock( side_effect=ValueError('something bad')) self.assertRaises(ValueError, slice_widget_presenter.notify, Command.DisplaySlice)
def create_cut_cache(self): axis = Axis("units", "0", "100", "1") integration_axis = Axis("units", 0.0, 100.0, 0) return Cut(axis, integration_axis, intensity_start=3.0, intensity_end=11.0, norm_to_one=True, width=None)
def test_plot_slice_successful(self): slice_widget_presenter = SliceWidgetPresenter(self.slice_view) slice_widget_presenter.register_master(self.main_presenter) slice_widget_presenter.set_slice_plotter_presenter( self.slice_plotter_presenter) x = Axis('x', '0', '10', '1') y = Axis('y', '2', '8', '3') intensity_start = '7' intensity_end = '8' norm_to_one = False colourmap = 'colormap' selected_workspace = 'workspace1' self.main_presenter.get_selected_workspaces.return_value = [ selected_workspace ] self.slice_view.get_slice_x_axis.return_value = x.units self.slice_view.get_slice_x_start.return_value = x.start self.slice_view.get_slice_x_end.return_value = x.end self.slice_view.get_slice_x_step.return_value = x.step self.slice_view.get_slice_y_axis.return_value = y.units self.slice_view.get_slice_y_start.return_value = y.start self.slice_view.get_slice_y_end.return_value = y.end self.slice_view.get_slice_y_step.return_value = y.step self.slice_view.get_slice_intensity_start.return_value = intensity_start self.slice_view.get_slice_intensity_end.return_value = intensity_end self.slice_view.get_slice_is_norm_to_one.return_value = norm_to_one self.slice_view.get_slice_colourmap.return_value = colourmap plot_info = ("plot_data", "boundaries", "colormap", "norm") self.slice_plotter_presenter.plot_slice = mock.Mock( return_value=plot_info) self.slice_plotter_presenter.validate_intensity = mock.Mock( return_value=(7.0, 8.0)) slice_widget_presenter.notify(Command.DisplaySlice) self.main_presenter.get_selected_workspaces.assert_called_once_with() self.slice_view.get_slice_x_axis.assert_called_once_with() self.slice_view.get_slice_x_start.assert_called_once_with() self.slice_view.get_slice_x_end.assert_called_once_with() self.slice_view.get_slice_x_step.assert_called_once_with() self.slice_view.get_slice_y_axis.assert_called_once_with() self.slice_view.get_slice_y_start.assert_called_once_with() self.slice_view.get_slice_y_end.assert_called_once_with() self.slice_view.get_slice_y_step.assert_called_once_with() self.slice_view.get_slice_intensity_start.assert_called_once_with() self.slice_view.get_slice_intensity_end.assert_called_once_with() self.slice_view.get_slice_is_norm_to_one.assert_called_once_with() self.slice_view.get_slice_colourmap.assert_called_once_with() self.slice_plotter_presenter.validate_intensity.assert_called_once_with( '7', '8') self.slice_plotter_presenter.plot_slice.assert_called_with( selected_workspace, Axis('x', 0, 10, 1), Axis('y', 2, 8, 3), float(intensity_start), float(intensity_end), norm_to_one, colourmap)
def _string_to_integration_axis(string): """Allows step to be omitted and set to default value""" axis_str = string.split(',') if len(axis_str) < 3: raise ValueError( 'axis should be specified in format <name>,<start>,<end>') valid_axis = Axis(axis_str[0], axis_str[1], axis_str[2], 0) try: valid_axis.step = axis_str[3] except IndexError: valid_axis.step = valid_axis.end - valid_axis.start return valid_axis
def test_plot_interactive_cut(self, draw_interact_mock, plot_cut_impl_mock, compute_cut_mock, get_ws_handle_mock): mock_ws = mock.MagicMock() mock_ws.name = 'workspace' get_ws_handle_mock.return_value = mock_ws cut_axis = Axis("units", "0", "100", "1") integration_axis = Axis("units", 0.0, 100.0, 0) self.cut_plotter_presenter.plot_interactive_cut( 'workspace', cut_axis, integration_axis, False) self.assertEqual(1, compute_cut_mock.call_count) self.assertEqual(1, plot_cut_impl_mock.call_count) self.assertEqual(1, draw_interact_mock.call_count)
def PyExec(self): workspace = self.getProperty('InputWorkspace').value cut_dict = self.getProperty('CutAxis').value cut_axis = Axis(cut_dict['units'].value, cut_dict['start'].value, cut_dict['end'].value, cut_dict['step'].value) int_dict = self.getProperty('IntegrationAxis').value int_axis = Axis(int_dict['units'].value, int_dict['start'].value, int_dict['end'].value, int_dict['step'].value) e_mode = self.getProperty('EMode').value PSD = self.getProperty('PSD').value norm_to_one = self.getProperty('NormToOne').value cut = compute_cut(workspace, cut_axis, int_axis, e_mode, PSD, norm_to_one) self.setProperty('OutputWorkspace', cut)
def _string_to_axis(string): axis = string.split(',') if len(axis) != 4: raise ValueError( 'axis should be specified in format <name>,<start>,<end>,<step_size>' ) return Axis(axis[0], axis[1], axis[2], axis[3])
def test_success_int(self): axis = Axis('x', 0, 5, 1) self.assertEqual(axis.start, 0.0) assert (isinstance(axis.start, float)) self.assertEqual(axis.end, 5.0) assert (isinstance(axis.end, float)) self.assertEqual(axis.step, 1.0) assert (isinstance(axis.end, float))
def PyExec(self): workspace = self.getProperty('InputWorkspace').value x_dict = self.getProperty('XAxis').value x_axis = Axis(x_dict['units'].value, x_dict['start'].value, x_dict['end'].value, x_dict['step'].value) y_dict = self.getProperty('YAxis').value y_axis = Axis(y_dict['units'].value, y_dict['start'].value, y_dict['end'].value, y_dict['step'].value) norm_to_one = self.getProperty('NormToOne') if self.getProperty('PSD').value: slice = self._compute_slice_PSD(workspace, x_axis, y_axis, norm_to_one) else: e_mode = self.getProperty('EMode').value slice = self._compute_slice_nonPSD(workspace, x_axis, y_axis, e_mode, norm_to_one) self.setProperty('OutputWorkspace', slice)
def test_success_string(self): axis = Axis('x', '0', '5', '1') self.assertEqual(axis.start, 0.0) assert (isinstance(axis.start, float)) self.assertEqual(axis.end, 5.0) assert (isinstance(axis.end, float)) self.assertEqual(axis.step, 1.0) assert (isinstance(axis.end, float))
def _parse_input(self): """Gets values entered by user. Validation is performed by the CutCache object.""" cut_axis = Axis(self._cut_view.get_cut_axis(), self._cut_view.get_cut_axis_start(), self._cut_view.get_cut_axis_end(), self._cut_view.get_cut_axis_step()) integration_axis = Axis(self._cut_view.get_integration_axis(), self._cut_view.get_integration_start(), self._cut_view.get_integration_end(), 0.) intensity_start = self._cut_view.get_intensity_start() intensity_end = self._cut_view.get_intensity_end() norm_to_one = bool(self._cut_view.get_intensity_is_norm_to_one()) width = self._cut_view.get_integration_width() return Cut(cut_axis, integration_axis, intensity_start, intensity_end, norm_to_one, width)
def get_cut_parameters(self, pos1, pos2): start = pos1[not self.horizontal] end = pos2[not self.horizontal] units = self._canvas.figure.gca().get_xaxis().units if self.horizontal else \ self._canvas.figure.gca().get_yaxis().units step = get_limits(get_workspace_handle(self._ws_title), units)[2] ax = Axis(units, start, end, step) integration_start = pos1[self.horizontal] integration_end = pos2[self.horizontal] return ax, integration_start, integration_end
def test_powder_line(self, ws_handle_mock): from mslice.models.axis import Axis ws_handle_mock.return_value.e_fixed = 20 x, y = compute_powder_line('ws_name', Axis('|Q|', 0.1, 9.1, 0.1), 'Copper') self.assertEqual(len(x), len(y)) self.assertAlmostEqual(x[0], 3.010539, 6) self.assertAlmostEqual(x[10], 5.764743, 6) self.assertTrue(np.isnan(x[29])) self.assertEqual(y[0], 1) self.assertEqual(y[1], -1)
def plot_cut(self, x1, x2, y1, y2, store=False): if x2 > x1 and y2 > y1: ax, integration_start, integration_end = self.get_cut_parameters( (x1, y1), (x2, y2)) units = self._canvas.figure.gca().get_yaxis().units if self.horizontal else \ self._canvas.figure.gca().get_xaxis().units integration_axis = Axis(units, integration_start, integration_end, 0) self._cut_plotter_presenter.plot_interactive_cut( str(self._ws_title), ax, integration_axis, store) self._cut_plotter_presenter.store_icut(self._ws_title, self)
def test_plot_slice_success(self, compute_slice_mock, sample_temp_mock, get_workspace_handle_mock, create_slice_mock, plot_cached_slice_mock): workspace_mock = mock.MagicMock() name = mock.PropertyMock(return_value='workspace') type(workspace_mock).name = name slice_mock = mock.MagicMock() slice_name = mock.PropertyMock(return_value='__workspace') type(slice_mock).name = slice_name get_workspace_handle_mock.return_value = workspace_mock sample_temp_mock.return_value = 5 compute_slice_mock.return_value = slice_mock x_axis = Axis('x', 0, 1, 0.1) y_axis = Axis('y', 0, 1, 0.1) slice_presenter = SlicePlotterPresenter() slice_presenter.plot_slice('workspace', x_axis, y_axis, 7, 8, False, DEFAULT_CMAP) self.assertTrue('workspace' in slice_presenter._slice_cache) create_slice_mock.assert_called_once() plot_cached_slice_mock.assert_called_once()
def test_powder_line_degrees(self, ws_handle_mock): from mslice.models.axis import Axis ws_handle_mock.return_value.e_fixed = 20 x, y = compute_powder_line('ws_name', Axis('Degrees', 3, 93, 1), 'Copper') self.assertEqual(len(x), len(y)) self.assertAlmostEqual(x[0], 57.9614, 4) self.assertAlmostEqual(x[4], 68.0383, 4) self.assertTrue(np.isnan(x[5])) self.assertEqual(y[0], 1) self.assertEqual(y[1], -1)
def _process_axis(axis, fallback_index, input_workspace, string_function=_string_to_axis): available_axes = get_available_axes(input_workspace) if axis is None: axis = available_axes[fallback_index] # check to see if axis is just a name e.g 'DeltaE' or a full binning spec e.g. 'DeltaE,0,1,100' if ',' in axis: axis = string_function(axis) elif axis in available_axes: range = get_axis_range(input_workspace, axis) range = list(map(float, range)) axis = Axis(units=axis, start=range[0], end=range[1], step=range[2]) else: raise RuntimeError( "Axis '%s' not recognised. Workspace has these axes: %s " % (axis, ', '.join(available_axes))) return axis
def test_invalid_string_step(self): with self.assertRaises(ValueError): Axis('x', '0', '1', 'aa')
def test_invalid_start_greater_than_end(self): with self.assertRaises(ValueError): Axis('x', '1', '0', '.1')
def _y_axis(self): return Axis(self._slice_view.get_slice_y_axis(), self._slice_view.get_slice_y_start(), self._slice_view.get_slice_y_end(), self._slice_view.get_slice_y_step())
def get_axis_from_dimension(workspace, ws_name, id): dim = workspace.raw_ws.getDimension(id).getName() min, max, step = workspace.limits[dim] return Axis(dim, min, max, step)
def test_invalid_string_end(self): with self.assertRaises(ValueError): Axis('x', '0', 'aa', '.1')