1 | import sys |
---|
2 | import unittest |
---|
3 | import numpy |
---|
4 | |
---|
5 | from PyQt4 import QtGui |
---|
6 | from mock import MagicMock |
---|
7 | |
---|
8 | # set up import paths |
---|
9 | import path_prepare |
---|
10 | |
---|
11 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
12 | import sas.qtgui.Plotting.Plotter as Plotter |
---|
13 | |
---|
14 | # Local |
---|
15 | from sas.qtgui.Plotting.LinearFit import LinearFit |
---|
16 | |
---|
17 | app = QtGui.QApplication(sys.argv) |
---|
18 | |
---|
19 | class LinearFitTest(unittest.TestCase): |
---|
20 | '''Test the LinearFit''' |
---|
21 | def setUp(self): |
---|
22 | '''Create the LinearFit''' |
---|
23 | self.data = Data1D(x=[1.0, 2.0, 3.0], |
---|
24 | y=[10.0, 11.0, 12.0], |
---|
25 | dx=[0.1, 0.2, 0.3], |
---|
26 | dy=[0.1, 0.2, 0.3]) |
---|
27 | plotter = Plotter.Plotter(None, quickplot=True) |
---|
28 | self.widget = LinearFit(parent=plotter, data=self.data, xlabel="log10(x^2)", ylabel="log10(y)") |
---|
29 | |
---|
30 | def tearDown(self): |
---|
31 | '''Destroy the GUI''' |
---|
32 | self.widget.close() |
---|
33 | self.widget = None |
---|
34 | |
---|
35 | def testDefaults(self): |
---|
36 | '''Test the GUI in its default state''' |
---|
37 | self.assertIsInstance(self.widget, QtGui.QDialog) |
---|
38 | self.assertEqual(self.widget.windowTitle(), "Linear Fit") |
---|
39 | self.assertEqual(self.widget.txtA.text(), "1") |
---|
40 | self.assertEqual(self.widget.txtB.text(), "1") |
---|
41 | self.assertEqual(self.widget.txtAerr.text(), "0") |
---|
42 | self.assertEqual(self.widget.txtBerr.text(), "0") |
---|
43 | |
---|
44 | self.assertEqual(self.widget.lblRange.text(), "Fit range of log10(x^2)") |
---|
45 | |
---|
46 | def testFit(self): |
---|
47 | '''Test the fitting wrapper ''' |
---|
48 | # Catch the update signal |
---|
49 | self.widget.parent.emit = MagicMock() |
---|
50 | |
---|
51 | # Set some initial values |
---|
52 | self.widget.txtRangeMin.setText("1.0") |
---|
53 | self.widget.txtRangeMax.setText("3.0") |
---|
54 | self.widget.txtFitRangeMin.setText("1.0") |
---|
55 | self.widget.txtFitRangeMax.setText("3.0") |
---|
56 | # Run the fitting |
---|
57 | self.widget.fit(None) |
---|
58 | return_values = self.widget.parent.emit.call_args[0][1] |
---|
59 | # Compare |
---|
60 | self.assertItemsEqual(return_values[0], [1.0, 3.0]) |
---|
61 | self.assertAlmostEqual(return_values[1][0], 10.004054329, 6) |
---|
62 | self.assertAlmostEqual(return_values[1][1], 12.030439848, 6) |
---|
63 | |
---|
64 | # Set the log scale |
---|
65 | self.widget.x_is_log = True |
---|
66 | self.widget.fit(None) |
---|
67 | return_values = self.widget.parent.emit.call_args[0][1] |
---|
68 | # Compare |
---|
69 | self.assertItemsEqual(return_values[0], [1.0, 3.0]) |
---|
70 | self.assertAlmostEqual(return_values[1][0], 9.987732937, 6) |
---|
71 | self.assertAlmostEqual(return_values[1][1], 11.84365082, 6) |
---|
72 | |
---|
73 | def testOrigData(self): |
---|
74 | ''' Assure the un-logged data is returned''' |
---|
75 | # log(x), log(y) |
---|
76 | self.widget.xminFit, self.widget.xmaxFit = self.widget.range() |
---|
77 | orig_x = [ 1., 2., 3.] |
---|
78 | orig_y = [1.0, 1.0413926851582251, 1.0791812460476249] |
---|
79 | orig_dy = [0.01, 0.018181818181818184, 0.024999999999999998] |
---|
80 | x, y, dy = self.widget.origData() |
---|
81 | |
---|
82 | self.assertItemsEqual(x, orig_x) |
---|
83 | self.assertEqual(y[0], orig_y[0]) |
---|
84 | self.assertAlmostEqual(y[1], orig_y[1], 8) |
---|
85 | self.assertAlmostEqual(y[2], orig_y[2], 8) |
---|
86 | self.assertEqual(dy[0], orig_dy[0]) |
---|
87 | self.assertAlmostEqual(dy[1], orig_dy[1], 8) |
---|
88 | self.assertAlmostEqual(dy[2], orig_dy[2], 8) |
---|
89 | |
---|
90 | # x, y |
---|
91 | self.widget.x_is_log = False |
---|
92 | self.widget.y_is_log = False |
---|
93 | self.widget.xminFit, self.widget.xmaxFit = self.widget.range() |
---|
94 | orig_x = [ 1., 2., 3.] |
---|
95 | orig_y = [10., 11., 12.] |
---|
96 | orig_dy = [0.1, 0.2, 0.3] |
---|
97 | x, y, dy = self.widget.origData() |
---|
98 | |
---|
99 | self.assertItemsEqual(x, orig_x) |
---|
100 | self.assertItemsEqual(y, orig_y) |
---|
101 | self.assertItemsEqual(dy, orig_dy) |
---|
102 | |
---|
103 | # x, log(y) |
---|
104 | self.widget.x_is_log = False |
---|
105 | self.widget.y_is_log = True |
---|
106 | self.widget.xminFit, self.widget.xmaxFit = self.widget.range() |
---|
107 | orig_x = [ 1., 2., 3.] |
---|
108 | orig_y = [1.0, 1.0413926851582251, 1.0791812460476249] |
---|
109 | orig_dy = [0.01, 0.018181818181818184, 0.024999999999999998] |
---|
110 | x, y, dy = self.widget.origData() |
---|
111 | |
---|
112 | self.assertItemsEqual(x, orig_x) |
---|
113 | self.assertEqual(y[0], orig_y[0]) |
---|
114 | self.assertAlmostEqual(y[1], orig_y[1], 8) |
---|
115 | self.assertAlmostEqual(y[2], orig_y[2], 8) |
---|
116 | self.assertEqual(dy[0], orig_dy[0]) |
---|
117 | self.assertAlmostEqual(dy[1], orig_dy[1], 8) |
---|
118 | self.assertAlmostEqual(dy[2], orig_dy[2], 8) |
---|
119 | |
---|
120 | def testCheckFitValues(self): |
---|
121 | '''Assure fit values are correct''' |
---|
122 | # Good values |
---|
123 | self.assertTrue(self.widget.checkFitValues(self.widget.txtFitRangeMin)) |
---|
124 | # Colors platform dependent |
---|
125 | #self.assertEqual(self.widget.txtFitRangeMin.palette().color(10).name(), "#f0f0f0") |
---|
126 | # Bad values |
---|
127 | self.widget.x_is_log = True |
---|
128 | self.widget.txtFitRangeMin.setText("-1.0") |
---|
129 | self.assertFalse(self.widget.checkFitValues(self.widget.txtFitRangeMin)) |
---|
130 | |
---|
131 | |
---|
132 | def testFloatInvTransform(self): |
---|
133 | '''Test the helper method for providing conversion function''' |
---|
134 | self.widget.xLabel="x" |
---|
135 | self.assertEqual(self.widget.floatInvTransform(5.0), 5.0) |
---|
136 | self.widget.xLabel="x^(2)" |
---|
137 | self.assertEqual(self.widget.floatInvTransform(25.0), 5.0) |
---|
138 | self.widget.xLabel="x^(4)" |
---|
139 | self.assertEqual(self.widget.floatInvTransform(81.0), 3.0) |
---|
140 | self.widget.xLabel="log10(x)" |
---|
141 | self.assertEqual(self.widget.floatInvTransform(2.0), 100.0) |
---|
142 | self.widget.xLabel="ln(x)" |
---|
143 | self.assertEqual(self.widget.floatInvTransform(1.0), numpy.exp(1)) |
---|
144 | self.widget.xLabel="log10(x^(4))" |
---|
145 | self.assertEqual(self.widget.floatInvTransform(4.0), 10.0) |
---|
146 | |
---|
147 | if __name__ == "__main__": |
---|
148 | unittest.main() |
---|