Changeset 95d58d3 in sasview for test/park_integration


Ignore:
Timestamp:
Apr 10, 2014 8:05:28 PM (11 years ago)
Author:
pkienzle
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:
90f49a8
Parents:
6fe5100
Message:

fix fit line test for bumps/scipy/park and enable it as part of test suite

File:
1 moved

Legend:

Unmodified
Added
Removed
  • test/park_integration/test/utest_fit_line.py

    r6fe5100 r95d58d3  
    66import math 
    77 
    8 from sans.fit.AbstractFitEngine import Model 
     8from sans.fit.AbstractFitEngine import Model, FitHandler 
    99from sans.dataloader.loader import Loader 
    1010from sans.fit.Fitting import Fit 
     
    1414class testFitModule(unittest.TestCase): 
    1515    """ test fitting """ 
    16     
    17     def test1(self): 
    18         """ Fit 1 data (testdata_line.txt)and 1 model(lineModel) """ 
    19         #load data 
     16 
     17    def test_bad_pars(self): 
     18        fitter = Fit('bumps') 
     19 
    2020        data = Loader().load("testdata_line.txt") 
    2121        data.name = data.filename 
    22         #Importing the Fit module 
    23         from bumps import fitters 
    24         fitters.FIT_DEFAULT = 'dream' 
    25         print fitters.FIT_OPTIONS['dream'].__dict__ 
    26         fitter = Fit('bumps') 
     22        fitter.set_data(data,1) 
     23 
     24        model1  = LineModel() 
     25        model1.name = "M1" 
     26        model = Model(model1, data) 
     27        pars1= ['param1','param2'] 
     28        try: 
     29            fitter.set_model(model,1,pars1) 
     30        except ValueError,exc: 
     31            #print "ValueError was correctly raised: "+str(msg) 
     32            assert str(exc).startswith('wrong parameter') 
     33        else: 
     34            raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 
     35 
     36    def fit_single(self, fitter_name, isdream=False): 
     37        fitter = Fit(fitter_name) 
     38 
     39        data = Loader().load("testdata_line.txt") 
     40        data.name = data.filename 
     41        fitter.set_data(data,1) 
     42 
    2743        # Receives the type of model for the fitting 
    2844        model1  = LineModel() 
     
    3046        model = Model(model1,data) 
    3147        #fit with scipy test 
    32          
    33         pars1= ['param1','param2'] 
    34         fitter.set_data(data,1) 
    35         try:fitter.set_model(model,1,pars1) 
    36         except ValueError,exc: 
    37             #print "ValueError was correctly raised: "+str(msg) 
    38             assert str(exc).startswith('wrong parameter') 
    39         else: raise AssertionError("No error raised for scipy fitting with wrong parameters name to fit") 
     48 
    4049        pars1= ['A','B'] 
    4150        fitter.set_model(model,1,pars1) 
    4251        fitter.select_problem_for_fit(id=1,value=1) 
    43         result1, = fitter.fit() 
    44  
    45         self.assertTrue( math.fabs(result1.pvec[0]-4)/3 <= result1.stderr[0] ) 
    46         self.assertTrue( math.fabs(result1.pvec[1]-2.5)/3 <= result1.stderr[1]) 
    47         self.assertTrue( result1.fitness/len(data.x) < 2 ) 
    48  
    49         return 
    50         #fit with park test 
    51         fitter = Fit('park') 
    52         fitter.set_data(data,1) 
    53         fitter.set_model(model,1,pars1) 
    54         fitter.select_problem_for_fit(id=1,value=1) 
    55         result2, = fitter.fit() 
    56          
    57         self.assert_(result2) 
    58         self.assertTrue( math.fabs(result2.pvec[0]-4)/3 <= result2.stderr[0] )  
    59         self.assertTrue( math.fabs(result2.pvec[1]-2.5)/3 <= result2.stderr[1] ) 
    60         self.assertTrue( result2.fitness/len(data.x) < 2) 
    61         # compare fit result result for scipy and park 
    62         self.assertAlmostEquals( result1.pvec[0], result2.pvec[0] ) 
    63         self.assertAlmostEquals( result1.pvec[1],result2.pvec[1] ) 
    64         self.assertAlmostEquals( result1.stderr[0],result2.stderr[0] ) 
    65         self.assertAlmostEquals( result1.stderr[1],result2.stderr[1] ) 
    66         self.assertAlmostEquals( result1.fitness, 
    67                                  result2.fitness/len(data.x),1 ) 
    68          
     52        result1, = fitter.fit(handler=FitHandler()) 
     53 
     54        # The target values were generated from the following statements 
     55        p,s,fx = result1.pvec, result1.stderr, result1.fitness 
     56        #print "p0,p1,s0,s1,fx_ = %g, %g, %g, %g, %g"%(p[0],p[1],s[0],s[1],fx) 
     57        p0,p1,s0,s1,fx_ = 3.68353, 2.61004, 0.336186, 0.105244, 1.20189 
     58 
     59        if isdream: 
     60            # Dream is not a minimizer: just check that the fit is within 
     61            # uncertainty 
     62            self.assertTrue( abs(p[0]-p0) <= s0 ) 
     63            self.assertTrue( abs(p[1]-p1) <= s1 ) 
     64        else: 
     65            self.assertTrue( abs(p[0]-p0) <= 1e-5 ) 
     66            self.assertTrue( abs(p[1]-p1) <= 1e-5 ) 
     67            self.assertTrue( abs(fx-fx_) <= 1e-2 ) 
     68 
     69    def fit_bumps(self, alg, **opts): 
     70        #Importing the Fit module 
     71        from bumps import fitters 
     72        fitters.FIT_DEFAULT = alg 
     73        fitters.FIT_OPTIONS[alg].options.update(opts) 
     74        fitters.FIT_OPTIONS[alg].options.update(monitors=[]) 
     75        #print "fitting",alg,opts 
     76        #kprint "options",fitters.FIT_OPTIONS[alg].__dict__ 
     77        self.fit_single('bumps', isdream=(alg=='dream')) 
     78 
     79    def test_bumps_de(self): 
     80        self.fit_bumps('de') 
     81 
     82    def test_bumps_dream(self): 
     83        self.fit_bumps('dream', burn=500, steps=100) 
     84 
     85    def test_bumps_amoeba(self): 
     86        self.fit_bumps('amoeba') 
     87 
     88    def test_bumps_newton(self): 
     89        self.fit_bumps('newton') 
     90 
     91    def test_scipy(self): 
     92        #print "fitting scipy" 
     93        self.fit_single('scipy') 
     94 
     95    def test_park(self): 
     96        #print "fitting park" 
     97        self.fit_single('park') 
     98 
    6999         
    70100    def test2(self): 
     
    97127        fitter.select_problem_for_fit(id=2,value=0) 
    98128         
    99         try: result1, = fitter.fit() 
     129        try: result1, = fitter.fit(handler=FitHandler()) 
    100130        except RuntimeError,msg: 
    101131           assert str(msg)=="No Assembly scheduled for Scipy fitting." 
     
    103133        fitter.select_problem_for_fit(id=1,value=1) 
    104134        fitter.select_problem_for_fit(id=2,value=1) 
    105         try: result1, = fitter.fit() 
     135        try: result1, = fitter.fit(handler=FitHandler()) 
    106136        except RuntimeError,msg: 
    107137           assert str(msg)=="Scipy can't fit more than a single fit problem at a time." 
    108138        else: raise AssertionError,"No error raised for scipy fitting with more than 2 models" 
    109139 
    110         return 
    111140        #fit with park test 
    112141        fitter = Fit('park') 
     
    117146        fitter.select_problem_for_fit(id=1,value=1) 
    118147        fitter.select_problem_for_fit(id=2,value=1) 
    119         R1,R2 = fitter.fit() 
     148        R1,R2 = fitter.fit(handler=FitHandler()) 
    120149         
    121150        self.assertTrue( math.fabs(R1.pvec[0]-4)/3 <= R1.stderr[0] ) 
     
    126155    def test3(self): 
    127156        """ fit 2 data and 2 model with 1 constrainst""" 
    128         return 
    129157        #load data 
    130158        l = Loader() 
     
    163191        fitter.select_problem_for_fit(id=2,value=1) 
    164192         
    165         R1,R2 = fitter.fit() 
     193        R1,R2 = fitter.fit(handler=FitHandler()) 
    166194        self.assertTrue( math.fabs(R1.pvec[0]-4.0)/3. <= R1.stderr[0]) 
    167195        self.assertTrue( math.fabs(R1.pvec[1]-2.5)/3. <= R1.stderr[1]) 
     
    194222        fitter.select_problem_for_fit(id=1,value=1) 
    195223         
    196         result1, = fitter.fit() 
     224        result1, = fitter.fit(handler=FitHandler()) 
    197225        #print(result1) 
    198226        self.assert_(result1) 
     
    202230        self.assertTrue( result1.fitness/len(data1.x) < 2 ) 
    203231 
    204         return 
    205232        #fit with park test 
    206233        fitter = Fit('park') 
     
    209236        fitter.set_data(data2,1,qmin=1,qmax=10) 
    210237        fitter.select_problem_for_fit(id=1,value=1) 
    211         result2, = fitter.fit() 
     238        result2, = fitter.fit(handler=FitHandler()) 
    212239         
    213240        self.assert_(result2) 
Note: See TracChangeset for help on using the changeset viewer.