def _do_show_data(self, names): # local import to allow this module to be imported without pyplot being imported import matplotlib.pyplot parent, flags = get_window_config() for ws in self._ads.retrieveWorkspaces(names, unrollGroups=True): try: MatrixWorkspaceDisplay.supports(ws) # the plot function is being injected in the presenter # this is done so that the plotting library is mockable in testing presenter = MatrixWorkspaceDisplay(ws, plot=plot, parent=parent, window_flags=flags) presenter.show_view() except ValueError: try: TableWorkspaceDisplay.supports(ws) presenter = TableWorkspaceDisplay(ws, plot=matplotlib.pyplot, parent=parent, window_flags=flags, batch=True) presenter.show_view() except ValueError: logger.error( "Could not open workspace: {0} with neither " "MatrixWorkspaceDisplay nor TableWorkspaceDisplay." "".format(ws.name()))
def test_that_copy_spectrum_to_table_does_not_cause_an_error_with_a_non_ragged_workspace( self): with mock.patch( 'qtpy.QtCore.QItemSelectionModel.selectedRows') as patch: self.presenter = MatrixWorkspaceDisplay(self.workspace) self.presenter.show_view() table = self.presenter.view.currentWidget() patch.return_value = [table.model().index(0, 0)] self.presenter.action_copy_spectrum_to_table(table)
def test_that_the_workspace_display_opens_and_closes_ok_with_a_ragged_workspace( self): self.presenter = MatrixWorkspaceDisplay(self.ragged_workspace) self.presenter.show_view() self.assert_widget_created() self.presenter.container.close() QApplication.sendPostedEvents() self.assert_no_toplevel_widgets()
def test_window_deleted_correctly(self): ws = CreateSampleWorkspace() p = MatrixWorkspaceDisplay(ws) self.assert_widget_created() p.close(ws.name()) self.assert_widget_created() QApplication.processEvents() self.assertEqual(None, p.ads_observer) self.assert_widget_not_present("work") self.assert_no_toplevel_widgets()
def test_window_deleted_correctly(self): ws = CreateSampleWorkspace() p = MatrixWorkspaceDisplay(ws) self.assert_window_created() p.close(ws.name()) self.assert_window_created() QApplication.processEvents() self.assertEqual(None, p.ads_observer) self.find_qt_widget("work") self.assert_no_widgets()
def test_window_deleted_correctly(self): ws = CreateSampleWorkspace() p = MatrixWorkspaceDisplay(ws) self.assert_widget_created() p.close(ws.name()) self.assert_widget_created() QApplication.sendPostedEvents() self.assertEqual(None, p.ads_observer) self.assert_widget_not_present("work") self.assert_no_toplevel_widgets()
def test_setup_table(self): ws = MockWorkspace() view = MockMatrixWorkspaceDisplayView() container = Mock(spec=StatusBarView) container.status_bar = Mock(spec=QStatusBar) MatrixWorkspaceDisplay(ws, view=view, container=container) self.assertEqual(3, view.set_context_menu_actions.call_count) self.assertEqual(1, view.set_model.call_count)
def wrapper(self, *args): ws = MockWorkspace() view = MockMatrixWorkspaceDisplayView() mock_observer = Mock() container = Mock(spec=StatusBarView) container.status_bar = Mock(spec=QStatusBar) presenter = MatrixWorkspaceDisplay(ws, view=view, ads_observer=mock_observer, container=container) return func(self, ws, view, presenter, *args)
def test_context_menu_correctly_disables_and_enables_overplot_options(self, mock_can_overplot): mock_can_overplot.return_value = False ws = CreateSampleWorkspace() mock_plot = mock.MagicMock() presenter = MatrixWorkspaceDisplay(ws, plot=mock_plot) view = presenter.view table = view.currentWidget() context_menu = view.setup_bin_context_menu(table) actions = context_menu.actions() self.assertEqual(actions[6].isEnabled(), False) self.assertEqual(actions[7].isEnabled(), False) mock_can_overplot.return_value = True context_menu = view.setup_bin_context_menu(table) actions = context_menu.actions() self.assertEqual(actions[6].isEnabled(), True) self.assertEqual(actions[7].isEnabled(), True) presenter.close(ws.name())
def test_context_has_expected_function_when_overplotting(self, mock_can_overplot): mock_can_overplot.return_value = True ws = CreateSampleWorkspace() mock_plot = mock.MagicMock() presenter = MatrixWorkspaceDisplay(ws, plot=mock_plot) view = presenter.view table = view.currentWidget() table.selectColumn(1) context_menu = view.setup_bin_context_menu(table) actions = context_menu.actions() # check triggering action 6 & 7 calls plot actions[6].trigger() presenter.plot.assert_called_with(mock.ANY, wksp_indices=mock.ANY, errors=False, overplot=True, plot_kwargs=mock.ANY) actions[7].trigger() presenter.plot.assert_called_with(mock.ANY, wksp_indices=mock.ANY, errors=True, overplot=True, plot_kwargs=mock.ANY) presenter.close(ws.name())
def test_context_has_expected_function_when_plotting(self): ws = CreateSampleWorkspace() mock_plot = mock.MagicMock() presenter = MatrixWorkspaceDisplay(ws, plot=mock_plot) view = presenter.view table = view.currentWidget() table.selectColumn(1) context_menu = view.setup_bin_context_menu(table) actions = context_menu.actions() # check we have 8 actions 2 separators - 2 copy - 2plot - 2 overplot self.assertEqual(len(actions), 8) # check triggering action 4 & 5 calls plot actions[3].trigger() presenter.plot.assert_called_with(mock.ANY, wksp_indices=mock.ANY, errors=False, overplot=False, plot_kwargs=mock.ANY) actions[4].trigger() presenter.plot.assert_called_with(mock.ANY, wksp_indices=mock.ANY, errors=True, overplot=False, plot_kwargs=mock.ANY) presenter.close(ws.name())
def _do_show_data(self, names): for ws in self._ads.retrieveWorkspaces(names, unrollGroups=True): try: MatrixWorkspaceDisplay.supports(ws) # the plot function is being injected in the presenter # this is done so that the plotting library is mockable in testing presenter = MatrixWorkspaceDisplay(ws, plot=plot, parent=self) presenter.show_view() except ValueError: try: TableWorkspaceDisplay.supports(ws) presenter = TableWorkspaceDisplay(ws, plot=matplotlib.pyplot, parent=self) presenter.show_view() except ValueError: logger.error( "Could not open workspace: {0} with neither " "MatrixWorkspaceDisplay nor TableWorkspaceDisplay." "".format(ws.name()))
def display_workspace(self, name): from mantidqt.widgets.workspacedisplay.matrix.presenter import MatrixWorkspaceDisplay from mantidqt.widgets.workspacedisplay.table.presenter import TableWorkspaceDisplay if AnalysisDataService.doesExist(name): ws = AnalysisDataService.retrieve(name) if isinstance(ws, MatrixWorkspace): presenter = MatrixWorkspaceDisplay(ws, plot=plot) presenter.show_view() elif isinstance(ws, ITableWorkspace): presenter = TableWorkspaceDisplay(ws, plot=matplotlib.pyplot) presenter.show_view()
def test_copy_cells(self): """Check that the data copied from cells is correct.""" ws = CreateSampleWorkspace() table = MatrixWorkspaceDisplay(ws) # Mock the copy_to_clipboard function with a side_effect that stores the clipboard content in a member variable. table.copy_to_clipboard = Mock(side_effect=self._mock_clipboard) # First tab is y values. current_table = table.view.tabs[0] selection = current_table.selectionModel() histogram_index = 0 y_index = 0 model_index = current_table.model().createIndex(histogram_index, y_index) selection.select(model_index, QItemSelectionModel.ClearAndSelect) # Call copy_cells top copy the currently selected cell. table.copy_cells(current_table) # Value should be the same as that taken from the workspace. y_from_ws = ws.readY(histogram_index)[y_index] self.assertEqual(str(y_from_ws), self.mock_clip)
def common_setup_action_plot(self, table_has_selection=True): mock_ws = MockWorkspace() mock_view = MockMatrixWorkspaceDisplayView() mock_plotter = Mock() container = Mock(spec=StatusBarView) container.status_bar = Mock(spec=QStatusBar) presenter = MatrixWorkspaceDisplay(mock_ws, plot=mock_plotter, view=mock_view, container=container) # monkey-patch the spectrum plot label to count the number of calls presenter.model.get_spectrum_plot_label = Mock() presenter.model.get_bin_plot_label = Mock() mock_table = MockQTableView() # configure the mock return values mock_table.mock_selection_model.hasSelection = Mock(return_value=table_has_selection) return mock_plotter, mock_table, mock_view, presenter
def decode(obj_dic, _=None): import matplotlib.pyplot as plt pres = MatrixWorkspaceDisplay(ADS.retrieve(obj_dic["workspace"]), plot=plt) return pres.container
class WorkspaceDisplayViewTest(unittest.TestCase, QtWidgetFinder): @classmethod def setUpClass(cls): cls.workspace = create_test_workspace("non-ragged") cls.ragged_workspace = create_test_workspace("ragged", ragged=True) @classmethod def tearDownClass(cls): AnalysisDataService.clear() def tearDown(self): if not sip.isdeleted(self.presenter.container): self.presenter.container.close() QApplication.sendPostedEvents() def test_that_the_workspace_display_opens_and_closes_ok_with_a_non_ragged_workspace( self): self.presenter = MatrixWorkspaceDisplay(self.workspace) self.presenter.show_view() self.assert_widget_created() self.presenter.container.close() QApplication.sendPostedEvents() self.assert_no_toplevel_widgets() def test_that_the_workspace_display_opens_and_closes_ok_with_a_ragged_workspace( self): self.presenter = MatrixWorkspaceDisplay(self.ragged_workspace) self.presenter.show_view() self.assert_widget_created() self.presenter.container.close() QApplication.sendPostedEvents() self.assert_no_toplevel_widgets() def test_that_copy_spectrum_to_table_does_not_cause_an_error_with_a_non_ragged_workspace( self): with mock.patch( 'qtpy.QtCore.QItemSelectionModel.selectedRows') as patch: self.presenter = MatrixWorkspaceDisplay(self.workspace) self.presenter.show_view() table = self.presenter.view.currentWidget() patch.return_value = [table.model().index(0, 0)] self.presenter.action_copy_spectrum_to_table(table) def test_that_copy_spectrum_to_table_does_not_cause_an_error_with_a_ragged_workspace( self): with mock.patch( 'qtpy.QtCore.QItemSelectionModel.selectedRows') as patch: self.presenter = MatrixWorkspaceDisplay(self.ragged_workspace) self.presenter.show_view() table = self.presenter.view.currentWidget() patch.return_value = [ table.model().index(0, 0), table.model().index(1, 0), table.model().index(2, 0) ] self.presenter.action_copy_spectrum_to_table(table)
# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source, # Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. # # # To Run - target this package with PyCharm, and __main__ will be executed import matplotlib matplotlib.use('Qt5Agg') from qtpy.QtWidgets import QApplication # noqa: F402 from mantid.simpleapi import Load # noqa: F402 from mantidqt.widgets.workspacedisplay.matrix.presenter import MatrixWorkspaceDisplay # noqa: F402 from mantidqt.plotting.functions import plot # noqa: F402 app = QApplication([]) LOQ74044 = Load("LOQ74044.nxs") window = MatrixWorkspaceDisplay(LOQ74044, plot) window.show_view() app.exec_()
# Mantid Repository : https://github.com/mantidproject/mantid # # Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI, # NScD Oak Ridge National Laboratory, European Spallation Source # & Institut Laue - Langevin # SPDX - License - Identifier: GPL - 3.0 + # This file is part of the mantid workbench. # # # To Run - target this package with PyCharm, and __main__ will be executed import matplotlib matplotlib.use('Qt5Agg') from qtpy.QtWidgets import QApplication # noqa: F402 from mantid.simpleapi import Load # noqa: F402 from mantidqt.widgets.workspacedisplay.matrix.presenter import MatrixWorkspaceDisplay # noqa: F402 from mantidqt.plotting.functions import plot # noqa: F402 app = QApplication([]) LOQ74044 = Load("LOQ74044.nxs") window = MatrixWorkspaceDisplay(LOQ74044, plot) window.show_view() app.exec_()