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 | """ |
---|
6 | import time |
---|
7 | import numpy |
---|
8 | import park |
---|
9 | from park import fit,fitresult |
---|
10 | from park import assembly |
---|
11 | from park.fitmc import FitSimplex, FitMC |
---|
12 | from sans.guitools.plottables import Data1D |
---|
13 | from Loader import Load |
---|
14 | from AbstractFitEngine import FitEngine,FitArrange,Model |
---|
15 | |
---|
16 | class ParkFit(FitEngine): |
---|
17 | """ |
---|
18 | ParkFit performs the Fit.This class can be used as follow: |
---|
19 | #Do the fit Park |
---|
20 | create an engine: engine = ParkFit() |
---|
21 | Use data must be of type plottable |
---|
22 | Use a sans model |
---|
23 | |
---|
24 | Add data with a dictionnary of FitArrangeList where Uid is a key and data |
---|
25 | is saved in FitArrange object. |
---|
26 | engine.set_data(data,Uid) |
---|
27 | |
---|
28 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
29 | @note: Set_param() if used must always preceded set_model() |
---|
30 | for the fit to be performed. |
---|
31 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
32 | |
---|
33 | Add model with a dictionnary of FitArrangeList{} where Uid is a key and model |
---|
34 | is save in FitArrange object. |
---|
35 | engine.set_model(model,Uid) |
---|
36 | |
---|
37 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
38 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
39 | @note: {model.parameter.name:value} is ignored in fit function since |
---|
40 | the user should make sure to call set_param himself. |
---|
41 | """ |
---|
42 | def __init__(self,data=[]): |
---|
43 | """ |
---|
44 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
45 | with Uid as keys |
---|
46 | """ |
---|
47 | self.fitArrangeDict={} |
---|
48 | self.paramList=[] |
---|
49 | |
---|
50 | def createAssembly(self): |
---|
51 | """ |
---|
52 | Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange} |
---|
53 | Create parkmodel and park data ,form a list couple of parkmodel and parkdata |
---|
54 | create an assembly self.problem= park.Assembly([(parkmodel,parkdata)]) |
---|
55 | """ |
---|
56 | mylist=[] |
---|
57 | listmodel=[] |
---|
58 | i=0 |
---|
59 | for k,value in self.fitArrangeDict.iteritems(): |
---|
60 | parkmodel = value.get_model() |
---|
61 | for p in parkmodel.parameterset: |
---|
62 | if p.isfixed() and p._getname()in self.paramList: |
---|
63 | p.set([-numpy.inf,numpy.inf]) |
---|
64 | i+=1 |
---|
65 | Ldata=value.get_data() |
---|
66 | parkdata=self._concatenateData(Ldata) |
---|
67 | |
---|
68 | fitness=(parkmodel,parkdata) |
---|
69 | mylist.append(fitness) |
---|
70 | |
---|
71 | self.problem = park.Assembly(mylist) |
---|
72 | |
---|
73 | |
---|
74 | def fit(self, qmin=None, qmax=None): |
---|
75 | """ |
---|
76 | Performs fit with park.fit module.It can perform fit with one model |
---|
77 | and a set of data, more than two fit of one model and sets of data or |
---|
78 | fit with more than two model associated with their set of data and constraints |
---|
79 | |
---|
80 | |
---|
81 | @param pars: Dictionary of parameter names for the model and their values. |
---|
82 | @param qmin: The minimum value of data's range to be fit |
---|
83 | @param qmax: The maximum value of data's range to be fit |
---|
84 | @note:all parameter are ignored most of the time.Are just there to keep ScipyFit |
---|
85 | and ParkFit interface the same. |
---|
86 | @return result.fitness: Value of the goodness of fit metric |
---|
87 | @return result.pvec: list of parameter with the best value found during fitting |
---|
88 | @return result.cov: Covariance matrix |
---|
89 | """ |
---|
90 | |
---|
91 | self.createAssembly() |
---|
92 | pars=self.problem.fit_parameters() |
---|
93 | self.problem.eval() |
---|
94 | localfit = FitSimplex() |
---|
95 | localfit.ftol = 1e-8 |
---|
96 | fitter = FitMC(localfit=localfit) |
---|
97 | list=self.problem[0]._parameterset() |
---|
98 | result = fit.fit(self.problem, |
---|
99 | fitter=fitter, |
---|
100 | handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) |
---|
101 | |
---|
102 | if result !=None: |
---|
103 | return result |
---|
104 | else: |
---|
105 | raise ValueError, "SVD did not converge" |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | |
---|