source: sasview/park_integration/ParkFitting.py @ 6d48919

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

working on documentation

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[aa36f96]1
2
3
[792db7d5]4"""
[aa36f96]5ParkFitting module contains SansParameter,Model,Data
6FitArrange, ParkFit,Parameter classes.All listed classes work together to perform a
7simple fit with park optimizer.
[792db7d5]8"""
[7705306]9import time
10import numpy
11import park
12from park import fit,fitresult
13from park import assembly
[cf3b781]14from park.fitmc import FitSimplex, FitMC
[7d0c1a8]15
[61cb28d]16#from Loader import Load
[7d0c1a8]17from AbstractFitEngine import FitEngine
[d4b0687]18
[fadea71]19
[4c718654]20class ParkFit(FitEngine):
[7705306]21    """
[aa36f96]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
[792db7d5]46        the user should make sure to call set_param himself.
[aa36f96]47       
[7705306]48    """
[916a15f]49    def __init__(self):
[792db7d5]50        """
[aa36f96]51        Creates a dictionary (self.fitArrangeList={})of FitArrange elements
52        with Uid as keys
[792db7d5]53        """
[ca6d914]54        self.fitArrangeDict={}
[ee5b04c]55        self.paramList=[]
[37d9521]56       
[ca6d914]57    def createAssembly(self):
[7705306]58        """
[792db7d5]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)])
[7705306]62        """
63        mylist=[]
[9e85792]64        listmodel=[]
[37d9521]65        i=0
[a9e04aa]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()
[9e85792]76            for p in parkmodel.parameterset:
[fe886ee]77                ## does not allow status change for constraint parameters
[aed7c57]78                if p.status!= 'computed':
79                    if p._getname()in item.pars:
[fe886ee]80                        ## make parameters selected for fit will be between boundaries
81                        p.set( p.range )
82                               
[aed7c57]83                    else:
84                        p.status= 'fixed'
85             
[916a15f]86            i+=1
[a9e04aa]87            Ldata=item.get_data()
[7d0c1a8]88            #parkdata=self._concatenateData(Ldata)
89            parkdata=Ldata
[ca6d914]90            fitness=(parkmodel,parkdata)
91            mylist.append(fitness)
[126a761]92       
[cf3b781]93        self.problem =  park.Assembly(mylist)
[792db7d5]94       
[fd6b789]95    def fit(self,q=None,handler=None, curr_thread= None):
[7705306]96        """
[aa36f96]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
[792db7d5]106            and ParkFit interface the same.
[aa36f96]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       
[7705306]112        """
[ca6d914]113        self.createAssembly()
[916a15f]114   
[cf3b781]115        localfit = FitSimplex()
116        localfit.ftol = 1e-8
[681f0dc]117       
[916a15f]118        # See `park.fitresult.FitHandler` for details.
[9c648c7]119        fitter = FitMC(localfit=localfit, start_points=1)
[681f0dc]120        if handler == None:
121            handler= fitresult.ConsoleUpdate(improvement_delta=0.1)
[aed7c57]122     
[9c648c7]123           
[ee5b04c]124        result = fit.fit(self.problem,
[fadea71]125                         fitter=fitter,
[681f0dc]126                         handler= handler)
[8296ff5]127        self.problem.all_results(result)
[ee5b04c]128        if result !=None:
[fd6b789]129            if q !=None:
130                q.put(result)
131                return q
[48882d1]132            return result
[ee5b04c]133        else:
134            raise ValueError, "SVD did not converge"
[aa36f96]135           
Note: See TracBrowser for help on using the repository browser.