[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 | |
---|
[4c718654] | 16 | class ParkFit(FitEngine): |
---|
[7705306] | 17 | """ |
---|
[792db7d5] | 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. |
---|
[7705306] | 41 | """ |
---|
| 42 | def __init__(self,data=[]): |
---|
[792db7d5] | 43 | """ |
---|
| 44 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
| 45 | with Uid as keys |
---|
| 46 | """ |
---|
[7705306] | 47 | self.fitArrangeList={} |
---|
[ee5b04c] | 48 | self.paramList=[] |
---|
[37d9521] | 49 | |
---|
[4dd63eb] | 50 | def createProblem(self): |
---|
[7705306] | 51 | """ |
---|
[792db7d5] | 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)]) |
---|
[7705306] | 55 | """ |
---|
[7924042] | 56 | print "ParkFitting: In createproblem" |
---|
[7705306] | 57 | mylist=[] |
---|
[9e85792] | 58 | listmodel=[] |
---|
[37d9521] | 59 | i=0 |
---|
[7705306] | 60 | for k,value in self.fitArrangeList.iteritems(): |
---|
[48882d1] | 61 | #sansmodel=value.get_model() |
---|
[792db7d5] | 62 | #wrap sans model |
---|
[48882d1] | 63 | #parkmodel = Model(sansmodel) |
---|
| 64 | parkmodel = value.get_model() |
---|
[6b126e8] | 65 | #print "ParkFitting: createproblem: just create a model",parkmodel.parameterset |
---|
[9e85792] | 66 | for p in parkmodel.parameterset: |
---|
[202f93a] | 67 | #self.param_list.append(p._getname()) |
---|
[37d9521] | 68 | #if p.isfixed(): |
---|
| 69 | #print 'parameters',p.name |
---|
[48882d1] | 70 | print "parkfitting: self.paramList",self.paramList |
---|
[37d9521] | 71 | if p.isfixed() and p._getname()in self.paramList: |
---|
[9e85792] | 72 | p.set([-numpy.inf,numpy.inf]) |
---|
[37d9521] | 73 | i+=1 |
---|
[7705306] | 74 | Ldata=value.get_data() |
---|
[48882d1] | 75 | parkdata=self._concatenateData(Ldata) |
---|
| 76 | |
---|
[792db7d5] | 77 | couple=(parkmodel,parkdata) |
---|
[6b126e8] | 78 | #print "Parkfitting: fitness",couple |
---|
[7705306] | 79 | mylist.append(couple) |
---|
[6b126e8] | 80 | #print "mylist",mylist |
---|
[cf3b781] | 81 | self.problem = park.Assembly(mylist) |
---|
[792db7d5] | 82 | |
---|
[7705306] | 83 | |
---|
[4dd63eb] | 84 | def fit(self, qmin=None, qmax=None): |
---|
[7705306] | 85 | """ |
---|
[792db7d5] | 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 |
---|
[7705306] | 99 | """ |
---|
[7924042] | 100 | #from numpy.linalg.linalg.LinAlgError import LinAlgError |
---|
[6b126e8] | 101 | #print "Parkfitting: fit method probably breaking just right before \ |
---|
| 102 | #call fit" |
---|
[4dd63eb] | 103 | self.createProblem() |
---|
[cf3b781] | 104 | pars=self.problem.fit_parameters() |
---|
| 105 | self.problem.eval() |
---|
[9855699] | 106 | #print "M0.B",self.problem[1].parameterset['B'].value,self.problem[0].parameterset['B'].value |
---|
| 107 | |
---|
[cf3b781] | 108 | localfit = FitSimplex() |
---|
| 109 | localfit.ftol = 1e-8 |
---|
| 110 | fitter = FitMC(localfit=localfit) |
---|
[48882d1] | 111 | print "ParkFitting: result1" |
---|
[ee5b04c] | 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: |
---|
[6b126e8] | 117 | #for p in result.parameters: |
---|
| 118 | # print "fit in park fitting", p.name, p.value,p.stderr |
---|
[48882d1] | 119 | #return result.fitness,result.pvec,result.cov,result |
---|
| 120 | return result |
---|
[ee5b04c] | 121 | else: |
---|
| 122 | raise ValueError, "SVD did not converge" |
---|
| 123 | |
---|
| 124 | |
---|
[7924042] | 125 | |
---|
[7705306] | 126 | |
---|
[d4b0687] | 127 | |
---|