1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | import numpy |
---|
5 | from mock import MagicMock |
---|
6 | |
---|
7 | # set up import paths |
---|
8 | import sas.qtgui.path_prepare |
---|
9 | |
---|
10 | # Local |
---|
11 | from sas.qtgui.GuiUtils import * |
---|
12 | from sas.qtgui.Perspectives.Fitting.FittingWidget import * |
---|
13 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
14 | |
---|
15 | app = QtGui.QApplication(sys.argv) |
---|
16 | |
---|
17 | |
---|
18 | class FittingLogicTest(unittest.TestCase): |
---|
19 | """Test the fitting logic class""" |
---|
20 | |
---|
21 | def setUp(self): |
---|
22 | """Create the component""" |
---|
23 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
24 | self.logic = FittingLogic(data=data) |
---|
25 | |
---|
26 | def testDefaults(self): |
---|
27 | """Test the component in its default state""" |
---|
28 | self.assertIsInstance(self.logic.data, Data1D) |
---|
29 | self.assertTrue(self.logic.data_is_loaded) |
---|
30 | self.assertEqual(self.logic.data, self.logic._data) |
---|
31 | |
---|
32 | def testComputeDataRange(self): |
---|
33 | """ |
---|
34 | Tests the data range calculator on Data1D/Data2D |
---|
35 | """ |
---|
36 | # Using the default data |
---|
37 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
38 | |
---|
39 | self.assertEqual(qmin, 1) |
---|
40 | self.assertEqual(qmax, 3) |
---|
41 | self.assertEqual(npts, 3) |
---|
42 | |
---|
43 | # data with more points |
---|
44 | data = Data1D(x=[-10, 2, 10, 20],y=[-3, 4, 10, 50]) |
---|
45 | self.logic.data=data |
---|
46 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
47 | |
---|
48 | self.assertEqual(qmin, -10) |
---|
49 | self.assertEqual(qmax, 20) |
---|
50 | self.assertEqual(npts, 4) |
---|
51 | |
---|
52 | def testCreateDefault1dData(self): |
---|
53 | """ |
---|
54 | Tests the default 1D set |
---|
55 | """ |
---|
56 | interval = numpy.linspace(start=1, stop=10, num=10, endpoint=True) |
---|
57 | self.logic.createDefault1dData(interval=interval) |
---|
58 | |
---|
59 | self.assertEqual(self.logic.data.id, ('0 data')) |
---|
60 | self.assertEqual(self.logic.data.group_id, ('0 Model1D')) |
---|
61 | self.assertFalse(self.logic.data.is_data) |
---|
62 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q}')) |
---|
63 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
64 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Intensity}')) |
---|
65 | self.assertEqual(self.logic.data._yunit, ('cm^{-1}')) |
---|
66 | |
---|
67 | def testCreateDefault2dData(self): |
---|
68 | """ |
---|
69 | Tests the default 2D set |
---|
70 | """ |
---|
71 | self.logic.createDefault2dData(qmax=0.35, qstep=50, tab_id=8) |
---|
72 | |
---|
73 | self.assertEqual(self.logic.data.id, ('8 data')) |
---|
74 | self.assertEqual(self.logic.data.group_id, ('8 Model2D')) |
---|
75 | self.assertFalse(self.logic.data.is_data) |
---|
76 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q_{x}}')) |
---|
77 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
78 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Q_{y}}')) |
---|
79 | self.assertEqual(self.logic.data._yunit, ('A^{-1}')) |
---|
80 | |
---|
81 | self.assertEqual(self.logic.data.xmin, -0.35) |
---|
82 | self.assertEqual(self.logic.data.xmax, 0.35) |
---|
83 | |
---|
84 | self.assertEqual(self.logic.data.ymin, -0.35) |
---|
85 | self.assertEqual(self.logic.data.ymax, 0.35) |
---|
86 | |
---|
87 | self.assertEqual(self.logic.data.data.sum(), 2500.0) # 50x50 array of 1's |
---|
88 | self.assertEqual(self.logic.data.err_data.sum(axis=0), 2500.0) |
---|
89 | self.assertAlmostEqual(self.logic.data.qx_data.sum(axis=0), 0.0) |
---|
90 | self.assertAlmostEqual(self.logic.data.qy_data.sum(), 0.0) |
---|
91 | self.assertAlmostEqual(self.logic.data.q_data.sum(), 683.106490, 6) |
---|
92 | self.assertTrue(numpy.all(self.logic.data.mask)) |
---|
93 | self.assertAlmostEqual(self.logic.data.x_bins.sum(), 0.0) |
---|
94 | self.assertAlmostEqual(self.logic.data.y_bins.sum(), 0.0) |
---|
95 | |
---|
96 | def testNew1DPlot(self): |
---|
97 | """ |
---|
98 | Test how the extra shells are presented |
---|
99 | """ |
---|
100 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
101 | data.name = "boop" |
---|
102 | return_data = (data.x,data.y, 7, None, None, |
---|
103 | 0, True, 0.0, 1, data, |
---|
104 | data, False, None) |
---|
105 | |
---|
106 | self.logic.new1DPlot(return_data=return_data) |
---|
107 | |
---|
108 | self.assertIsInstance(self.logic.data, Data1D) |
---|
109 | self.assertFalse(self.logic.data.is_data) |
---|
110 | self.assertEqual(self.logic.data.dy.size, 3) |
---|
111 | self.assertEqual(self.logic.data.title, "boop") |
---|
112 | self.assertEqual(self.logic.data.name, "boop [boop]") |
---|
113 | |
---|
114 | def testNew2DPlot(self): |
---|
115 | """ |
---|
116 | Test the additional rows added by modifying the shells combobox |
---|
117 | """ |
---|
118 | x_0 = 2.0*numpy.ones(25) |
---|
119 | dx_0 = 0.5*numpy.ones(25) |
---|
120 | qx_0 = numpy.arange(25) |
---|
121 | qy_0 = numpy.arange(25) |
---|
122 | mask_0 = numpy.zeros(25) |
---|
123 | dqx_0 = numpy.arange(25)/100 |
---|
124 | dqy_0 = numpy.arange(25)/100 |
---|
125 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
126 | |
---|
127 | data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, |
---|
128 | qy_data=qy_0, q_data=q_0, mask=mask_0, |
---|
129 | dqx_data=dqx_0, dqy_data=dqy_0) |
---|
130 | |
---|
131 | data.name = "boop" |
---|
132 | data.ymin = numpy.amin(q_0) |
---|
133 | data.ymax = numpy.amax(q_0) |
---|
134 | data.xmin = numpy.amin(x_0) |
---|
135 | data.xmax = numpy.amax(x_0) |
---|
136 | self.logic.data = data |
---|
137 | |
---|
138 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
139 | |
---|
140 | return_data = (x_0, data, 7, data, None, |
---|
141 | True, 0.0, 1, 0, qmin, qmax, |
---|
142 | 0.1, False, None) |
---|
143 | |
---|
144 | self.logic.new2DPlot(return_data=return_data) |
---|
145 | |
---|
146 | self.assertIsInstance(self.logic.data, Data2D) |
---|
147 | self.assertFalse(self.logic.data.is_data) |
---|
148 | self.assertEqual(self.logic.data.title, "Analytical model 2D ") |
---|
149 | self.assertEqual(self.logic.data.name, "boop [boop]") |
---|
150 | |
---|
151 | |
---|
152 | |
---|
153 | if __name__ == "__main__": |
---|
154 | unittest.main() |
---|