source: sasview/park_integration/ParkFitting.py @ e7430b6

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since e7430b6 was 388309d, checked in by Gervaise Alina <gervyh@…>, 16 years ago

park_integration working better for large model

  • Property mode set to 100644
File size: 5.7 KB
Line 
1"""
2    @organization: ParkFitting module contains SansParameter,Model,Data
3    FitArrange, ParkFit,Parameter classes.All listed classes work together to perform a
4    simple fit with park optimizer.
5"""
6import time
7import numpy
8import park
9from park import fit,fitresult
10from park import assembly
11from park.fitmc import FitSimplex, FitMC
12from sans.guitools.plottables import Data1D
13from Loader import Load
14from AbstractFitEngine import FitEngine,FitArrange,Model
15
16class ParkFit(FitEngine):
17    """
18        ParkFit performs the Fit.This class can be used as follow:
19        #Do the fit Park
20        create an engine: engine = ParkFit()
21        Use data must be of type plottable
22        Use a sans model
23       
24        Add data with a dictionnary of FitArrangeList where Uid is a key and data
25        is saved in FitArrange object.
26        engine.set_data(data,Uid)
27       
28        Set model parameter "M1"= model.name add {model.parameter.name:value}.
29        @note: Set_param() if used must always preceded set_model()
30             for the fit to be performed.
31        engine.set_param( model,"M1", {'A':2,'B':4})
32       
33        Add model with a dictionnary of FitArrangeList{} where Uid is a key and model
34        is save in FitArrange object.
35        engine.set_model(model,Uid)
36       
37        engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]]
38        chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax)
39        @note: {model.parameter.name:value} is ignored in fit function since
40        the user should make sure to call set_param himself.
41    """
42    def __init__(self,data=[]):
43        """
44            Creates a dictionary (self.fitArrangeList={})of FitArrange elements
45            with Uid as keys
46        """
47        self.fitArrangeList={}
48        self.paramList=[]
49       
50    def createProblem(self):
51        """
52        Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange}
53        Create parkmodel and park data ,form a list couple of parkmodel and parkdata
54        create an assembly self.problem=  park.Assembly([(parkmodel,parkdata)])
55        """
56        print "ParkFitting: In createproblem"
57        mylist=[]
58        listmodel=[]
59        i=0
60        for k,value in self.fitArrangeList.iteritems():
61            #sansmodel=value.get_model()
62            #wrap sans model
63            #parkmodel = Model(sansmodel)
64            parkmodel = value.get_model()
65            #print "ParkFitting: createproblem: just create a model",parkmodel.parameterset
66            for p in parkmodel.parameterset:
67                #self.param_list.append(p._getname())
68                #if p.isfixed():
69                #print 'parameters',p.name
70                #print "parkfitting: self.paramList",self.paramList
71                if p.isfixed() and p._getname()in self.paramList:
72                #if p.isfixed():
73                    p.set([-numpy.inf,numpy.inf])
74            i+=1   
75            Ldata=value.get_data()
76            parkdata=self._concatenateData(Ldata)
77           
78            couple=(parkmodel,parkdata)
79            #print "Parkfitting: fitness",couple   
80            mylist.append(couple)
81        #print "mylist",mylist
82        self.problem =  park.Assembly(mylist)
83       
84   
85    def fit(self, qmin=None, qmax=None):
86        """
87            Performs fit with park.fit module.It can  perform fit with one model
88            and a set of data, more than two fit of  one model and sets of data or
89            fit with more than two model associated with their set of data and constraints
90           
91           
92            @param pars: Dictionary of parameter names for the model and their values.
93            @param qmin: The minimum value of data's range to be fit
94            @param qmax: The maximum value of data's range to be fit
95            @note:all parameter are ignored most of the time.Are just there to keep ScipyFit
96            and ParkFit interface the same.
97            @return result.fitness: Value of the goodness of fit metric
98            @return result.pvec: list of parameter with the best value found during fitting
99            @return result.cov: Covariance matrix
100        """
101        #from numpy.linalg.linalg.LinAlgError import LinAlgError
102        #print "Parkfitting: fit method probably breaking just right before \
103        #call fit"
104        self.createProblem()
105        pars=self.problem.fit_parameters()
106        self.problem.eval()
107        #print "M0.B",self.problem[1].parameterset['B'].value,self.problem[0].parameterset['B'].value
108
109        localfit = FitSimplex()
110        localfit.ftol = 1e-8
111        #localfit.ftol = 1e-6
112        fitter = FitMC(localfit=localfit)
113        print "ParkFitting: result1",pars
114        print "Parkfitting: in fit function fitness resid",self.problem[0].residuals()
115       
116        list=self.problem[0]._parameterset()
117        print "Parkfitting: in fit function fitness paramset",list
118        for item in list:
119            print "Parkfitting: in fit function fitness",item.name, item.value,item.path,item.range
120        result = fit.fit(self.problem,
121                     fitter=fitter,
122                     handler= fitresult.ConsoleUpdate(improvement_delta=0.1))
123        #result = fit.fit(self.problem)
124        print "ParkFitting: result",result.fitness,result.pvec,result.cov
125        if result !=None:
126            #for p in result.parameters:
127            #    print "fit in park fitting", p.name, p.value,p.stderr
128            #return result.fitness,result.pvec,result.cov,result
129            return result
130        else:
131            raise ValueError, "SVD did not converge"
132           
133       
134       
135   
136   
Note: See TracBrowser for help on using the repository browser.