Changeset 444c900e in sasview for park_integration/src
- Timestamp:
- Oct 11, 2011 5:51:37 PM (13 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 6a4002d
- Parents:
- d59cb8c
- Location:
- park_integration/src/sans/fit
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/src/sans/fit/AbstractFitEngine.py
r5722d66 r444c900e 2 2 import copy 3 3 #import logging 4 #import sys4 import sys 5 5 import numpy 6 6 import math … … 284 284 raise RuntimeError, msg 285 285 return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 286 286 287 287 288 def residuals_deriv(self, model, pars=[]): … … 303 304 """ 304 305 self.res_err_image = [] 305 self.i ndex_model= []306 self.idx = [] 306 307 self.qmin = None 307 308 self.qmax = None … … 340 341 341 342 # Note: mask = True: for MASK while mask = False for NOT to mask 342 self.i ndex_model= ((self.qmin <= self.radius)&\343 self.idx = ((self.qmin <= self.radius)&\ 343 344 (self.radius <= self.qmax)) 344 self.i ndex_model = (self.index_model) & (self.mask)345 self.i ndex_model = (self.index_model) & (numpy.isfinite(self.data))345 self.idx = (self.idx) & (self.mask) 346 self.idx = (self.idx) & (numpy.isfinite(self.data)) 346 347 347 348 def set_smearer(self, smearer): … … 352 353 return 353 354 self.smearer = smearer 354 self.smearer.set_index(self.i ndex_model)355 self.smearer.set_index(self.idx) 355 356 self.smearer.get_data() 356 357 … … 366 367 self.index_model = ((self.qmin <= self.radius)&\ 367 368 (self.radius <= self.qmax)) 368 self.i ndex_model = (self.index_model) &(self.mask)369 self.i ndex_model = (self.index_model) & (numpy.isfinite(self.data))370 self.i ndex_model = (self.index_model) & (self.res_err_data != 0)369 self.idx = (self.idx) &(self.mask) 370 self.idx = (self.idx) & (numpy.isfinite(self.data)) 371 self.idx = (self.idx) & (self.res_err_data != 0) 371 372 372 373 def get_fit_range(self): … … 381 382 """ 382 383 if self.smearer != None: 383 fn.set_index(self.i ndex_model)384 fn.set_index(self.idx) 384 385 # Get necessary data from self.data and set the data for smearing 385 386 fn.get_data() … … 387 388 gn = fn.get_value() 388 389 else: 389 gn = fn([self.qx_data[self.i ndex_model],390 self.qy_data[self.i ndex_model]])390 gn = fn([self.qx_data[self.idx], 391 self.qy_data[self.idx]]) 391 392 # use only the data point within ROI range 392 res = (self.data[self.index_model] - gn)/\ 393 self.res_err_data[self.index_model] 394 return res, gn 393 res = (self.data[self.index_model] - gn)/self.res_err_data[self.idx] 394 return res, gn 395 395 396 396 def residuals_deriv(self, model, pars=[]): … … 470 470 if self.fitresult is not None and self.handler is not None: 471 471 self.fitresult.set_model(model=self.model) 472 self.fitresult.residuals = self.true_res 473 self.fitresult.theory = theory 472 474 #fitness = self.chisq(params=params) 473 475 fitness = self.chisq() … … 760 762 """ 761 763 return self.selected 764 765 766 IS_MAC = True 767 if sys.platform.count("win32") > 0: 768 IS_MAC = False 769 770 class FResult(object): 771 """ 772 Storing fit result 773 """ 774 def __init__(self, model=None, param_list=None, data=None): 775 self.calls = None 776 self.fitness = None 777 self.chisqr = None 778 self.pvec = [] 779 self.cov = [] 780 self.info = None 781 self.mesg = None 782 self.success = None 783 self.stderr = None 784 self.residuals = [] 785 self.index = [] 786 self.parameters = None 787 self.is_mac = IS_MAC 788 self.model = model 789 self.data = data 790 self.theory = [] 791 self.param_list = param_list 792 self.iterations = 0 793 self.inputs = [] 794 if self.model is not None and self.data is not None: 795 self.inputs = [(self.model, self.data)] 796 797 def set_model(self, model): 798 """ 799 """ 800 self.model = model 801 802 def set_fitness(self, fitness): 803 """ 804 """ 805 self.fitness = fitness 806 807 def __str__(self): 808 """ 809 """ 810 if self.pvec == None and self.model is None and self.param_list is None: 811 return "No results" 812 n = len(self.model.parameterset) 813 self.iterations += 1 814 result_param = zip(xrange(n), self.model.parameterset) 815 msg1 = ["[Iteration #: %s ]" % self.iterations] 816 msg3 = ["=== goodness of fit: %s ===" % (str(self.fitness))] 817 if not self.is_mac: 818 msg2 = ["P%-3d %s......|.....%s" % \ 819 (p[0], p[1], p[1].value)\ 820 for p in result_param if p[1].name in self.param_list] 821 msg = msg1 + msg3 + msg2 822 else: 823 msg = msg1 + msg3 824 msg = "\n".join(msg) 825 return msg 826 827 def print_summary(self): 828 """ 829 """ 830 print self -
park_integration/src/sans/fit/ParkFitting.py
rd59cb8c r444c900e 25 25 #from Loader import Load 26 26 from sans.fit.AbstractFitEngine import FitEngine 27 from sans.fit.AbstractFitEngine import FResult 27 28 28 29 class SansFitResult(fitresult.FitResult): … … 208 209 class MyAssembly(Assembly): 209 210 def __init__(self, models, curr_thread=None): 210 Assembly.__init__(self, models) 211 """Build an assembly from a list of models.""" 212 self.parts = [] 213 for m in models: 214 self.parts.append(SansPart(m)) 211 215 self.curr_thread = curr_thread 212 216 self.chisq = None 213 217 self._cancel = False 214 218 self.theory = None 219 self._reset() 215 220 216 221 def fit_parameters(self): … … 398 403 if handler == None: 399 404 handler = fitresult.ConsoleUpdate(improvement_delta=0.1) 405 406 result_list = [] 400 407 try: 401 408 result = fit.fit(self.problem, fitter=fitter, handler=handler) 402 409 self.problem.all_results(result) 410 403 411 except LinAlgError: 404 412 raise ValueError, "SVD did not converge" 405 406 if result != None: 407 if q != None: 408 q.put(result) 409 return q 410 return result 411 else: 412 raise ValueError, "SVD did not converge" 413 413 414 for m in self.problem.parts: 415 residuals, theory = m.fitness.residuals() 416 small_result = FResult(model=m.model, data=m.data.sans_data) 417 small_result.theory = theory 418 small_result.residuals = residuals 419 small_result.pvec = [] 420 small_result.cov = [] 421 small_result.stderr = [] 422 small_result.param_list = [] 423 small_result.residuals = m.residuals 424 if result is not None: 425 for p in result.parameters: 426 if p.data.name == small_result.data.name: 427 small_result.index = m.data.idx 428 small_result.fitness = result.fitness 429 small_result.pvec.append(p.value) 430 small_result.stderr.append(p.stderr) 431 name = p.name.split('.')[1].strip() 432 small_result.param_list.append(name) 433 result_list.append(small_result) 434 if q != None: 435 q.put(result) 436 return q 437 print "park", len(result_list) 438 return result_list 439 -
park_integration/src/sans/fit/ScipyFitting.py
r425e49ca r444c900e 15 15 from sans.fit.AbstractFitEngine import FitAbort 16 16 from sans.fit.AbstractFitEngine import Model 17 IS_MAC = True 18 if sys.platform.count("win32") > 0: 19 IS_MAC = False 20 21 class fitresult(object): 22 """ 23 Storing fit result 24 """ 25 def __init__(self, model=None, param_list=None, data=None): 26 self.calls = None 27 self.fitness = None 28 self.chisqr = None 29 self.pvec = None 30 self.cov = None 31 self.info = None 32 self.mesg = None 33 self.success = None 34 self.stderr = None 35 self.parameters = None 36 self.is_mac = IS_MAC 37 if issubclass(model.__class__, Model): 38 model = model.model 39 self.model = model 40 self.data = data 41 self.theory = None 42 self.param_list = param_list 43 self.iterations = 0 44 45 self.inputs = [(self.model, self.data)] 46 47 def set_model(self, model): 48 """ 49 """ 50 self.model = model 51 52 def set_fitness(self, fitness): 53 """ 54 """ 55 self.fitness = fitness 56 57 def __str__(self): 58 """ 59 """ 60 if self.pvec == None and self.model is None and self.param_list is None: 61 return "No results" 62 n = len(self.model.parameterset) 63 self.iterations += 1 64 result_param = zip(xrange(n), self.model.parameterset) 65 msg1 = ["[Iteration #: %s ]" % self.iterations] 66 msg3 = ["=== goodness of fit: %s ===" % (str(self.fitness))] 67 if not self.is_mac: 68 msg2 = ["P%-3d %s......|.....%s" % \ 69 (p[0], p[1], p[1].value)\ 70 for p in result_param if p[1].name in self.param_list] 71 msg = msg1 + msg3 + msg2 72 else: 73 msg = msg1 + msg3 74 msg = "\n".join(msg) 75 return msg 76 77 def print_summary(self): 78 """ 79 """ 80 print self 17 from sans.fit.AbstractFitEngine import FResult 81 18 82 19 class ScipyFit(FitEngine): … … 146 83 self._check_param_range(model) 147 84 148 result = fitresult(model=model, data=data.sans_data, param_list=self.param_list)85 result = FResult(model=model, data=data, param_list=self.param_list) 149 86 if handler is not None: 150 87 handler.set_result(result=result) … … 197 134 if success < 1 or success > 5: 198 135 result = None 199 return result 136 return [result] 137 """ 200 138 else: 201 139 return None 140 """ 202 141 # Error will be present to the client, not here 203 142 #else:
Note: See TracChangeset
for help on using the changeset viewer.