def _create_plot(self, names, errors, overplot, wksp_indices=None, plot_type=SpectraSelection.Tiled, fig=None):
        selection = SpectraSelection(names)
        selection.wksp_indices = wksp_indices if wksp_indices else [0]
        selection.plot_type = plot_type
        self.get_spectra_selection_mock.return_value = selection

        return plot_from_names(names=names, errors=errors, overplot=overplot, fig=fig)
    def _do_plot_from_names_test(self,
                                 get_spectra_selection_mock,
                                 expected_labels,
                                 wksp_indices,
                                 errors,
                                 overplot,
                                 target_fig=None):
        ws_name = 'test_plot_from_names-1'
        AnalysisDataService.Instance().addOrReplace(ws_name, self._test_ws)

        selection = SpectraSelection([self._test_ws])
        selection.wksp_indices = wksp_indices
        get_spectra_selection_mock.return_value = selection
        fig = plot_from_names([ws_name], errors, overplot, target_fig)
        if target_fig is not None:
            self.assertEqual(target_fig, fig)

        plotted_lines = fig.gca().get_legend().get_lines()
        self.assertEqual(len(expected_labels), len(plotted_lines))
        for label_part, line in zip(expected_labels, plotted_lines):
            if label_part is not None:
                self.assertTrue(
                    label_part in line.get_label(),
                    msg="Label fragment '{}' not found in line label".format(
                        label_part))
        return fig
 def test_plot_from_names_calls_plot(self, get_spectra_selection_mock, plot_mock):
     ws_name = 'test_plot_from_names_calls_plot-1'
     AnalysisDataService.Instance().addOrReplace(ws_name, self._test_ws)
     selection = SpectraSelection([self._test_ws])
     selection.wksp_indices = [0]
     get_spectra_selection_mock.return_value = selection
     plot_from_names([ws_name], errors=False, overplot=False)
     self.assertEqual(1, plot_mock.call_count)
    def test_plot_from_names_calls_plot(self, get_spectra_selection_mock, plot_mock):
        ws_name = 'test_plot_from_names_calls_plot-1'
        AnalysisDataService.Instance().addOrReplace(ws_name, self._test_ws)
        selection = SpectraSelection([self._test_ws])
        selection.wksp_indices = [0]
        get_spectra_selection_mock.return_value = selection
        plot_from_names([ws_name], errors=False, overplot=False)

        self.assertEqual(1, plot_mock.call_count)
Exemple #5
0
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 _do_plot_from_names_test(self, get_spectra_selection_mock, expected_labels,
                                 wksp_indices, errors, overplot, target_fig=None):
        ws_name = 'test_plot_from_names-1'
        AnalysisDataService.Instance().addOrReplace(ws_name, self._test_ws)

        selection = SpectraSelection([self._test_ws])
        selection.wksp_indices = wksp_indices
        get_spectra_selection_mock.return_value = selection
        fig = plot_from_names([ws_name], errors, overplot, target_fig)
        if target_fig is not None:
            self.assertEqual(target_fig, fig)

        plotted_lines = fig.gca().get_legend().get_lines()
        self.assertEqual(len(expected_labels), len(plotted_lines))
        for label_part, line in zip(expected_labels, plotted_lines):
            if label_part is not None:
                self.assertTrue(label_part in line.get_label(),
                                msg="Label fragment '{}' not found in line label".format(label_part))
        return fig
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