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 | |
---|
7 | import numpy |
---|
8 | from scipy import optimize |
---|
9 | |
---|
10 | from AbstractFitEngine import FitEngine, SansAssembly,FitAbort |
---|
11 | |
---|
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 | |
---|
27 | |
---|
28 | class ScipyFit(FitEngine): |
---|
29 | """ |
---|
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 | |
---|
36 | Add data with a dictionnary of FitArrangeDict where Uid is a key and data |
---|
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 | |
---|
46 | Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model |
---|
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) |
---|
52 | """ |
---|
53 | def __init__(self): |
---|
54 | """ |
---|
55 | Creates a dictionary (self.fitArrangeDict={})of FitArrange elements |
---|
56 | with Uid as keys |
---|
57 | """ |
---|
58 | self.fitArrangeDict={} |
---|
59 | self.paramList=[] |
---|
60 | #def fit(self, *args, **kw): |
---|
61 | # return profile(self._fit, *args, **kw) |
---|
62 | |
---|
63 | def fit(self ,handler=None,curr_thread= None): |
---|
64 | |
---|
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 | |
---|
76 | listdata=[] |
---|
77 | model = fitproblem[0].get_model() |
---|
78 | listdata = fitproblem[0].get_data() |
---|
79 | # Concatenate dList set (contains one or more data)before fitting |
---|
80 | #data=self._concatenateData( listdata) |
---|
81 | data=listdata |
---|
82 | self.curr_thread= curr_thread |
---|
83 | |
---|
84 | try: |
---|
85 | functor= SansAssembly(self.paramList,model,data, curr_thread= self.curr_thread) |
---|
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 |
---|
99 | result.success = success |
---|
100 | return result |
---|
101 | else: |
---|
102 | raise ValueError, "SVD did not converge"+str(success) |
---|
103 | except FitAbort: |
---|
104 | ## fit engine is stop |
---|
105 | return None |
---|
106 | |
---|
107 | except: |
---|
108 | raise |
---|
109 | |
---|
110 | def profile(fn, *args, **kw): |
---|
111 | import cProfile, pstats, os |
---|
112 | global call_result |
---|
113 | def call(): |
---|
114 | global call_result |
---|
115 | call_result = fn(*args, **kw) |
---|
116 | cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
117 | stats = pstats.Stats('profile.out') |
---|
118 | #stats.sort_stats('time') |
---|
119 | stats.sort_stats('calls') |
---|
120 | stats.print_stats() |
---|
121 | os.unlink('profile.out') |
---|
122 | return call_result |
---|
123 | |
---|
124 | |
---|