1 | import sys |
---|
2 | import unittest |
---|
3 | import numpy |
---|
4 | import platform |
---|
5 | |
---|
6 | from PyQt5 import QtGui, QtWidgets, QtPrintSupport |
---|
7 | from PyQt5 import QtCore |
---|
8 | from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
---|
9 | from unittest.mock import MagicMock |
---|
10 | from mpl_toolkits.mplot3d import Axes3D |
---|
11 | |
---|
12 | ####### TEMP |
---|
13 | import path_prepare |
---|
14 | ####### |
---|
15 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
16 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
17 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
18 | |
---|
19 | # Tested module |
---|
20 | import sas.qtgui.Plotting.Plotter2D as Plotter2D |
---|
21 | |
---|
22 | if not QtWidgets.QApplication.instance(): |
---|
23 | app = QtWidgets.QApplication(sys.argv) |
---|
24 | |
---|
25 | class 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), 7) |
---|
149 | |
---|
150 | # Trigger Save Image and make sure the method is called |
---|
151 | self.assertEqual(actions[0].text(), "Save Image") |
---|
152 | self.plotter.toolbar.save_figure = MagicMock() |
---|
153 | actions[0].trigger() |
---|
154 | self.assertTrue(self.plotter.toolbar.save_figure.called) |
---|
155 | |
---|
156 | # Trigger Print Image and make sure the method is called |
---|
157 | self.assertEqual(actions[1].text(), "Print Image") |
---|
158 | QtPrintSupport.QPrintDialog.exec_ = MagicMock(return_value=QtWidgets.QDialog.Rejected) |
---|
159 | actions[1].trigger() |
---|
160 | self.assertTrue(QtPrintSupport.QPrintDialog.exec_.called) |
---|
161 | |
---|
162 | # Trigger Copy to Clipboard and make sure the method is called |
---|
163 | self.assertEqual(actions[2].text(), "Copy to Clipboard") |
---|
164 | |
---|
165 | # Trigger Toggle Grid and make sure the method is called |
---|
166 | self.assertEqual(actions[4].text(), "Toggle Grid On/Off") |
---|
167 | self.plotter.ax.grid = MagicMock() |
---|
168 | actions[4].trigger() |
---|
169 | self.assertTrue(self.plotter.ax.grid.called) |
---|
170 | |
---|
171 | # Trigger Change Scale and make sure the method is called |
---|
172 | self.assertEqual(actions[6].text(), "Toggle Linear/Log Scale") |
---|
173 | FigureCanvas.draw_idle = MagicMock() |
---|
174 | actions[6].trigger() |
---|
175 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
176 | |
---|
177 | # Spy on cliboard's dataChanged() signal |
---|
178 | if not self.isWindows: |
---|
179 | return |
---|
180 | self.clipboard_called = False |
---|
181 | def done(): |
---|
182 | self.clipboard_called = True |
---|
183 | QtCore.QObject.connect(QtWidgets.qApp.clipboard(), QtCore.SIGNAL("dataChanged()"), done) |
---|
184 | actions[2].trigger() |
---|
185 | QtWidgets.qApp.processEvents() |
---|
186 | # Make sure clipboard got updated. |
---|
187 | self.assertTrue(self.clipboard_called) |
---|
188 | self.plotter.figure.clf() |
---|
189 | |
---|
190 | def testShowNoPlot(self): |
---|
191 | """ Test the plot rendering and generation """ |
---|
192 | |
---|
193 | FigureCanvas.draw_idle = MagicMock() |
---|
194 | FigureCanvas.draw = MagicMock() |
---|
195 | |
---|
196 | # Test early return on no data |
---|
197 | self.plotter.showPlot(data=None, |
---|
198 | qx_data=self.data.qx_data, |
---|
199 | qy_data=self.data.qy_data, |
---|
200 | xmin=self.data.xmin, |
---|
201 | xmax=self.data.xmax, |
---|
202 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
203 | cmap=None, zmin=None, |
---|
204 | zmax=None) |
---|
205 | |
---|
206 | self.assertFalse(FigureCanvas.draw_idle.called) |
---|
207 | self.assertFalse(FigureCanvas.draw.called) |
---|
208 | self.plotter.figure.clf() |
---|
209 | |
---|
210 | def testShow3DPlot(self): |
---|
211 | """ Test the 3Dplot rendering and generation """ |
---|
212 | # Test 3D printout |
---|
213 | FigureCanvas.draw = MagicMock() |
---|
214 | Axes3D.plot_surface = MagicMock() |
---|
215 | self.plotter.figure.colorbar = MagicMock() |
---|
216 | |
---|
217 | self.plotter.dimension = 3 |
---|
218 | self.plotter.data = self.data |
---|
219 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
220 | qx_data=self.data.qx_data, |
---|
221 | qy_data=self.data.qy_data, |
---|
222 | xmin=self.data.xmin, |
---|
223 | xmax=self.data.xmax, |
---|
224 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
225 | cmap=None, zmin=None, |
---|
226 | zmax=None) |
---|
227 | self.assertTrue(Axes3D.plot_surface.called) |
---|
228 | self.assertTrue(FigureCanvas.draw.called) |
---|
229 | self.plotter.figure.clf() |
---|
230 | |
---|
231 | def testShow2DPlot(self): |
---|
232 | """ Test the 2Dplot rendering and generation """ |
---|
233 | # Test 2D printout |
---|
234 | FigureCanvas.draw_idle = MagicMock() |
---|
235 | self.plotter.figure.colorbar = MagicMock() |
---|
236 | |
---|
237 | self.plotter.dimension = 2 |
---|
238 | self.plotter.data = self.data |
---|
239 | self.plotter.showPlot(data=self.plotter.data.data, |
---|
240 | qx_data=self.data.qx_data, |
---|
241 | qy_data=self.data.qy_data, |
---|
242 | xmin=self.data.xmin, |
---|
243 | xmax=self.data.xmax, |
---|
244 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
245 | cmap=None, zmin=None, |
---|
246 | zmax=None) |
---|
247 | self.assertTrue(FigureCanvas.draw_idle.called) |
---|
248 | self.plotter.figure.clf() |
---|
249 | |
---|
250 | |
---|
251 | if __name__ == "__main__": |
---|
252 | unittest.main() |
---|