def get_spectra_selection(workspaces, parent_widget=None, show_colorfill_btn=False, overplot=False, advanced=False): """ Decides whether it is necessary to request user input when asked to plot a list of workspaces. The input dialog will only be shown in the case where all workspaces have more than 1 spectrum :param workspaces: A list of MatrixWorkspaces that will be plotted :param parent_widget: An optional parent_widget to use for the input selection dialog :param show_colorfill_btn: An optional flag controlling whether the colorfill button should be shown :param overplot: An optional flag detailing whether to overplot onto the current figure :param advanced: If true then the advanced options will be shown in the spectra selector dialog. :returns: Either a SpectraSelection object containing the details of workspaces to plot or None indicating the request was cancelled :raises ValueError: if the workspaces are not of type MatrixWorkspace """ workspaces = SpectraSelectionDialog.get_compatible_workspaces(workspaces) if len(workspaces) == 0: return None single_spectra_ws = [wksp.getNumberHistograms() for wksp in workspaces if wksp.getNumberHistograms() == 1] if len(workspaces) == len(single_spectra_ws): plottable = [] else: ws_spectra = [{ws.getSpectrum(i).getSpectrumNo() for i in range(ws.getNumberHistograms())} for ws in workspaces] plottable = ws_spectra[0] # check if there are no common spectra in workspaces if len(ws_spectra) > 1: for sp_set in ws_spectra[1:]: plottable = plottable.intersection(sp_set) if (len(single_spectra_ws) == len(workspaces) or len(plottable) == 0) and not advanced: # At least 1 workspace contains only a single spectrum and these are no # common spectra selection = SpectraSelection(workspaces) selection.wksp_indices = [0] return selection else: selection_dlg = SpectraSelectionDialog(workspaces, parent=parent_widget, show_colorfill_btn=show_colorfill_btn, overplot=overplot, advanced=advanced) res = selection_dlg.exec_() if res == SpectraSelectionDialog.Rejected: # cancelled return None else: user_selection = selection_dlg.selection if user_selection == 'colorfill': return user_selection # the dialog should guarantee that only 1 of spectrum/indices is supplied assert user_selection.spectra is None or user_selection.wksp_indices is None return user_selection
def test_construction_with_non_MatrixWorkspace_type_removes_non_MatrixWorkspaces_from_list( self): table = WorkspaceFactory.Instance().createTable() workspaces = [self._single_spec_ws, table] ssd = SpectraSelectionDialog(workspaces) spectraselectordialog.RED_ASTERISK = None self.assertEqual(ssd._workspaces, [self._single_spec_ws])
def test_log_value_combo_box_contains_workspace_name_option_for_non_surface_or_contour_plots( self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) self.assertNotEqual( ssd._ui.advanced_options_widget.ui.log_value_combo_box.findText( "Workspace name"), -1)
def test_filling_workspace_details_multiple_workspaces_of_same_size(self): workspaces = [self._multi_spec_ws, self._multi_spec_ws] dlg = SpectraSelectionDialog(workspaces) self.assertEqual("valid range: 1-200", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-199", dlg._ui.wkspIndices.placeholderText())
def test_filling_workspace_details_multiple_workspaces_of_different_sizes(self): cropped_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=50, XLength=1, YLength=1) for i in range(cropped_ws.getNumberHistograms()): cropped_ws.getSpectrum(i).setSpectrumNo(51 + i) dlg = SpectraSelectionDialog([cropped_ws, self._multi_spec_ws]) self.assertEqual("valid range: 51-100", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-49", dlg._ui.wkspIndices.placeholderText())
def test_log_value_combo_box_contains_workspace_index_option_for_contour_plots( self): workspaces = [self._single_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(4) self.assertNotEqual( ssd._ui.advanced_options_widget.ui.log_value_combo_box.findText( "Workspace"), -1)
def test_entered_workspace_indices_gives_correct_selection_back(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.wkspIndices.setText("1-3,5") dlg._ui.buttonBox.button(QDialogButtonBox.Ok).click() self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual([1, 2, 3, 5], dlg.selection.wksp_indices)
def test_ok_button_disabled_when_plot_type_is_contour_and_more_than_one_workspace_index_entered( self): workspaces = [self._multi_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(4) ssd._ui.wkspIndices.setText("1,2") self.assertFalse( ssd._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled())
def test_entered_spectra_gives_correct_selection_back(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.wkspIndices.setText("50-60") dlg._ui.buttonBox.button(QDialogButtonBox.Ok).click() self._mock_get_icon.assert_called_once_with('fa.asterisk', color='red', scale_factor=0.6) self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual(list(range(50, 61)), dlg.selection.wksp_indices)
def test_ok_button_disabled_when_log_value_set_to_custom_and_custom_log_values_left_blank( self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.specNums.setText("1") custom_index = ssd._ui.advanced_options_widget.ui.log_value_combo_box.count( ) - 1 ssd._ui.advanced_options_widget.ui.log_value_combo_box.setCurrentIndex( custom_index) self.assertFalse( ssd._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled())
def test_valid_text_in_boxes_activates_ok(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) def do_test(input_box): input_box.setText("1") self.assertTrue(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled()) input_box.clear() self.assertFalse(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled()) do_test(dlg._ui.wkspIndices) do_test(dlg._ui.specNums)
def get_spectra_selection(workspaces, parent_widget=None, show_colorfill_btn=False): """ Decides whether it is necessary to request user input when asked to plot a list of workspaces. The input dialog will only be shown in the case where all workspaces have more than 1 spectrum :param workspaces: A list of MatrixWorkspaces that will be plotted :param parent_widget: An optional parent_widget to use for the input selection dialog :returns: Either a SpectraSelection object containing the details of workspaces to plot or None indicating the request was cancelled :raises ValueError: if the workspaces are not of type MatrixWorkspace """ SpectraSelectionDialog.raise_error_if_workspaces_not_compatible(workspaces) single_spectra_ws = [ wksp.getNumberHistograms() for wksp in workspaces if wksp.getNumberHistograms() == 1 ] if len(single_spectra_ws) > 0: # At least 1 workspace contains only a single spectrum so this is all # that is possible to plot for all of them selection = SpectraSelection(workspaces) selection.wksp_indices = [0] return selection else: selection_dlg = SpectraSelectionDialog( workspaces, parent=parent_widget, show_colorfill_btn=show_colorfill_btn) res = selection_dlg.exec_() if res == SpectraSelectionDialog.Rejected: # cancelled return None else: user_selection = selection_dlg.selection if user_selection == 'colorfill': return user_selection # the dialog should guarantee that only 1 of spectrum/indices is supplied assert user_selection.spectra is None or user_selection.wksp_indices is None return user_selection
def test_log_value_combo_box_contains_sample_logs(self): ws1 = CreateSampleWorkspace() ws2 = CreateSampleWorkspace() workspaces = [ws1, ws2] for i, ws in enumerate(workspaces): AddSampleLog(Workspace=ws.name(), LogName="Test", LogText=str(i)) ssd = SpectraSelectionDialog(workspaces, advanced=True) self.assertNotEqual( ssd._ui.advanced_options_widget.ui.log_value_combo_box.findText( "Test"), -1)
def test_custom_log_values_line_edit_becomes_enabled_when_log_value_set_to_custom( self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) custom_index = ssd._ui.advanced_options_widget.ui.log_value_combo_box.count( ) - 1 ssd._ui.advanced_options_widget.ui.log_value_combo_box.setCurrentIndex( custom_index) self._mock_get_icon.assert_called_once_with('mdi.asterisk', 'red', 0.6) self.assertTrue(ssd._ui.advanced_options_widget.ui. custom_log_line_edit.isEnabled())
def test_ok_button_disabled_when_number_of_custom_log_values_does_not_equal_number_of_plots( self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.specNums.setText("1") custom_index = ssd._ui.advanced_options_widget.ui.log_value_combo_box.count( ) - 1 ssd._ui.advanced_options_widget.ui.log_value_combo_box.setCurrentIndex( custom_index) ssd._ui.advanced_options_widget.ui.custom_log_line_edit.setText("0,1") self.assertFalse( ssd._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled())
def test_plot_all_sets_correct_selection_values(self): workspaces = [self._multi_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(2) ssd._ui.buttonBox.button(QDialogButtonBox.YesToAll).click() self.assertEqual(ssd.selection.wksp_indices, range(0, 200)) self.assertEqual(ssd.selection.plot_type, 2) self.assertEqual(ssd.selection.errors, False) self.assertEqual(ssd.selection.log_name, "Workspace name") self.assertEqual(ssd.selection.axis_name, "Workspace name")
def test_filling_workspace_details_multiple_workspace_with_spectra_gaps( self): gappy_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=50, XLength=1, YLength=1) for i in range(20): gappy_ws.getSpectrum(i).setSpectrumNo(1 + i) for i in range(20, gappy_ws.getNumberHistograms()): gappy_ws.getSpectrum(i).setSpectrumNo(161 + i) dlg = SpectraSelectionDialog([gappy_ws, self._multi_spec_ws]) self.assertEqual("valid range: 1-20, 181-200", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-49", dlg._ui.wkspIndices.placeholderText())
def test_filling_workspace_details_single_workspace_with_spectra_gaps( self): gappy_ws = WorkspaceFactory.Instance().create("Workspace2D", NVectors=50, XLength=1, YLength=1) for i in range(10): gappy_ws.getSpectrum(i).setSpectrumNo(1 + i) for i in range(10, 16): gappy_ws.getSpectrum(i).setSpectrumNo(1 + (2 * i)) for i in range(17, 20): gappy_ws.getSpectrum(i).setSpectrumNo(1 + i) for i in range(20, gappy_ws.getNumberHistograms()): gappy_ws.getSpectrum(i).setSpectrumNo(51 + i) dlg = SpectraSelectionDialog([gappy_ws]) self.assertEqual( "valid range: 1-10, 17-21, 23, 25, 27, 29, 31, 71-100", dlg._ui.specNums.placeholderText()) self.assertEqual("valid range: 0-49", dlg._ui.wkspIndices.placeholderText())
def test_plot_all_gives_only_workspaces_indices(self): dlg = SpectraSelectionDialog([self._multi_spec_ws]) dlg._ui.buttonBox.button(QDialogButtonBox.YesToAll).click() self.assertTrue(dlg.selection is not None) self.assertTrue(dlg.selection.spectra is None) self.assertEqual(range(200), dlg.selection.wksp_indices)
def test_advanced_options_widget_created_when_advanced_is_true(self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) self.assertTrue(hasattr(ssd._ui, 'advanced_options_widget'))
def test_initial_dialog_setup(self): workspaces = [self._multi_spec_ws] dlg = SpectraSelectionDialog(workspaces) self.assertFalse(dlg._ui.buttonBox.button(QDialogButtonBox.Ok).isEnabled())
def test_advanced_plots_includes_surface_and_contour_when_there_are_more_than_two_workspaces( self): workspaces = [self._single_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) self.assertNotEqual(ssd._ui.plotType.findText("Surface"), -1) self.assertNotEqual(ssd._ui.plotType.findText("Contour"), -1)
def test_error_bars_check_box_is_disabled_for_contour_plots(self): workspaces = [self._single_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(4) self.assertFalse(ssd._ui.advanced_options_widget.ui. error_bars_check_box.isEnabled())
def test_log_value_combo_box_contains_custom_option(self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) self.assertNotEqual( ssd._ui.advanced_options_widget.ui.log_value_combo_box.findText( "Custom"), -1)
def test_log_options_are_disabled_for_tiled_plots(self): workspaces = [self._single_spec_ws] ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(2) self.assertFalse( ssd._ui.advanced_options_widget.ui.log_value_combo_box.isEnabled())
def test_plot_all_button_disabled_when_plot_type_is_contour(self): workspaces = [self._single_spec_ws] * 3 ssd = SpectraSelectionDialog(workspaces, advanced=True) ssd._ui.plotType.setCurrentIndex(4) self.assertFalse( ssd._ui.buttonBox.button(QDialogButtonBox.YesToAll).isEnabled())