source: sasview/src/sas/qtgui/UnitTesting/LinearFitTest.py @ db5cd8d

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalc
Last change on this file since db5cd8d was b46f285, checked in by Piotr Rozyczko <rozyczko@…>, 7 years ago

Unit tests for linear fit

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