Changeset c4d6900 in sasview
- Timestamp:
- Oct 27, 2010 12:07:46 PM (14 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:
- b0ab6cb
- Parents:
- 7406040
- Location:
- park_integration
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
park_integration/AbstractFitEngine.py
rb2f25dc5 rc4d6900 1 1 2 2 import copy 3 import logging4 import sys3 #import logging 4 #import sys 5 5 import numpy 6 6 import math … … 23 23 24 24 """ 25 park.Parameter.__init__(self, name) 25 26 self._model, self._name = model, name 26 27 #set the value for the parameter of the given name … … 45 46 self._model.setParam(self.name, value) 46 47 47 value = property(_getvalue, _setvalue)48 value = property(_getvalue, _setvalue) 48 49 49 50 def _getrange(self): … … 95 96 self.sansp = sans_model.getParamList() 96 97 #list of park parameter 97 self.parkp = [SansParameter(p, sans_model) for p in self.sansp]98 self.parkp = [SansParameter(p, sans_model) for p in self.sansp] 98 99 #list of parameterset 99 100 self.parameterset = park.ParameterSet(sans_model.name, pars=self.parkp) 100 101 self.pars = [] 101 102 102 def get Params(self, fitparams):103 def get_params(self, fitparams): 103 104 """ 104 105 return a list of value of paramter to fit … … 107 108 108 109 """ 109 list = []110 list_params = [] 110 111 self.pars = [] 111 112 self.pars = fitparams 112 113 for item in fitparams: 113 114 for element in self.parkp: 114 115 list.append(element.value)116 return list 117 118 def set Params(self, paramlist, params):115 if element.name == str(item): 116 list_params.append(element.value) 117 return list_params 118 119 def set_params(self, paramlist, params): 119 120 """ 120 121 Set value for parameters to fit … … 143 144 except: 144 145 raise 146 147 def eval_derivs(self, x, pars=[]): 148 """ 149 Evaluate the model and derivatives wrt pars at x. 150 151 pars is a list of the names of the parameters for which derivatives 152 are desired. 153 154 This method needs to be specialized in the model to evaluate the 155 model function. Alternatively, the model can implement is own 156 version of residuals which calculates the residuals directly 157 instead of calling eval. 158 """ 159 return [] 160 145 161 146 162 … … 174 190 175 191 self.smearer = smearer 176 192 self._first_unsmeared_bin = None 193 self._last_unsmeared_bin = None 177 194 # Check error bar; if no error bar found, set it constant(=1) 178 # TODO: Should provide an option for users to set it like percent, constant, or dy data 195 # TODO: Should provide an option for users to set it like percent, 196 # constant, or dy data 179 197 if dy == None or dy == [] or dy.all() == 0: 180 198 self.dy = numpy.ones(len(y)) … … 200 218 & (self.x <= self._qmax_unsmeared) 201 219 202 def set FitRange(self, qmin=None, qmax=None):220 def set_fit_range(self, qmin=None, qmax=None): 203 221 """ to set the fit range""" 204 222 # Skip Q=0 point, (especially for y(q=0)=None at x[0]). … … 218 236 self._last_unsmeared_bin = len(self.x) - 1 219 237 220 if self.smearer !=None:238 if self.smearer != None: 221 239 self._first_unsmeared_bin, self._last_unsmeared_bin = \ 222 240 self.smearer.get_bin_range(self.qmin, self.qmax) … … 231 249 & (self.x <= self._qmax_unsmeared) 232 250 233 def get FitRange(self):251 def get_fit_range(self): 234 252 """ 235 253 return the range of data.x to fit … … 276 294 """ Wrapper class for SANS data """ 277 295 def __init__(self, sans_data2d, data=None, err_data=None): 278 Data2D.__init__(self, data=data, err_data= 296 Data2D.__init__(self, data=data, err_data=err_data) 279 297 """ 280 298 Data can be initital with a data (sans plottable) … … 286 304 self.qmax = None 287 305 self.smearer = None 306 self.radius = 0 307 self.res_err_data = [] 288 308 self.set_data(sans_data2d) 289 309 290 291 310 def set_data(self, sans_data2d, qmin=None, qmax=None): 292 311 """ … … 322 341 self.index_model = (self.index_model) & (numpy.isfinite(self.data)) 323 342 324 def set_smearer(self, smearer):343 def set_smearer(self, smearer): 325 344 """ 326 345 Set smearer … … 332 351 self.smearer.get_data() 333 352 334 def set FitRange(self, qmin=None, qmax=None):353 def set_fit_range(self, qmin=None, qmax=None): 335 354 """ to set the fit range""" 336 355 if qmin == 0.0: … … 345 364 self.index_model = (self.index_model) &(self.mask) 346 365 self.index_model = (self.index_model) & (numpy.isfinite(self.data)) 347 self.index_model = (self.index_model) & (self.res_err_data !=0)348 349 def get FitRange(self):366 self.index_model = (self.index_model) & (self.res_err_data != 0) 367 368 def get_fit_range(self): 350 369 """ 351 370 return the range of data.x to fit … … 407 426 self.func_name = "Functor" 408 427 409 def chisq(self, params): 428 #def chisq(self, params): 429 def chisq(self): 410 430 """ 411 431 Calculates chi^2 … … 418 438 sum = 0 419 439 for item in self.res: 420 sum += item *item421 if len(self.res) ==0:440 sum += item * item 441 if len(self.res) == 0: 422 442 return None 423 return sum / len(self.res)424 425 def __call__(self, params):443 return sum / len(self.res) 444 445 def __call__(self, params): 426 446 """ 427 447 Compute residuals … … 431 451 """ 432 452 #import thread 433 self.model.set Params(self.paramlist,params)453 self.model.set_params(self.paramlist,params) 434 454 self.res = self.data.residuals(self.model.eval) 435 455 if self.fitresult is not None and self.handler is not None: 436 456 self.fitresult.set_model(model=self.model) 437 fitness = self.chisq(params=params) 457 #fitness = self.chisq(params=params) 458 fitness = self.chisq() 438 459 self.fitresult.set_fitness(fitness=fitness) 439 460 self.handler.set_result(result=self.fitresult) … … 456 477 #Dictionnary of fitArrange element (fit problems) 457 478 self.fit_arrange_dict = {} 458 459 def _concatenateData(self, listdata=[]): 460 """ 461 _concatenateData method concatenates each fields of all data 462 contains ins listdata. 463 464 :param listdata: list of data 465 466 :return Data: Data is wrapper class for sans plottable. 467 it is created with all parameters of data concatenanted 468 469 :raise: if listdata is empty will return None 470 :raise: if data in listdata don't contain dy field , 471 will create an error during fitting 472 473 """ 474 #TODO: we have to refactor the way we handle data. 475 # We should move away from plottables and move towards the Data1D objects 476 # defined in DataLoader. Data1D allows data manipulations, which should be 477 # used to concatenate. 478 # In the meantime we should switch off the concatenation. 479 #if len(listdata)>1: 480 # raise RuntimeError, "FitEngine._concatenateData: Multiple data files is not currently supported" 481 #return listdata[0] 482 483 if listdata == []: 484 raise ValueError, " data list missing" 485 else: 486 xtemp = [] 487 ytemp = [] 488 dytemp = [] 489 self.mini = None 490 self.maxi = None 491 492 for item in listdata: 493 data = item.data 494 mini, maxi = data.getFitRange() 495 if self.mini == None and self.maxi == None: 496 self.mini = mini 497 self.maxi = maxi 498 else: 499 if mini < self.mini: 500 self.mini = mini 501 if self.maxi < maxi: 502 self.maxi = maxi 503 for i in range(len(data.x)): 504 xtemp.append(data.x[i]) 505 ytemp.append(data.y[i]) 506 if data.dy is not None and len(data.dy) == len(data.y): 507 dytemp.append(data.dy[i]) 508 else: 509 msg = "Fit._concatenateData: y-errors missing" 510 raise RuntimeError, msg 511 data = Data(x=xtemp, y=ytemp, dy=dytemp) 512 data.setFitRange(self.mini, self.maxi) 513 return data 514 515 516 def set_model(self, model, Uid, pars=[], constraints=[]): 517 """ 518 set a model on a given uid in the fit engine. 479 480 def set_model(self, model, id, pars=[], constraints=[]): 481 """ 482 set a model on a given in the fit engine. 519 483 520 484 :param model: sans.models type 521 :param Uid: is the key of the fitArrange dictionary where model is485 :param : is the key of the fitArrange dictionary where model is 522 486 saved as a value 523 487 :param pars: the list of parameters to fit … … 549 513 except: 550 514 msg = "Fit Engine: Error occurs when setting the constraint" 551 msg += " %s for parameter %s " %(value, name)515 msg += " %s for parameter %s " % (value, name) 552 516 raise ValueError, msg 553 517 … … 566 530 raise ValueError, msg 567 531 568 #A fitArrange is already created but contains d List only at Uid569 if self.fit_arrange_dict.has_key( Uid):570 self.fit_arrange_dict[ Uid].set_model(new_model)571 self.fit_arrange_dict[ Uid].pars = pars532 #A fitArrange is already created but contains data_list only at id 533 if self.fit_arrange_dict.has_key(id): 534 self.fit_arrange_dict[id].set_model(new_model) 535 self.fit_arrange_dict[id].pars = pars 572 536 else: 573 #no fitArrange object has been create with this Uid537 #no fitArrange object has been create with this id 574 538 fitproblem = FitArrange() 575 539 fitproblem.set_model(new_model) 576 540 fitproblem.pars = pars 577 self.fit_arrange_dict[ Uid] = fitproblem541 self.fit_arrange_dict[id] = fitproblem 578 542 579 543 else: 580 544 raise ValueError, "park_integration:missing parameters" 581 545 582 def set_data(self, data, Uid, smearer=None, qmin=None, qmax=None):546 def set_data(self, data, id, smearer=None, qmin=None, qmax=None): 583 547 """ 584 548 Receives plottable, creates a list of data to fit,set data 585 549 in a FitArrange object and adds that object in a dictionary 586 with key Uid.550 with key id. 587 551 588 552 :param data: data added 589 :param Uid: unique key corresponding to a fitArrange object with data553 :param id: unique key corresponding to a fitArrange object with data 590 554 591 555 """ … … 597 561 dx=data.dx, dy=data.dy, smearer=smearer) 598 562 599 fitdata.set FitRange(qmin=qmin, qmax=qmax)600 #A fitArrange is already created but contains model only at Uid601 if self.fit_arrange_dict.has_key( Uid):602 self.fit_arrange_dict[ Uid].add_data(fitdata)563 fitdata.set_fit_range(qmin=qmin, qmax=qmax) 564 #A fitArrange is already created but contains model only at id 565 if self.fit_arrange_dict.has_key(id): 566 self.fit_arrange_dict[id].add_data(fitdata) 603 567 else: 604 #no fitArrange object has been create with this Uid568 #no fitArrange object has been create with this id 605 569 fitproblem = FitArrange() 606 570 fitproblem.add_data(fitdata) 607 self.fit_arrange_dict[ Uid] = fitproblem571 self.fit_arrange_dict[id] = fitproblem 608 572 609 def get_model(self, Uid):610 """ 611 612 :param Uid: Uid is key in the dictionary containing the model to return613 614 :return: a model at this uid or None if no FitArrange element was created615 with this Uid616 617 """ 618 if self.fit_arrange_dict.has_key( Uid):619 return self.fit_arrange_dict[ Uid].get_model()573 def get_model(self, id): 574 """ 575 576 :param id: id is key in the dictionary containing the model to return 577 578 :return: a model at this id or None if no FitArrange element was 579 created with this id 580 581 """ 582 if self.fit_arrange_dict.has_key(id): 583 return self.fit_arrange_dict[id].get_model() 620 584 else: 621 585 return None 622 586 623 def remove_ Fit_Problem(self, Uid):624 """remove fitarrange in Uid"""625 if self.fit_arrange_dict.has_key( Uid):626 del self.fit_arrange_dict[ Uid]627 628 def select_problem_for_fit(self, Uid, value):629 """ 630 select a couple of model and data at the Uid position in dictionary587 def remove_fit_problem(self, id): 588 """remove fitarrange in id""" 589 if self.fit_arrange_dict.has_key(id): 590 del self.fit_arrange_dict[id] 591 592 def select_problem_for_fit(self, id, value): 593 """ 594 select a couple of model and data at the id position in dictionary 631 595 and set in self.selected value to value 632 596 … … 635 599 636 600 """ 637 if self.fit_arrange_dict.has_key( Uid):638 self.fit_arrange_dict[Uid].set_to_fit(value)601 if self.fit_arrange_dict.has_key(id): 602 self.fit_arrange_dict[id].set_to_fit(value) 639 603 640 def get_problem_to_fit(self, Uid):641 """ 642 return the self.selected value of the fit problem of Uid643 644 :param Uid: the Uid of the problem645 646 """ 647 if self.fit_arrange_dict.has_key( Uid):648 self.fit_arrange_dict[Uid].get_to_fit()604 def get_problem_to_fit(self, id): 605 """ 606 return the self.selected value of the fit problem of id 607 608 :param id: the id of the problem 609 610 """ 611 if self.fit_arrange_dict.has_key(id): 612 self.fit_arrange_dict[id].get_to_fit() 649 613 650 614 class FitArrange: … … 660 624 """ 661 625 self.model = None 662 self.d List = []626 self.data_list = [] 663 627 self.pars = [] 664 628 #self.selected is zero when this fit problem is not schedule to fit … … 677 641 def add_data(self, data): 678 642 """ 679 add_data fill a self.d List with data to fit643 add_data fill a self.data_list with data to fit 680 644 681 645 :param data: Data to add in the list 682 646 683 647 """ 684 if not data in self.d List:685 self.d List.append(data)648 if not data in self.data_list: 649 self.data_list.append(data) 686 650 687 651 def get_model(self): … … 696 660 """ 697 661 698 :return: list of data d List699 700 """ 701 #return self.d List702 return self.d List[0]662 :return: list of data data_list 663 664 """ 665 #return self.data_list 666 return self.data_list[0] 703 667 704 668 def remove_data(self, data): … … 706 670 Remove one element from the list 707 671 708 :param data: Data to remove from d List709 710 """ 711 if data in self.d List:712 self.d List.remove(data)672 :param data: Data to remove from data_list 673 674 """ 675 if data in self.data_list: 676 self.data_list.remove(data) 713 677 714 678 def set_to_fit (self, value=0): -
park_integration/Fitting.py
r89f3b66 rc4d6900 6 6 7 7 #from scipy import optimize 8 from ScipyFitting import ScipyFit9 from ParkFitting import ParkFit8 from sans.fit.ScipyFitting import ScipyFit 9 from sans.fit.ParkFitting import ParkFit 10 10 11 11 … … 19 19 fitter.fit_engine('scipy') or fitter.fit_engine('park') 20 20 engine = fitter.returnEngine() 21 engine.set_data(data, Uid)21 engine.set_data(data,id) 22 22 engine.set_param( model,model.name, pars) 23 engine.set_model(model, Uid)23 engine.set_model(model,id) 24 24 25 25 chisqr1, out1, cov1=engine.fit(pars,qmin,qmax) … … 46 46 self._engine = ScipyFit() 47 47 elif word == "park": 48 self._engine =ParkFit()48 self._engine = ParkFit() 49 49 else: 50 50 raise ValueError, "enter the keyword scipy or park" … … 52 52 def fit(self, q=None, handler=None, curr_thread=None): 53 53 """Perform the fit """ 54 try:55 return self._engine.fit(q,handler, curr_thread=curr_thread)56 except:57 raise54 #try: 55 return self._engine.fit(q, handler, curr_thread=curr_thread) 56 #except: 57 # raise 58 58 59 def set_model(self, model, Uid, pars=[], constraints=[]):59 def set_model(self, model, id, pars=[], constraints=[]): 60 60 """ 61 store a model model to fit at the position Uid of the fit engine61 store a model model to fit at the position id of the fit engine 62 62 """ 63 self._engine.set_model(model, Uid, pars, constraints)63 self._engine.set_model(model, id, pars, constraints) 64 64 65 def set_data(self, data, Uid, smearer=None, qmin=None, qmax=None):65 def set_data(self, data, id, smearer=None, qmin=None, qmax=None): 66 66 """ 67 Store data to fit at the psotion Uid of the fit engine67 Store data to fit at the psotion id of the fit engine 68 68 69 69 :param data: data to fit … … 73 73 74 74 """ 75 self._engine.set_data(data, Uid, smearer, qmin, qmax)75 self._engine.set_data(data, id, smearer, qmin, qmax) 76 76 77 def get_model(self, Uid):77 def get_model(self, id): 78 78 """ return list of data""" 79 self._engine.get_model( Uid)79 self._engine.get_model(id) 80 80 81 81 82 def remove_ Fit_Problem(self, Uid):83 """remove fitarrange in Uid"""84 self._engine.remove_ Fit_Problem(Uid)82 def remove_fit_problem(self, id): 83 """remove fitarrange in id""" 84 self._engine.remove_fit_problem(id) 85 85 86 def select_problem_for_fit(self, Uid, value):86 def select_problem_for_fit(self, id, value): 87 87 """ 88 select a couple of model and data at the Uid position in dictionary88 select a couple of model and data at the id position in dictionary 89 89 and set in self.selected value to value 90 90 … … 92 92 can only have the value one or zero 93 93 """ 94 self._engine.select_problem_for_fit( Uid, value)94 self._engine.select_problem_for_fit(id, value) 95 95 96 def get_problem_to_fit(self, Uid):96 def get_problem_to_fit(self, id): 97 97 """ 98 return the self.selected value of the fit problem of Uid98 return the self.selected value of the fit problem of id 99 99 100 :param Uid: the Uid of the problem100 :param id: the id of the problem 101 101 102 102 """ 103 return self._engine.get_problem_to_fit( Uid)103 return self._engine.get_problem_to_fit(id) -
park_integration/Loader.py
r89f3b66 rc4d6900 3 3 #import string 4 4 import numpy 5 5 6 class Load: 6 7 """ … … 8 9 """ 9 10 10 def _ init_(self, x=None, y=None, dx=None, dy=None):11 def __init__(self, x=None, y=None, dx=None, dy=None): 11 12 # variable to store loaded values 12 13 self.x = x … … 18 19 def set_filename(self, path=None): 19 20 """ 20 Store path into a variable.If the user doesn't give a path as a parameter a pop-up 21 Store path into a variable.If the user doesn't give 22 a path as a parameter a pop-up 21 23 window appears to select the file. 22 24 … … 53 55 except: 54 56 print "READ ERROR", line 55 56 57 57 # Sanity check 58 58 if not len(self.x) == len(self.dx): -
park_integration/ScipyFitting.py
rb2f25dc5 rc4d6900 12 12 from sans.fit.AbstractFitEngine import FitEngine 13 13 from sans.fit.AbstractFitEngine import SansAssembly 14 from sans.fit.AbstractFitEngine import FitAbort14 #from sans.fit.AbstractFitEngine import FitAbort 15 15 16 16 class fitresult(object): … … 18 18 Storing fit result 19 19 """ 20 def __init__(self, model=None, param List=None):20 def __init__(self, model=None, param_list=None): 21 21 self.calls = None 22 22 self.fitness = None … … 30 30 self.parameters = None 31 31 self.model = model 32 self.param_list = param List32 self.param_list = param_list 33 33 34 34 def set_model(self, model): … … 50 50 51 51 result_param = zip(xrange(n), self.model.parameterset) 52 L = ["P%-3d %s......|.....%s"%(p[0], p[1], p[1].value)\52 msg = ["P%-3d %s......|.....%s" % (p[0], p[1], p[1].value)\ 53 53 for p in result_param if p[1].name in self.param_list] 54 L.append("=== goodness of fit: %s" % (str(self.fitness)))55 return "\n".join( L)54 msg.append("=== goodness of fit: %s" % (str(self.fitness))) 55 return "\n".join(msg) 56 56 57 57 def print_summary(self): … … 95 95 self.fit_arrange_dict = {} 96 96 self.param_list = [] 97 self.curr_thread = None 97 98 #def fit(self, *args, **kw): 98 99 # return profile(self._fit, *args, **kw) … … 102 103 """ 103 104 fitproblem = [] 104 for id, fproblem in self.fit_arrange_dict.iteritems():105 for fproblem in self.fit_arrange_dict.itervalues(): 105 106 if fproblem.get_to_fit() == 1: 106 107 fitproblem.append(fproblem) … … 119 120 data = listdata 120 121 self.curr_thread = curr_thread 121 result = fitresult(model=model, param List=self.param_list)122 result = fitresult(model=model, param_list=self.param_list) 122 123 if handler is not None: 123 124 handler.set_result(result=result) … … 125 126 functor = SansAssembly(self.param_list, model, data, handler=handler, 126 127 fitresult=result, curr_thread= self.curr_thread) 127 128 129 out, cov_x, info, mesg, success = optimize.leastsq(functor, 130 model.getParams(self.param_list), 131 full_output=1, warning=True) 128 out, cov_x, _, _, success = optimize.leastsq(functor, 129 model.get_params(self.param_list), 130 full_output=1, 131 warning=True) 132 132 133 chisqr = functor.chisq(out)134 133 #chisqr = functor.chisq(out) 134 chisqr = functor.chisq() 135 135 if cov_x is not None and numpy.isfinite(cov_x).all(): 136 136 stderr = numpy.sqrt(numpy.diag(cov_x)) 137 137 else: 138 138 stderr = None 139 if not (numpy.isnan(out).any()) or ( cov_x !=None):140 141 142 143 144 145 if q is notNone:146 147 148 149 150 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 151 151 else: 152 152 raise ValueError, "SVD did not converge" + str(success) … … 154 154 155 155 156 def profile(fn, *args, **kw):157 import cProfile, pstats, os158 global call_result159 160 global call_result161 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_result156 #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 169 169 170 170
Note: See TracChangeset
for help on using the changeset viewer.