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