Skip to content
Snippets Groups Projects
test_plotssaver.py 11.1 KiB
Newer Older
# 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 mantidqt package
#
from __future__ import (absolute_import, division, print_function, unicode_literals)

import unittest

Samuel Jones's avatar
Samuel Jones committed
from mantid.api import AnalysisDataService as ADS
from mantidqt.project.plotssaver import PlotsSaver
Samuel Jones's avatar
Samuel Jones committed
from mantidqt.project.plotsloader import PlotsLoader
from mantid.simpleapi import CreateSampleWorkspace
Samuel Jones's avatar
Samuel Jones committed
import matplotlib
matplotlib.use('AGG')

Samuel Jones's avatar
Samuel Jones committed
class PlotsSaverTest(unittest.TestCase):
    def setUp(self):
Samuel Jones's avatar
Samuel Jones committed
        CreateSampleWorkspace(OutputWorkspace="ws1")
        self.plots_loader = PlotsLoader()

        # Make a figure with a given input with all these values already set
        self.loader_plot_dict = {u'axes': [{u'colorbar': {u'exists': False},
                                            u'legend': {u'exists': False}, u'lines': [{u'alpha': 1,
Samuel Jones's avatar
Samuel Jones committed
                                                                                       u'color': u'#1f77b4',
                                                                                       u'label': u'ws1: spec 2',
                                                                                       u'lineIndex': 0,
                                                                                       u'lineStyle': u'-',
                                                                                       u'lineWidth': 1.5,
                                                                                       u'markerStyle': {
                                                                                           u'edgeColor': u'#1f77b4',
                                                                                           u'edgeWidth': 1.0,
                                                                                           u'faceColor': u'#1f77b4',
                                                                                           u'markerSize': 6.0,
                                                                                           u'markerType': u'None',
                                                                                           u'zOrder': 2},
                                                                                       u'errorbars': {
                                                                                           u'exists': False
                                                                                       }}],
Samuel Jones's avatar
Samuel Jones committed
                                 u'properties': {u'axisOn': True, u'bounds': (0.0, 0.0, 0.0, 0.0), u'dynamic': True,
                                                 u'frameOn': True, u'visible': True,
                                                 u'xAxisProperties': {u'fontSize': 10.0,
                                                                      u'gridStyle': {u'gridOn': False},
                                                                      u'majorTickFormat': None,
                                                                      u'majorTickFormatter': u'ScalarFormatter',
                                                                      u'majorTickLocator': u'AutoLocator',
Samuel Jones's avatar
Samuel Jones committed
                                                                      u'majorTickLocatorValues': None,
                                                                      u'minorTickFormat': None,
                                                                      u'minorTickFormatter': u'NullFormatter',
                                                                      u'minorTickLocator': u'NullLocator',
Samuel Jones's avatar
Samuel Jones committed
                                                                      u'minorTickLocatorValues': None,
                                                                      u'position': u'Bottom',
                                                                      u'visible': True},
                                                 u'xAxisScale': u'linear', u'xLim': (0.0, 1.0),
                                                 u'yAxisProperties': {u'fontSize': 10.0,
                                                                      u'gridStyle': {u'gridOn': False},
                                                                      u'majorTickFormat': None,
                                                                      u'majorTickFormatter': u'ScalarFormatter',
                                                                      u'majorTickLocator': u'AutoLocator',
Samuel Jones's avatar
Samuel Jones committed
                                                                      u'majorTickLocatorValues': None,
                                                                      u'minorTickFormat': None,
                                                                      u'minorTickFormatter': u'NullFormatter',
                                                                      u'minorTickLocator': u'NullLocator',
Samuel Jones's avatar
Samuel Jones committed
                                                                      u'minorTickLocatorValues': None,
                                                                      u'position': u'Left',
                                                                      u'visible': True},
                                                 u'yAxisScale': u'linear', u'yLim': (0.0, 1.0)},
                                            u'textFromArtists': {}, u'texts': [{u'position': (0, 0),
                                                                                u'style': {u'alpha': 1,
                                                                                           u'color': u'#000000',
                                                                                           u'hAlign': u'left',
                                                                                           u'rotation': 0.0,
                                                                                           u'textSize': 10.0,
                                                                                           u'vAlign': u'baseline',
                                                                                           u'zOrder': 3},
                                                                                u'text': u'text', u'useTeX': False}],
                                            u'title': u'', u'xAxisTitle': u'',
                                            u'yAxisTitle': u''}],
                                 u'creationArguments': [[{u"workspaces": u"ws1", u"specNum": 2, u"function": u"plot"}]],
Samuel Jones's avatar
Samuel Jones committed
                                 u'label': u'',
                                 u'properties': {u'dpi': 100.0, u'figHeight': 4.8, u'figWidth': 6.4}}
        self.fig = self.plots_loader.make_fig(self.loader_plot_dict, create_plot=False)
        self.plot_saver = PlotsSaver()

