source: sasview/src/sas/qtgui/Perspectives/Fitting/UnitTesting/FittingUtilitiesTest.py @ d7ff531

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 d7ff531 was 6fd4e36, checked in by Piotr Rozyczko <rozyczko@…>, 8 years ago

Chi2 display + minor refactoring

  • Property mode set to 100755
File size: 9.0 KB
Line 
1import sys
2import unittest
3from PyQt4 import QtGui
4
5from sas.sasgui.guiframe.dataFitting import Data1D
6from sas.sasgui.guiframe.dataFitting import Data2D
7
8from UnitTesting.TestUtils import WarningTestNotImplemented
9
10from sasmodels import generate
11from sasmodels import modelinfo
12
13# Tested module
14from sas.qtgui.Perspectives.Fitting import FittingUtilities
15
16class FittingUtilitiesTest(unittest.TestCase):
17    '''Test the Fitting Utilities functions'''
18    def setUp(self):
19        '''Empty'''
20        pass
21
22    def tearDown(self):
23        '''Empty'''
24        pass
25
26    def testReplaceShellName(self):
27        """
28        Test the utility function for string manipulation
29        """
30        param_name = "test [123]"
31        value = "replaced"
32        result = FittingUtilities.replaceShellName(param_name, value)
33       
34        self.assertEqual(result, "test replaced")
35
36        # Assert!
37        param_name = "no brackets"
38        with self.assertRaises(AssertionError):
39            result = FittingUtilities.replaceShellName(param_name, value)
40
41       
42    def testGetIterParams(self):
43        """
44        Assure the right multishell parameters are returned
45        """
46        # Use a single-shell parameter
47        model_name = "barbell"
48        kernel_module = generate.load_kernel_module(model_name)
49        barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
50
51        params = FittingUtilities.getIterParams(barbell_parameters)
52        # returns empty list
53        self.assertEqual(params, [])
54
55        # Use a multi-shell parameter
56        model_name = "core_multi_shell"
57        kernel_module = generate.load_kernel_module(model_name)
58        multishell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
59
60        params = FittingUtilities.getIterParams(multishell_parameters)
61        # returns a non-empty list
62        self.assertNotEqual(params, [])
63        self.assertIn('sld', str(params))
64        self.assertIn('thickness', str(params))
65
66    def testGetMultiplicity(self):
67        """
68        Assure more multishell parameters are evaluated correctly
69        """
70        # Use a single-shell parameter
71        model_name = "barbell"
72        kernel_module = generate.load_kernel_module(model_name)
73        barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
74
75        param_name, param_length = FittingUtilities.getMultiplicity(barbell_parameters)
76        # returns nothing
77        self.assertEqual(param_name, "")
78        self.assertEqual(param_length, 0)
79
80        # Use a multi-shell parameter
81        model_name = "core_multi_shell"
82        kernel_module = generate.load_kernel_module(model_name)
83        multishell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
84
85        param_name, param_length = FittingUtilities.getMultiplicity(multishell_parameters)
86
87        self.assertEqual(param_name, "n")
88        self.assertEqual(param_length, 10)
89
90    def testAddParametersToModel(self):
91        """
92        Checks the QModel update from Sasmodel parameters
93        """
94        # Use a single-shell parameter
95        model_name = "barbell"
96        kernel_module = generate.load_kernel_module(model_name)
97        barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
98
99        model = QtGui.QStandardItemModel()
100
101        FittingUtilities.addParametersToModel(barbell_parameters, model)
102
103        # Test the resulting model
104        self.assertEqual(model.rowCount(), 5)
105        self.assertTrue(model.item(0).isCheckable())
106        self.assertEqual(model.item(0).text(), "sld")
107        self.assertEqual(model.item(1).text(), "sld_solvent")
108
109        # Use a multi-shell parameter to see that the method includes shell params
110        model_name = "core_multi_shell"
111        kernel_module = generate.load_kernel_module(model_name)
112        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
113
114        model = QtGui.QStandardItemModel()
115
116        FittingUtilities.addParametersToModel(multi_parameters, model)
117
118        # Test the resulting model
119        self.assertEqual(model.rowCount(), 3)
120        self.assertTrue(model.item(0).isCheckable())
121        self.assertEqual(model.item(0).text(), "sld_core")
122        self.assertEqual(model.item(1).text(), "radius")
123        self.assertEqual(model.item(2).text(), "sld_solvent")
124
125    def testAddSimpleParametersToModel(self):
126        """
127        Checks the QModel update from Sasmodel parameters - no polydisp
128        """
129        # Use a multi-shell parameter to see that the method doesn't include shells
130        model_name = "core_multi_shell"
131        kernel_module = generate.load_kernel_module(model_name)
132        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
133
134        model = QtGui.QStandardItemModel()
135
136        FittingUtilities.addParametersToModel(multi_parameters, model)
137
138        # Test the resulting model
139        self.assertEqual(model.rowCount(), 3)
140        self.assertTrue(model.item(0).isCheckable())
141        self.assertEqual(model.item(0).text(), "sld_core")
142        self.assertEqual(model.item(1).text(), "radius")
143
144    def testAddCheckedListToModel(self):
145        """
146        Test for inserting a checkboxed item into a QModel
147        """
148        model = QtGui.QStandardItemModel()
149        params = ["row1", "row2", "row3"]
150
151        FittingUtilities.addCheckedListToModel(model, params)
152
153        # Check the model
154        self.assertEqual(model.rowCount(), 1)
155        self.assertTrue(model.item(0).isCheckable())
156        self.assertEqual(model.item(0, 0).text(), params[0])
157        self.assertEqual(model.item(0, 1).text(), params[1])
158        self.assertEqual(model.item(0, 2).text(), params[2])
159
160    def testAddShellsToModel(self):
161        """
162        Test for inserting a list of QItems into a model
163        """
164        # Use a multi-shell parameter to see that the method doesn't include shells
165        model_name = "core_multi_shell"
166        kernel_module = generate.load_kernel_module(model_name)
167        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
168
169        model = QtGui.QStandardItemModel()
170
171        index = 2
172        FittingUtilities.addShellsToModel(multi_parameters, model, index)
173        # There should be index*len(multi_parameters) new rows
174        self.assertEqual(model.rowCount(), 4)
175
176        model = QtGui.QStandardItemModel()
177        index = 5
178        FittingUtilities.addShellsToModel(multi_parameters, model, index)
179        self.assertEqual(model.rowCount(), 10)
180       
181        self.assertEqual(model.item(1).child(0).text(), "Polydispersity")
182        self.assertEqual(model.item(1).child(0).child(0).text(), "Distribution")
183        self.assertEqual(model.item(1).child(0).child(0,1).text(), "40.0")
184
185    def testCalculate1DChi2(self):
186        """
187        Test the chi2 calculator for Data1D
188        """
189        reference_data = Data1D(x=[0.1, 0.2], y=[0.0, 0.0])
190
191        # 1. identical data
192        current_data = Data1D(x=[0.1, 0.2], y=[0.0, 0.0])
193
194        chi = FittingUtilities.calculateChi2(reference_data, current_data)
195
196        # Should be zero
197        self.assertAlmostEqual(chi, 0.0, 8)
198
199        # 2. far data
200        current_data = Data1D(x=[0.1, 0.2], y=[200.0, 150.0])
201
202        chi = FittingUtilities.calculateChi2(reference_data, current_data)
203
204        # Should not be zero
205        self.assertAlmostEqual(chi, 31250.0, 8)
206
207        # 3. Wrong data
208        current_data = Data1D(x=[0.1, 0.2], y=[200.0, 150.0, 200.0])
209        chi = FittingUtilities.calculateChi2(reference_data, current_data)
210        # Should be None
211        self.assertIsNone(chi)
212
213    def testCalculate2DChi2(self):
214        """
215        Test the chi2 calculator for Data2D
216        """
217        reference_data = Data2D(image=[1.0, 2.0, 3.0],
218                      err_image=[0.01, 0.02, 0.03],
219                      qx_data=[0.1, 0.2, 0.3],
220                      qy_data=[0.1, 0.2, 0.3])
221
222        # 1. identical data
223        current_data = Data2D(image=[1.0, 2.0, 3.0],
224                      err_image=[0.01, 0.02, 0.03],
225                      qx_data=[0.1, 0.2, 0.3],
226                      qy_data=[0.1, 0.2, 0.3])
227
228        chi = FittingUtilities.calculateChi2(reference_data, current_data)
229
230        # Should be zero
231        self.assertAlmostEqual(chi, 0.0, 8)
232
233        # 2. far data
234        current_data = Data2D(image=[100.0, 200.0, 300.0],
235                      err_image=[1.01, 2.02, 3.03],
236                      qx_data=[0.1, 0.2, 0.3],
237                      qy_data=[100.0, 200., 300.])
238
239        chi = FittingUtilities.calculateChi2(reference_data, current_data)
240
241        # Should not be zero
242        self.assertAlmostEqual(chi, 9607.88, 2)
243
244        # 3. Wrong data
245        current_data = Data2D(image=[1.0, 2.0, 3.0],
246                      err_image=[0.01, 0.02],
247                      qx_data=[0.1, 0.2],
248                      qy_data=[0.1, 0.2, 0.3])
249        # Should throw
250        with self.assertRaises(ValueError):
251            chi = FittingUtilities.calculateChi2(reference_data, current_data)
252
253if __name__ == "__main__":
254    unittest.main()
Note: See TracBrowser for help on using the repository browser.