[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 | """ |
---|
[61cb28d] | 6 | |
---|
[88b5e83] | 7 | import numpy |
---|
[7705306] | 8 | from scipy import optimize |
---|
| 9 | |
---|
[342d9197] | 10 | from AbstractFitEngine import FitEngine, SansAssembly,FitAbort |
---|
[61cb28d] | 11 | |
---|
[48882d1] | 12 | class fitresult: |
---|
| 13 | """ |
---|
| 14 | Storing fit result |
---|
| 15 | """ |
---|
| 16 | calls = None |
---|
| 17 | fitness = None |
---|
| 18 | chisqr = None |
---|
| 19 | pvec = None |
---|
| 20 | cov = None |
---|
| 21 | info = None |
---|
| 22 | mesg = None |
---|
| 23 | success = None |
---|
| 24 | stderr = None |
---|
| 25 | parameters= None |
---|
| 26 | |
---|
[88b5e83] | 27 | |
---|
[4c718654] | 28 | class ScipyFit(FitEngine): |
---|
[7705306] | 29 | """ |
---|
[792db7d5] | 30 | ScipyFit performs the Fit.This class can be used as follow: |
---|
| 31 | #Do the fit SCIPY |
---|
| 32 | create an engine: engine = ScipyFit() |
---|
| 33 | Use data must be of type plottable |
---|
| 34 | Use a sans model |
---|
| 35 | |
---|
[ca6d914] | 36 | Add data with a dictionnary of FitArrangeDict where Uid is a key and data |
---|
[792db7d5] | 37 | is saved in FitArrange object. |
---|
| 38 | engine.set_data(data,Uid) |
---|
| 39 | |
---|
| 40 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
| 41 | @note: Set_param() if used must always preceded set_model() |
---|
| 42 | for the fit to be performed.In case of Scipyfit set_param is called in |
---|
| 43 | fit () automatically. |
---|
| 44 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
| 45 | |
---|
[ca6d914] | 46 | Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model |
---|
[792db7d5] | 47 | is save in FitArrange object. |
---|
| 48 | engine.set_model(model,Uid) |
---|
| 49 | |
---|
| 50 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
| 51 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
[7705306] | 52 | """ |
---|
[792db7d5] | 53 | def __init__(self): |
---|
| 54 | """ |
---|
[ca6d914] | 55 | Creates a dictionary (self.fitArrangeDict={})of FitArrange elements |
---|
[792db7d5] | 56 | with Uid as keys |
---|
| 57 | """ |
---|
[ca6d914] | 58 | self.fitArrangeDict={} |
---|
[ee5b04c] | 59 | self.paramList=[] |
---|
[d9dc518] | 60 | #def fit(self, *args, **kw): |
---|
| 61 | # return profile(self._fit, *args, **kw) |
---|
[9c648c7] | 62 | |
---|
[d9dc518] | 63 | def fit(self ,handler=None,curr_thread= None): |
---|
[681f0dc] | 64 | |
---|
[a9e04aa] | 65 | fitproblem=[] |
---|
| 66 | for id ,fproblem in self.fitArrangeDict.iteritems(): |
---|
| 67 | if fproblem.get_to_fit()==1: |
---|
| 68 | fitproblem.append(fproblem) |
---|
| 69 | if len(fitproblem)>1 : |
---|
| 70 | raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." |
---|
| 71 | return |
---|
| 72 | elif len(fitproblem)==0 : |
---|
| 73 | raise RuntimeError, "No Assembly scheduled for Scipy fitting." |
---|
| 74 | return |
---|
| 75 | |
---|
[7705306] | 76 | listdata=[] |
---|
[a9e04aa] | 77 | model = fitproblem[0].get_model() |
---|
| 78 | listdata = fitproblem[0].get_data() |
---|
[792db7d5] | 79 | # Concatenate dList set (contains one or more data)before fitting |
---|
[7d0c1a8] | 80 | #data=self._concatenateData( listdata) |
---|
| 81 | data=listdata |
---|
[4bd557d] | 82 | self.curr_thread= curr_thread |
---|
[e71440c] | 83 | |
---|
[4bd557d] | 84 | try: |
---|
[70bf68c] | 85 | functor= SansAssembly(self.paramList,model,data, curr_thread= self.curr_thread) |
---|
[4bd557d] | 86 | out, cov_x, info, mesg, success = optimize.leastsq(functor,model.getParams(self.paramList), full_output=1, warning=True) |
---|
| 87 | |
---|
| 88 | chisqr = functor.chisq(out) |
---|
| 89 | |
---|
| 90 | if cov_x is not None and numpy.isfinite(cov_x).all(): |
---|
| 91 | stderr = numpy.sqrt(numpy.diag(cov_x)) |
---|
| 92 | else: |
---|
| 93 | stderr=None |
---|
| 94 | if not (numpy.isnan(out).any()) or ( cov_x !=None) : |
---|
| 95 | result = fitresult() |
---|
| 96 | result.fitness = chisqr |
---|
| 97 | result.stderr = stderr |
---|
| 98 | result.pvec = out |
---|
[88b5e83] | 99 | result.success = success |
---|
[4bd557d] | 100 | return result |
---|
| 101 | else: |
---|
| 102 | raise ValueError, "SVD did not converge"+str(success) |
---|
| 103 | except FitAbort: |
---|
| 104 | ## fit engine is stop |
---|
[6963aa3] | 105 | return None |
---|
[7705306] | 106 | |
---|
[4bd557d] | 107 | except: |
---|
[88b5e83] | 108 | raise |
---|
[48882d1] | 109 | |
---|
[d8a2e31] | 110 | |
---|
| 111 | |
---|
[9c648c7] | 112 | def profile(fn, *args, **kw): |
---|
| 113 | import cProfile, pstats, os |
---|
| 114 | global call_result |
---|
| 115 | def call(): |
---|
| 116 | global call_result |
---|
| 117 | call_result = fn(*args, **kw) |
---|
| 118 | cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
| 119 | stats = pstats.Stats('profile.out') |
---|
| 120 | #stats.sort_stats('time') |
---|
| 121 | stats.sort_stats('calls') |
---|
| 122 | stats.print_stats() |
---|
| 123 | os.unlink('profile.out') |
---|
| 124 | return call_result |
---|
| 125 | |
---|
[48882d1] | 126 | |
---|