source: sasview/park_integration/ParkFitting.py @ f118fe2f

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 f118fe2f was 89f3b66, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working pylint

  • Property mode set to 100644
File size: 4.7 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"""
9#import time
10#import 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        if len(fitproblems) == 0: 
71            raise RuntimeError, "No Assembly scheduled for Park fitting."
72            return
73        for item in fitproblems:
74            parkmodel = item.get_model()
75            for p in parkmodel.parameterset:
76                ## does not allow status change for constraint parameters
77                if p.status != 'computed':
78                    if p._getname()in item.pars:
79                        ## make parameters selected for fit will be between boundaries
80                        p.set(p.range)         
81                    else:
82                        p.status = 'fixed'
83            i += 1
84            Ldata = item.get_data()
85            #parkdata=self._concatenateData(Ldata)
86            parkdata = Ldata
87            fitness = (parkmodel, parkdata)
88            mylist.append(fitness)
89        self.problem = park.Assembly(mylist)
90       
91    def fit(self, q=None, handler=None, curr_thread=None):
92        """
93        Performs fit with park.fit module.It can  perform fit with one model
94        and a set of data, more than two fit of  one model and sets of data or
95        fit with more than two model associated with their set of data and constraints
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       
101        :note: all parameter are ignored most of the time.Are just there to keep ScipyFit
102            and ParkFit interface the same.
103           
104        :return: result.fitness Value of the goodness of fit metric
105        :return: result.pvec list of parameter with the best value found during fitting
106        :return: result.cov Covariance matrix
107       
108        """
109        self.createAssembly()
110        localfit = FitSimplex()
111        localfit.ftol = 1e-8
112       
113        # See `park.fitresult.FitHandler` for details.
114        fitter = FitMC(localfit=localfit, start_points=1)
115        if handler == None:
116            handler = fitresult.ConsoleUpdate(improvement_delta=0.1)
117        result = fit.fit(self.problem,
118                         fitter=fitter,
119                         handler=handler)
120        self.problem.all_results(result)
121        if result != None:
122            if q != None:
123                q.put(result)
124                return q
125            return result
126        else:
127            raise ValueError, "SVD did not converge"
128           
Note: See TracBrowser for help on using the repository browser.