From c2e2b9a5ea313802586d4291b50c20277af88eee Mon Sep 17 00:00:00 2001 From: Stephen <stephen.smith@stfc.ac.uk> Date: Thu, 9 Jan 2020 09:41:55 +0000 Subject: [PATCH] Writing additional tests based on new plotting options Introduction of new tests for the plotting options, covering: Errors selected Axis limits changed in options Axis limits changed through the figure toolbar Autoscale requested --- .../Common/plotting_widget/plotting_widget.py | 3 - .../plotting_widget/plotting_widget_model.py | 9 +- .../plotting_widget_presenter.py | 27 ++-- scripts/test/Muon/plotting_widget_test.py | 119 ++++++++++++++++++ 4 files changed, 137 insertions(+), 21 deletions(-) diff --git a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget.py b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget.py index 37372c95e48..976ba776e6a 100644 --- a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget.py +++ b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget.py @@ -13,11 +13,8 @@ from Muon.GUI.Common.plotting_widget.plotting_widget_model import PlotWidgetMode class PlottingWidget(object): def __init__(self, context=None): - # initialise the view, presenter and model. self.view = PlotWidgetView(parent=None) - # model self.model = PlotWidgetModel() - # presenter self.presenter = PlotWidgetPresenter(self.view, self.model, context) diff --git a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_model.py b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_model.py index b6932915a67..599340a8026 100644 --- a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_model.py +++ b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_model.py @@ -231,12 +231,13 @@ class PlotWidgetModel(object): def autoscale_axes(self, axes, xlimits): ymin, ymax = self._get_autoscale_y_limits(axes, xlimits[0], xlimits[1]) plt.setp(axes, xlim=xlimits, ylim=[ymin, ymax]) + return ymin, ymax - def set_axis_xlim(self, axis, xmin, xmax): - axis.set_xlim(left=xmin, right=xmax) + def set_axis_xlim(self, axis, xlims): + axis.set_xlim(left=xlims[0], right=xlims[1]) - def set_axis_ylim(self, axis, ymin, ymax): - axis.set_ylim(ymin, ymax) + def set_axis_ylim(self, axis, ylims): + axis.set_ylim(ylims[0], ylims[1]) def _get_autoscale_y_limits(self, axes, xmin, xmax): new_bottom = 1e9 diff --git a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_presenter.py b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_presenter.py index 689ad571d4d..aad4c9184f4 100644 --- a/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_presenter.py +++ b/scripts/Muon/GUI/Common/plotting_widget/plotting_widget_presenter.py @@ -54,7 +54,7 @@ class PlotWidgetPresenter(HomeTabSubWidget): self._view.plot_options.connect_x_range_changed(self.handle_xlim_changed_in_options_view) self._view.plot_options.connect_y_range_changed(self.handle_ylim_changed_in_options_view) self._view.plot_options.connect_autoscale_changed(self.handle_autoscale_requested_in_view) - self._view.plot_options.connect_plot_selection(self.handle_subplot_changed_in_options_view) + self._view.plot_options.connect_plot_selection(self.handle_subplot_changed_in_options) if self.context._frequency_context: for ext in FREQUENCY_EXTENSIONS.keys(): @@ -178,19 +178,19 @@ class PlotWidgetPresenter(HomeTabSubWidget): self._model.autoscale_axes(self._view.get_axes(), self.get_x_limits()) self._view.force_redraw() - def handle_xlim_changed_in_options_view(self, xlim): + def handle_xlim_changed_in_options_view(self, xlims): subplots = self._view.plot_options.get_selection() for subplot in subplots: index = int(subplot) - 1 - self._model.set_axis_xlim(self._view.get_axes()[index], xlim[0], xlim[1]) + self._model.set_axis_xlim(self._view.get_axes()[index], xlims) self._view.force_redraw() - def handle_ylim_changed_in_options_view(self, ylim): + def handle_ylim_changed_in_options_view(self, ylims): subplots = self._view.plot_options.get_selection() for subplot in subplots: index = int(subplot) - 1 - self._model.set_axis_ylim(self._view.get_axes()[index], ylim[0], ylim[1]) + self._model.set_axis_ylim(self._view.get_axes()[index], ylims) self._view.force_redraw() @@ -202,30 +202,29 @@ class PlotWidgetPresenter(HomeTabSubWidget): indices = [ix - 1 for ix in list(map(int, subplots))] axes = [self._view.get_axes()[index] for index in indices] - self._model.autoscale_axes(axes, xlims) - ymin, ymax = self.get_y_lim_from_subplot(0) + ymin, ymax = self._model.autoscale_axes(axes, xlims) self._view.plot_options.set_plot_y_range([ymin, ymax]) self._view.force_redraw() - def handle_x_axis_limits_changed_in_figure_view(self, axes): + def handle_x_axis_limits_changed_in_figure_view(self, axis): subplots = self._view.plot_options.get_selection() if len(subplots) > 1 or len(subplots) == 0: return if subplots[0].isdigit(): - if axes == self._view.get_axes()[int(subplots[0]) - 1]: - xmin, xmax = axes.get_xlim() + if axis == self._view.get_axes()[int(subplots[0]) - 1]: + xmin, xmax = axis.get_xlim() self._view.plot_options.set_plot_x_range([xmin, xmax]) - def handle_y_axis_limits_changed_in_figure_view(self, axes): + def handle_y_axis_limits_changed_in_figure_view(self, axis): subplots = self._view.plot_options.get_selection() if len(subplots) > 1 or len(subplots) == 0: return if subplots[0].isdigit(): - if axes == self._view.get_axes()[int(subplots[0]) - 1]: - ymin, ymax = axes.get_ylim() + if axis == self._view.get_axes()[int(subplots[0]) - 1]: + ymin, ymax = axis.get_ylim() self._view.plot_options.set_plot_y_range([ymin, ymax]) - def handle_subplot_changed_in_options_view(self): + def handle_subplot_changed_in_options(self): subplots = self._view.plot_options.get_selection() if len(subplots) == 0 or len(subplots) > 1: return diff --git a/scripts/test/Muon/plotting_widget_test.py b/scripts/test/Muon/plotting_widget_test.py index e6a35674c7d..78038a04ba2 100644 --- a/scripts/test/Muon/plotting_widget_test.py +++ b/scripts/test/Muon/plotting_widget_test.py @@ -8,6 +8,7 @@ import unittest from mantid.py3compat import mock from mantidqt.utils.qt.testing import start_qapplication + from Muon.GUI.Common.plotting_widget.plotting_widget_presenter import PlotWidgetPresenter from Muon.GUI.Common.muon_group import MuonGroup from Muon.GUI.Common.contexts.fitting_context import FitInformation @@ -226,6 +227,124 @@ class PlottingWidgetPresenterTest(unittest.TestCase): workspace_indices=[1], errors=False, plot_kwargs=plot_kwargs) + def test_handle_subplot_changes_correctly_retrieves_axis_limits(self): + subplot = '1' + self.view.plot_options.get_selection.return_value = [subplot] + + self.presenter.handle_subplot_changed_in_options() + + self.presenter.get_x_lim_from_subplot.assert_called_with(int(subplot) - 1) + self.presenter.get_y_lim_from_subplot.assert_called_with(int(subplot) - 1) + + def test_handle_subplot_changes_correctly_updates_options(self): + subplot = '1' + self.view.plot_options.get_selection.return_value = [subplot] + + self.presenter.handle_subplot_changed_in_options() + + self.view.plot_options.set_plot_x_range.assert_called_with(self.presenter.get_x_lim_from_subplot()) + self.view.plot_options.set_plot_y_range.assert_called_with(self.presenter.get_y_lim_from_subplot()) + + def test_handle_subplot_changes_does_not_update_options_if_all_subplots_selected(self): + subplots = ['1', '2', '3', '4'] + self.view.plot_options.get_selection.return_value = subplots + + self.presenter.handle_subplot_changed_in_options() + + self.view.plot_options.set_plot_x_range.assert_not_called() + self.view.plot_options.set_plot_y_range.assert_not_called() + + def test_handle_autoscale_correctly_updates_axis(self): + subplot = '1' + xlims = [0, 10] + ylims = [-3, 3] + self.view.plot_options.get_selection.return_value = [subplot] + self.view.plot_options.get_plot_x_range.return_value = xlims + self.view.plot_options.set_plot_y_range = mock.MagicMock() + self.model.autoscale_axes = mock.MagicMock(return_value=ylims) + + self.presenter.handle_autoscale_requested_in_view() + + self.model.autoscale_axes.assert_called_with(mock.ANY, xlims) + self.view.plot_options.set_plot_y_range.assert_called_once_with(ylims) + + def test_handle_x_axis_limits_changed_in_options_updates_plot_correctly(self): + subplot = '1' + self.view.plot_options.get_selection.return_value = [subplot] + self.model.set_axis_xlim = mock.MagicMock() + xlims = [0, 10] + + self.presenter.handle_xlim_changed_in_options_view(xlims) + + self.model.set_axis_xlim.assert_called_once_with(mock.ANY, xlims) + + def test_handle_y_axis_limits_changed_in_options_updates_plot_correctly(self): + subplot = '1' + self.view.plot_options.get_selection.return_value = [subplot] + self.model.set_axis_ylim = mock.MagicMock() + ylims = [-1, 1] + + self.presenter.handle_ylim_changed_in_options_view(ylims) + + self.model.set_axis_ylim.assert_called_once_with(mock.ANY, ylims) + + def test_handle_x_axis_limits_changed_in_figure_updates_plot_correctly(self): + subplot = '1' + xlims = [0, 15] + self.view.plot_options.get_selection.return_value = [subplot] + self.view.plot_options.set_plot_x_range = mock.MagicMock() + axis = self.view.get_axes()[0] + axis.get_xlim.return_value = xlims + + self.presenter.handle_x_axis_limits_changed_in_figure_view(axis) + + self.view.plot_options.set_plot_x_range.assert_called_once_with(xlims) + + def test_handle_x_axis_limits_changed_in_figure_does_not_update_if_all_subplots_chosen(self): + subplots = ['1,2,3,4'] + self.view.plot_options.get_selection.return_value = subplots + axis = self.view.get_axes()[0] + self.view.plot_options.set_plot_x_range = mock.MagicMock() + + self.presenter.handle_x_axis_limits_changed_in_figure_view(axis) + + self.view.plot_options.set_plot_x_range.assert_not_called() + + def test_handle_y_axis_limits_changed_in_figure_updates_plot_correctly(self): + subplot = '1' + ylims = [-1, 1] + self.view.plot_options.get_selection.return_value = [subplot] + self.view.plot_options.set_plot_y_range = mock.MagicMock() + axis = self.view.get_axes()[0] + axis.get_ylim.return_value = ylims + + self.presenter.handle_y_axis_limits_changed_in_figure_view(axis) + + self.view.plot_options.set_plot_y_range.assert_called_once_with(ylims) + + def test_handle_y_axis_limits_changed_in_figure_does_not_update_if_all_subplots_chosen(self): + subplots = ['1,2,3,4'] + self.view.plot_options.get_selection.return_value = subplots + axis = self.view.get_axes()[0] + self.view.plot_options.set_plot_x_range = mock.MagicMock() + + self.presenter.handle_y_axis_limits_changed_in_figure_view(axis) + + self.view.plot_options.set_plot_y_range.assert_not_called() + + def test_handle_error_selection_changed_correctly_replots_workspace_data(self): + errors = True + self.model.replot_workspace = mock.MagicMock() + workspaces = ["fwd", "bwd"] + self.model.plotted_workspaces = workspaces + self.presenter.get_workspace_legend_label = mock.MagicMock(return_value='label') + + self.presenter.handle_error_selection_changed(errors) + + self.assertEqual(self.model.replot_workspace.call_count, len(workspaces)) + self.model.replot_workspace.assert_any_call(workspaces[0], self.view.get_axes()[0], errors, mock.ANY) + self.model.replot_workspace.assert_called_with(workspaces[1], self.view.get_axes()[0], errors, mock.ANY) + if __name__ == '__main__': unittest.main(buffer=False, verbosity=2) -- GitLab