[792db7d5] | 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 | """ |
---|
[7705306] | 6 | import time |
---|
| 7 | import numpy |
---|
| 8 | import park |
---|
| 9 | from park import fit,fitresult |
---|
| 10 | from park import assembly |
---|
[cf3b781] | 11 | from park.fitmc import FitSimplex, FitMC |
---|
[7705306] | 12 | from sans.guitools.plottables import Data1D |
---|
| 13 | from Loader import Load |
---|
[48882d1] | 14 | from AbstractFitEngine import FitEngine,FitArrange,Model |
---|
[d4b0687] | 15 | |
---|
[fadea71] | 16 | |
---|
[4c718654] | 17 | class ParkFit(FitEngine): |
---|
[7705306] | 18 | """ |
---|
[792db7d5] | 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. |
---|
[7705306] | 42 | """ |
---|
[916a15f] | 43 | def __init__(self): |
---|
[792db7d5] | 44 | """ |
---|
| 45 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
| 46 | with Uid as keys |
---|
| 47 | """ |
---|
[ca6d914] | 48 | self.fitArrangeDict={} |
---|
[ee5b04c] | 49 | self.paramList=[] |
---|
[37d9521] | 50 | |
---|
[ca6d914] | 51 | def createAssembly(self): |
---|
[7705306] | 52 | """ |
---|
[792db7d5] | 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)]) |
---|
[7705306] | 56 | """ |
---|
| 57 | mylist=[] |
---|
[9e85792] | 58 | listmodel=[] |
---|
[37d9521] | 59 | i=0 |
---|
[ca6d914] | 60 | for k,value in self.fitArrangeDict.iteritems(): |
---|
[48882d1] | 61 | parkmodel = value.get_model() |
---|
[9e85792] | 62 | for p in parkmodel.parameterset: |
---|
[916a15f] | 63 | if p._getname()in self.paramList and not p.iscomputed(): |
---|
| 64 | p.status = 'fitted' # make it a fitted parameter |
---|
| 65 | #iscomputed paramter with string inside |
---|
| 66 | |
---|
| 67 | i+=1 |
---|
[7705306] | 68 | Ldata=value.get_data() |
---|
[48882d1] | 69 | parkdata=self._concatenateData(Ldata) |
---|
| 70 | |
---|
[ca6d914] | 71 | fitness=(parkmodel,parkdata) |
---|
| 72 | mylist.append(fitness) |
---|
| 73 | |
---|
[cf3b781] | 74 | self.problem = park.Assembly(mylist) |
---|
[792db7d5] | 75 | |
---|
[7705306] | 76 | |
---|
[4dd63eb] | 77 | def fit(self, qmin=None, qmax=None): |
---|
[7705306] | 78 | """ |
---|
[792db7d5] | 79 | Performs fit with park.fit module.It can perform fit with one model |
---|
| 80 | and a set of data, more than two fit of one model and sets of data or |
---|
| 81 | fit with more than two model associated with their set of data and constraints |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | @param pars: Dictionary of parameter names for the model and their values. |
---|
| 85 | @param qmin: The minimum value of data's range to be fit |
---|
| 86 | @param qmax: The maximum value of data's range to be fit |
---|
| 87 | @note:all parameter are ignored most of the time.Are just there to keep ScipyFit |
---|
| 88 | and ParkFit interface the same. |
---|
| 89 | @return result.fitness: Value of the goodness of fit metric |
---|
| 90 | @return result.pvec: list of parameter with the best value found during fitting |
---|
| 91 | @return result.cov: Covariance matrix |
---|
[7705306] | 92 | """ |
---|
[ca6d914] | 93 | self.createAssembly() |
---|
[916a15f] | 94 | |
---|
[cf3b781] | 95 | localfit = FitSimplex() |
---|
| 96 | localfit.ftol = 1e-8 |
---|
[916a15f] | 97 | # fitmc(fitness,localfit,n,handler): |
---|
| 98 | #Run a monte carlo fit. |
---|
| 99 | #This procedure maps a local optimizer across a set of n initial points. |
---|
| 100 | #The initial parameter value defined by the fitness parameters defines |
---|
| 101 | #one initial point. The remainder are randomly generated within the |
---|
| 102 | #bounds of the problem. |
---|
| 103 | #localfit is the local optimizer to use. It should be a bounded |
---|
| 104 | #optimizer following the `park.fitmc.LocalFit` interface. |
---|
| 105 | #handler accepts updates to the current best set of fit parameters. |
---|
| 106 | # See `park.fitresult.FitHandler` for details. |
---|
[cf3b781] | 107 | fitter = FitMC(localfit=localfit) |
---|
[fadea71] | 108 | #result = fit.fit(self.problem, |
---|
| 109 | # fitter=fitter, |
---|
| 110 | # handler= GuiUpdate(window)) |
---|
[ee5b04c] | 111 | result = fit.fit(self.problem, |
---|
[fadea71] | 112 | fitter=fitter, |
---|
| 113 | handler= fitresult.ConsoleUpdate(improvement_delta=0.1)) |
---|
[ee5b04c] | 114 | if result !=None: |
---|
[48882d1] | 115 | return result |
---|
[ee5b04c] | 116 | else: |
---|
| 117 | raise ValueError, "SVD did not converge" |
---|
| 118 | |
---|
| 119 | |
---|
[7924042] | 120 | |
---|
[7705306] | 121 | |
---|
[d4b0687] | 122 | |
---|