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