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