[6fe5100] | 1 | """ |
---|
| 2 | BumpsFitting module runs the bumps optimizer. |
---|
| 3 | """ |
---|
| 4 | import numpy |
---|
| 5 | |
---|
| 6 | from bumps import fitters |
---|
| 7 | from bumps.mapper import SerialMapper |
---|
[e3efa6b3] | 8 | from bumps import parameter |
---|
| 9 | from bumps.fitproblem import FitProblem |
---|
[6fe5100] | 10 | |
---|
[4e9f227] | 11 | from .AbstractFitEngine import FitEngine |
---|
| 12 | from .AbstractFitEngine import FResult |
---|
| 13 | from .expression import compile_constraints |
---|
[6fe5100] | 14 | |
---|
[85f17f6] | 15 | class BumpsMonitor(object): |
---|
| 16 | def __init__(self, handler, max_step=0): |
---|
| 17 | self.handler = handler |
---|
| 18 | self.max_step = max_step |
---|
[ed4aef2] | 19 | |
---|
[85f17f6] | 20 | def config_history(self, history): |
---|
| 21 | history.requires(time=1, value=2, point=1, step=1) |
---|
[ed4aef2] | 22 | |
---|
[85f17f6] | 23 | def __call__(self, history): |
---|
[e3efa6b3] | 24 | if self.handler is None: return |
---|
[85f17f6] | 25 | self.handler.progress(history.step[0], self.max_step) |
---|
| 26 | if len(history.step)>1 and history.step[1] > history.step[0]: |
---|
| 27 | self.handler.improvement() |
---|
| 28 | self.handler.update_fit() |
---|
| 29 | |
---|
[ed4aef2] | 30 | class ConvergenceMonitor(object): |
---|
| 31 | """ |
---|
| 32 | ConvergenceMonitor contains population summary statistics to show progress |
---|
| 33 | of the fit. This is a list [ (best, 0%, 25%, 50%, 75%, 100%) ] or |
---|
| 34 | just a list [ (best, ) ] if population size is 1. |
---|
| 35 | """ |
---|
| 36 | def __init__(self): |
---|
| 37 | self.convergence = [] |
---|
| 38 | |
---|
| 39 | def config_history(self, history): |
---|
| 40 | history.requires(value=1, population_values=1) |
---|
| 41 | |
---|
| 42 | def __call__(self, history): |
---|
| 43 | best = history.value[0] |
---|
| 44 | try: |
---|
| 45 | p = history.population_values[0] |
---|
| 46 | n,p = len(p), numpy.sort(p) |
---|
| 47 | QI,Qmid, = int(0.2*n),int(0.5*n) |
---|
| 48 | self.convergence.append((best, p[0],p[QI],p[Qmid],p[-1-QI],p[-1])) |
---|
| 49 | except: |
---|
[e3efa6b3] | 50 | self.convergence.append((best, best,best,best,best,best)) |
---|
[ed4aef2] | 51 | |
---|
[e3efa6b3] | 52 | |
---|
[4e9f227] | 53 | # Note: currently using bumps parameters for each parameter object so that |
---|
| 54 | # a SasFitness can be used directly in bumps with the usual semantics. |
---|
| 55 | # The disadvantage of this technique is that we need to copy every parameter |
---|
| 56 | # back into the model each time the function is evaluated. We could instead |
---|
| 57 | # define reference parameters for each sans parameter, but then we would not |
---|
| 58 | # be able to express constraints using python expressions in the usual way |
---|
| 59 | # from bumps, and would instead need to use string expressions. |
---|
[e3efa6b3] | 60 | class SasFitness(object): |
---|
[6fe5100] | 61 | """ |
---|
[e3efa6b3] | 62 | Wrap SAS model as a bumps fitness object |
---|
[6fe5100] | 63 | """ |
---|
[4e9f227] | 64 | def __init__(self, model, data, fitted=[], constraints={}, **kw): |
---|
| 65 | self.name = model.name |
---|
| 66 | self.model = model.model |
---|
[6fe5100] | 67 | self.data = data |
---|
[e3efa6b3] | 68 | self._define_pars() |
---|
| 69 | self._init_pars(kw) |
---|
[4e9f227] | 70 | self.constraints = dict(constraints) |
---|
[e3efa6b3] | 71 | self.set_fitted(fitted) |
---|
| 72 | self._dirty = True |
---|
| 73 | |
---|
| 74 | def _define_pars(self): |
---|
| 75 | self._pars = {} |
---|
| 76 | for k in self.model.getParamList(): |
---|
| 77 | name = ".".join((self.name,k)) |
---|
| 78 | value = self.model.getParam(k) |
---|
| 79 | bounds = self.model.details.get(k,["",None,None])[1:3] |
---|
| 80 | self._pars[k] = parameter.Parameter(value=value, bounds=bounds, |
---|
| 81 | fixed=True, name=name) |
---|
[4e9f227] | 82 | #print parameter.summarize(self._pars.values()) |
---|
[e3efa6b3] | 83 | |
---|
| 84 | def _init_pars(self, kw): |
---|
| 85 | for k,v in kw.items(): |
---|
| 86 | # dispersion parameters initialized with _field instead of .field |
---|
| 87 | if k.endswith('_width'): k = k[:-6]+'.width' |
---|
| 88 | elif k.endswith('_npts'): k = k[:-5]+'.npts' |
---|
| 89 | elif k.endswith('_nsigmas'): k = k[:-7]+'.nsigmas' |
---|
| 90 | elif k.endswith('_type'): k = k[:-5]+'.type' |
---|
| 91 | if k not in self._pars: |
---|
| 92 | formatted_pars = ", ".join(sorted(self._pars.keys())) |
---|
| 93 | raise KeyError("invalid parameter %r for %s--use one of: %s" |
---|
| 94 | %(k, self.model, formatted_pars)) |
---|
| 95 | if '.' in k and not k.endswith('.width'): |
---|
| 96 | self.model.setParam(k, v) |
---|
| 97 | elif isinstance(v, parameter.BaseParameter): |
---|
| 98 | self._pars[k] = v |
---|
| 99 | elif isinstance(v, (tuple,list)): |
---|
| 100 | low, high = v |
---|
| 101 | self._pars[k].value = (low+high)/2 |
---|
| 102 | self._pars[k].range(low,high) |
---|
[95d58d3] | 103 | else: |
---|
[e3efa6b3] | 104 | self._pars[k].value = v |
---|
| 105 | self.update() |
---|
| 106 | |
---|
| 107 | def set_fitted(self, param_list): |
---|
[6fe5100] | 108 | """ |
---|
[e3efa6b3] | 109 | Flag a set of parameters as fitted parameters. |
---|
[6fe5100] | 110 | """ |
---|
[e3efa6b3] | 111 | for k,p in self._pars.items(): |
---|
[4e9f227] | 112 | p.fixed = (k not in param_list or k in self.constraints) |
---|
[4a0dc427] | 113 | self.fitted_pars = [self._pars[k] for k in param_list if k not in self.constraints] |
---|
| 114 | self.fitted_par_names = [k for k in param_list if k not in self.constraints] |
---|
[6fe5100] | 115 | |
---|
[e3efa6b3] | 116 | # ===== Fitness interface ==== |
---|
| 117 | def parameters(self): |
---|
| 118 | return self._pars |
---|
[6fe5100] | 119 | |
---|
[e3efa6b3] | 120 | def update(self): |
---|
| 121 | for k,v in self._pars.items(): |
---|
[4e9f227] | 122 | #print "updating",k,v,v.value |
---|
[e3efa6b3] | 123 | self.model.setParam(k,v.value) |
---|
| 124 | self._dirty = True |
---|
[6fe5100] | 125 | |
---|
[e3efa6b3] | 126 | def _recalculate(self): |
---|
| 127 | if self._dirty: |
---|
| 128 | self._residuals, self._theory = self.data.residuals(self.model.evalDistribution) |
---|
| 129 | self._dirty = False |
---|
[6fe5100] | 130 | |
---|
[e3efa6b3] | 131 | def numpoints(self): |
---|
| 132 | return numpy.sum(self.data.idx) # number of fitted points |
---|
[6fe5100] | 133 | |
---|
[e3efa6b3] | 134 | def nllf(self): |
---|
| 135 | return 0.5*numpy.sum(self.residuals()**2) |
---|
| 136 | |
---|
| 137 | def theory(self): |
---|
| 138 | self._recalculate() |
---|
| 139 | return self._theory |
---|
| 140 | |
---|
| 141 | def residuals(self): |
---|
| 142 | self._recalculate() |
---|
| 143 | return self._residuals |
---|
| 144 | |
---|
| 145 | # Not implementing the data methods for now: |
---|
| 146 | # |
---|
| 147 | # resynth_data/restore_data/save/plot |
---|
[6fe5100] | 148 | |
---|
| 149 | class BumpsFit(FitEngine): |
---|
| 150 | """ |
---|
| 151 | Fit a model using bumps. |
---|
| 152 | """ |
---|
| 153 | def __init__(self): |
---|
| 154 | """ |
---|
| 155 | Creates a dictionary (self.fit_arrange_dict={})of FitArrange elements |
---|
| 156 | with Uid as keys |
---|
| 157 | """ |
---|
| 158 | FitEngine.__init__(self) |
---|
| 159 | self.curr_thread = None |
---|
| 160 | |
---|
| 161 | def fit(self, msg_q=None, |
---|
| 162 | q=None, handler=None, curr_thread=None, |
---|
| 163 | ftol=1.49012e-8, reset_flag=False): |
---|
[e3efa6b3] | 164 | # Build collection of bumps fitness calculators |
---|
[4e9f227] | 165 | models = [ SasFitness(model=M.get_model(), |
---|
[e3efa6b3] | 166 | data=M.get_data(), |
---|
[4e9f227] | 167 | constraints=M.constraints, |
---|
[e3efa6b3] | 168 | fitted=M.pars) |
---|
| 169 | for i,M in enumerate(self.fit_arrange_dict.values()) |
---|
| 170 | if M.get_to_fit() == 1 ] |
---|
| 171 | problem = FitProblem(models) |
---|
| 172 | |
---|
[4e9f227] | 173 | |
---|
| 174 | # Build constraint expressions |
---|
| 175 | exprs = {} |
---|
| 176 | for M in models: |
---|
| 177 | exprs.update((".".join((M.name,k)),v) for k,v in M.constraints.items()) |
---|
| 178 | if exprs: |
---|
| 179 | symtab = dict((".".join((M.name,k)),p) |
---|
| 180 | for M in models |
---|
| 181 | for k,p in M.parameters().items()) |
---|
| 182 | constraints = compile_constraints(symtab,exprs) |
---|
| 183 | else: |
---|
| 184 | constraints = lambda: 0 |
---|
| 185 | |
---|
| 186 | # Override model update so that parameter constraints are applied |
---|
| 187 | problem._model_update = problem.model_update |
---|
| 188 | def model_update(): |
---|
| 189 | constraints() |
---|
| 190 | problem._model_update() |
---|
| 191 | problem.model_update = model_update |
---|
| 192 | |
---|
[e3efa6b3] | 193 | # Run the fit |
---|
| 194 | result = run_bumps(problem, handler, curr_thread) |
---|
[6fe5100] | 195 | if handler is not None: |
---|
| 196 | handler.update_fit(last=True) |
---|
[e3efa6b3] | 197 | |
---|
[eff93b8] | 198 | # TODO: shouldn't reference internal parameters of fit problem |
---|
[e3efa6b3] | 199 | varying = problem._parameters |
---|
| 200 | # collect the results |
---|
| 201 | all_results = [] |
---|
| 202 | for M in problem.models: |
---|
| 203 | fitness = M.fitness |
---|
| 204 | fitted_index = [varying.index(p) for p in fitness.fitted_pars] |
---|
| 205 | R = FResult(model=fitness.model, data=fitness.data, |
---|
| 206 | param_list=fitness.fitted_par_names) |
---|
| 207 | R.theory = fitness.theory() |
---|
| 208 | R.residuals = fitness.residuals() |
---|
| 209 | R.fitter_id = self.fitter_id |
---|
[eff93b8] | 210 | # TODO: should scale stderr by sqrt(chisq/DOF) if dy is unknown |
---|
[e3efa6b3] | 211 | R.stderr = result['stderr'][fitted_index] |
---|
| 212 | R.pvec = result['value'][fitted_index] |
---|
| 213 | R.success = result['success'] |
---|
| 214 | R.fitness = numpy.sum(R.residuals**2)/(fitness.numpoints() - len(fitted_index)) |
---|
| 215 | R.convergence = result['convergence'] |
---|
| 216 | if result['uncertainty'] is not None: |
---|
| 217 | R.uncertainty_state = result['uncertainty'] |
---|
| 218 | all_results.append(R) |
---|
| 219 | |
---|
[6fe5100] | 220 | if q is not None: |
---|
[e3efa6b3] | 221 | q.put(all_results) |
---|
[6fe5100] | 222 | return q |
---|
[e3efa6b3] | 223 | else: |
---|
| 224 | return all_results |
---|
[6fe5100] | 225 | |
---|
[e3efa6b3] | 226 | def run_bumps(problem, handler, curr_thread): |
---|
[85f17f6] | 227 | def abort_test(): |
---|
| 228 | if curr_thread is None: return False |
---|
| 229 | try: curr_thread.isquit() |
---|
| 230 | except KeyboardInterrupt: |
---|
| 231 | if handler is not None: |
---|
| 232 | handler.stop("Fitting: Terminated!!!") |
---|
| 233 | return True |
---|
| 234 | return False |
---|
| 235 | |
---|
[6fe5100] | 236 | fitopts = fitters.FIT_OPTIONS[fitters.FIT_DEFAULT] |
---|
[95d58d3] | 237 | fitclass = fitopts.fitclass |
---|
| 238 | options = fitopts.options.copy() |
---|
[e3efa6b3] | 239 | max_step = fitopts.options.get('steps', 0) + fitopts.options.get('burn', 0) |
---|
| 240 | options['monitors'] = [ |
---|
| 241 | BumpsMonitor(handler, max_step), |
---|
| 242 | ConvergenceMonitor(), |
---|
| 243 | ] |
---|
[95d58d3] | 244 | fitdriver = fitters.FitDriver(fitclass, problem=problem, |
---|
[042f065] | 245 | abort_test=abort_test, **options) |
---|
[6fe5100] | 246 | mapper = SerialMapper |
---|
| 247 | fitdriver.mapper = mapper.start_mapper(problem, None) |
---|
[e3efa6b3] | 248 | import time; T0 = time.time() |
---|
[6fe5100] | 249 | try: |
---|
| 250 | best, fbest = fitdriver.fit() |
---|
| 251 | except: |
---|
| 252 | import traceback; traceback.print_exc() |
---|
| 253 | raise |
---|
[95d58d3] | 254 | finally: |
---|
| 255 | mapper.stop_mapper(fitdriver.mapper) |
---|
[e3efa6b3] | 256 | |
---|
| 257 | |
---|
| 258 | convergence_list = options['monitors'][-1].convergence |
---|
| 259 | convergence = (2*numpy.asarray(convergence_list)/problem.dof |
---|
| 260 | if convergence_list else numpy.empty((0,1),'d')) |
---|
| 261 | return { |
---|
| 262 | 'value': best, |
---|
| 263 | 'stderr': fitdriver.stderr(), |
---|
| 264 | 'success': True, # better success reporting in bumps |
---|
| 265 | 'convergence': convergence, |
---|
| 266 | 'uncertainty': getattr(fitdriver.fitter, 'state', None), |
---|
| 267 | } |
---|
[6fe5100] | 268 | |
---|