source: sasview/park_integration/ParkFitting.py @ cbaa2f4

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

working on pylint

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