1 | import sys |
---|
2 | import unittest |
---|
3 | |
---|
4 | import numpy |
---|
5 | from unittest.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 | |
---|
16 | class FittingLogicTest(unittest.TestCase): |
---|
17 | """Test the fitting logic class""" |
---|
18 | |
---|
19 | def setUp(self): |
---|
20 | """Create the component""" |
---|
21 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
22 | self.logic = FittingLogic(data=data) |
---|
23 | |
---|
24 | def testDefaults(self): |
---|
25 | """Test the component in its default state""" |
---|
26 | self.assertIsInstance(self.logic.data, Data1D) |
---|
27 | self.assertTrue(self.logic.data_is_loaded) |
---|
28 | self.assertEqual(self.logic.data, self.logic._data) |
---|
29 | |
---|
30 | def testComputeDataRange(self): |
---|
31 | """ |
---|
32 | Tests the data range calculator on Data1D/Data2D |
---|
33 | """ |
---|
34 | # Using the default data |
---|
35 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
36 | |
---|
37 | self.assertEqual(qmin, 1) |
---|
38 | self.assertEqual(qmax, 3) |
---|
39 | self.assertEqual(npts, 3) |
---|
40 | |
---|
41 | # data with more points |
---|
42 | data = Data1D(x=[-10, 2, 10, 20],y=[-3, 4, 10, 50]) |
---|
43 | self.logic.data=data |
---|
44 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
45 | |
---|
46 | self.assertEqual(qmin, -10) |
---|
47 | self.assertEqual(qmax, 20) |
---|
48 | self.assertEqual(npts, 4) |
---|
49 | |
---|
50 | def testCreateDefault1dData(self): |
---|
51 | """ |
---|
52 | Tests the default 1D set |
---|
53 | """ |
---|
54 | interval = numpy.linspace(start=1, stop=10, num=10, endpoint=True) |
---|
55 | self.logic.createDefault1dData(interval=interval) |
---|
56 | |
---|
57 | self.assertEqual(self.logic.data.id, ('0 data')) |
---|
58 | self.assertEqual(self.logic.data.group_id, ('0 Model1D')) |
---|
59 | self.assertFalse(self.logic.data.is_data) |
---|
60 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q}')) |
---|
61 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
62 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Intensity}')) |
---|
63 | self.assertEqual(self.logic.data._yunit, ('cm^{-1}')) |
---|
64 | |
---|
65 | def testCreateDefault2dData(self): |
---|
66 | """ |
---|
67 | Tests the default 2D set |
---|
68 | """ |
---|
69 | self.logic.createDefault2dData(qmax=0.35, qstep=50, tab_id=8) |
---|
70 | |
---|
71 | self.assertEqual(self.logic.data.id, ('8 data')) |
---|
72 | self.assertEqual(self.logic.data.group_id, ('8 Model2D')) |
---|
73 | self.assertFalse(self.logic.data.is_data) |
---|
74 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q_{x}}')) |
---|
75 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
76 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Q_{y}}')) |
---|
77 | self.assertEqual(self.logic.data._yunit, ('A^{-1}')) |
---|
78 | |
---|
79 | self.assertEqual(self.logic.data.xmin, -0.35) |
---|
80 | self.assertEqual(self.logic.data.xmax, 0.35) |
---|
81 | |
---|
82 | self.assertEqual(self.logic.data.ymin, -0.35) |
---|
83 | self.assertEqual(self.logic.data.ymax, 0.35) |
---|
84 | |
---|
85 | self.assertEqual(self.logic.data.data.sum(), 2500.0) # 50x50 array of 1's |
---|
86 | self.assertEqual(self.logic.data.err_data.sum(axis=0), 2500.0) |
---|
87 | self.assertAlmostEqual(self.logic.data.qx_data.sum(axis=0), 0.0) |
---|
88 | self.assertAlmostEqual(self.logic.data.qy_data.sum(), 0.0) |
---|
89 | self.assertAlmostEqual(self.logic.data.q_data.sum(), 683.106490, 6) |
---|
90 | self.assertTrue(numpy.all(self.logic.data.mask)) |
---|
91 | self.assertAlmostEqual(self.logic.data.x_bins.sum(), 0.0) |
---|
92 | self.assertAlmostEqual(self.logic.data.y_bins.sum(), 0.0) |
---|
93 | |
---|
94 | def testNew1DPlot(self): |
---|
95 | """ |
---|
96 | Test how the extra shells are presented |
---|
97 | """ |
---|
98 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
99 | data.name = "boop" |
---|
100 | data.id = "poop" |
---|
101 | return_data = (data.x,data.y, 7, None, None, |
---|
102 | 0, True, 0.0, 1, data, |
---|
103 | data, False, None, |
---|
104 | None, None, None, |
---|
105 | None, 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 [poop]") |
---|
113 | self.assertEqual(new_plot.name, "boop [poop]") |
---|
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() |
---|