Changeset 251ef684 in sasview
- Timestamp:
- Jun 27, 2017 2:19:02 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:
- 277257f
- Parents:
- 50fcb09
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/fitting/fitproblem.py
r50fcb09 r251ef684 17 17 from sas.sascalc.fit.qsmearing import smear_selection 18 18 19 class FitProblem Component(object):19 class FitProblem(object): 20 20 """ 21 Inferface containing information to store data, model, range of data, etc... 22 and retreive this information. This is an inferface 23 for a fitProblem i.e relationship between data and model. 21 Define the relationship between data and model, including range, weights, 22 etc. 24 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 25 55 def enable_smearing(self, flag=False): 26 56 """ … … 28 58 flag is 0 ingore smear value. 29 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 30 70 31 71 def get_smearer(self): … … 33 73 return smear object 34 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 35 84 def save_model_name(self, name): 36 85 """ 37 86 """ 87 self.name_per_page = name 38 88 39 89 def get_name(self): 40 90 """ 41 91 """ 92 return self.name_per_page 42 93 43 94 def set_model(self, model): … … 47 98 :param name: name created for model 48 99 """ 100 self.model = model 101 self.smearer_computer_value = smear_selection(self.fit_data, 102 self.model) 103 self.smearer_computed = True 49 104 50 105 def get_model(self): … … 52 107 :return: saved model 53 108 """ 109 return self.model 54 110 55 111 def set_residuals(self, residuals): … … 58 114 :param data: data selected 59 115 """ 116 self.residuals = residuals 60 117 61 118 def get_residuals(self): … … 63 120 :return: residuals 64 121 """ 122 return self.residuals 65 123 66 124 def set_theory_data(self, data): 67 125 """ 68 126 save a copy of the data select to fit 127 69 128 :param data: data selected 70 """ 129 130 """ 131 self.theory_data = copy.deepcopy(data) 71 132 72 133 def get_theory_data(self): 73 134 """ 74 :return: list of data dList 75 """ 135 :return: theory generated with the current model and data of this class 136 """ 137 return self.theory_data 76 138 77 139 def set_fit_data(self, data): 78 140 """ 79 Store of list of data and create by create new fitproblem of each data 80 id, if there was existing information about model, this information 81 get copy to the new fitproblem 141 Store data associated with this class 82 142 :param data: list of data selected 83 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 84 153 85 154 def get_fit_data(self): 86 155 """ 87 """ 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 88 197 89 198 def set_model_param(self, name, value=None): … … 93 202 :param value: value of that parameter 94 203 """ 95 96 def set_param2fit(self, list): 97 """ 98 Store param names to fit (checked) 99 :param list: list of the param names 100 """ 101 102 def get_param2fit(self): 103 """ 104 return the list param names to fit 105 """ 204 self.list_param.append([name, value]) 106 205 107 206 def get_model_param(self): … … 109 208 return list of couple of parameter name and value 110 209 """ 210 return self.list_param 111 211 112 212 def schedule_tofit(self, schedule=0): … … 114 214 set schedule to true to decide if this fit must be performed 115 215 """ 216 self.schedule = schedule 116 217 117 218 def get_scheduled(self): … … 119 220 return true or false if a problem as being schedule for fitting 120 221 """ 222 return self.schedule 121 223 122 224 def set_range(self, qmin=None, qmax=None): 123 225 """ 124 226 set fitting range 125 """ 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 126 232 127 233 def get_range(self): 128 234 """ 129 235 :return: fitting range 130 """ 131 132 def set_weight(self, flag=None): 133 """ 134 set fitting range 135 """ 136 137 def get_weight(self): 138 """ 139 get fitting weight 140 """ 236 237 """ 238 return self.qmin, self.qmax 141 239 142 240 def clear_model_param(self): … … 144 242 clear constraint info 145 243 """ 244 self.list_param = [] 146 245 147 246 def set_fit_tab_caption(self, caption): 148 247 """ 149 store the caption of the page associated with object150 """248 """ 249 self.fit_tab_caption = str(caption) 151 250 152 251 def get_fit_tab_caption(self): 153 252 """ 154 Return the caption of the page associated with object155 """253 """ 254 return self.fit_tab_caption 156 255 157 256 def set_graph_id(self, id): … … 159 258 Set graph id (from data_group_id at the time the graph produced) 160 259 """ 260 self.graph_id = id 161 261 162 262 def get_graph_id(self): … … 164 264 Get graph_id 165 265 """ 266 return self.graph_id 166 267 167 268 def set_result(self, result): 168 269 """ 169 270 """ 271 self.result = result 170 272 171 273 def get_result(self): … … 173 275 get result 174 276 """ 175 176 177 class FitProblemDictionary(FitProblemComponent, dict): 277 return self.result 278 279 280 class FitProblemDictionary(dict): 178 281 """ 179 282 This module implements a dictionary of fitproblem objects 180 283 """ 181 284 def __init__(self): 182 FitProblemComponent.__init__(self)183 285 dict.__init__(self) 184 286 ## the current model … … 207 309 self._smear_on = flag 208 310 if fid is None: 209 for value in self. itervalues():311 for value in self.values(): 210 312 value.enable_smearing(flag) 211 else: 212 if fid in self.iterkeys(): 213 self[fid].enable_smearing(flag) 313 elif fid in self: 314 self[fid].enable_smearing(flag) 214 315 215 316 def set_smearer(self, smearer, fid=None): … … 219 320 """ 220 321 if fid is None: 221 for value in self. itervalues():322 for value in self.values(): 222 323 value.set_smearer(smearer) 223 else: 224 if fid in self.iterkeys(): 225 self[fid].set_smearer(smearer) 324 elif fid in self: 325 self[fid].set_smearer(smearer) 226 326 227 327 def get_smearer(self, fid=None): … … 229 329 return smear object 230 330 """ 231 if fid in self .iterkeys():331 if fid in self: 232 332 return self[fid].get_smearer() 233 333 … … 236 336 """ 237 337 if fid is None: 238 for value in self. itervalues():338 for value in self.values(): 239 339 value.save_model_name(name) 240 else: 241 if fid in self.iterkeys(): 242 self[fid].save_model_name(name) 340 elif fid in self: 341 self[fid].save_model_name(name) 243 342 244 343 def get_name(self, fid=None): … … 247 346 result = [] 248 347 if fid is None: 249 for value in self. itervalues():348 for value in self.values(): 250 349 result.append(value.get_name()) 251 else: 252 if fid in self.iterkeys(): 253 result.append(self[fid].get_name()) 350 elif fid in self: 351 result.append(self[fid].get_name()) 254 352 return result 255 353 … … 262 360 self.model = model 263 361 if fid is None: 264 for value in self. itervalues():362 for value in self.values(): 265 363 value.set_model(self.model) 266 else: 267 if fid in self.iterkeys(): 268 self[fid].set_model(self.model) 364 elif fid in self: 365 self[fid].set_model(self.model) 269 366 270 367 def get_model(self, fid): … … 272 369 :return: saved model 273 370 """ 274 if fid in self .iterkeys():371 if fid in self: 275 372 return self[fid].get_model() 276 373 … … 292 389 :param data: data selected 293 390 """ 294 if fid in self .iterkeys():391 if fid in self: 295 392 self[fid].set_residuals(residuals) 296 393 … … 299 396 :return: residuals 300 397 """ 301 if fid in self .iterkeys():398 if fid in self: 302 399 return self[fid].get_residuals() 303 400 … … 307 404 :param data: data selected 308 405 """ 309 if fid in self .iterkeys():406 if fid in self: 310 407 self[fid].set_theory_data(data) 311 408 … … 314 411 :return: list of data dList 315 412 """ 316 if fid in self .iterkeys():413 if fid in self: 317 414 return self[fid].get_theory_data() 318 415 … … 323 420 :note: only data changes in the fit problem 324 421 """ 325 if data.id not in self .iterkeys():422 if data.id not in self: 326 423 self[data.id] = FitProblem() 327 424 self[data.id].set_fit_data(data) … … 337 434 data = [] 338 435 for d in data: 339 if (d is not None):340 if (d.id not in self.iterkeys()):436 if d is not None: 437 if d.id not in self: 341 438 self[d.id] = FitProblem() 342 439 self[d.id].set_fit_data(d) … … 349 446 :param fid: key representing a fitproblem, usually extract from data id 350 447 """ 351 if fid in self .iterkeys():448 if fid in self: 352 449 return self[fid].get_fit_data() 353 450 … … 359 456 """ 360 457 if fid is None: 361 for value in self. itervalues():458 for value in self.values(): 362 459 value.set_model_param(name, value) 363 else: 364 if fid in self.iterkeys(): 365 self[fid].set_model_param(name, value) 460 elif fid in self: 461 self[fid].set_model_param(name, value) 366 462 367 463 def get_model_param(self, fid): … … 369 465 return list of couple of parameter name and value 370 466 """ 371 if fid in self .iterkeys():467 if fid in self: 372 468 return self[fid].get_model_param() 373 469 … … 390 486 """ 391 487 self.scheduled = schedule 392 for value in self. itervalues():488 for value in self.values(): 393 489 value.schedule_tofit(schedule) 394 490 … … 406 502 self.qmax = qmax 407 503 if fid is None: 408 for value in self. itervalues():504 for value in self.values(): 409 505 value.set_range(self.qmin, self.qmax) 410 else: 411 if fid in self.iterkeys(): 412 self[fid].value.set_range(self.qmin, self.qmax) 506 elif fid in self: 507 self[fid].value.set_range(self.qmin, self.qmax) 413 508 414 509 def get_range(self, fid): … … 416 511 :return: fitting range 417 512 """ 418 if fid in self .iterkeys():513 if fid in self: 419 514 return self[fid].get_range() 420 515 … … 424 519 """ 425 520 if fid is None: 426 for value in self. itervalues():521 for value in self.values(): 427 522 value.set_weight(flag=flag, is2d=is2d) 428 else: 429 if fid in self.iterkeys(): 430 self[fid].set_weight(flag=flag, is2d=is2d) 523 elif fid in self: 524 self[fid].set_weight(flag=flag, is2d=is2d) 431 525 432 526 def get_weight(self, fid=None): … … 434 528 return fit weight 435 529 """ 436 if fid in self .iterkeys():530 if fid in self: 437 531 return self[fid].get_weight() 438 532 … … 442 536 """ 443 537 if fid is None: 444 for value in self. itervalues():538 for value in self.values(): 445 539 value.clear_model_param() 446 else: 447 if fid in self.iterkeys(): 448 self[fid].clear_model_param() 540 elif fid in self: 541 self[fid].clear_model_param() 449 542 450 543 def get_fit_problem(self): … … 452 545 return fitproblem contained in this dictionary 453 546 """ 454 return self. itervalues()547 return self.values() 455 548 456 549 def set_result(self, result, fid): 457 550 """ 458 551 """ 459 if fid in self .iterkeys():552 if fid in self: 460 553 self[fid].set_result(result) 461 554 … … 471 564 get result 472 565 """ 473 if fid in self .iterkeys():566 if fid in self: 474 567 return self[fid].get_result() 475 568 … … 491 584 """ 492 585 return self.graph_id 493 494 495 class FitProblem(FitProblemComponent):496 """497 FitProblem class allows to link a model with the new name created in _on_model,498 a name theory created with that model and the data fitted with the model.499 FitProblem is mostly used as value of the dictionary by fitting module.500 """501 def __init__(self):502 FitProblemComponent.__init__(self)503 """504 contains information about data and model to fit505 """506 ## data used for fitting507 self.fit_data = None508 self.theory_data = None509 self.residuals = None510 # original data: should not be modified511 self.original_data = None512 ## the current model513 self.model = None514 ## if 1 this fit problem will be selected to fit , if 0515 ## it will not be selected for fit516 self.schedule = 0517 ##list containing parameter name and value518 self.list_param = []519 ## smear object to smear or not data1D520 self.smearer_computed = False521 self.smearer_enable = False522 self.smearer_computer_value = None523 ## fitting range524 self.qmin = None525 self.qmax = None526 # fit weight527 self.weight = None528 self.result = None529 530 def enable_smearing(self, flag=False):531 """532 :param flag: bool.When flag is 1 get the computer smear value. When533 flag is 0 ingore smear value.534 """535 self.smearer_enable = flag536 537 def set_smearer(self, smearer):538 """539 save reference of smear object on fitdata540 541 :param smear: smear object from DataLoader542 543 """544 self.smearer_computer_value = smearer545 546 def get_smearer(self):547 """548 return smear object549 """550 if not self.smearer_enable:551 return None552 if not self.smearer_computed:553 #smeari_selection should be call only once per fitproblem554 self.smearer_computer_value = smear_selection(self.fit_data,555 self.model)556 self.smearer_computed = True557 return self.smearer_computer_value558 559 def save_model_name(self, name):560 """561 """562 self.name_per_page = name563 564 def get_name(self):565 """566 """567 return self.name_per_page568 569 def set_model(self, model):570 """571 associates each model with its new created name572 :param model: model selected573 :param name: name created for model574 """575 self.model = model576 self.smearer_computer_value = smear_selection(self.fit_data,577 self.model)578 self.smearer_computed = True579 580 def get_model(self):581 """582 :return: saved model583 """584 return self.model585 586 def set_residuals(self, residuals):587 """588 save a copy of residual589 :param data: data selected590 """591 self.residuals = residuals592 593 def get_residuals(self):594 """595 :return: residuals596 """597 return self.residuals598 599 def set_theory_data(self, data):600 """601 save a copy of the data select to fit602 603 :param data: data selected604 605 """606 self.theory_data = copy.deepcopy(data)607 608 def get_theory_data(self):609 """610 :return: theory generated with the current model and data of this class611 """612 return self.theory_data613 614 def set_fit_data(self, data):615 """616 Store data associated with this class617 :param data: list of data selected618 """619 self.original_data = None620 self.fit_data = None621 # original data: should not be modified622 self.original_data = data623 # fit data: used for fit and can be modified for convenience624 self.fit_data = copy.deepcopy(data)625 self.smearer_computer_value = smear_selection(self.fit_data, 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.