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

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

Removed qtgui dependency on sasgui and wx SASVIEW-590

  • Property mode set to 100755
File size: 9.0 KB
Line 
1import sys
2import unittest
3from PyQt4 import QtGui
4
5from sas.qtgui.Plotting.PlotterData import Data1D
6from sas.qtgui.Plotting.PlotterData 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        params = FittingUtilities.addParametersToModel(barbell_parameters, True)
100
101        # Test the resulting model
102        self.assertEqual(len(params), 7)
103        self.assertEqual(len(params[0]), 5)
104        self.assertTrue(params[0][0].isCheckable())
105        self.assertEqual(params[0][0].text(), "sld")
106        self.assertEqual(params[1][0].text(), "sld_solvent")
107
108        # Use a multi-shell parameter to see that the method includes shell params
109        model_name = "core_multi_shell"
110        kernel_module = generate.load_kernel_module(model_name)
111        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
112
113        params = FittingUtilities.addParametersToModel(multi_parameters, False)
114
115        # Test the resulting model
116        self.assertEqual(len(params), 3)
117        self.assertEqual(len(params[0]), 5)
118        self.assertTrue(params[0][0].isCheckable())
119        self.assertEqual(params[0][0].text(), "sld_core")
120        self.assertEqual(params[1][0].text(), "radius")
121
122    def testAddSimpleParametersToModel(self):
123        """
124        Checks the QModel update from Sasmodel parameters - no polydisp
125        """
126        # Use a multi-shell parameter to see that the method doesn't include shells
127        model_name = "core_multi_shell"
128        kernel_module = generate.load_kernel_module(model_name)
129        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
130
131        params = FittingUtilities.addParametersToModel(multi_parameters, True)
132
133        # Test the resulting model
134        self.assertEqual(len(params), 3)
135        self.assertEqual(len(params[0]), 5)
136        self.assertTrue(params[0][0].isCheckable())
137        self.assertEqual(params[0][0].text(), "sld_core")
138        self.assertEqual(params[1][0].text(), "radius")
139
140    def testAddCheckedListToModel(self):
141        """
142        Test for inserting a checkboxed item into a QModel
143        """
144        model = QtGui.QStandardItemModel()
145        params = ["row1", "row2", "row3"]
146
147        FittingUtilities.addCheckedListToModel(model, params)
148
149        # Check the model
150        self.assertEqual(model.rowCount(), 1)
151        self.assertTrue(model.item(0).isCheckable())
152        self.assertEqual(model.item(0, 0).text(), params[0])
153        self.assertEqual(model.item(0, 1).text(), params[1])
154        self.assertEqual(model.item(0, 2).text(), params[2])
155
156    def testAddShellsToModel(self):
157        """
158        Test for inserting a list of QItems into a model
159        """
160        # Use a multi-shell parameter to see that the method doesn't include shells
161        model_name = "core_multi_shell"
162        kernel_module = generate.load_kernel_module(model_name)
163        multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', []))
164
165        model = QtGui.QStandardItemModel()
166
167        index = 2
168        FittingUtilities.addShellsToModel(multi_parameters, model, index)
169        # There should be index*len(multi_parameters) new rows
170        self.assertEqual(model.rowCount(), 4)
171
172        model = QtGui.QStandardItemModel()
173        index = 5
174        FittingUtilities.addShellsToModel(multi_parameters, model, index)
175        self.assertEqual(model.rowCount(), 10)
176       
177        self.assertEqual(model.item(1).child(0).text(), "Polydispersity")
178        self.assertEqual(model.item(1).child(0).child(0).text(), "Distribution")
179        self.assertEqual(model.item(1).child(0).child(0,1).text(), "40.0")
180
181    def testCalculate1DChi2(self):
182        """
183        Test the chi2 calculator for Data1D
184        """
185        reference_data = Data1D(x=[0.1, 0.2], y=[0.0, 0.0])
186
187        # 1. identical data
188        current_data = Data1D(x=[0.1, 0.2], y=[0.0, 0.0])
189
190        chi = FittingUtilities.calculateChi2(reference_data, current_data)
191
192        # Should be zero
193        self.assertAlmostEqual(chi, 0.0, 8)
194
195        # 2. far data
196        current_data = Data1D(x=[0.1, 0.2], y=[200.0, 150.0])
197
198        chi = FittingUtilities.calculateChi2(reference_data, current_data)
199
200        # Should not be zero
201        self.assertAlmostEqual(chi, 31250.0, 8)
202
203        # 3. Wrong data
204        current_data = Data1D(x=[0.1, 0.2], y=[200.0, 150.0, 200.0])
205        chi = FittingUtilities.calculateChi2(reference_data, current_data)
206        # Should be None
207        self.assertIsNone(chi)
208
209    def testCalculate2DChi2(self):
210        """
211        Test the chi2 calculator for Data2D
212        """
213        reference_data = Data2D(image=[1.0, 2.0, 3.0],
214                      err_image=[0.01, 0.02, 0.03],
215                      qx_data=[0.1, 0.2, 0.3],
216                      qy_data=[0.1, 0.2, 0.3])
217
218        # 1. identical data
219        current_data = Data2D(image=[1.0, 2.0, 3.0],
220                      err_image=[0.01, 0.02, 0.03],
221                      qx_data=[0.1, 0.2, 0.3],
222                      qy_data=[0.1, 0.2, 0.3])
223
224        chi = FittingUtilities.calculateChi2(reference_data, current_data)
225
226        # Should be zero
227        self.assertAlmostEqual(chi, 0.0, 8)
228
229        # 2. far data
230        current_data = Data2D(image=[100.0, 200.0, 300.0],
231                      err_image=[1.01, 2.02, 3.03],
232                      qx_data=[0.1, 0.2, 0.3],
233                      qy_data=[100.0, 200., 300.])
234
235        chi = FittingUtilities.calculateChi2(reference_data, current_data)
236
237        # Should not be zero
238        self.assertAlmostEqual(chi, 9607.88, 2)
239
240        # 3. Wrong data
241        current_data = Data2D(image=[1.0, 2.0, 3.0],
242                      err_image=[0.01, 0.02],
243                      qx_data=[0.1, 0.2],
244                      qy_data=[0.1, 0.2, 0.3])
245        # Should throw
246        with self.assertRaises(ValueError):
247            chi = FittingUtilities.calculateChi2(reference_data, current_data)
248
249if __name__ == "__main__":
250    unittest.main()
Note: See TracBrowser for help on using the repository browser.