Changeset 5a8cdbb in sasview for test/sasinvariant
- Timestamp:
- Aug 1, 2017 12:02:35 PM (7 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 511ccb2d
- Parents:
- 248ff73 (diff), bc04647 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Location:
- test/sasinvariant
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
test/sasinvariant/test/utest_use_cases.py
r959eb01 rb09095a 6 6 import unittest 7 7 from sas.sascalc.dataloader.loader import Loader 8 9 8 from sas.sascalc.invariant import invariant 9 10 10 11 11 class Data1D: 12 12 pass 13 13 14 14 15 class TestLineFit(unittest.TestCase): 15 16 """ … … 17 18 """ 18 19 def setUp(self): 19 self.data = Loader().load("linefittest.txt") 20 20 self.data_list = Loader().load("linefittest.txt") 21 self.data = self.data_list[0] 22 21 23 def test_fit_line_data(self): 22 24 """ 23 25 Fit_Test_1: test linear fit, ax +b, without fixed 24 26 """ 25 27 26 28 # Create invariant object. Background and scale left as defaults. 27 29 fit = invariant.Extrapolator(data=self.data) 28 30 29 # #Without holding31 # Without holding 30 32 p, dp = fit.fit(power=None) 31 33 … … 33 35 self.assertAlmostEquals(p[0], 2.3983,3) 34 36 self.assertAlmostEquals(p[1], 0.87833,3) 35 36 37 37 38 def test_fit_line_data_fixed(self): … … 39 40 Fit_Test_2: test linear fit, ax +b, with 'a' fixed 40 41 """ 41 42 42 43 # Create invariant object. Background and scale left as defaults. 43 44 fit = invariant.Extrapolator(data=self.data) 44 45 # With holding a = -power =445 46 # With holding a = -power =4 46 47 p, dp = fit.fit(power=-4) 47 48 … … 49 50 self.assertAlmostEquals(p[0], 4) 50 51 self.assertAlmostEquals(p[1], -4.0676,3) 51 52 53 52 54 class TestLineFitNoweight(unittest.TestCase): 53 55 """ … … 55 57 """ 56 58 def setUp(self): 57 self.data = Loader().load("linefittest_no_weight.txt") 58 59 self.data_list = Loader().load("linefittest_no_weight.txt") 60 self.data = self.data_list[0] 61 59 62 def skip_test_fit_line_data_no_weight(self): 60 63 """ 61 64 Fit_Test_1: test linear fit, ax +b, without fixed 62 65 """ 63 66 64 67 # Create invariant object. Background and scale left as defaults. 65 68 fit = invariant.Extrapolator(data=self.data) 66 67 # #Without holding69 70 # Without holding 68 71 p, dp = fit.fit(power=None) 69 72 … … 71 74 self.assertAlmostEquals(p[0], 2.4727,3) 72 75 self.assertAlmostEquals(p[1], 0.6,3) 73 74 76 75 77 def test_fit_line_data_fixed_no_weight(self): … … 77 79 Fit_Test_2: test linear fit, ax +b, with 'a' fixed 78 80 """ 79 81 80 82 # Create invariant object. Background and scale left as defaults. 81 83 fit = invariant.Extrapolator(data=self.data) 82 84 83 85 #With holding a = -power =4 84 86 p, dp = fit.fit(power=-4) … … 87 89 self.assertAlmostEquals(p[0], 4) 88 90 self.assertAlmostEquals(p[1], -7.8,3) 89 91 92 90 93 class TestInvPolySphere(unittest.TestCase): 91 94 """ … … 93 96 """ 94 97 def setUp(self): 95 self.data = Loader().load("PolySpheres.txt") 96 98 self.data_list = Loader().load("PolySpheres.txt") 99 self.data = self.data_list[0] 100 97 101 def test_wrong_data(self): 98 102 """ test receiving Data1D not of type loader""" 99 100 101 103 self.assertRaises(ValueError,invariant.InvariantCalculator, Data1D()) 102 104 103 105 def test_use_case_1(self): 104 106 """ … … 107 109 # Create invariant object. Background and scale left as defaults. 108 110 inv = invariant.InvariantCalculator(data=self.data) 109 111 110 112 # We have to be able to tell the InvariantCalculator whether we want the 111 113 # extrapolation or not. By default, when the user doesn't specify, we 112 # should compute Q* without extrapolation. That's what should be done in __init__. 113 114 # should compute Q* without extrapolation. That's what should be done 115 # in __init__. 116 114 117 # We call get_qstar() with no argument, which signifies that we do NOT 115 118 # want extrapolation. 116 119 qstar = inv.get_qstar() 117 120 118 121 # The volume fraction and surface use Q*. That means that the following 119 122 # methods should check that Q* has been computed. If not, it should … … 121 124 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 122 125 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 123 126 124 127 # Test results 125 128 self.assertAlmostEquals(qstar, 7.48959e-5,2) 126 129 self.assertAlmostEquals(v, 0.005644689, 4) 127 130 self.assertAlmostEquals(s , 941.7452, 3) 128 131 129 132 def test_use_case_2(self): 130 133 """ 131 132 133 """ 134 # Create invariant object. Background and scale left as defaults. 135 inv = invariant.InvariantCalculator(data=self.data) 136 134 Invariant without extrapolation. Invariant, volume fraction and surface 135 are given with errors. 136 """ 137 # Create invariant object. Background and scale left as defaults. 138 inv = invariant.InvariantCalculator(data=self.data) 139 137 140 # Get the invariant with errors 138 141 qstar, qstar_err = inv.get_qstar_with_error() 139 142 140 143 # The volume fraction and surface use Q*. That means that the following 141 144 # methods should check that Q* has been computed. If not, it should … … 147 150 self.assertAlmostEquals(v, 0.005644689, 1) 148 151 self.assertAlmostEquals(s , 941.7452, 3) 149 150 152 151 153 def test_use_case_3(self): 152 154 """ … … 155 157 # Create invariant object. Background and scale left as defaults. 156 158 inv = invariant.InvariantCalculator(data=self.data) 157 159 158 160 # Set the extrapolation parameters for the low-Q range 159 161 160 162 # The npts parameter should have a good default. 161 163 # The range parameter should be 'high' or 'low' 162 164 # The function parameter should default to None. If it is None, 163 # the method should pick a good default (Guinier at low-Q and 1/q^4 at high-Q). 164 # The method should also check for consistency of the extrapolation and function 165 # parameters. For instance, you might not want to allow 'high' and 'guinier'. 165 # the method should pick a good default 166 # (Guinier at low-Q and 1/q^4 at high-Q). 167 # The method should also check for consistency of the extrapolation 168 # and function parameters. For instance, you might not want to allow 169 # 'high' and 'guinier'. 166 170 # The power parameter (not shown below) should default to 4. 167 171 inv.set_extrapolation(range='low', npts=10, function='guinier') 168 169 # The version of the call without error 170 # At this point, we could still compute Q* without extrapolation by calling171 # get_qstar with arguments, or with extrapolation=None.172 173 # The version of the call without error 174 # At this point, we could still compute Q* without extrapolation by 175 # calling get_qstar with arguments, or with extrapolation=None. 172 176 qstar = inv.get_qstar(extrapolation='low') 173 177 174 178 # The version of the call with error 175 179 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') … … 178 182 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 179 183 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 180 184 181 185 # Test results 182 186 self.assertAlmostEquals(qstar, 7.49e-5, 1) 183 187 self.assertAlmostEquals(v, 0.005648401, 4) 184 188 self.assertAlmostEquals(s , 941.7452, 3) 185 189 186 190 def test_use_case_4(self): 187 191 """ … … 190 194 # Create invariant object. Background and scale left as defaults. 191 195 inv = invariant.InvariantCalculator(data=self.data) 196 197 # Set the extrapolation parameters for the high-Q range 198 inv.set_extrapolation(range='high', npts=10, function='power_law', 199 power=4) 192 200 193 # Set the extrapolation parameters for the high-Q range 194 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 195 196 # The version of the call without error 197 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 201 # The version of the call without error 202 # The function parameter defaults to None, then is picked to be 203 # 'power_law' for extrapolation='high' 198 204 qstar = inv.get_qstar(extrapolation='high') 199 205 200 206 # The version of the call with error 201 207 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') … … 204 210 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 205 211 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 206 212 207 213 # Test results 208 214 self.assertAlmostEquals(qstar, 7.49e-5,2) 209 215 self.assertAlmostEquals(v, 0.005952674, 3) 210 216 self.assertAlmostEquals(s , 941.7452, 3) 211 217 212 218 def test_use_case_5(self): 213 219 """ … … 216 222 # Create invariant object. Background and scale left as defaults. 217 223 inv = invariant.InvariantCalculator(data=self.data) 218 224 219 225 # Set the extrapolation parameters for the low- and high-Q ranges 220 226 inv.set_extrapolation(range='low', npts=10, function='guinier') 221 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 222 223 # The version of the call without error 224 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 227 inv.set_extrapolation(range='high', npts=10, function='power_law', 228 power=4) 229 230 # The version of the call without error 231 # The function parameter defaults to None, then is picked to be 232 # 'power_law' for extrapolation='high' 225 233 qstar = inv.get_qstar(extrapolation='both') 226 234 … … 231 239 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 232 240 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 233 241 234 242 # Test results 235 243 self.assertAlmostEquals(qstar, 7.88981e-5,2) 236 244 self.assertAlmostEquals(v, 0.005952674, 3) 237 245 self.assertAlmostEquals(s , 941.7452, 3) 238 246 239 247 def test_use_case_6(self): 240 248 """ … … 243 251 # Create invariant object. Background and scale left as defaults. 244 252 inv = invariant.InvariantCalculator(data=self.data) 245 253 246 254 # Set the extrapolation parameters for the high-Q range 247 255 inv.set_extrapolation(range='low', npts=10, function='power_law', power=4) 248 256 249 257 # The version of the call without error 250 258 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 251 259 qstar = inv.get_qstar(extrapolation='low') 252 260 253 261 # The version of the call with error 254 262 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') … … 257 265 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 258 266 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 259 267 260 268 # Test results 261 269 self.assertAlmostEquals(qstar, 7.49e-5,2) 262 270 self.assertAlmostEquals(v, 0.005952674, 3) 263 271 self.assertAlmostEquals(s , 941.7452, 3) 264 272 273 265 274 class TestInvPinholeSmear(unittest.TestCase): 266 275 """ … … 271 280 list = Loader().load("latex_smeared.xml") 272 281 self.data_q_smear = list[0] 273 282 274 283 def test_use_case_1(self): 275 284 """ … … 278 287 inv = invariant.InvariantCalculator(data=self.data_q_smear) 279 288 qstar = inv.get_qstar() 280 289 281 290 v = inv.get_volume_fraction(contrast=2.6e-6) 282 291 s = inv.get_surface(contrast=2.6e-6, porod_const=2) … … 285 294 self.assertAlmostEquals(v, 0.115352622, 2) 286 295 self.assertAlmostEquals(s , 941.7452, 3 ) 287 296 288 297 def test_use_case_2(self): 289 298 """ … … 293 302 # Create invariant object. Background and scale left as defaults. 294 303 inv = invariant.InvariantCalculator(data=self.data_q_smear) 295 304 296 305 # Get the invariant with errors 297 306 qstar, qstar_err = inv.get_qstar_with_error() … … 303 312 self.assertAlmostEquals(v, 0.115352622, 2) 304 313 self.assertAlmostEquals(s , 941.7452, 3 ) 305 314 306 315 def test_use_case_3(self): 307 316 """ … … 319 328 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 320 329 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 321 330 322 331 # Test results 323 332 self.assertAlmostEquals(qstar, 0.00138756,2) 324 333 self.assertAlmostEquals(v, 0.117226896,2) 325 334 self.assertAlmostEquals(s ,941.7452, 3) 326 335 327 336 def test_use_case_4(self): 328 337 """ … … 337 346 # The version of the call with error 338 347 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 339 340 # Get the volume fraction and surface341 # WHY SHOULD THIS FAIL?342 #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6)343 344 # Check that an exception is raised when the 'surface' is not defined345 # WHY SHOULD THIS FAIL?346 #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2)347 348 348 349 # Test results 349 350 self.assertAlmostEquals(qstar, 0.0045773,2) 350 351 351 352 def test_use_case_5(self): 352 353 """ … … 357 358 # Set the extrapolation parameters for the low- and high-Q ranges 358 359 inv.set_extrapolation(range='low', npts=10, function='guinier') 359 inv.set_extrapolation(range='high', npts=10, function='power_law', power=4) 360 # The version of the call without error 361 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 360 inv.set_extrapolation(range='high', npts=10, function='power_law', 361 power=4) 362 # The version of the call without error 363 # The function parameter defaults to None, then is picked to be 364 # 'power_law' for extrapolation='high' 362 365 qstar = inv.get_qstar(extrapolation='both') 363 366 # The version of the call with error 364 367 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') 365 366 # Get the volume fraction and surface 367 # WHY SHOULD THIS FAIL? 368 #self.assertRaises(RuntimeError, inv.get_volume_fraction_with_error, 2.6e-6) 369 #self.assertRaises(RuntimeError, inv.get_surface_with_error, 2.6e-6, 2) 370 368 371 369 # Test results 372 370 self.assertAlmostEquals(qstar, 0.00460319,3)
Note: See TracChangeset
for help on using the changeset viewer.