Changes in src/sas/sasgui/perspectives/fitting/fitproblem.py [959eb01:251ef684] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/fitting/fitproblem.py
r959eb01 r251ef684 14 14 ################################################################################ 15 15 import copy 16 from sas.sascalc.data_util.qsmearing import smear_selection 17 18 class FitProblemComponent(object): 16 17 from sas.sascalc.fit.qsmearing import smear_selection 18 19 class FitProblem(object): 19 20 """ 20 Inferface containing information to store data, model, range of data, etc... 21 and retreive this information. This is an inferface 22 for a fitProblem i.e relationship between data and model. 21 Define the relationship between data and model, including range, weights, 22 etc. 23 23 """ 24 def __init__(self): 25 """ 26 contains information about data and model to fit 27 """ 28 ## data used for fitting 29 self.fit_data = None 30 self.theory_data = None 31 self.residuals = None 32 # original data: should not be modified 33 self.original_data = None 34 ## the current model 35 self.model = None 36 ## if 1 this fit problem will be selected to fit , if 0 37 ## it will not be selected for fit 38 self.schedule = 0 39 ##list containing parameter name and value 40 self.list_param = [] 41 self.list_param2fit = [] 42 ## smear object to smear or not data1D 43 self.smearer_computed = False 44 self.smearer_enable = False 45 self.smearer_computer_value = None 46 ## fitting range 47 self.qmin = None 48 self.qmax = None 49 # fit weight 50 self.weight = None 51 self.result = None 52 self.fit_tab_caption = None 53 self.name_per_page = None 54 24 55 def enable_smearing(self, flag=False): 25 56 """ … … 27 58 flag is 0 ingore smear value. 28 59 """ 60 self.smearer_enable = flag 61 62 def set_smearer(self, smearer): 63 """ 64 save reference of smear object on fitdata 65 66 :param smear: smear object from DataLoader 67 68 """ 69 self.smearer_computer_value = smearer 29 70 30 71 def get_smearer(self): … … 32 73 return smear object 33 74 """ 75 if not self.smearer_enable: 76 return None 77 if not self.smearer_computed: 78 #smeari_selection should be call only once per fitproblem 79 self.smearer_computer_value = smear_selection(self.fit_data, 80 self.model) 81 self.smearer_computed = True 82 return self.smearer_computer_value 83 34 84 def save_model_name(self, name): 35 85 """ 36 86 """ 87 self.name_per_page = name 37 88 38 89 def get_name(self): 39 90 """ 40 91 """ 92 return self.name_per_page 41 93 42 94 def set_model(self, model): … … 46 98 :param name: name created for model 47 99 """ 100 self.model = model 101 self.smearer_computer_value = smear_selection(self.fit_data, 102 self.model) 103 self.smearer_computed = True 48 104 49 105 def get_model(self): … … 51 107 :return: saved model 52 108 """ 109 return self.model 53 110 54 111 def set_residuals(self, residuals): … … 57 114 :param data: data selected 58 115 """ 116 self.residuals = residuals 59 117 60 118 def get_residuals(self): … … 62 120 :return: residuals 63 121 """ 122 return self.residuals 64 123 65 124 def set_theory_data(self, data): 66 125 """ 67 126 save a copy of the data select to fit 127 68 128 :param data: data selected 69 """ 129 130 """ 131 self.theory_data = copy.deepcopy(data) 70 132 71 133 def get_theory_data(self): 72 134 """ 73 :return: list of data dList 74 """ 135 :return: theory generated with the current model and data of this class 136 """ 137 return self.theory_data 75 138 76 139 def set_fit_data(self, data): 77 140 """ 78 Store of list of data and create by create new fitproblem of each data 79 id, if there was existing information about model, this information 80 get copy to the new fitproblem 141 Store data associated with this class 81 142 :param data: list of data selected 82 143 """ 144 self.original_data = None 145 self.fit_data = None 146 # original data: should not be modified 147 self.original_data = data 148 # fit data: used for fit and can be modified for convenience 149 self.fit_data = copy.deepcopy(data) 150 self.smearer_computer_value = smear_selection(self.fit_data, self.model) 151 self.smearer_computed = True 152 self.result = None 83 153 84 154 def get_fit_data(self): 85 155 """ 86 """ 156 :return: data associate with this class 157 """ 158 return self.fit_data 159 160 def get_origin_data(self): 161 """ 162 """ 163 return self.original_data 164 165 def set_weight(self, is2d, flag=None): 166 """ 167 Received flag and compute error on data. 168 :param flag: flag to transform error of data. 169 :param is2d: flag to distinguish 1D to 2D Data 170 """ 171 from sas.sasgui.perspectives.fitting.utils import get_weight 172 # send original data for weighting 173 self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag) 174 if is2d: 175 self.fit_data.err_data = self.weight 176 else: 177 self.fit_data.dy = self.weight 178 179 def get_weight(self): 180 """ 181 returns weight array 182 """ 183 return self.weight 184 185 def set_param2fit(self, list): 186 """ 187 Store param names to fit (checked) 188 :param list: list of the param names 189 """ 190 self.list_param2fit = list 191 192 def get_param2fit(self): 193 """ 194 return the list param names to fit 195 """ 196 return self.list_param2fit 87 197 88 198 def set_model_param(self, name, value=None): … … 92 202 :param value: value of that parameter 93 203 """ 94 95 def set_param2fit(self, list): 96 """ 97 Store param names to fit (checked) 98 :param list: list of the param names 99 """ 100 101 def get_param2fit(self): 102 """ 103 return the list param names to fit 104 """ 204 self.list_param.append([name, value]) 105 205 106 206 def get_model_param(self): … … 108 208 return list of couple of parameter name and value 109 209 """ 210 return self.list_param 110 211 111 212 def schedule_tofit(self, schedule=0): … … 113 214 set schedule to true to decide if this fit must be performed 114 215 """ 216 self.schedule = schedule 115 217 116 218 def get_scheduled(self): … … 118 220 return true or false if a problem as being schedule for fitting 119 221 """ 222 return self.schedule 120 223 121 224 def set_range(self, qmin=None, qmax=None): 122 225 """ 123 226 set fitting range 124 """ 227 :param qmin: minimum value to consider for the fit range 228 :param qmax: maximum value to consider for the fit range 229 """ 230 self.qmin = qmin 231 self.qmax = qmax 125 232 126 233 def get_range(self): 127 234 """ 128 235 :return: fitting range 129 """ 130 131 def set_weight(self, flag=None): 132 """ 133 set fitting range 134 """ 135 136 def get_weight(self): 137 """ 138 get fitting weight 139 """ 236 237 """ 238 return self.qmin, self.qmax 140 239 141 240 def clear_model_param(self): … … 143 242 clear constraint info 144 243 """ 244 self.list_param = [] 145 245 146 246 def set_fit_tab_caption(self, caption): 147 247 """ 148 store the caption of the page associated with object149 """248 """ 249 self.fit_tab_caption = str(caption) 150 250 151 251 def get_fit_tab_caption(self): 152 252 """ 153 Return the caption of the page associated with object154 """253 """ 254 return self.fit_tab_caption 155 255 156 256 def set_graph_id(self, id): … … 158 258 Set graph id (from data_group_id at the time the graph produced) 159 259 """ 260 self.graph_id = id 160 261 161 262 def get_graph_id(self): … … 163 264 Get graph_id 164 265 """ 266 return self.graph_id 165 267 166 268 def set_result(self, result): 167 269 """ 168 270 """ 271 self.result = result 169 272 170 273 def get_result(self): … … 172 275 get result 173 276 """ 174 175 176 class FitProblemDictionary(FitProblemComponent, dict): 277 return self.result 278 279 280 class FitProblemDictionary(dict): 177 281 """ 178 282 This module implements a dictionary of fitproblem objects 179 283 """ 180 284 def __init__(self): 181 FitProblemComponent.__init__(self)182 285 dict.__init__(self) 183 286 ## the current model … … 206 309 self._smear_on = flag 207 310 if fid is None: 208 for value in self. itervalues():311 for value in self.values(): 209 312 value.enable_smearing(flag) 210 else: 211 if fid in self.iterkeys(): 212 self[fid].enable_smearing(flag) 313 elif fid in self: 314 self[fid].enable_smearing(flag) 213 315 214 316 def set_smearer(self, smearer, fid=None): … … 218 320 """ 219 321 if fid is None: 220 for value in self. itervalues():322 for value in self.values(): 221 323 value.set_smearer(smearer) 222 else: 223 if fid in self.iterkeys(): 224 self[fid].set_smearer(smearer) 324 elif fid in self: 325 self[fid].set_smearer(smearer) 225 326 226 327 def get_smearer(self, fid=None): … … 228 329 return smear object 229 330 """ 230 if fid in self .iterkeys():331 if fid in self: 231 332 return self[fid].get_smearer() 232 333 … … 235 336 """ 236 337 if fid is None: 237 for value in self. itervalues():338 for value in self.values(): 238 339 value.save_model_name(name) 239 else: 240 if fid in self.iterkeys(): 241 self[fid].save_model_name(name) 340 elif fid in self: 341 self[fid].save_model_name(name) 242 342 243 343 def get_name(self, fid=None): … … 246 346 result = [] 247 347 if fid is None: 248 for value in self. itervalues():348 for value in self.values(): 249 349 result.append(value.get_name()) 250 else: 251 if fid in self.iterkeys(): 252 result.append(self[fid].get_name()) 350 elif fid in self: 351 result.append(self[fid].get_name()) 253 352 return result 254 353 … … 261 360 self.model = model 262 361 if fid is None: 263 for value in self. itervalues():362 for value in self.values(): 264 363 value.set_model(self.model) 265 else: 266 if fid in self.iterkeys(): 267 self[fid].set_model(self.model) 364 elif fid in self: 365 self[fid].set_model(self.model) 268 366 269 367 def get_model(self, fid): … … 271 369 :return: saved model 272 370 """ 273 if fid in self .iterkeys():371 if fid in self: 274 372 return self[fid].get_model() 275 373 … … 291 389 :param data: data selected 292 390 """ 293 if fid in self .iterkeys():391 if fid in self: 294 392 self[fid].set_residuals(residuals) 295 393 … … 298 396 :return: residuals 299 397 """ 300 if fid in self .iterkeys():398 if fid in self: 301 399 return self[fid].get_residuals() 302 400 … … 306 404 :param data: data selected 307 405 """ 308 if fid in self .iterkeys():406 if fid in self: 309 407 self[fid].set_theory_data(data) 310 408 … … 313 411 :return: list of data dList 314 412 """ 315 if fid in self .iterkeys():413 if fid in self: 316 414 return self[fid].get_theory_data() 317 415 … … 322 420 :note: only data changes in the fit problem 323 421 """ 324 if data.id not in self .iterkeys():422 if data.id not in self: 325 423 self[data.id] = FitProblem() 326 424 self[data.id].set_fit_data(data) … … 336 434 data = [] 337 435 for d in data: 338 if (d is not None):339 if (d.id not in self.iterkeys()):436 if d is not None: 437 if d.id not in self: 340 438 self[d.id] = FitProblem() 341 439 self[d.id].set_fit_data(d) … … 348 446 :param fid: key representing a fitproblem, usually extract from data id 349 447 """ 350 if fid in self .iterkeys():448 if fid in self: 351 449 return self[fid].get_fit_data() 352 450 … … 358 456 """ 359 457 if fid is None: 360 for value in self. itervalues():458 for value in self.values(): 361 459 value.set_model_param(name, value) 362 else: 363 if fid in self.iterkeys(): 364 self[fid].set_model_param(name, value) 460 elif fid in self: 461 self[fid].set_model_param(name, value) 365 462 366 463 def get_model_param(self, fid): … … 368 465 return list of couple of parameter name and value 369 466 """ 370 if fid in self .iterkeys():467 if fid in self: 371 468 return self[fid].get_model_param() 372 469 … … 389 486 """ 390 487 self.scheduled = schedule 391 for value in self. itervalues():488 for value in self.values(): 392 489 value.schedule_tofit(schedule) 393 490 … … 405 502 self.qmax = qmax 406 503 if fid is None: 407 for value in self. itervalues():504 for value in self.values(): 408 505 value.set_range(self.qmin, self.qmax) 409 else: 410 if fid in self.iterkeys(): 411 self[fid].value.set_range(self.qmin, self.qmax) 506 elif fid in self: 507 self[fid].value.set_range(self.qmin, self.qmax) 412 508 413 509 def get_range(self, fid): … … 415 511 :return: fitting range 416 512 """ 417 if fid in self .iterkeys():513 if fid in self: 418 514 return self[fid].get_range() 419 515 … … 423 519 """ 424 520 if fid is None: 425 for value in self. itervalues():521 for value in self.values(): 426 522 value.set_weight(flag=flag, is2d=is2d) 427 else: 428 if fid in self.iterkeys(): 429 self[fid].set_weight(flag=flag, is2d=is2d) 523 elif fid in self: 524 self[fid].set_weight(flag=flag, is2d=is2d) 430 525 431 526 def get_weight(self, fid=None): … … 433 528 return fit weight 434 529 """ 435 if fid in self .iterkeys():530 if fid in self: 436 531 return self[fid].get_weight() 437 532 … … 441 536 """ 442 537 if fid is None: 443 for value in self. itervalues():538 for value in self.values(): 444 539 value.clear_model_param() 445 else: 446 if fid in self.iterkeys(): 447 self[fid].clear_model_param() 540 elif fid in self: 541 self[fid].clear_model_param() 448 542 449 543 def get_fit_problem(self): … … 451 545 return fitproblem contained in this dictionary 452 546 """ 453 return self. itervalues()547 return self.values() 454 548 455 549 def set_result(self, result, fid): 456 550 """ 457 551 """ 458 if fid in self .iterkeys():552 if fid in self: 459 553 self[fid].set_result(result) 460 554 … … 470 564 get result 471 565 """ 472 if fid in self .iterkeys():566 if fid in self: 473 567 return self[fid].get_result() 474 568 … … 490 584 """ 491 585 return self.graph_id 492 493 494 class FitProblem(FitProblemComponent):495 """496 FitProblem class allows to link a model with the new name created in _on_model,497 a name theory created with that model and the data fitted with the model.498 FitProblem is mostly used as value of the dictionary by fitting module.499 """500 def __init__(self):501 FitProblemComponent.__init__(self)502 """503 contains information about data and model to fit504 """505 ## data used for fitting506 self.fit_data = None507 self.theory_data = None508 self.residuals = None509 # original data: should not be modified510 self.original_data = None511 ## the current model512 self.model = None513 ## if 1 this fit problem will be selected to fit , if 0514 ## it will not be selected for fit515 self.schedule = 0516 ##list containing parameter name and value517 self.list_param = []518 ## smear object to smear or not data1D519 self.smearer_computed = False520 self.smearer_enable = False521 self.smearer_computer_value = None522 ## fitting range523 self.qmin = None524 self.qmax = None525 # fit weight526 self.weight = None527 self.result = None528 529 def enable_smearing(self, flag=False):530 """531 :param flag: bool.When flag is 1 get the computer smear value. When532 flag is 0 ingore smear value.533 """534 self.smearer_enable = flag535 536 def set_smearer(self, smearer):537 """538 save reference of smear object on fitdata539 540 :param smear: smear object from DataLoader541 542 """543 self.smearer_computer_value = smearer544 545 def get_smearer(self):546 """547 return smear object548 """549 if not self.smearer_enable:550 return None551 if not self.smearer_computed:552 #smeari_selection should be call only once per fitproblem553 self.smearer_computer_value = smear_selection(self.fit_data,554 self.model)555 self.smearer_computed = True556 return self.smearer_computer_value557 558 def save_model_name(self, name):559 """560 """561 self.name_per_page = name562 563 def get_name(self):564 """565 """566 return self.name_per_page567 568 def set_model(self, model):569 """570 associates each model with its new created name571 :param model: model selected572 :param name: name created for model573 """574 self.model = model575 self.smearer_computer_value = smear_selection(self.fit_data,576 self.model)577 self.smearer_computed = True578 579 def get_model(self):580 """581 :return: saved model582 """583 return self.model584 585 def set_residuals(self, residuals):586 """587 save a copy of residual588 :param data: data selected589 """590 self.residuals = residuals591 592 def get_residuals(self):593 """594 :return: residuals595 """596 return self.residuals597 598 def set_theory_data(self, data):599 """600 save a copy of the data select to fit601 602 :param data: data selected603 604 """605 self.theory_data = copy.deepcopy(data)606 607 def get_theory_data(self):608 """609 :return: theory generated with the current model and data of this class610 """611 return self.theory_data612 613 def set_fit_data(self, data):614 """615 Store data associated with this class616 :param data: list of data selected617 """618 self.original_data = None619 self.fit_data = None620 # original data: should not be modified621 self.original_data = data622 # fit data: used for fit and can be modified for convenience623 self.fit_data = copy.deepcopy(data)624 self.smearer_computer_value = smear_selection(self.fit_data,625 self.model)626 self.smearer_computed = True627 self.result = None628 629 def get_fit_data(self):630 """631 :return: data associate with this class632 """633 return self.fit_data634 635 def get_origin_data(self):636 """637 """638 return self.original_data639 640 def set_weight(self, is2d, flag=None):641 """642 Received flag and compute error on data.643 :param flag: flag to transform error of data.644 :param is2d: flag to distinguish 1D to 2D Data645 """646 from sas.sasgui.perspectives.fitting.utils import get_weight647 # send original data for weighting648 self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag)649 if is2d:650 self.fit_data.err_data = self.weight651 else:652 self.fit_data.dy = self.weight653 654 def get_weight(self):655 """656 returns weight array657 """658 return self.weight659 660 def set_param2fit(self, list):661 """662 Store param names to fit (checked)663 :param list: list of the param names664 """665 self.list_param2fit = list666 667 def get_param2fit(self):668 """669 return the list param names to fit670 """671 return self.list_param2fit672 673 def set_model_param(self, name, value=None):674 """675 Store the name and value of a parameter of this fitproblem's model676 :param name: name of the given parameter677 :param value: value of that parameter678 """679 self.list_param.append([name, value])680 681 def get_model_param(self):682 """683 return list of couple of parameter name and value684 """685 return self.list_param686 687 def schedule_tofit(self, schedule=0):688 """689 set schedule to true to decide if this fit must be performed690 """691 self.schedule = schedule692 693 def get_scheduled(self):694 """695 return true or false if a problem as being schedule for fitting696 """697 return self.schedule698 699 def set_range(self, qmin=None, qmax=None):700 """701 set fitting range702 :param qmin: minimum value to consider for the fit range703 :param qmax: maximum value to consider for the fit range704 """705 self.qmin = qmin706 self.qmax = qmax707 708 def get_range(self):709 """710 :return: fitting range711 712 """713 return self.qmin, self.qmax714 715 def clear_model_param(self):716 """717 clear constraint info718 """719 self.list_param = []720 721 def set_fit_tab_caption(self, caption):722 """723 """724 self.fit_tab_caption = str(caption)725 726 def get_fit_tab_caption(self):727 """728 """729 return self.fit_tab_caption730 731 def set_graph_id(self, id):732 """733 Set graph id (from data_group_id at the time the graph produced)734 """735 self.graph_id = id736 737 def get_graph_id(self):738 """739 Get graph_id740 """741 return self.graph_id742 743 def set_result(self, result):744 """745 """746 self.result = result747 748 def get_result(self):749 """750 get result751 """752 return self.result
Note: See TracChangeset
for help on using the changeset viewer.