1 | import sys |
---|
2 | import unittest |
---|
3 | from PyQt4 import QtGui |
---|
4 | |
---|
5 | from UnitTesting.TestUtils import WarningTestNotImplemented |
---|
6 | #from sasmodels.sasview_model import load_standard_models |
---|
7 | from sasmodels import generate |
---|
8 | from sasmodels import modelinfo |
---|
9 | |
---|
10 | # Tested module |
---|
11 | from sas.qtgui.Perspectives.Fitting import FittingUtilities |
---|
12 | |
---|
13 | class FittingUtilitiesTest(unittest.TestCase): |
---|
14 | '''Test the Fitting Utilities functions''' |
---|
15 | def setUp(self): |
---|
16 | '''Empty''' |
---|
17 | pass |
---|
18 | |
---|
19 | def tearDown(self): |
---|
20 | '''Empty''' |
---|
21 | pass |
---|
22 | |
---|
23 | def testReplaceShellName(self): |
---|
24 | """ |
---|
25 | Test the utility function for string manipulation |
---|
26 | """ |
---|
27 | param_name = "test [123]" |
---|
28 | value = "replaced" |
---|
29 | result = FittingUtilities.replaceShellName(param_name, value) |
---|
30 | |
---|
31 | self.assertEqual(result, "test replaced") |
---|
32 | |
---|
33 | # Assert! |
---|
34 | param_name = "no brackets" |
---|
35 | with self.assertRaises(AssertionError): |
---|
36 | result = FittingUtilities.replaceShellName(param_name, value) |
---|
37 | |
---|
38 | |
---|
39 | def testGetIterParams(self): |
---|
40 | """ |
---|
41 | Assure the right multishell parameters are returned |
---|
42 | """ |
---|
43 | # Use a single-shell parameter |
---|
44 | model_name = "barbell" |
---|
45 | kernel_module = generate.load_kernel_module(model_name) |
---|
46 | barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
47 | |
---|
48 | params = FittingUtilities.getIterParams(barbell_parameters) |
---|
49 | # returns empty list |
---|
50 | self.assertEqual(params, []) |
---|
51 | |
---|
52 | # Use a multi-shell parameter |
---|
53 | model_name = "core_multi_shell" |
---|
54 | kernel_module = generate.load_kernel_module(model_name) |
---|
55 | multishell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
56 | |
---|
57 | params = FittingUtilities.getIterParams(multishell_parameters) |
---|
58 | # returns a non-empty list |
---|
59 | self.assertNotEqual(params, []) |
---|
60 | self.assertIn('sld', str(params)) |
---|
61 | self.assertIn('thickness', str(params)) |
---|
62 | |
---|
63 | def testGetMultiplicity(self): |
---|
64 | """ |
---|
65 | Assure more multishell parameters are evaluated correctly |
---|
66 | """ |
---|
67 | # Use a single-shell parameter |
---|
68 | model_name = "barbell" |
---|
69 | kernel_module = generate.load_kernel_module(model_name) |
---|
70 | barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
71 | |
---|
72 | param_name, param_length = FittingUtilities.getMultiplicity(barbell_parameters) |
---|
73 | # returns nothing |
---|
74 | self.assertEqual(param_name, "") |
---|
75 | self.assertEqual(param_length, 0) |
---|
76 | |
---|
77 | # Use a multi-shell parameter |
---|
78 | model_name = "core_multi_shell" |
---|
79 | kernel_module = generate.load_kernel_module(model_name) |
---|
80 | multishell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
81 | |
---|
82 | param_name, param_length = FittingUtilities.getMultiplicity(multishell_parameters) |
---|
83 | |
---|
84 | self.assertEqual(param_name, "n") |
---|
85 | self.assertEqual(param_length, 10) |
---|
86 | |
---|
87 | def testAddParametersToModel(self): |
---|
88 | """ |
---|
89 | Checks the QModel update from Sasmodel parameters |
---|
90 | """ |
---|
91 | # Use a single-shell parameter |
---|
92 | model_name = "barbell" |
---|
93 | kernel_module = generate.load_kernel_module(model_name) |
---|
94 | barbell_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
95 | |
---|
96 | model = QtGui.QStandardItemModel() |
---|
97 | |
---|
98 | FittingUtilities.addParametersToModel(barbell_parameters, model) |
---|
99 | |
---|
100 | # Test the resulting model |
---|
101 | self.assertEqual(model.rowCount(), 5) |
---|
102 | self.assertTrue(model.item(0).isCheckable()) |
---|
103 | self.assertEqual(model.item(0).text(), "sld") |
---|
104 | self.assertEqual(model.item(1).text(), "sld_solvent") |
---|
105 | |
---|
106 | # Use a multi-shell parameter to see that the method includes shell params |
---|
107 | model_name = "core_multi_shell" |
---|
108 | kernel_module = generate.load_kernel_module(model_name) |
---|
109 | multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
110 | |
---|
111 | model = QtGui.QStandardItemModel() |
---|
112 | |
---|
113 | FittingUtilities.addParametersToModel(multi_parameters, model) |
---|
114 | |
---|
115 | # Test the resulting model |
---|
116 | self.assertEqual(model.rowCount(), 3) |
---|
117 | self.assertTrue(model.item(0).isCheckable()) |
---|
118 | self.assertEqual(model.item(0).text(), "sld_core") |
---|
119 | self.assertEqual(model.item(1).text(), "radius") |
---|
120 | self.assertEqual(model.item(2).text(), "sld_solvent") |
---|
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 | model = QtGui.QStandardItemModel() |
---|
132 | |
---|
133 | FittingUtilities.addParametersToModel(multi_parameters, model) |
---|
134 | |
---|
135 | # Test the resulting model |
---|
136 | self.assertEqual(model.rowCount(), 3) |
---|
137 | self.assertTrue(model.item(0).isCheckable()) |
---|
138 | self.assertEqual(model.item(0).text(), "sld_core") |
---|
139 | self.assertEqual(model.item(1).text(), "radius") |
---|
140 | |
---|
141 | def testAddCheckedListToModel(self): |
---|
142 | """ |
---|
143 | Test for inserting a checkboxed item into a QModel |
---|
144 | """ |
---|
145 | model = QtGui.QStandardItemModel() |
---|
146 | params = ["row1", "row2", "row3"] |
---|
147 | |
---|
148 | FittingUtilities.addCheckedListToModel(model, params) |
---|
149 | |
---|
150 | # Check the model |
---|
151 | self.assertEqual(model.rowCount(), 1) |
---|
152 | self.assertTrue(model.item(0).isCheckable()) |
---|
153 | self.assertEqual(model.item(0, 0).text(), params[0]) |
---|
154 | self.assertEqual(model.item(0, 1).text(), params[1]) |
---|
155 | self.assertEqual(model.item(0, 2).text(), params[2]) |
---|
156 | |
---|
157 | def testAddShellsToModel(self): |
---|
158 | """ |
---|
159 | Test for inserting a list of QItems into a model |
---|
160 | """ |
---|
161 | # Use a multi-shell parameter to see that the method doesn't include shells |
---|
162 | model_name = "core_multi_shell" |
---|
163 | kernel_module = generate.load_kernel_module(model_name) |
---|
164 | multi_parameters = modelinfo.make_parameter_table(getattr(kernel_module, 'parameters', [])) |
---|
165 | |
---|
166 | model = QtGui.QStandardItemModel() |
---|
167 | |
---|
168 | index = 2 |
---|
169 | FittingUtilities.addShellsToModel(multi_parameters, model, index) |
---|
170 | # There should be index*len(multi_parameters) new rows |
---|
171 | self.assertEqual(model.rowCount(), 4) |
---|
172 | |
---|
173 | model = QtGui.QStandardItemModel() |
---|
174 | index = 5 |
---|
175 | FittingUtilities.addShellsToModel(multi_parameters, model, index) |
---|
176 | self.assertEqual(model.rowCount(), 10) |
---|
177 | |
---|
178 | self.assertEqual(model.item(1).child(0).text(), "Polydispersity") |
---|
179 | self.assertEqual(model.item(1).child(0).child(0).text(), "Distribution") |
---|
180 | self.assertEqual(model.item(1).child(0).child(0,1).text(), "40.0") |
---|
181 | |
---|
182 | if __name__ == "__main__": |
---|
183 | unittest.main() |
---|