Samuel Jones's avatar
Samuel Jones committed
    def tearDown(self):
        ADS.clear()

    def test_save_plots(self):
        plot_dict = {}
        return_value = self.plot_saver.save_plots(plot_dict)

        self.assertEqual(return_value, [])

    def test_get_dict_from_fig(self):
        self.fig.axes[0].creation_args = [{u"specNum": 2, "function": "plot"}]
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_from_fig(self.fig)
Tom Titcombe's avatar
Tom Titcombe committed
        # If post_creation_args not in creation_args, we should add error_added = False and
        # errors_visible = False by default
        self.loader_plot_dict[u'creationArguments'] = [[{u"specNum": 2, "function": "plot",
                                                         "post_creation_args": {"errors_added": False,
                                                                                "errors_visible": False}}]]
Samuel Jones's avatar
Samuel Jones committed
        self.maxDiff = None
        self.assertDictEqual(return_value, self.loader_plot_dict)
Tom Titcombe's avatar
Tom Titcombe committed
    def test_post_creation_args_are_not_overwritten_if_they_exist(self):
        self.fig.axes[0].creation_args = [{u"specNum": 2, "function": "plot",
                                           "post_creation_args": {"errors_added": True,
                                                                  "errors_visible": True}}]
        return_value = self.plot_saver.get_dict_from_fig(self.fig)

        # Since the creation args contain post_creation_args, they are not changed during saving
        self.loader_plot_dict[u'creationArguments'] = [[{u"specNum": 2, "function": "plot",
                                                         "post_creation_args": {"errors_added": True,
                                                                                "errors_visible": True}}]]

        self.maxDiff = None
        self.assertEqual(return_value[u"creationArguments"], self.loader_plot_dict[u"creationArguments"])

    def test_get_dict_from_axes(self):
        self.plot_saver.figure_creation_args = [{"function": "plot"}]
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_for_axes(self.fig.axes[0])

        expected_value = self.loader_plot_dict["axes"][0]

        self.maxDiff = None
        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_axes_properties(self):
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_from_axes_properties(self.fig.axes[0])

        expected_value = self.loader_plot_dict["axes"][0]["properties"]

        self.maxDiff = None
        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_axis_properties(self):
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_from_axis_properties(self.fig.axes[0].xaxis)
Samuel Jones's avatar
Samuel Jones committed
        expected_value = self.loader_plot_dict["axes"][0]["properties"]["xAxisProperties"]

        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_for_grid_style(self):
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_for_grid_style(self.fig.axes[0].xaxis)
Samuel Jones's avatar
Samuel Jones committed
        expected_value = self.loader_plot_dict["axes"][0]["properties"]["xAxisProperties"]["gridStyle"]

        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_line(self):
        self.plot_saver.figure_creation_args = [{"function": "plot"}]
Samuel Jones's avatar
Samuel Jones committed
        line = self.fig.axes[0].lines[0]
        return_value = self.plot_saver.get_dict_from_line(line, 0)

Samuel Jones's avatar
Samuel Jones committed
        expected_value = self.loader_plot_dict["axes"][0]["lines"][0]

        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_marker_style(self):
Samuel Jones's avatar
Samuel Jones committed
        line = self.fig.axes[0].lines[0]
        return_value = self.plot_saver.get_dict_from_marker_style(line)

Samuel Jones's avatar
Samuel Jones committed
        expected_value = self.loader_plot_dict["axes"][0]["lines"][0]["markerStyle"]

        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_text_style(self):
Samuel Jones's avatar
Samuel Jones committed
        text = self.fig.axes[0].texts[0]
        return_value = self.plot_saver.get_dict_from_text(text)

Samuel Jones's avatar
Samuel Jones committed
        expected_value = self.loader_plot_dict["axes"][0]["texts"][0]
Samuel Jones's avatar
Samuel Jones committed
        self.maxDiff = None
        self.assertDictEqual(return_value, expected_value)

    def test_get_dict_from_fig_properties(self):
Samuel Jones's avatar
Samuel Jones committed
        return_value = self.plot_saver.get_dict_from_fig_properties(self.fig)

        expected_value = {u'dpi': 100.0, u'figHeight': 4.8, u'figWidth': 6.4}

        self.assertDictEqual(return_value, expected_value)