source: sasview/park_integration/ParkFitting.py @ 9bc4ed23

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 9bc4ed23 was d9dc518, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

park_integration: remove profiling from committed code. remove non-standard setup default.

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[792db7d5]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"""
[7705306]6import time
7import numpy
8import park
9from park import fit,fitresult
10from park import assembly
[cf3b781]11from park.fitmc import FitSimplex, FitMC
[7d0c1a8]12
[61cb28d]13#from Loader import Load
[7d0c1a8]14from AbstractFitEngine import FitEngine
[d4b0687]15
[fadea71]16
[4c718654]17class ParkFit(FitEngine):
[7705306]18    """
[792db7d5]19        ParkFit performs the Fit.This class can be used as follow:
20        #Do the fit Park
21        create an engine: engine = ParkFit()
22        Use data must be of type plottable
23        Use a sans model
24       
25        Add data with a dictionnary of FitArrangeList where Uid is a key and data
26        is saved in FitArrange object.
27        engine.set_data(data,Uid)
28       
29        Set model parameter "M1"= model.name add {model.parameter.name:value}.
30        @note: Set_param() if used must always preceded set_model()
31             for the fit to be performed.
32        engine.set_param( model,"M1", {'A':2,'B':4})
33       
34        Add model with a dictionnary of FitArrangeList{} where Uid is a key and model
35        is save in FitArrange object.
36        engine.set_model(model,Uid)
37       
38        engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]]
39        chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax)
40        @note: {model.parameter.name:value} is ignored in fit function since
41        the user should make sure to call set_param himself.
[7705306]42    """
[916a15f]43    def __init__(self):
[792db7d5]44        """
45            Creates a dictionary (self.fitArrangeList={})of FitArrange elements
46            with Uid as keys
47        """
[ca6d914]48        self.fitArrangeDict={}
[ee5b04c]49        self.paramList=[]
[37d9521]50       
[ca6d914]51    def createAssembly(self):
[7705306]52        """
[792db7d5]53        Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange}
54        Create parkmodel and park data ,form a list couple of parkmodel and parkdata
55        create an assembly self.problem=  park.Assembly([(parkmodel,parkdata)])
[7705306]56        """
57        mylist=[]
[9e85792]58        listmodel=[]
[37d9521]59        i=0
[a9e04aa]60        fitproblems=[]
61        for id ,fproblem in self.fitArrangeDict.iteritems():
62            if fproblem.get_to_fit()==1:
63                fitproblems.append(fproblem)
64               
65        if len(fitproblems)==0 : 
66            raise RuntimeError, "No Assembly scheduled for Park fitting."
67            return
68        for item in fitproblems:
69            parkmodel = item.get_model()
[9e85792]70            for p in parkmodel.parameterset:
[fe886ee]71                ## does not allow status change for constraint parameters
[aed7c57]72                if p.status!= 'computed':
73                    if p._getname()in item.pars:
[fe886ee]74                        ## make parameters selected for fit will be between boundaries
75                        p.set( p.range )
76                               
[aed7c57]77                    else:
78                        p.status= 'fixed'
79             
[916a15f]80            i+=1
[a9e04aa]81            Ldata=item.get_data()
[7d0c1a8]82            #parkdata=self._concatenateData(Ldata)
83            parkdata=Ldata
[ca6d914]84            fitness=(parkmodel,parkdata)
85            mylist.append(fitness)
[126a761]86       
[cf3b781]87        self.problem =  park.Assembly(mylist)
[792db7d5]88       
[7705306]89   
[d9dc518]90    def fit(self,handler=None, curr_thread= None):
[7705306]91        """
[792db7d5]92            Performs fit with park.fit module.It can  perform fit with one model
93            and a set of data, more than two fit of  one model and sets of data or
94            fit with more than two model associated with their set of data and constraints
95           
96           
97            @param pars: Dictionary of parameter names for the model and their values.
98            @param qmin: The minimum value of data's range to be fit
99            @param qmax: The maximum value of data's range to be fit
100            @note:all parameter are ignored most of the time.Are just there to keep ScipyFit
101            and ParkFit interface the same.
102            @return result.fitness: Value of the goodness of fit metric
103            @return result.pvec: list of parameter with the best value found during fitting
104            @return result.cov: Covariance matrix
[7705306]105        """
[ca6d914]106        self.createAssembly()
[916a15f]107   
[cf3b781]108        localfit = FitSimplex()
109        localfit.ftol = 1e-8
[681f0dc]110       
[916a15f]111        # See `park.fitresult.FitHandler` for details.
[9c648c7]112        fitter = FitMC(localfit=localfit, start_points=1)
[681f0dc]113        if handler == None:
114            handler= fitresult.ConsoleUpdate(improvement_delta=0.1)
[aed7c57]115     
[9c648c7]116           
[ee5b04c]117        result = fit.fit(self.problem,
[fadea71]118                         fitter=fitter,
[681f0dc]119                         handler= handler)
[8296ff5]120        self.problem.all_results(result)
[ee5b04c]121        if result !=None:
[48882d1]122            return result
[ee5b04c]123        else:
124            raise ValueError, "SVD did not converge"
125           
[8296ff5]126
[d8a2e31]127 
[7705306]128   
[d4b0687]129   
Note: See TracBrowser for help on using the repository browser.