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 | |
---|
13 | from Loader import Load |
---|
14 | from AbstractFitEngine import FitEngine |
---|
15 | |
---|
16 | |
---|
17 | class ParkFit(FitEngine): |
---|
18 | """ |
---|
19 | ParkFit performs the Fit.This class can be used as follow: |
---|
20 | #Do the fit Park |
---|
21 | create an engine: engine = ParkFit() |
---|
22 | Use data must be of type plottable |
---|
23 | Use a sans model |
---|
24 | |
---|
25 | Add data with a dictionnary of FitArrangeList where Uid is a key and data |
---|
26 | is saved in FitArrange object. |
---|
27 | engine.set_data(data,Uid) |
---|
28 | |
---|
29 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
30 | @note: Set_param() if used must always preceded set_model() |
---|
31 | for the fit to be performed. |
---|
32 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
33 | |
---|
34 | Add model with a dictionnary of FitArrangeList{} where Uid is a key and model |
---|
35 | is save in FitArrange object. |
---|
36 | engine.set_model(model,Uid) |
---|
37 | |
---|
38 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
39 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
40 | @note: {model.parameter.name:value} is ignored in fit function since |
---|
41 | the user should make sure to call set_param himself. |
---|
42 | """ |
---|
43 | def __init__(self): |
---|
44 | """ |
---|
45 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
46 | with Uid as keys |
---|
47 | """ |
---|
48 | self.fitArrangeDict={} |
---|
49 | self.paramList=[] |
---|
50 | |
---|
51 | def createAssembly(self): |
---|
52 | """ |
---|
53 | Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange} |
---|
54 | Create parkmodel and park data ,form a list couple of parkmodel and parkdata |
---|
55 | create an assembly self.problem= park.Assembly([(parkmodel,parkdata)]) |
---|
56 | """ |
---|
57 | mylist=[] |
---|
58 | listmodel=[] |
---|
59 | i=0 |
---|
60 | fitproblems=[] |
---|
61 | for id ,fproblem in self.fitArrangeDict.iteritems(): |
---|
62 | if fproblem.get_to_fit()==1: |
---|
63 | fitproblems.append(fproblem) |
---|
64 | |
---|
65 | if len(fitproblems)==0 : |
---|
66 | raise RuntimeError, "No Assembly scheduled for Park fitting." |
---|
67 | return |
---|
68 | for item in fitproblems: |
---|
69 | parkmodel = item.get_model() |
---|
70 | for p in parkmodel.parameterset: |
---|
71 | if p._getname()in self.paramList and not p.iscomputed(): |
---|
72 | p.status = 'fitted' # make it a fitted parameter |
---|
73 | #iscomputed paramter with string inside |
---|
74 | |
---|
75 | i+=1 |
---|
76 | Ldata=item.get_data() |
---|
77 | #parkdata=self._concatenateData(Ldata) |
---|
78 | parkdata=Ldata |
---|
79 | fitness=(parkmodel,parkdata) |
---|
80 | mylist.append(fitness) |
---|
81 | |
---|
82 | self.problem = park.Assembly(mylist) |
---|
83 | |
---|
84 | |
---|
85 | def fit(self, qmin=None, qmax=None): |
---|
86 | """ |
---|
87 | Performs fit with park.fit module.It can perform fit with one model |
---|
88 | and a set of data, more than two fit of one model and sets of data or |
---|
89 | fit with more than two model associated with their set of data and constraints |
---|
90 | |
---|
91 | |
---|
92 | @param pars: Dictionary of parameter names for the model and their values. |
---|
93 | @param qmin: The minimum value of data's range to be fit |
---|
94 | @param qmax: The maximum value of data's range to be fit |
---|
95 | @note:all parameter are ignored most of the time.Are just there to keep ScipyFit |
---|
96 | and ParkFit interface the same. |
---|
97 | @return result.fitness: Value of the goodness of fit metric |
---|
98 | @return result.pvec: list of parameter with the best value found during fitting |
---|
99 | @return result.cov: Covariance matrix |
---|
100 | """ |
---|
101 | self.createAssembly() |
---|
102 | |
---|
103 | localfit = FitSimplex() |
---|
104 | localfit.ftol = 1e-8 |
---|
105 | # fitmc(fitness,localfit,n,handler): |
---|
106 | #Run a monte carlo fit. |
---|
107 | #This procedure maps a local optimizer across a set of n initial points. |
---|
108 | #The initial parameter value defined by the fitness parameters defines |
---|
109 | #one initial point. The remainder are randomly generated within the |
---|
110 | #bounds of the problem. |
---|
111 | #localfit is the local optimizer to use. It should be a bounded |
---|
112 | #optimizer following the `park.fitmc.LocalFit` interface. |
---|
113 | #handler accepts updates to the current best set of fit parameters. |
---|
114 | # See `park.fitresult.FitHandler` for details. |
---|
115 | fitter = FitMC(localfit=localfit) |
---|
116 | #result = fit.fit(self.problem, |
---|
117 | # fitter=fitter, |
---|
118 | # handler= GuiUpdate(window)) |
---|
119 | result = fit.fit(self.problem, |
---|
120 | fitter=fitter, |
---|
121 | handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) |
---|
122 | #handler = fitresult.ConsoleUpdate(improvement_delta=0.1) |
---|
123 | #models=self.problem |
---|
124 | #service=None |
---|
125 | #if models is None: raise RuntimeError('fit expected a list of models') |
---|
126 | #from park.fit import LocalQueue,FitJob |
---|
127 | #if service is None: service = LocalQueue() |
---|
128 | #if fitter is None: fitter = fitmc.FitMC() |
---|
129 | #if handler is None: handler = fitresult.FitHandler() |
---|
130 | |
---|
131 | #objective = assembly.Assembly(models) if isinstance(models,list) else models |
---|
132 | #job = FitJob(self.problem,fitter,handler) |
---|
133 | #service.start(job) |
---|
134 | #import wx |
---|
135 | #while not self.job.handler.done: |
---|
136 | # time.sleep(interval) |
---|
137 | # wx.Yield() |
---|
138 | #result=service.job.handler.result |
---|
139 | |
---|
140 | if result !=None: |
---|
141 | return result |
---|
142 | else: |
---|
143 | raise ValueError, "SVD did not converge" |
---|
144 | |
---|
145 | |
---|
146 | |
---|
147 | |
---|
148 | |
---|