[aa36f96] | 1 | |
---|
| 2 | |
---|
[792db7d5] | 3 | """ |
---|
[aa36f96] | 4 | ScipyFitting module contains FitArrange , ScipyFit, |
---|
| 5 | Parameter classes.All listed classes work together to perform a |
---|
| 6 | simple fit with scipy optimizer. |
---|
[792db7d5] | 7 | """ |
---|
[61cb28d] | 8 | |
---|
[88b5e83] | 9 | import numpy |
---|
[7705306] | 10 | from scipy import optimize |
---|
| 11 | |
---|
[b2f25dc5] | 12 | from sans.fit.AbstractFitEngine import FitEngine |
---|
| 13 | from sans.fit.AbstractFitEngine import SansAssembly |
---|
[c4d6900] | 14 | #from sans.fit.AbstractFitEngine import FitAbort |
---|
[61cb28d] | 15 | |
---|
[e0072082] | 16 | class fitresult(object): |
---|
[48882d1] | 17 | """ |
---|
[aa36f96] | 18 | Storing fit result |
---|
[48882d1] | 19 | """ |
---|
[c4d6900] | 20 | def __init__(self, model=None, param_list=None): |
---|
[89f3b66] | 21 | self.calls = None |
---|
| 22 | self.fitness = None |
---|
| 23 | self.chisqr = None |
---|
| 24 | self.pvec = None |
---|
| 25 | self.cov = None |
---|
| 26 | self.info = None |
---|
| 27 | self.mesg = None |
---|
| 28 | self.success = None |
---|
| 29 | self.stderr = None |
---|
[e0072082] | 30 | self.parameters = None |
---|
| 31 | self.model = model |
---|
[c4d6900] | 32 | self.param_list = param_list |
---|
[e0072082] | 33 | |
---|
| 34 | def set_model(self, model): |
---|
[aa36f96] | 35 | """ |
---|
| 36 | """ |
---|
[e0072082] | 37 | self.model = model |
---|
| 38 | |
---|
[90c9cdf] | 39 | def set_fitness(self, fitness): |
---|
[aa36f96] | 40 | """ |
---|
| 41 | """ |
---|
[90c9cdf] | 42 | self.fitness = fitness |
---|
| 43 | |
---|
[e0072082] | 44 | def __str__(self): |
---|
[aa36f96] | 45 | """ |
---|
| 46 | """ |
---|
[b2f25dc5] | 47 | if self.pvec == None and self.model is None and self.param_list is None: |
---|
[e0072082] | 48 | return "No results" |
---|
| 49 | n = len(self.model.parameterset) |
---|
| 50 | |
---|
| 51 | result_param = zip(xrange(n), self.model.parameterset) |
---|
[c4d6900] | 52 | msg = ["P%-3d %s......|.....%s" % (p[0], p[1], p[1].value)\ |
---|
[b2f25dc5] | 53 | for p in result_param if p[1].name in self.param_list] |
---|
[c4d6900] | 54 | msg.append("=== goodness of fit: %s" % (str(self.fitness))) |
---|
| 55 | return "\n".join(msg) |
---|
[48882d1] | 56 | |
---|
[e0072082] | 57 | def print_summary(self): |
---|
[aa36f96] | 58 | """ |
---|
| 59 | """ |
---|
[e0072082] | 60 | print self |
---|
[88b5e83] | 61 | |
---|
[4c718654] | 62 | class ScipyFit(FitEngine): |
---|
[7705306] | 63 | """ |
---|
[aa36f96] | 64 | ScipyFit performs the Fit.This class can be used as follow: |
---|
| 65 | #Do the fit SCIPY |
---|
| 66 | create an engine: engine = ScipyFit() |
---|
| 67 | Use data must be of type plottable |
---|
| 68 | Use a sans model |
---|
| 69 | |
---|
| 70 | Add data with a dictionnary of FitArrangeDict where Uid is a key and data |
---|
| 71 | is saved in FitArrange object. |
---|
| 72 | engine.set_data(data,Uid) |
---|
| 73 | |
---|
| 74 | Set model parameter "M1"= model.name add {model.parameter.name:value}. |
---|
| 75 | |
---|
| 76 | :note: Set_param() if used must always preceded set_model() |
---|
| 77 | for the fit to be performed.In case of Scipyfit set_param is called in |
---|
| 78 | fit () automatically. |
---|
| 79 | |
---|
| 80 | engine.set_param( model,"M1", {'A':2,'B':4}) |
---|
| 81 | |
---|
| 82 | Add model with a dictionnary of FitArrangeDict{} where Uid is a key and model |
---|
| 83 | is save in FitArrange object. |
---|
| 84 | engine.set_model(model,Uid) |
---|
| 85 | |
---|
| 86 | engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] |
---|
| 87 | chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax) |
---|
[7705306] | 88 | """ |
---|
[792db7d5] | 89 | def __init__(self): |
---|
| 90 | """ |
---|
[b2f25dc5] | 91 | Creates a dictionary (self.fit_arrange_dict={})of FitArrange elements |
---|
[aa36f96] | 92 | with Uid as keys |
---|
[792db7d5] | 93 | """ |
---|
[b2f25dc5] | 94 | FitEngine.__init__(self) |
---|
| 95 | self.fit_arrange_dict = {} |
---|
| 96 | self.param_list = [] |
---|
[c4d6900] | 97 | self.curr_thread = None |
---|
[d9dc518] | 98 | #def fit(self, *args, **kw): |
---|
| 99 | # return profile(self._fit, *args, **kw) |
---|
[393f0f3] | 100 | |
---|
[e0072082] | 101 | def fit(self, q=None, handler=None, curr_thread=None): |
---|
[aa36f96] | 102 | """ |
---|
| 103 | """ |
---|
[89f3b66] | 104 | fitproblem = [] |
---|
[c4d6900] | 105 | for fproblem in self.fit_arrange_dict.itervalues(): |
---|
[89f3b66] | 106 | if fproblem.get_to_fit() == 1: |
---|
[393f0f3] | 107 | fitproblem.append(fproblem) |
---|
[89f3b66] | 108 | if len(fitproblem) > 1 : |
---|
[e0072082] | 109 | msg = "Scipy can't fit more than a single fit problem at a time." |
---|
| 110 | raise RuntimeError, msg |
---|
[a9e04aa] | 111 | return |
---|
[89f3b66] | 112 | elif len(fitproblem) == 0 : |
---|
[a9e04aa] | 113 | raise RuntimeError, "No Assembly scheduled for Scipy fitting." |
---|
| 114 | return |
---|
| 115 | |
---|
[89f3b66] | 116 | listdata = [] |
---|
[393f0f3] | 117 | model = fitproblem[0].get_model() |
---|
| 118 | listdata = fitproblem[0].get_data() |
---|
[792db7d5] | 119 | # Concatenate dList set (contains one or more data)before fitting |
---|
[e0072082] | 120 | data = listdata |
---|
[89f3b66] | 121 | self.curr_thread = curr_thread |
---|
[c4d6900] | 122 | result = fitresult(model=model, param_list=self.param_list) |
---|
[e0072082] | 123 | if handler is not None: |
---|
| 124 | handler.set_result(result=result) |
---|
[fd6b789] | 125 | #try: |
---|
[b2f25dc5] | 126 | functor = SansAssembly(self.param_list, model, data, handler=handler, |
---|
[89f3b66] | 127 | fitresult=result, curr_thread= self.curr_thread) |
---|
[c4d6900] | 128 | out, cov_x, _, _, success = optimize.leastsq(functor, |
---|
| 129 | model.get_params(self.param_list), |
---|
| 130 | full_output=1, |
---|
| 131 | warning=True) |
---|
[e71440c] | 132 | |
---|
[c4d6900] | 133 | #chisqr = functor.chisq(out) |
---|
| 134 | chisqr = functor.chisq() |
---|
[fd6b789] | 135 | if cov_x is not None and numpy.isfinite(cov_x).all(): |
---|
| 136 | stderr = numpy.sqrt(numpy.diag(cov_x)) |
---|
| 137 | else: |
---|
[e0072082] | 138 | stderr = None |
---|
[c4d6900] | 139 | if not (numpy.isnan(out).any()) or (cov_x != None): |
---|
| 140 | result.fitness = chisqr |
---|
| 141 | result.stderr = stderr |
---|
| 142 | result.pvec = out |
---|
| 143 | result.success = success |
---|
| 144 | #print result |
---|
| 145 | if q is not None: |
---|
| 146 | #print "went here" |
---|
| 147 | q.put(result) |
---|
| 148 | #print "get q scipy fit enfine",q.get() |
---|
| 149 | return q |
---|
| 150 | return result |
---|
[fd6b789] | 151 | else: |
---|
[89f3b66] | 152 | raise ValueError, "SVD did not converge" + str(success) |
---|
[e0072082] | 153 | |
---|
[d8a2e31] | 154 | |
---|
| 155 | |
---|
[c4d6900] | 156 | #def profile(fn, *args, **kw): |
---|
| 157 | # import cProfile, pstats, os |
---|
| 158 | # global call_result |
---|
| 159 | # def call(): |
---|
| 160 | # global call_result |
---|
| 161 | # call_result = fn(*args, **kw) |
---|
| 162 | # cProfile.runctx('call()', dict(call=call), {}, 'profile.out') |
---|
| 163 | # stats = pstats.Stats('profile.out') |
---|
| 164 | # stats.sort_stats('time') |
---|
| 165 | # stats.sort_stats('calls') |
---|
| 166 | # stats.print_stats() |
---|
| 167 | # os.unlink('profile.out') |
---|
| 168 | # return call_result |
---|
[9c648c7] | 169 | |
---|
[48882d1] | 170 | |
---|