source: sasview/park_integration/ParkFitting.py @ 0b12abb5

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 0b12abb5 was aa36f96, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on documentation

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