source: sasview/src/sas/qtgui/Plotting/UnitTesting/Plotter2DTest.py @ 863ebca

ESS_GUIESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since 863ebca was 863ebca, checked in by Piotr Rozyczko <piotrrozyczko@…>, 6 years ago

Introduced navigation bar toggle in context menu for all types of
charts. SASVIEW-890

  • Property mode set to 100644
File size: 8.7 KB
Line 
1import sys
2import unittest
3import numpy
4import platform
5
6from PyQt5 import QtGui, QtWidgets, QtPrintSupport
7from PyQt5 import QtCore
8from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
9from unittest.mock import MagicMock
10from mpl_toolkits.mplot3d import Axes3D
11
12####### TEMP
13import path_prepare
14#######
15from sas.qtgui.Plotting.PlotterData import Data1D
16from sas.qtgui.Plotting.PlotterData import Data2D
17from UnitTesting.TestUtils import WarningTestNotImplemented
18
19# Tested module
20import sas.qtgui.Plotting.Plotter2D as Plotter2D
21
22if not QtWidgets.QApplication.instance():
23    app = QtWidgets.QApplication(sys.argv)
24
25class Plotter2DTest(unittest.TestCase):
26    '''Test the Plotter 2D class'''
27    def setUp(self):
28        '''create'''
29        class dummy_manager(object):
30            def communicator(self):
31                return Communicate()
32            def perspective(self):
33                return MyPerspective()
34            def workspace(self):
35                return None
36
37        self.plotter = Plotter2D.Plotter2D(parent=dummy_manager(), quickplot=True)
38
39        self.data = Data2D(image=[0.1]*4,
40                           qx_data=[1.0, 2.0, 3.0, 4.0],
41                           qy_data=[10.0, 11.0, 12.0, 13.0],
42                           dqx_data=[0.1, 0.2, 0.3, 0.4],
43                           dqy_data=[0.1, 0.2, 0.3, 0.4],
44                           q_data=[1,2,3,4],
45                           xmin=-1.0, xmax=5.0,
46                           ymin=-1.0, ymax=15.0,
47                           zmin=-1.0, zmax=20.0)
48
49        self.data.title="Test data"
50        self.data.id = 1
51        self.isWindows = platform.system=="Windows"
52
53    def tearDown(self):
54        '''destroy'''
55        self.plotter.figure.clf()
56        self.plotter = None
57
58    def testDataProperty(self):
59        """ Adding data """
60        self.plotter.data = self.data
61
62        self.assertEqual(self.plotter.data, self.data)
63        self.assertEqual(self.plotter._title, self.data.title)
64        self.assertEqual(self.plotter.xLabel, "$\\rm{Q_{x}}(A^{-1})$")
65        self.assertEqual(self.plotter.yLabel, "$\\rm{Q_{y}}(A^{-1})$")
66
67    def testPlot(self):
68        """ Look at the plotting """
69        self.plotter.data = self.data
70        self.plotter.show()
71        FigureCanvas.draw_idle = MagicMock()
72
73        self.plotter.plot()
74
75        self.assertTrue(FigureCanvas.draw_idle.called)
76        self.plotter.figure.clf()
77
78    def testCalculateDepth(self):
79        ''' Test the depth calculator '''
80        self.plotter.data = self.data
81
82        # Default, log scale
83        depth = self.plotter.calculateDepth()
84        self.assertEqual(depth, (0.1, 1.e20))
85
86        # Change the scale to linear
87        self.plotter.scale = 'linear'
88        depth = self.plotter.calculateDepth()
89        self.assertEqual(depth[0], -32.)
90        self.assertAlmostEqual(depth[1], 1.30103, 5)
91
92    def testOnColorMap(self):
93        ''' Respond to the color map event '''
94        self.plotter.data = self.data
95        self.plotter.plot()
96        self.plotter.show()
97
98        QtWidgets.QDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Accepted)
99
100        # Just this one plot
101        self.plotter.onColorMap()
102
103        # Check that exec_ got called
104        self.assertTrue(QtWidgets.QDialog.exec_.called)
105
106        self.assertEqual(self.plotter.cmap, "jet")
107        self.assertAlmostEqual(self.plotter.vmin, 0.1, 6)
108        self.assertAlmostEqual(self.plotter.vmax, 1e+20, 6)
109        self.plotter.figure.clf()
110
111    def testOnToggleScale(self):
112        """ Respond to the event by replotting """
113        self.plotter.data = self.data
114        self.plotter.show()
115        FigureCanvas.draw_idle = MagicMock()
116
117        self.plotter.onToggleScale(None)
118
119        self.assertTrue(FigureCanvas.draw_idle.called)
120        self.plotter.figure.clf()
121
122    def testOnBoxSum(self):
123        """ Test the box sum display and functionality """
124
125        # hacky way to make things work in manipulations._sum
126        self.data.detector = [1]
127        self.data.err_data = numpy.array([0.0, 0.0, 0.1, 0.0])
128        self.plotter.data = self.data
129        self.plotter.show()
130
131        # Mock the main window
132        self.plotter.manager.parent = MagicMock()
133
134        # Call the main tested method
135        self.plotter.onBoxSum()
136
137        # Test various properties
138        self.assertIsInstance(self.plotter.slicer.model(), QtGui.QStandardItemModel)
139        self.assertTrue(self.plotter.boxwidget.isVisible())
140        self.assertIsInstance(self.plotter.boxwidget.model, QtGui.QStandardItemModel)
141        self.plotter.figure.clf()
142
143    def testContextMenuQuickPlot(self):
144        """ Test the right click menu """
145        self.plotter.data = self.data
146        self.plotter.createContextMenuQuick()
147        actions = self.plotter.contextMenu.actions()
148        self.assertEqual(len(actions), 9)
149
150        # Trigger Print Image and make sure the method is called
151        self.assertEqual(actions[1].text(), "Print Image")
152        QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected)
153        actions[1].trigger()
154        self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called)
155
156        # Trigger Copy to Clipboard and make sure the method is called
157        self.assertEqual(actions[2].text(), "Copy to Clipboard")
158
159        # Trigger Toggle Grid and make sure the method is called
160        self.assertEqual(actions[6].text(), "Toggle Grid On/Off")
161        self.plotter.ax.grid = MagicMock()
162        actions[6].trigger()
163        self.assertTrue(self.plotter.ax.grid.called)
164
165        # Trigger Change Scale and make sure the method is called
166        self.assertEqual(actions[8].text(), "Toggle Linear/Log Scale")
167        FigureCanvas.draw_idle = MagicMock()
168        actions[8].trigger()
169        self.assertTrue(FigureCanvas.draw_idle.called)
170
171        # Spy on cliboard's dataChanged() signal
172        if not self.isWindows:
173            return
174        self.clipboard_called = False
175        def done():
176            self.clipboard_called = True
177        QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done)
178        actions[2].trigger()
179        QtWidgets.qApp.processEvents()
180        # Make sure clipboard got updated.
181        self.assertTrue(self.clipboard_called)
182        self.plotter.figure.clf()
183
184    def testShowNoPlot(self):
185        """ Test the plot rendering and generation """
186
187        FigureCanvas.draw_idle = MagicMock()
188        FigureCanvas.draw = MagicMock()
189
190        # Test early return on no data
191        self.plotter.showPlot(data=None,
192                              qx_data=self.data.qx_data,
193                              qy_data=self.data.qy_data,
194                              xmin=self.data.xmin,
195                              xmax=self.data.xmax,
196                              ymin=self.data.ymin, ymax=self.data.ymax,
197                              cmap=None, zmin=None,
198                              zmax=None)
199
200        self.assertFalse(FigureCanvas.draw_idle.called)
201        self.assertFalse(FigureCanvas.draw.called)
202        self.plotter.figure.clf()
203
204    def testShow3DPlot(self):
205        """ Test the 3Dplot rendering and generation """
206        # Test 3D printout
207        FigureCanvas.draw = MagicMock()
208        Axes3D.plot_surface = MagicMock()
209        self.plotter.figure.colorbar = MagicMock()
210
211        self.plotter.dimension = 3
212        self.plotter.data = self.data
213        self.plotter.showPlot(data=self.plotter.data.data,
214                              qx_data=self.data.qx_data,
215                              qy_data=self.data.qy_data,
216                              xmin=self.data.xmin,
217                              xmax=self.data.xmax,
218                              ymin=self.data.ymin, ymax=self.data.ymax,
219                              cmap=None, zmin=None,
220                              zmax=None)
221        self.assertTrue(Axes3D.plot_surface.called)
222        self.assertTrue(FigureCanvas.draw.called)
223        self.plotter.figure.clf()
224
225    def testShow2DPlot(self):
226        """ Test the 2Dplot rendering and generation """
227        # Test 2D printout
228        FigureCanvas.draw_idle = MagicMock()
229        self.plotter.figure.colorbar = MagicMock()
230
231        self.plotter.dimension = 2
232        self.plotter.data = self.data
233        self.plotter.showPlot(data=self.plotter.data.data,
234                              qx_data=self.data.qx_data,
235                              qy_data=self.data.qy_data,
236                              xmin=self.data.xmin,
237                              xmax=self.data.xmax,
238                              ymin=self.data.ymin, ymax=self.data.ymax,
239                              cmap=None, zmin=None,
240                              zmax=None)
241        self.assertTrue(FigureCanvas.draw_idle.called)
242        self.plotter.figure.clf()
243
244
245if __name__ == "__main__":
246    unittest.main()
Note: See TracBrowser for help on using the repository browser.