Changeset 36948c92 in sasview for sansmodels/src
- Timestamp:
- Apr 15, 2008 8:08:40 AM (17 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:
- 3de85b8
- Parents:
- 5aa3bbd
- Location:
- sansmodels/src/sans/models
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sansmodels/src/sans/models/FractalModel.py
rf629e346 r36948c92 49 49 ## Parameter details [units, min, max] 50 50 self.details = {} 51 self.details['scale'] = ['', None, None]52 self.details['radius'] = ['A', None, None]53 self.details['fractal_dim'] = ['', None,None]54 self.details['corr_length'] = ['A', None, None]55 self.details['block_sld'] = ['A-2', None, None]56 self.details['solvent_sld'] = ['A-2', None, None]51 self.details['scale'] = ['', None, None] 52 self.details['radius'] = ['A', None, None] 53 self.details['fractal_dim'] = ['', 0, None] 54 self.details['corr_length'] = ['A', None, None] 55 self.details['block_sld'] = ['A-2', None, None] 56 self.details['solvent_sld'] = ['A-2', None, None] 57 57 self.details['background'] = ['cm-1', None, None] 58 58 … … 63 63 F(x) = p(x) * s(x) + bkd 64 64 """ 65 if x<0 and self.params['fractal_dim']>0: 66 raise ValueError, "negative number cannot be raised to a fractional power" 67 65 68 return self.params['background']+ self._scatterRanDom(x)* self._Block(x) 69 66 70 67 71 def _Block(self,x): 68 69 70 71 72 return 1.0 + (math.sin((self.params['fractal_dim']-1.0) * math.atan(x * self.params['corr_length']))\ 72 73 * self.params['fractal_dim'] * gamma(self.params['fractal_dim']-1.0))\ … … 96 97 """ 97 98 if x.__class__.__name__ == 'list': 98 return self._Fractal(x[0]*math.cos(x[1]))*self._Fractal(x[0]*math.sin(x[1])) 99 # Take absolute value of Q, since this model is really meant to 100 # be defined in 1D for a given length of Q 101 qx = math.fabs(x[0]*math.cos(x[1])) 102 qy = math.fabs(x[0]*math.sin(x[1])) 103 104 return self._Fractal(qx)*self._Fractal(qy) 99 105 elif x.__class__.__name__ == 'tuple': 100 106 raise ValueError, "Tuples are not allowed as input to BaseComponent models" -
sansmodels/src/sans/models/PorodModel.py
r3db3895 r36948c92 26 26 ## Define parameters 27 27 self.params = {} 28 self.params['scale'] = 0.0 28 self.params['scale'] = 1.0 29 self.params['background'] = 0.0 29 30 30 31 31 32 ## Parameter details [units, min, max] 32 33 self.details = {} 33 self.details['scale'] = ['', None, None] 34 self.details['scale'] = ['', None, None] 35 self.details['background'] = ['', None, None] 34 36 35 37 36 38 def _porod(self, x): 37 return self.params['scale']/x**4.0 39 return self.params['scale']/x**4.0 + self.params['background'] 38 40 39 41 def run(self, x = 0.0): -
sansmodels/src/sans/models/PowerLawModel.py
r3db3895 r36948c92 38 38 ## Parameter details [units, min, max] 39 39 self.details = {} 40 self.details['m'] = ['', None, None]40 self.details['m'] = ['', 0, None] 41 41 self.details['scale'] = ['', None, None] 42 42 self.details['background'] = ['', None, None] … … 47 47 48 48 """ 49 return self.params['scale']*math.pow(x ,-self.params['m'])\ 49 if x<0 and self.params['m']>0: 50 raise ValueError, "negative number cannot be raised to a fractional power" 51 52 return self.params['scale']*math.pow(x ,-1.0*self.params['m'])\ 50 53 + self.params['background'] 51 54 52 53 55 def run(self, x = 0.0): 54 56 """ Evaluate the model … … 57 59 """ 58 60 if x.__class__.__name__ == 'list': 59 return self._PowerLaw(x[0]*math.cos(x[1]))*self._PowerLaw(x[0]*math.sin(x[1])) 61 # Take absolute value of Q, since this model is really meant to 62 # be defined in 1D for a given length of Q 63 qx = math.fabs(x[0]*math.cos(x[1])) 64 qy = math.fabs(x[0]*math.sin(x[1])) 65 return self._PowerLaw(qx)*self._PowerLaw(qy) 60 66 elif x.__class__.__name__ == 'tuple': 61 67 raise ValueError, "Tuples are not allowed as input to BaseComponent models" -
sansmodels/src/sans/models/test/testcase_generator.py
rae3ce4e r36948c92 344 344 345 345 if not self.passed: 346 print "\nFailure:" 346 347 print report.trace 347 348 return self.passed … … 353 354 @return: True if passed, False otherwise 354 355 """ 355 minval = 0 356 maxval = 20356 minval = 0.001 357 maxval = 1.0 357 358 # Call run with random input 358 359 import random, math … … 368 369 output = model.run([input_value, 2*math.pi*random.random()]) 369 370 except ZeroDivisionError: 370 output = -1 371 print "Error evaluating model %s: %g" % (model.name, input_value) 372 output = None 371 373 372 374 #print "Eval: %g" % output … … 374 376 # Oracle bit - just check that we have a number... 375 377 passed = False 376 if math.fabs(output) >= 0: 377 passed = True 378 try: 379 if output != None and math.fabs(output) >= 0: 380 passed = True 381 except: 382 print "---> Could not compute abs val", output, model.name 383 378 384 379 385 report = ReportCard() … … 382 388 report.n_eval_pass = 1 383 389 else: 384 report.log = "Eval: bad output value % g\n" % output385 386 report.trace = "Eval(%g) = % g %i\n" % (input_value, output, passed)390 report.log = "Eval: bad output value %s\n" % str(output) 391 392 report.trace = "Eval(%g) = %s %i\n" % (input_value, str(output), passed) 387 393 return model, report 388 394 … … 395 401 import random, math 396 402 minval = 1 397 maxval = 5 0403 maxval = 5 398 404 399 405 # Choose a parameter to change … … 407 413 p_name = keys[i_param] 408 414 409 # Chose a value 415 # Choose a value 416 # Check for min/max 417 if hasattr(model, "details"): 418 if model.details.has_key(p_name): 419 if model.details[p_name][1] != None: 420 minval = model.details[p_name][1] 421 if model.details[p_name][2] != None: 422 maxval = model.details[p_name][2] 423 elif model.other.details.has_key(p_name): 424 if model.other.details[p_name][1] != None: 425 minval = model.other.details[p_name][1] 426 if model.other.details[p_name][2] != None: 427 maxval = model.other.details[p_name][2] 428 elif model.operateOn.details.has_key(p_name): 429 if model.operateOn.details[p_name][1] != None: 430 minval = model.operateOn.details[p_name][1] 431 if model.operateOn.details[p_name][2] != None: 432 maxval = model.operateOn.details[p_name][2] 433 410 434 input_val = random.random()*(maxval-minval)+minval 411 435 model.setParam(p_name, input_val) … … 613 637 614 638 try: 615 value2 = tmp.run(1 )639 value2 = tmp.run(1.1 * (1.0 + random())) 616 640 value2 = float(value2) 617 641 except: 642 value2 = None 618 643 passed = False 619 644 … … 628 653 report.log = "Sub: bad output from composite model\n" 629 654 630 report.trace = "Sub %s (%g - %g = % g) %i\n" % \655 report.trace = "Sub %s (%g - %g = %s) %i\n" % \ 631 656 (model.name, model.run(1), \ 632 sub_model.run(1), value2, passed)657 sub_model.run(1), str(value2), passed) 633 658 return tmp, report 634 659 … … 647 672 # Oracle bit 648 673 passed = False 649 674 675 from random import random 676 input_val = 1.1 * (1.0 + random()) 677 v1 = None 678 v2 = None 650 679 try: 651 value2 = tmp.run(1) 680 v1 = mul_model.run(input_val) 681 v2 = model.run(input_val) 682 value2 = tmp.run(input_val) 652 683 value2 = float(value2) 653 except :654 passed = False684 except ZeroDivisionError: 685 value2 = None 655 686 656 687 # If we made it this far, we have a float … … 664 695 report.log = "Mul: bad output from composite model\n" 665 696 666 report.trace = "Mul %s (%g * %g = %g) %i\n" % \ 667 (model.name, model.run(1), \ 668 mul_model.run(1), value2, passed) 697 report.trace = "Mul %s (%s * %s = %s) %i\n" % \ 698 (model.name, str(v1), str(v2), str(value2), passed) 669 699 return tmp, report 670 700 … … 678 708 #div_model = factory.getModel("SphereModel") 679 709 #div_model = factory.getModel(randomModel()) 680 div_model = getRandomModelObject() 681 710 711 from random import random 712 input_val = 1.5 * random() 713 714 # Find a model that will not evaluate to zero 715 # at the input value 716 try_again = True 717 while try_again: 718 div_model = getRandomModelObject() 719 try: 720 v2 = div_model.run(input_val) 721 try_again = False 722 except: 723 pass 724 682 725 tmp = model / div_model 683 726 … … 685 728 passed = False 686 729 730 v1 = None 731 v2 = None 687 732 try: 688 from random import random 689 input_val = 1.5 * random() 690 if div_model.run(input_val)==0: 691 print "Skipped (DIV) because denominator evaluated to zero" 692 else: 733 734 # Check individual models against bad combinations, 735 # which happen from time to time given that all 736 # parameters are random 737 try: 738 v2 = div_model.run(input_val) 739 v1 = model.run(input_val) 693 740 value2 = tmp.run(input_val) 694 741 value2 = float(value2) 742 except ZeroDivisionError: 743 value2 = None 695 744 except: 696 745 passed = False … … 706 755 report.log = "Div: bad output from composite model\n" 707 756 708 report.trace = "Div %s (%g / %g = %g) %i\n" % \ 709 (model.name, model.run(1), \ 710 div_model.run(1), value2, passed) 757 report.trace = "Div %s/%s (%g) = %s / %s = %s %i\n" % \ 758 (model.name, div_model.name, input_val, str(v1), str(v2), str(value2), passed) 711 759 return tmp, report 712 760 … … 715 763 #print randomModel() 716 764 g = TestCaseGenerator() 717 g.generateAndRun(2000 )765 g.generateAndRun(20000) 718 766 719 767 #t = TestCase(filename = "error_1.17721e+009.xml")
Note: See TracChangeset
for help on using the changeset viewer.