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