[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 |
---|
[61cb28d] | 8 | |
---|
[7705306] | 9 | from Loader import Load |
---|
| 10 | from scipy import optimize |
---|
| 11 | |
---|
[48882d1] | 12 | from AbstractFitEngine import FitEngine, sansAssembly |
---|
[61cb28d] | 13 | |
---|
[48882d1] | 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 | |
---|
[ca6d914] | 37 | Add data with a dictionnary of FitArrangeDict where Uid is a key and data |
---|
[792db7d5] | 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 | |
---|
[ca6d914] | 47 | Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model |
---|
[792db7d5] | 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 | """ |
---|
[ca6d914] | 56 | Creates a dictionary (self.fitArrangeDict={})of FitArrange elements |
---|
[792db7d5] | 57 | with Uid as keys |
---|
| 58 | """ |
---|
[ca6d914] | 59 | self.fitArrangeDict={} |
---|
[ee5b04c] | 60 | self.paramList=[] |
---|
[4dd63eb] | 61 | def fit(self,qmin=None, qmax=None): |
---|
[a9e04aa] | 62 | # Protect against simultanous fitting attempts |
---|
| 63 | #if len(self.fitArrangeDict)>1: |
---|
| 64 | # raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." |
---|
[792db7d5] | 65 | # fitproblem contains first fitArrange object(one model and a list of data) |
---|
[a9e04aa] | 66 | #list of fitproblem |
---|
| 67 | fitproblem=[] |
---|
| 68 | for id ,fproblem in self.fitArrangeDict.iteritems(): |
---|
| 69 | print "ScipyFitting:fproblem.get_to_fit() ",fproblem.get_to_fit() |
---|
| 70 | if fproblem.get_to_fit()==1: |
---|
| 71 | fitproblem.append(fproblem) |
---|
| 72 | if len(fitproblem)>1 : |
---|
| 73 | raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." |
---|
| 74 | return |
---|
| 75 | elif len(fitproblem)==0 : |
---|
| 76 | raise RuntimeError, "No Assembly scheduled for Scipy fitting." |
---|
| 77 | return |
---|
| 78 | |
---|
[7705306] | 79 | listdata=[] |
---|
[a9e04aa] | 80 | model = fitproblem[0].get_model() |
---|
| 81 | listdata = fitproblem[0].get_data() |
---|
[792db7d5] | 82 | # Concatenate dList set (contains one or more data)before fitting |
---|
[7d0c1a8] | 83 | #data=self._concatenateData( listdata) |
---|
| 84 | data=listdata |
---|
[792db7d5] | 85 | #Assign a fit range is not boundaries were given |
---|
[f8ce013] | 86 | if not hasattr(data, 'image'): |
---|
[7d0c1a8] | 87 | if qmin==None: |
---|
| 88 | qmin= min(data.x) |
---|
| 89 | if qmax==None: |
---|
| 90 | qmax= max(data.x) |
---|
| 91 | else: |
---|
| 92 | if qmin==None: |
---|
| 93 | qmin= numpy.min(data.image) |
---|
| 94 | if qmax==None: |
---|
| 95 | qmax= numpy.max(data.image) |
---|
[e71440c] | 96 | functor= sansAssembly(self.paramList,model,data) |
---|
[48882d1] | 97 | out, cov_x, info, mesg, success = optimize.leastsq(functor,model.getParams(self.paramList), full_output=1, warning=True) |
---|
| 98 | chisqr = functor.chisq(out) |
---|
[e71440c] | 99 | |
---|
[ca6d914] | 100 | if cov_x is not None and numpy.isfinite(cov_x).all(): |
---|
| 101 | stderr = numpy.sqrt(numpy.diag(cov_x)) |
---|
[e71440c] | 102 | else: |
---|
| 103 | stderr=None |
---|
[ca6d914] | 104 | if not (numpy.isnan(out).any()) or ( cov_x !=None) : |
---|
[48882d1] | 105 | result = fitresult() |
---|
| 106 | result.fitness = chisqr |
---|
[ca6d914] | 107 | result.stderr = stderr |
---|
[48882d1] | 108 | result.pvec = out |
---|
| 109 | result.success =success |
---|
| 110 | |
---|
| 111 | return result |
---|
| 112 | else: |
---|
[ca6d914] | 113 | raise ValueError, "SVD did not converge"+str(success) |
---|
[7705306] | 114 | |
---|
[48882d1] | 115 | |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | |
---|