[792db7d5] | 1 | """ |
---|
| 2 | @organization: ScipyFitting module contains FitArrange , ScipyFit, |
---|
| 3 | Parameter classes.All listed classes work together to perform a |
---|
| 4 | simple fit with scipy optimizer. |
---|
| 5 | """ |
---|
[48882d1] | 6 | #import scipy.linalg |
---|
| 7 | import numpy |
---|
[7705306] | 8 | from sans.guitools.plottables import Data1D |
---|
| 9 | from Loader import Load |
---|
| 10 | from scipy import optimize |
---|
| 11 | |
---|
[48882d1] | 12 | from AbstractFitEngine import FitEngine, sansAssembly |
---|
| 13 | from AbstractFitEngine import FitArrange,Data |
---|
| 14 | class fitresult: |
---|
| 15 | """ |
---|
| 16 | Storing fit result |
---|
| 17 | """ |
---|
| 18 | calls = None |
---|
| 19 | fitness = None |
---|
| 20 | chisqr = None |
---|
| 21 | pvec = None |
---|
| 22 | cov = None |
---|
| 23 | info = None |
---|
| 24 | mesg = None |
---|
| 25 | success = None |
---|
| 26 | stderr = None |
---|
| 27 | parameters= None |
---|
| 28 | |
---|
[4c718654] | 29 | class ScipyFit(FitEngine): |
---|
[7705306] | 30 | """ |
---|
[792db7d5] | 31 | ScipyFit performs the Fit.This class can be used as follow: |
---|
| 32 | #Do the fit SCIPY |
---|
| 33 | create an engine: engine = ScipyFit() |
---|
| 34 | Use data must be of type plottable |
---|
| 35 | Use a sans model |
---|
| 36 | |
---|
| 37 | Add data with a dictionnary of FitArrangeList where Uid is a key and data |
---|
| 38 | is saved in FitArrange object. |
---|
| 39 | engine.set_data(data,Uid) |
---|
| 40 | |
---|
| 41 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
| 42 | @note: Set_param() if used must always preceded set_model() |
---|
| 43 | for the fit to be performed.In case of Scipyfit set_param is called in |
---|
| 44 | fit () automatically. |
---|
| 45 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
| 46 | |
---|
| 47 | Add model with a dictionnary of FitArrangeList{} where Uid is a key and model |
---|
| 48 | is save in FitArrange object. |
---|
| 49 | engine.set_model(model,Uid) |
---|
| 50 | |
---|
| 51 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
| 52 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
[7705306] | 53 | """ |
---|
[792db7d5] | 54 | def __init__(self): |
---|
| 55 | """ |
---|
| 56 | Creates a dictionary (self.fitArrangeList={})of FitArrange elements |
---|
| 57 | with Uid as keys |
---|
| 58 | """ |
---|
[7705306] | 59 | self.fitArrangeList={} |
---|
[ee5b04c] | 60 | self.paramList=[] |
---|
[4dd63eb] | 61 | def fit(self,qmin=None, qmax=None): |
---|
[48882d1] | 62 | # Protect against simultanous fitting attempts |
---|
[0eb801a] | 63 | if len(self.fitArrangeList)>1: |
---|
| 64 | raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." |
---|
| 65 | |
---|
[792db7d5] | 66 | # fitproblem contains first fitArrange object(one model and a list of data) |
---|
[7705306] | 67 | fitproblem=self.fitArrangeList.values()[0] |
---|
| 68 | listdata=[] |
---|
| 69 | model = fitproblem.get_model() |
---|
| 70 | listdata = fitproblem.get_data() |
---|
[792db7d5] | 71 | # Concatenate dList set (contains one or more data)before fitting |
---|
[48882d1] | 72 | data=self._concatenateData( listdata) |
---|
[792db7d5] | 73 | #Assign a fit range is not boundaries were given |
---|
[7705306] | 74 | if qmin==None: |
---|
[48882d1] | 75 | qmin= min(data.x) |
---|
[7705306] | 76 | if qmax==None: |
---|
[48882d1] | 77 | qmax= max(data.x) |
---|
| 78 | functor= sansAssembly(model,data) |
---|
| 79 | print "scipyfitting:param list",model.getParams(self.paramList) |
---|
| 80 | print "scipyfitting:functor",functor(model.getParams(self.paramList)) |
---|
[7705306] | 81 | |
---|
[48882d1] | 82 | out, cov_x, info, mesg, success = optimize.leastsq(functor,model.getParams(self.paramList), full_output=1, warning=True) |
---|
| 83 | chisqr = functor.chisq(out) |
---|
[7705306] | 84 | |
---|
[48882d1] | 85 | print "scipyfitting: info",mesg |
---|
| 86 | print"scipyfitting : success",success |
---|
| 87 | print "scipyfitting: out", out |
---|
| 88 | print "scipyfitting: cov_x", cov_x |
---|
| 89 | print "scipyfitting: chisqr", chisqr |
---|
[7705306] | 90 | |
---|
[48882d1] | 91 | if not (numpy.isnan(out).any()): |
---|
| 92 | result = fitresult() |
---|
| 93 | result.fitness = chisqr |
---|
| 94 | result.cov = cov_x |
---|
| 95 | |
---|
| 96 | result.pvec = out |
---|
| 97 | result.success =success |
---|
| 98 | |
---|
| 99 | return result |
---|
| 100 | else: |
---|
| 101 | raise ValueError, "SVD did not converge" |
---|
[7705306] | 102 | |
---|
[48882d1] | 103 | |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|