Changeset ef9ed58 in sasview for Invariant/test
- Timestamp:
- Nov 30, 2009 7:10:39 PM (15 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.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- c5607fa
- Parents:
- 2733188
- Location:
- Invariant/test
- Files:
-
- 2 added
- 1 deleted
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/test/utest_use_cases.py
rb6666d4 ref9ed58 4 4 """ 5 5 import unittest 6 import numpy 6 7 from DataLoader.loader import Loader 7 8 from sans.invariant import invariant 8 9 9 10 class Data1D: 11 print "I am not of type Dataloader.Data1D" 12 10 13 class TestInvPolySphere(unittest.TestCase): 11 14 """ … … 14 17 def setUp(self): 15 18 self.data = Loader().load("PolySpheres.txt") 19 20 def test_wrong_data(self): 21 """ test receiving Data1D not of type loader""" 22 23 wrong_data= Data1D() 24 try: 25 self.assertRaises(ValueError,invariant.InvariantCalculator(wrong_data)) 26 except ValueError, msg: 27 print "test pass "+ str(msg) 28 else: raise ValueError, "fail to raise exception when expected" 29 16 30 17 31 def test_use_case_1(self): … … 33 47 # methods should check that Q* has been computed. If not, it should 34 48 # compute it by calling get_qstare(), leaving the parameters as default. 35 volume_fraction = inv.get_volume_fraction(contrast=1.0) 36 surface = inv.get_surface(contrast=1.0, porod_const=1.0) 49 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 50 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 51 52 # Test results 53 self.assertAlmostEquals(qstar, 7.48959e-5,2) 54 self.assertAlmostEquals(v, 0.005644689) 55 self.assertAlmostEquals(s, 9.42e+2,2) 37 56 38 57 def test_use_case_2(self): … … 46 65 # Get the invariant with errors 47 66 qstar, qstar_err = inv.get_qstar_with_error() 48 v, dv = inv.get_volume_fraction_with_error(contrast=1.0) 49 s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=1.0) 67 68 # The volume fraction and surface use Q*. That means that the following 69 # methods should check that Q* has been computed. If not, it should 70 # compute it by calling get_qstare(), leaving the parameters as default. 71 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 72 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 73 # Test results 74 self.assertAlmostEquals(qstar, 7.48959e-5,2) 75 self.assertAlmostEquals(v, 0.005644689) 76 self.assertAlmostEquals(s, 9.42e+2,2) 77 50 78 51 79 def test_use_case_3(self): … … 62 90 # The function parameter should default to None. If it is None, 63 91 # the method should pick a good default (Guinier at low-Q and 1/q^4 at high-Q). 64 # The method should also check for consistency of the extrapolat eand function92 # The method should also check for consistency of the extrapolation and function 65 93 # parameters. For instance, you might not want to allow 'high' and 'guinier'. 66 94 # The power parameter (not shown below) should default to 4. 67 inv.set_extrap lotation(range='low', npts=5, function='guinier')95 inv.set_extrapolation(range='low', npts=20, function='guinier') 68 96 69 97 # The version of the call without error 70 98 # At this point, we could still compute Q* without extrapolation by calling 71 # get_qstar with arguments, or with extrapolate=None. 72 qstar = inv.get_qstar(extrapolate='low') 73 74 # The version of the call with error 75 qstar, qstar_err = inv.get_qstar_with_error(extrapolate='low') 76 77 # Get the volume fraction and surface 78 v, dv = inv.get_volume_fraction_with_error(contrast=1.0) 79 s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=1.0) 80 99 # get_qstar with arguments, or with extrapolation=None. 100 qstar = inv.get_qstar(extrapolation='low') 101 102 # The version of the call with error 103 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') 104 105 # Get the volume fraction and surface 106 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 107 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 108 109 # Test results 110 self.assertAlmostEquals(qstar, 7.49e-5,2) 111 self.assertAlmostEquals(v, 0.005648401) 112 self.assertAlmostEquals(s, 9.42e+2,2) 113 81 114 def test_use_case_4(self): 82 115 """ … … 86 119 inv = invariant.InvariantCalculator(data=self.data) 87 120 88 89 121 # Set the extrapolation parameters for the high-Q range 90 91 # The npts parameter should have a good default. 92 # The range parameter should be 'high' or 'low'. Those should be stored separately. 93 # The function parameter should default to None. If it is None, 94 # the method should pick a good default (Guinier at low-Q and 1/q^4 at high-Q). 95 # The method should also check for consistency of the extrapolate and function 96 # parameters. For instance, you might not want to allow 'high' and 'guinier'. 97 # The power parameter should default to 4. 98 inv.set_extraplotation(range='high', npts=5, function='power_law', power=4) 99 100 101 # The version of the call without error 102 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolate='high' 103 qstar = inv.get_qstar(extrapolate='high') 104 105 # The version of the call with error 106 qstar, qstar_err = inv.get_qstar_with_error(extrapolate='high') 107 108 # Get the volume fraction and surface 109 v, dv = inv.get_volume_fraction_with_error(contrast=1.0) 110 s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=1.0) 122 inv.set_extrapolation(range='high', npts=20, function='power_law', power=4) 123 124 # The version of the call without error 125 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 126 qstar = inv.get_qstar(extrapolation='high') 127 128 # The version of the call with error 129 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 130 131 # Get the volume fraction and surface 132 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 133 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 134 135 # Test results 136 self.assertAlmostEquals(qstar, 7.49e-5,2) 137 self.assertAlmostEquals(v, 0.005952674) 138 self.assertAlmostEquals(s, 9.42e+2,2) 111 139 112 140 def test_use_case_5(self): … … 118 146 119 147 # Set the extrapolation parameters for the low- and high-Q ranges 120 inv.set_extraplotation(range='low', npts=5, function='guinier') 121 inv.set_extraplotation(range='high', npts=5, function='power_law', power=4) 122 123 # The version of the call without error 124 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolate='high' 125 qstar = inv.get_qstar(extrapolate='both') 126 127 # The version of the call with error 128 qstar, qstar_err = inv.get_qstar_with_error(extrapolate='both') 129 130 # Get the volume fraction and surface 131 v, dv = inv.get_volume_fraction_with_error(contrast=1.0) 132 s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=1.0) 133 134 148 inv.set_extrapolation(range='low', npts=5, function='guinier') 149 inv.set_extrapolation(range='high', npts=5, function='power_law', power=4) 150 151 # The version of the call without error 152 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 153 qstar = inv.get_qstar(extrapolation='both') 154 155 # The version of the call with error 156 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') 157 158 # Get the volume fraction and surface 159 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 160 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 161 162 # Test results 163 self.assertAlmostEquals(qstar, 7.90069e-5,2) 164 self.assertAlmostEquals(v, 0.005952674) 165 self.assertAlmostEquals(s, 9.42e+2,2) 166 167 class TestInvSlitSmear(unittest.TestCase): 168 """ 169 Test slit smeared data for invariant computation 170 """ 171 def setUp(self): 172 # Data with slit smear 173 list = Loader().load("latex_smeared.xml") 174 self.data_slit_smear = list[1] 175 176 def test_use_case_1(self): 177 """ 178 Invariant without extrapolation 179 """ 180 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 181 # get invariant 182 qstar = inv.get_qstar() 183 # Get the volume fraction and surface 184 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 185 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 186 # Test results 187 self.assertAlmostEquals(qstar, 4.1539e-4) 188 self.assertAlmostEquals(v, 0.032164596) 189 self.assertAlmostEquals(s, 9.42e+2,2) 190 191 def test_use_case_2(self): 192 """ 193 Invariant without extrapolation. Invariant, volume fraction and surface 194 are given with errors. 195 """ 196 # Create invariant object. Background and scale left as defaults. 197 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 198 # Get the invariant with errors 199 qstar, qstar_err = inv.get_qstar_with_error() 200 # Get the volume fraction and surface 201 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 202 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 203 # Test results 204 self.assertAlmostEquals(qstar, 4.1539e-4) 205 self.assertAlmostEquals(v, 0.032164596) 206 self.assertAlmostEquals(s, 9.42e+2,2) 207 208 def test_use_case_3(self): 209 """ 210 Invariant with low-Q extrapolation 211 """ 212 # Create invariant object. Background and scale left as defaults. 213 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 214 # Set the extrapolation parameters for the low-Q range 215 inv.set_extrapolation(range='low', npts=20, function='guinier') 216 # The version of the call without error 217 # At this point, we could still compute Q* without extrapolation by calling 218 # get_qstar with arguments, or with extrapolation=None. 219 qstar = inv.get_qstar(extrapolation='low') 220 # The version of the call with error 221 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') 222 # Get the volume fraction and surface 223 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 224 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 225 226 # Test results 227 self.assertAlmostEquals(qstar,4.1534e-4,3) 228 self.assertAlmostEquals(v, 0.032164596) 229 self.assertAlmostEquals(s, 9.42e+2,2) 230 231 def test_use_case_4(self): 232 """ 233 Invariant with high-Q extrapolation 234 """ 235 # Create invariant object. Background and scale left as defaults. 236 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 237 238 # Set the extrapolation parameters for the high-Q range 239 inv.set_extrapolation(range='high', npts=20, function='power_law', power=4) 240 241 # The version of the call without error 242 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 243 qstar = inv.get_qstar(extrapolation='high') 244 245 # The version of the call with error 246 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 247 248 # Get the volume fraction and surface 249 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 250 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 251 252 # Test results 253 self.assertAlmostEquals(qstar, 4.1539e-4) 254 self.assertAlmostEquals(v, 0.032164596) 255 self.assertAlmostEquals(s, 9.42e+2,2) 256 257 def test_use_case_5(self): 258 """ 259 Invariant with both high- and low-Q extrapolation 260 """ 261 # Create invariant object. Background and scale left as defaults. 262 inv = invariant.InvariantCalculator(data=self.data_slit_smear) 263 264 # Set the extrapolation parameters for the low- and high-Q ranges 265 inv.set_extrapolation(range='low', npts=20, function='guinier') 266 inv.set_extrapolation(range='high', npts=20, function='power_law', power=4) 267 268 # The version of the call without error 269 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 270 qstar = inv.get_qstar(extrapolation='both') 271 272 # The version of the call with error 273 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') 274 275 # Get the volume fraction and surface 276 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 277 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 278 279 # Test results 280 self.assertAlmostEquals(qstar, 4.1534e-4,3) 281 self.assertAlmostEquals(v, 0.032164596) 282 self.assertAlmostEquals(s, 9.42e+2,2) 283 284 class TestInvPinholeSmear(unittest.TestCase): 285 """ 286 Test pinhole smeared data for invariant computation 287 """ 288 def setUp(self): 289 # data with smear info 290 list = Loader().load("latex_smeared.xml") 291 self.data_q_smear = list[0] 292 293 def test_use_case_1(self): 294 """ 295 Invariant without extrapolation 296 """ 297 inv = invariant.InvariantCalculator(data=self.data_q_smear) 298 qstar = inv.get_qstar() 299 300 volume_fraction = inv.get_volume_fraction(contrast=2.6e-6) 301 surface = inv.get_surface(contrast=2.6e-6, porod_const=2) 302 # Test results 303 self.assertAlmostEquals(qstar, 1.361677e-3) 304 self.assertAlmostEquals(v, 0.115352622) 305 self.assertAlmostEquals(s, 9.42e+2,2) 306 307 def test_use_case_2(self): 308 """ 309 Invariant without extrapolation. Invariant, volume fraction and surface 310 are given with errors. 311 """ 312 # Create invariant object. Background and scale left as defaults. 313 inv = invariant.InvariantCalculator(data=self.data_q_smear) 314 315 # Get the invariant with errors 316 qstar, qstar_err = inv.get_qstar_with_error() 317 # Get the volume fraction and surface 318 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 319 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 320 # Test results 321 self.assertAlmostEquals(qstar, 1.361677e-3) 322 self.assertAlmostEquals(v, 0.115352622) 323 self.assertAlmostEquals(s, 9.42e+2,2) 324 325 def test_use_case_3(self): 326 """ 327 Invariant with low-Q extrapolation 328 """ 329 # Create invariant object. Background and scale left as defaults. 330 inv = invariant.InvariantCalculator(data=self.data_q_smear) 331 # Set the extrapolation parameters for the low-Q range 332 inv.set_extrapolation(range='low', npts=20, function='guinier') 333 # The version of the call without error 334 qstar = inv.get_qstar(extrapolation='low') 335 # The version of the call with error 336 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='low') 337 # Get the volume fraction and surface 338 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 339 s, ds = inv.get_surface_with_error(contrast=1.0, porod_const=0.08) 340 341 # Test results 342 self.assertAlmostEquals(qstar, 0.002037677) 343 self.assertAlmostEquals(v, 0.115352622) 344 self.assertAlmostEquals(s, 9.42e+2,2) 345 346 def test_use_case_4(self): 347 """ 348 Invariant with high-Q extrapolation 349 """ 350 # Create invariant object. Background and scale left as defaults. 351 inv = invariant.InvariantCalculator(data=self.data_q_smear) 352 # Set the extrapolation parameters for the high-Q range 353 inv.set_extrapolation(range='high', npts=20, function='power_law', power=4) 354 # The version of the call without error 355 qstar = inv.get_qstar(extrapolation='high') 356 # The version of the call with error 357 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='high') 358 # Get the volume fraction and surface 359 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 360 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 361 362 # Test results 363 self.assertAlmostEquals(qstar, 1.481677e-3) 364 self.assertAlmostEquals(v, 0.127225804) 365 self.assertAlmostEquals(s, 9.42e+2,2) 366 367 def test_use_case_5(self): 368 """ 369 Invariant with both high- and low-Q extrapolation 370 """ 371 # Create invariant object. Background and scale left as defaults. 372 inv = invariant.InvariantCalculator(data=self.data_q_smear) 373 # Set the extrapolation parameters for the low- and high-Q ranges 374 inv.set_extrapolation(range='low', npts=20, function='guinier') 375 inv.set_extrapolation(range='high', npts=20, function='power_law', power=4) 376 # The version of the call without error 377 # The function parameter defaults to None, then is picked to be 'power_law' for extrapolation='high' 378 qstar = inv.get_qstar(extrapolation='both') 379 # The version of the call with error 380 qstar, qstar_err = inv.get_qstar_with_error(extrapolation='both') 381 # Get the volume fraction and surface 382 v, dv = inv.get_volume_fraction_with_error(contrast=2.6e-6) 383 s, ds = inv.get_surface_with_error(contrast=2.6e-6, porod_const=2) 384 385 # Test results 386 self.assertAlmostEquals(qstar, 0.002157677) 387 self.assertAlmostEquals(v, 0.202846825) 388 self.assertAlmostEquals(s, 9.42e+2,2)
Note: See TracChangeset
for help on using the changeset viewer.