Changeset 76c1727 in sasview for Invariant/test
- Timestamp:
- Jan 21, 2010 4:42:07 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:
- deeba75
- Parents:
- 889d5ab4
- Location:
- Invariant/test
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Invariant/test/utest_data_handling.py
r889d5ab4 r76c1727 192 192 def test_guinier_incompatible_length(self): 193 193 g = invariant.Guinier() 194 self.assertRaises(AssertionError, g.linearize_data, [1],[1,2],None) 195 self.assertRaises(AssertionError, g.linearize_data, [1,2],[1,2],[1]) 194 data_in = Data1D(x=[1], y=[1,2], dy=None) 195 self.assertRaises(AssertionError, g.linearize_data, data_in) 196 data_in = Data1D(x=[1,1], y=[1,2], dy=[1]) 197 self.assertRaises(AssertionError, g.linearize_data, data_in) 196 198 197 199 def test_linearization(self): … … 203 205 y = numpy.asarray(numpy.asarray([1,1,1,1])) 204 206 g = invariant.Guinier() 205 x_out, y_out, dy_out = g.linearize_data(x,y,None) 207 data_in = Data1D(x=x, y=y) 208 data_out = g.linearize_data(data_in) 209 x_out, y_out, dy_out = data_out.x, data_out.y, data_out.dy 206 210 self.assertEqual(len(x_out), 3) 207 211 self.assertEqual(len(y_out), 3) … … 261 265 class TestDataExtraLowSlit(unittest.TestCase): 262 266 """ 263 Generate a Guinier distribution and verify that the extrapolation 264 produce the correct ditribution. Tested if the data generated by the 265 invariant calculator is correct 266 """ 267 268 def setUp(self): 269 """ 270 Generate a Guinier distribution. After extrapolating, we will 271 verify that we obtain the scale and rg parameters 267 for a smear data, test that the fitting go through 268 reel data for the 2 first points 269 """ 270 def setUp(self): 271 """ 272 Reel data containing slit smear information 273 .Use 2 points of data to fit with power_law when exptrapolating 272 274 """ 273 275 list = Loader().load("latex_smeared.xml") … … 275 277 self.data.dxl = list[0].dxl 276 278 self.data.dxw = list[0].dxw 279 self.npts = 2 277 280 278 281 def test_low_q(self): … … 283 286 inv = invariant.InvariantCalculator(data=self.data) 284 287 # Set the extrapolation parameters for the low-Q range 285 inv.set_extrapolation(range='low', npts= 20, function='guinier')286 287 self.assertEqual(inv._low_extrapolation_npts, 20)288 self.assertEqual(inv._low_extrapolation_function.__class__, invariant. Guinier)288 inv.set_extrapolation(range='low', npts=self.npts, function='power_law') 289 290 self.assertEqual(inv._low_extrapolation_npts, self.npts) 291 self.assertEqual(inv._low_extrapolation_function.__class__, invariant.PowerLaw) 289 292 290 293 # Data boundaries for fiiting … … 298 301 power=inv._low_extrapolation_power) 299 302 300 301 303 qstar = inv.get_qstar(extrapolation='low') 302 304 reel_y = self.data.y … … 304 306 #low data . expect the fit engine to have been already called and the guinier 305 307 # to have the radius and the scale fitted 306 307 test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x) 308 for i in range(len(self.data.x)): 308 test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) 309 #Check any points generated from the reel data and the extrapolation have 310 #very close value 311 self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 312 for i in range(inv._low_extrapolation_npts): 313 value = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 314 self.assert_(value < 0.001) 315 data_out_range, data_in_range= inv.get_extra_data_low(npts_in=None) 316 317 class TestDataExtraLowSlitGuinier(unittest.TestCase): 318 """ 319 for a smear data, test that the fitting go through 320 reel data for atleast the 2 first points 321 """ 322 323 def setUp(self): 324 """ 325 Generate a Guinier distribution. After extrapolating, we will 326 verify that we obtain the scale and rg parameters 327 """ 328 self.scale = 1.5 329 self.rg = 30.0 330 x = numpy.arange(0.0001, 0.1, 0.0001) 331 y = numpy.asarray([self.scale * math.exp( -(q*self.rg)**2 / 3.0 ) for q in x]) 332 dy = y*.1 333 dxl = 0.117 * numpy.ones(len(x)) 334 self.data = Data1D(x=x, y=y, dy=dy) 335 self.data.dxl = dxl 336 self.npts = len(x)-10 337 338 def test_low_q(self): 339 """ 340 Invariant with low-Q extrapolation with slit smear 341 """ 342 # Create invariant object. Background and scale left as defaults. 343 inv = invariant.InvariantCalculator(data=self.data) 344 # Set the extrapolation parameters for the low-Q range 345 inv.set_extrapolation(range='low', npts=self.npts, function='guinier') 346 347 self.assertEqual(inv._low_extrapolation_npts, self.npts) 348 self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 349 350 # Data boundaries for fiiting 351 qmin = inv._data.x[0] 352 qmax = inv._data.x[inv._low_extrapolation_npts - 1] 353 354 # Extrapolate the low-Q data 355 a, b = inv._fit(model=inv._low_extrapolation_function, 356 qmin=qmin, 357 qmax=qmax, 358 power=inv._low_extrapolation_power) 359 360 361 qstar = inv.get_qstar(extrapolation='low') 362 reel_y = self.data.y 363 #Compution the y 's coming out of the invariant when computing extrapolated 364 #low data . expect the fit engine to have been already called and the guinier 365 # to have the radius and the scale fitted 366 test_y = inv._low_extrapolation_function.evaluate_model(x=self.data.x[:inv._low_extrapolation_npts]) 367 self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 368 369 for i in range(inv._low_extrapolation_npts): 309 370 value = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 310 371 self.assert_(value < 0.001) 311 372 373 def test_low_data(self): 374 """ 375 Invariant with low-Q extrapolation with slit smear 376 """ 377 # Create invariant object. Background and scale left as defaults. 378 inv = invariant.InvariantCalculator(data=self.data) 379 # Set the extrapolation parameters for the low-Q range 380 inv.set_extrapolation(range='low', npts=self.npts, function='guinier') 381 382 self.assertEqual(inv._low_extrapolation_npts, self.npts) 383 self.assertEqual(inv._low_extrapolation_function.__class__, invariant.Guinier) 384 385 # Data boundaries for fiiting 386 qmin = inv._data.x[0] 387 qmax = inv._data.x[inv._low_extrapolation_npts - 1] 388 389 # Extrapolate the low-Q data 390 a, b = inv._fit(model=inv._low_extrapolation_function, 391 qmin=qmin, 392 qmax=qmax, 393 power=inv._low_extrapolation_power) 394 395 396 qstar = inv.get_qstar(extrapolation='low') 397 reel_y = self.data.y 398 #Compution the y 's coming out of the invariant when computing extrapolated 399 #low data . expect the fit engine to have been already called and the guinier 400 # to have the radius and the scale fitted 401 data_out_range, data_in_range= inv.get_extra_data_low() 402 test_y = data_in_range.y 403 self.assert_(len(test_y))== len(reel_y[:inv._low_extrapolation_npts]) 404 for i in range(inv._low_extrapolation_npts): 405 value = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 406 self.assert_(value < 0.001) 407 408 data_out_range, data_in_range= inv.get_extra_data_low(npts_in= 2, nsteps=10, 409 q_start= 1e-4) 410 test_y = data_in_range.y 411 self.assert_(len(test_y))== len(reel_y[:2]) 412 for i in range(2): 413 value = math.fabs(test_y[i]-reel_y[i])/reel_y[i] 414 self.assert_(value < 0.001) 415 #test the data out of range 416 test_out_y = data_out_range.y 417 self.assertEqual(len(test_out_y), 10) 312 418 419 class TestDataExtraHighSlitPowerLaw(unittest.TestCase): 420 """ 421 for a smear data, test that the fitting go through 422 reel data for atleast the 2 first points 423 """ 424 425 def setUp(self): 426 """ 427 Generate a Guinier distribution. After extrapolating, we will 428 verify that we obtain the scale and rg parameters 429 """ 430 self.scale = 1.5 431 self.m = 3.0 432 x = numpy.arange(0.0001, 0.1, 0.0001) 433 y = numpy.asarray([self.scale * math.pow(q ,-1.0*self.m) for q in x]) 434 dy = y*.1 435 self.data = Data1D(x=x, y=y, dy=dy) 436 dxl = 0.117 * numpy.ones(len(x)) 437 self.data.dxl = dxl 438 self.npts = 20 439 440 def test_high_q(self): 441 """ 442 Invariant with high-Q extrapolation with slit smear 443 """ 444 # Create invariant object. Background and scale left as defaults. 445 inv = invariant.InvariantCalculator(data=self.data) 446 # Set the extrapolation parameters for the low-Q range 447 inv.set_extrapolation(range='high', npts=self.npts, function='power_law') 448 449 self.assertEqual(inv._high_extrapolation_npts, self.npts) 450 self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) 451 452 # Data boundaries for fiiting 453 xlen = len(self.data.x) 454 start = xlen - inv._high_extrapolation_npts 455 qmin = inv._data.x[start] 456 qmax = inv._data.x[xlen-1] 457 458 # Extrapolate the high-Q data 459 a, b = inv._fit(model=inv._high_extrapolation_function, 460 qmin=qmin, 461 qmax=qmax, 462 power=inv._high_extrapolation_power) 463 464 465 qstar = inv.get_qstar(extrapolation='high') 466 reel_y = self.data.y 467 #Compution the y 's coming out of the invariant when computing extrapolated 468 #low data . expect the fit engine to have been already called and the power law 469 # to have the radius and the scale fitted 470 471 472 test_y = inv._high_extrapolation_function.evaluate_model(x=self.data.x[start: ]) 473 self.assert_(len(test_y))== len(reel_y[start:]) 474 475 for i in range(len(self.data.x[start:])): 476 value = math.fabs(test_y[i]-reel_y[start+i])/reel_y[start+i] 477 self.assert_(value < 0.001) 478 479 def test_high_data(self): 480 """ 481 Invariant with low-Q extrapolation with slit smear 482 """ 483 # Create invariant object. Background and scale left as defaults. 484 inv = invariant.InvariantCalculator(data=self.data) 485 # Set the extrapolation parameters for the low-Q range 486 inv.set_extrapolation(range='high', npts=self.npts, function='power_law') 487 488 self.assertEqual(inv._high_extrapolation_npts, self.npts) 489 self.assertEqual(inv._high_extrapolation_function.__class__, invariant.PowerLaw) 490 491 # Data boundaries for fiiting 492 xlen = len(self.data.x) 493 start = xlen - inv._high_extrapolation_npts 494 qmin = inv._data.x[start] 495 qmax = inv._data.x[xlen-1] 496 497 # Extrapolate the high-Q data 498 a, b = inv._fit(model=inv._high_extrapolation_function, 499 qmin=qmin, 500 qmax=qmax, 501 power=inv._high_extrapolation_power) 502 503 504 qstar = inv.get_qstar(extrapolation='high') 505 reel_y = self.data.y 506 #Compution the y 's coming out of the invariant when computing extrapolated 507 #low data . expect the fit engine to have been already called and the power law 508 # to have the radius and the scale fitted 509 510 data_out_range, data_in_range= inv.get_extra_data_high() 511 test_y = data_in_range.y 512 self.assert_(len(test_y))== len(reel_y[start:]) 513 temp = reel_y[start:] 514 515 for i in range(len(self.data.x[start:])): 516 value = math.fabs(test_y[i]- temp[i])/temp[i] 517 self.assert_(value < 0.001) 313 518 314 519 data_out_range, data_in_range= inv.get_extra_data_high(npts_in=5, nsteps=10, 520 q_end= 2) 521 test_y = data_in_range.y 522 self.assert_(len(test_y)==5) 523 temp = reel_y[start:start+5] 524 525 for i in range(len(self.data.x[start:start+5])): 526 527 value = math.fabs(test_y[i]- temp[i])/temp[i] 528 self.assert_(value < 0.06) 529 #test the data out of range 530 test_out_y = data_out_range.y 531 self.assertEqual(len(test_out_y), 10) 532 -
Invariant/test/utest_use_cases.py
raafa962 r76c1727 326 326 # Test results 327 327 self.assertAlmostEquals(qstar, 4.1539e-4, 2) 328 self.assertAlmostEquals(v, 0.032164596, 3)328 self.assertAlmostEquals(v, 0.032164596, 2) 329 329 self.assertAlmostEquals(s , 941.7452, 3) 330 330
Note: See TracChangeset
for help on using the changeset viewer.