Changeset 0315b63 in sasview for src/sas/sascalc/fit
- Timestamp:
- Sep 25, 2017 5:24:46 PM (7 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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 24d9e84, fca1f50
- Parents:
- b277220 (diff), 66000ae (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent. - Location:
- src/sas/sascalc/fit
- Files:
-
- 1 added
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/fit/pagestate.py
r277257f r0315b63 1226 1226 namespaces=CANSAS_NS) 1227 1227 for entry in entry_list: 1228 try:1229 sas_entry, _ = self._parse_save_state_entry(entry)1230 except:1231 raise1232 1228 fitstate = self._parse_state(entry) 1233 1234 1229 # state could be None when .svs file is loaded 1235 1230 # in this case, skip appending to output 1236 1231 if fitstate is not None: 1232 try: 1233 sas_entry, _ = self._parse_save_state_entry( 1234 entry) 1235 except: 1236 raise 1237 1237 sas_entry.meta_data['fitstate'] = fitstate 1238 1238 sas_entry.filename = fitstate.file -
src/sas/sascalc/fit/AbstractFitEngine.py
ra1b8fee r50fcb09 137 137 that will smear the theory data (slit smearing or resolution 138 138 smearing) when set. 139 139 140 140 The proper way to set the smearing object would be to 141 141 do the following: :: 142 143 from sas.sascalc. data_util.qsmearing import smear_selection142 143 from sas.sascalc.fit.qsmearing import smear_selection 144 144 smearer = smear_selection(some_data) 145 145 fitdata1d = FitData1D( x= [1,3,..,], … … 147 147 dx=None, 148 148 dy=[1,2...], smearer= smearer) 149 149 150 150 :Note: that some_data _HAS_ to be of 151 151 class DataLoader.data_info.Data1D 152 152 Setting it back to None will turn smearing off. 153 153 154 154 """ 155 155 Data1D.__init__(self, x=x, y=y, dx=dx, dy=dy, lam=lam, dlam=dlam) … … 176 176 ## Max Q-value 177 177 self.qmax = max(self.x) 178 178 179 179 # Range used for input to smearing 180 180 self._qmin_unsmeared = self.qmin … … 184 184 self.idx_unsmeared = (self.x >= self._qmin_unsmeared) \ 185 185 & (self.x <= self._qmax_unsmeared) 186 186 187 187 def set_fit_range(self, qmin=None, qmax=None): 188 188 """ to set the fit range""" … … 199 199 self._qmin_unsmeared = self.qmin 200 200 self._qmax_unsmeared = self.qmax 201 201 202 202 self._first_unsmeared_bin = 0 203 203 self._last_unsmeared_bin = len(self.x) - 1 204 204 205 205 if self.smearer is not None: 206 206 self._first_unsmeared_bin, self._last_unsmeared_bin = \ … … 208 208 self._qmin_unsmeared = self.x[self._first_unsmeared_bin] 209 209 self._qmax_unsmeared = self.x[self._last_unsmeared_bin] 210 210 211 211 # Identify the bin range for the unsmeared and smeared spaces 212 212 self.idx = (self.x >= self.qmin) & (self.x <= self.qmax) … … 231 231 """ 232 232 Compute residuals. 233 233 234 234 If self.smearer has been set, use if to smear 235 235 the data before computing chi squared. 236 236 237 237 :param fn: function that return model value 238 238 239 239 :return: residuals 240 240 """ … … 242 242 fx = np.zeros(len(self.x)) 243 243 fx[self.idx_unsmeared] = fn(self.x[self.idx_unsmeared]) 244 244 245 245 ## Smear theory data 246 246 if self.smearer is not None: … … 253 253 raise RuntimeError, msg 254 254 return (self.y[self.idx] - fx[self.idx]) / self.dy[self.idx], fx[self.idx] 255 255 256 256 def residuals_deriv(self, model, pars=[]): 257 257 """ 258 258 :return: residuals derivatives . 259 260 :note: in this case just return empty array 259 260 :note: in this case just return empty array 261 261 """ 262 262 return [] … … 293 293 x_max = max(math.fabs(sas_data2d.xmin), math.fabs(sas_data2d.xmax)) 294 294 y_max = max(math.fabs(sas_data2d.ymin), math.fabs(sas_data2d.ymax)) 295 295 296 296 ## fitting range 297 297 if qmin is None: … … 305 305 self.res_err_data = copy.deepcopy(self.err_data) 306 306 #self.res_err_data[self.res_err_data==0]=1 307 307 308 308 self.radius = np.sqrt(self.qx_data**2 + self.qy_data**2) 309 309 310 310 # Note: mask = True: for MASK while mask = False for NOT to mask 311 311 self.idx = ((self.qmin <= self.radius) &\ … … 368 368 369 369 return res, gn 370 370 371 371 def residuals_deriv(self, model, pars=[]): 372 372 """ 373 373 :return: residuals derivatives . 374 374 375 375 :note: in this case just return empty array 376 376 377 377 """ 378 378 return [] 379 380 379 380 381 381 class FitAbort(Exception): 382 382 """ … … 396 396 self.fit_arrange_dict = {} 397 397 self.fitter_id = None 398 398 399 399 def set_model(self, model, id, pars=[], constraints=[], data=None): 400 400 """ 401 401 set a model on a given in the fit engine. 402 403 :param model: sas.models type 402 403 :param model: sas.models type 404 404 :param id: is the key of the fitArrange dictionary where model is saved as a value 405 :param pars: the list of parameters to fit 406 :param constraints: list of 405 :param pars: the list of parameters to fit 406 :param constraints: list of 407 407 tuple (name of parameter, value of parameters) 408 408 the value of parameter must be a string to constraint 2 different 409 409 parameters. 410 Example: 410 Example: 411 411 we want to fit 2 model M1 and M2 both have parameters A and B. 412 412 constraints can be ``constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]`` 413 414 413 414 415 415 :note: pars must contains only name of existing model's parameters 416 416 417 417 """ 418 418 if not pars: … … 445 445 in a FitArrange object and adds that object in a dictionary 446 446 with key id. 447 447 448 448 :param data: data added 449 449 :param id: unique key corresponding to a fitArrange object with data … … 456 456 dx=data.dx, dy=data.dy, smearer=smearer) 457 457 fitdata.sas_data = data 458 458 459 459 fitdata.set_fit_range(qmin=qmin, qmax=qmax) 460 460 #A fitArrange is already created but contains model only at id … … 466 466 fitproblem.add_data(fitdata) 467 467 self.fit_arrange_dict[id] = fitproblem 468 468 469 469 def get_model(self, id): 470 470 """ 471 471 :param id: id is key in the dictionary containing the model to return 472 472 473 473 :return: a model at this id or None if no FitArrange element was 474 474 created with this id … … 478 478 else: 479 479 return None 480 480 481 481 def remove_fit_problem(self, id): 482 482 """remove fitarrange in id""" 483 483 if id in self.fit_arrange_dict: 484 484 del self.fit_arrange_dict[id] 485 485 486 486 def select_problem_for_fit(self, id, value): 487 487 """ 488 488 select a couple of model and data at the id position in dictionary 489 489 and set in self.selected value to value 490 490 491 491 :param value: the value to allow fitting. 492 492 can only have the value one or zero … … 494 494 if id in self.fit_arrange_dict: 495 495 self.fit_arrange_dict[id].set_to_fit(value) 496 496 497 497 def get_problem_to_fit(self, id): 498 498 """ 499 499 return the self.selected value of the fit problem of id 500 500 501 501 :param id: the id of the problem 502 502 """ 503 503 if id in self.fit_arrange_dict: 504 504 self.fit_arrange_dict[id].get_to_fit() 505 506 505 506 507 507 class FitArrange: 508 508 def __init__(self): … … 511 511 to perform the Fit.FitArrange must contain exactly one model 512 512 and at least one data for the fit to be performed. 513 513 514 514 model: the model selected by the user 515 515 Ldata: a list of data what the user wants to fit 516 516 517 517 """ 518 518 self.model = None … … 525 525 """ 526 526 set_model save a copy of the model 527 527 528 528 :param model: the model being set 529 529 """ 530 530 self.model = model 531 531 532 532 def add_data(self, data): 533 533 """ 534 534 add_data fill a self.data_list with data to fit 535 535 536 536 :param data: Data to add in the list 537 537 """ 538 538 if not data in self.data_list: 539 539 self.data_list.append(data) 540 540 541 541 def get_model(self): 542 542 """ … … 544 544 """ 545 545 return self.model 546 546 547 547 def get_data(self): 548 548 """ … … 550 550 """ 551 551 return self.data_list[0] 552 552 553 553 def remove_data(self, data): 554 554 """ 555 555 Remove one element from the list 556 556 557 557 :param data: Data to remove from data_list 558 558 """ 559 559 if data in self.data_list: 560 560 self.data_list.remove(data) 561 561 562 562 def set_to_fit(self, value=0): 563 563 """ 564 564 set self.selected to 0 or 1 for other values raise an exception 565 565 566 566 :param value: integer between 0 or 1 567 567 """ 568 568 self.selected = value 569 569 570 570 def get_to_fit(self): 571 571 """ … … 599 599 if self.model is not None and self.data is not None: 600 600 self.inputs = [(self.model, self.data)] 601 601 602 602 def set_model(self, model): 603 603 """ 604 604 """ 605 605 self.model = model 606 606 607 607 def set_fitness(self, fitness): 608 608 """ 609 609 """ 610 610 self.fitness = fitness 611 611 612 612 def __str__(self): 613 613 """ … … 624 624 msg = [msg1, msg3] + msg2 625 625 return "\n".join(msg) 626 626 627 627 def print_summary(self): 628 628 """ -
src/sas/sascalc/fit/BumpsFitting.py
r9a5097c r1386b2f 15 15 def get_fitter(): 16 16 return FIT_CONFIG.selected_fitter, FIT_CONFIG.selected_values 17 except :17 except ImportError: 18 18 # CRUFT: Bumps changed its handling of fit options around 0.7.5.6 19 19 # Default bumps to use the Levenberg-Marquardt optimizer … … 56 56 header = "=== Steps: %s chisq: %s ETA: %s\n"%(step, chisq, time) 57 57 parameters = ["%15s: %-10.3g%s"%(k,v,("\n" if i%3==2 else " | ")) 58 for i, (k,v) in enumerate(zip(pars,history.point[0]))]58 for i, (k, v) in enumerate(zip(pars, history.point[0]))] 59 59 self.msg = "".join([header]+parameters) 60 60 … … 77 77 self.handler.set_result(Progress(history, self.max_step, self.pars, self.dof)) 78 78 self.handler.progress(history.step[0], self.max_step) 79 if len(history.step) >1 and history.step[1] > history.step[0]:79 if len(history.step) > 1 and history.step[1] > history.step[0]: 80 80 self.handler.improvement() 81 81 self.handler.update_fit() … … 97 97 try: 98 98 p = history.population_values[0] 99 n, p = len(p), np.sort(p)100 QI, Qmid, = int(0.2*n),int(0.5*n)101 self.convergence.append((best, p[0], p[QI],p[Qmid],p[-1-QI],p[-1]))102 except :103 self.convergence.append((best, best, best,best,best,best))99 n, p = len(p), np.sort(p) 100 QI, Qmid = int(0.2*n), int(0.5*n) 101 self.convergence.append((best, p[0], p[QI], p[Qmid], p[-1-QI], p[-1])) 102 except Exception: 103 self.convergence.append((best, best, best, best, best, best)) 104 104 105 105 … … 131 131 132 132 def _reset_pars(self, names, values): 133 for k, v in zip(names, values):133 for k, v in zip(names, values): 134 134 self._pars[k].value = v 135 135 … … 137 137 self._pars = {} 138 138 for k in self.model.getParamList(): 139 name = ".".join((self.name, k))139 name = ".".join((self.name, k)) 140 140 value = self.model.getParam(k) 141 bounds = self.model.details.get(k, ["",None,None])[1:3]141 bounds = self.model.details.get(k, ["", None, None])[1:3] 142 142 self._pars[k] = parameter.Parameter(value=value, bounds=bounds, 143 143 fixed=True, name=name) … … 145 145 146 146 def _init_pars(self, kw): 147 for k, v in kw.items():147 for k, v in kw.items(): 148 148 # dispersion parameters initialized with _field instead of .field 149 if k.endswith('_width'): k = k[:-6]+'.width' 150 elif k.endswith('_npts'): k = k[:-5]+'.npts' 151 elif k.endswith('_nsigmas'): k = k[:-7]+'.nsigmas' 152 elif k.endswith('_type'): k = k[:-5]+'.type' 149 if k.endswith('_width'): 150 k = k[:-6]+'.width' 151 elif k.endswith('_npts'): 152 k = k[:-5]+'.npts' 153 elif k.endswith('_nsigmas'): 154 k = k[:-7]+'.nsigmas' 155 elif k.endswith('_type'): 156 k = k[:-5]+'.type' 153 157 if k not in self._pars: 154 158 formatted_pars = ", ".join(sorted(self._pars.keys())) … … 159 163 elif isinstance(v, parameter.BaseParameter): 160 164 self._pars[k] = v 161 elif isinstance(v, (tuple, list)):165 elif isinstance(v, (tuple, list)): 162 166 low, high = v 163 167 self._pars[k].value = (low+high)/2 164 self._pars[k].range(low, high)168 self._pars[k].range(low, high) 165 169 else: 166 170 self._pars[k].value = v … … 170 174 Flag a set of parameters as fitted parameters. 171 175 """ 172 for k, p in self._pars.items():176 for k, p in self._pars.items(): 173 177 p.fixed = (k not in param_list or k in self.constraints) 174 178 self.fitted_par_names = [k for k in param_list if k not in self.constraints] … … 182 186 183 187 def update(self): 184 for k, v in self._pars.items():188 for k, v in self._pars.items(): 185 189 #print "updating",k,v,v.value 186 self.model.setParam(k, v.value)190 self.model.setParam(k, v.value) 187 191 self._dirty = True 188 192 … … 223 227 symtab = dict((".".join((M.name, k)), p) 224 228 for M in self.models 225 for k, p in M.parameters().items())229 for k, p in M.parameters().items()) 226 230 self.update = compile_constraints(symtab, exprs) 227 231 else: … … 300 304 np.NaN*np.ones(len(fitness.computed_pars)))) 301 305 R.pvec = np.hstack((result['value'][fitted_index], 302 306 [p.value for p in fitness.computed_pars])) 303 307 R.fitness = np.sum(R.residuals**2)/(fitness.numpoints() - len(fitted_index)) 304 308 else: 305 309 R.stderr = np.NaN*np.ones(len(param_list)) 306 R.pvec = np.asarray( 310 R.pvec = np.asarray([p.value for p in fitness.fitted_pars+fitness.computed_pars]) 307 311 R.fitness = np.NaN 308 312 R.convergence = result['convergence'] … … 331 335 steps = options.get('steps', 0) 332 336 if steps == 0: 333 pop = options.get('pop', 0)*len(problem._parameters)337 pop = options.get('pop', 0)*len(problem._parameters) 334 338 samples = options.get('samples', 0) 335 339 steps = (samples+pop-1)/pop if pop != 0 else samples … … 343 347 fitdriver = fitters.FitDriver(fitclass, problem=problem, 344 348 abort_test=abort_test, **options) 345 omp_threads = int(os.environ.get('OMP_NUM_THREADS', '0'))349 omp_threads = int(os.environ.get('OMP_NUM_THREADS', '0')) 346 350 mapper = MPMapper if omp_threads == 1 else SerialMapper 347 351 fitdriver.mapper = mapper.start_mapper(problem, None) … … 359 363 convergence_list = options['monitors'][-1].convergence 360 364 convergence = (2*np.asarray(convergence_list)/problem.dof 361 if convergence_list else np.empty((0, 1),'d'))365 if convergence_list else np.empty((0, 1), 'd')) 362 366 363 367 success = best is not None … … 376 380 'errors': '\n'.join(errors), 377 381 } 378 -
src/sas/sascalc/fit/qsmearing.py
r8938502 r50fcb09 5 5 #This software was developed by the University of Tennessee as part of the 6 6 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 7 #project funded by the US National Science Foundation. 7 #project funded by the US National Science Foundation. 8 8 #See the license text in license.txt 9 9 #copyright 2008, University of Tennessee … … 19 19 from sasmodels.sesans import SesansTransform 20 20 from sasmodels.resolution2d import Pinhole2D 21 from .nxsunit import Converter 21 22 from sas.sascalc.data_util.nxsunit import Converter 22 23 23 24 def smear_selection(data, model = None):
Note: See TracChangeset
for help on using the changeset viewer.