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 | def createFunctor(self): |
---|
63 | fitproblem=None |
---|
64 | fit_count = 0 |
---|
65 | #print " self.fitArrangeDict", self.fitArrangeDict |
---|
66 | for id, fproblem in self.fitArrangeDict.iteritems(): |
---|
67 | if fproblem.get_to_fit() == 1: |
---|
68 | fitproblem = fproblem |
---|
69 | fit_count += 1 |
---|
70 | if fit_count > 1 : |
---|
71 | raise RuntimeError, "Scipy can't fit more than a single fit problem at a time." |
---|
72 | return |
---|
73 | elif fit_count == 0 : |
---|
74 | raise RuntimeError, "No Assembly scheduled for Scipy fitting." |
---|
75 | return |
---|
76 | |
---|
77 | listdata = [] |
---|
78 | model = fitproblem.get_model().clone() |
---|
79 | data = fitproblem.get_data() |
---|
80 | |
---|
81 | # Concatenate dList set (contains one or more data)before fitting |
---|
82 | #data=self._concatenateData( listdata) |
---|
83 | #data = listdata |
---|
84 | #self.curr_thread= curr_thread |
---|
85 | |
---|
86 | #try: |
---|
87 | functor = SansAssembly(self.paramList, model, data)#, curr_thread= curr_thread) |
---|
88 | return functor , model |
---|
89 | |
---|
90 | def fit(self, q=None, handler=None, curr_thread=None): |
---|
91 | |
---|
92 | functor , model = self.createFunctor() |
---|
93 | |
---|
94 | out, cov_x, info, mesg, success = optimize.leastsq(functor, |
---|
95 | model.getParams(self.paramList), |
---|
96 | full_output=1, warning=True) |
---|
97 | |
---|
98 | chisqr = functor.chisq(out) |
---|
99 | |
---|
100 | if cov_x is not None and numpy.isfinite(cov_x).all(): |
---|
101 | stderr = numpy.sqrt(numpy.diag(cov_x)) |
---|
102 | else: |
---|
103 | stderr=None |
---|
104 | if not (numpy.isnan(out).any()) or ( cov_x !=None) : |
---|
105 | result = fitresult() |
---|
106 | result.fitness = chisqr |
---|
107 | result.stderr = stderr |
---|
108 | result.pvec = out |
---|
109 | result.success = success |
---|
110 | if q is not None: |
---|
111 | print "went here" |
---|
112 | q.put(result) |
---|
113 | print "get q scipy fit enfine",q.get() |
---|
114 | return q |
---|
115 | return result |
---|
116 | else: |
---|
117 | raise ValueError, "SVD did not converge" + str(success) |
---|
118 | #except FitAbort: |
---|
119 | ## fit engine is stop |
---|
120 | # return None |
---|
121 | |
---|
122 | #except: |
---|
123 | # raise |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | def profile(fn, *args, **kw): |
---|
128 | import cProfile, pstats, os |
---|
129 | global call_result |
---|
130 | def call(): |
---|
131 | global call_result |
---|
132 | call_result = fn(*args, **kw) |
---|
133 | cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
134 | stats = pstats.Stats('profile.out') |
---|
135 | #stats.sort_stats('time') |
---|
136 | stats.sort_stats('calls') |
---|
137 | stats.print_stats() |
---|
138 | os.unlink('profile.out') |
---|
139 | return call_result |
---|
140 | |
---|
141 | |
---|