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.fitArrangeList={} |
---|
48 | self.paramList=[] |
---|
49 | |
---|
50 | def createProblem(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 | print "ParkFitting: In createproblem" |
---|
57 | mylist=[] |
---|
58 | listmodel=[] |
---|
59 | i=0 |
---|
60 | for k,value in self.fitArrangeList.iteritems(): |
---|
61 | #sansmodel=value.get_model() |
---|
62 | #wrap sans model |
---|
63 | #parkmodel = Model(sansmodel) |
---|
64 | parkmodel = value.get_model() |
---|
65 | #print "ParkFitting: createproblem: just create a model",parkmodel.parameterset |
---|
66 | for p in parkmodel.parameterset: |
---|
67 | #self.param_list.append(p._getname()) |
---|
68 | #if p.isfixed(): |
---|
69 | #print 'parameters',p.name |
---|
70 | print "parkfitting: self.paramList",self.paramList |
---|
71 | if p.isfixed() and p._getname()in self.paramList: |
---|
72 | p.set([-numpy.inf,numpy.inf]) |
---|
73 | i+=1 |
---|
74 | Ldata=value.get_data() |
---|
75 | parkdata=self._concatenateData(Ldata) |
---|
76 | |
---|
77 | couple=(parkmodel,parkdata) |
---|
78 | #print "Parkfitting: fitness",couple |
---|
79 | mylist.append(couple) |
---|
80 | #print "mylist",mylist |
---|
81 | self.problem = park.Assembly(mylist) |
---|
82 | |
---|
83 | |
---|
84 | def fit(self, qmin=None, qmax=None): |
---|
85 | """ |
---|
86 | Performs fit with park.fit module.It can perform fit with one model |
---|
87 | and a set of data, more than two fit of one model and sets of data or |
---|
88 | fit with more than two model associated with their set of data and constraints |
---|
89 | |
---|
90 | |
---|
91 | @param pars: Dictionary of parameter names for the model and their values. |
---|
92 | @param qmin: The minimum value of data's range to be fit |
---|
93 | @param qmax: The maximum value of data's range to be fit |
---|
94 | @note:all parameter are ignored most of the time.Are just there to keep ScipyFit |
---|
95 | and ParkFit interface the same. |
---|
96 | @return result.fitness: Value of the goodness of fit metric |
---|
97 | @return result.pvec: list of parameter with the best value found during fitting |
---|
98 | @return result.cov: Covariance matrix |
---|
99 | """ |
---|
100 | #from numpy.linalg.linalg.LinAlgError import LinAlgError |
---|
101 | #print "Parkfitting: fit method probably breaking just right before \ |
---|
102 | #call fit" |
---|
103 | self.createProblem() |
---|
104 | pars=self.problem.fit_parameters() |
---|
105 | self.problem.eval() |
---|
106 | #print "M0.B",self.problem[1].parameterset['B'].value,self.problem[0].parameterset['B'].value |
---|
107 | |
---|
108 | localfit = FitSimplex() |
---|
109 | localfit.ftol = 1e-8 |
---|
110 | fitter = FitMC(localfit=localfit) |
---|
111 | print "ParkFitting: result1" |
---|
112 | result = fit.fit(self.problem, |
---|
113 | fitter=fitter, |
---|
114 | handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) |
---|
115 | print "ParkFitting: result",result |
---|
116 | if result !=None: |
---|
117 | #for p in result.parameters: |
---|
118 | # print "fit in park fitting", p.name, p.value,p.stderr |
---|
119 | #return result.fitness,result.pvec,result.cov,result |
---|
120 | return result |
---|
121 | else: |
---|
122 | raise ValueError, "SVD did not converge" |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|