Changes in src/sas/sasgui/perspectives/fitting/fitproblem.py [251ef684:959eb01] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/fitting/fitproblem.py
r251ef684 r959eb01 14 14 ################################################################################ 15 15 import copy 16 17 from sas.sascalc.fit.qsmearing import smear_selection 18 19 class FitProblem(object): 16 from sas.sascalc.data_util.qsmearing import smear_selection 17 18 class FitProblemComponent(object): 20 19 """ 21 Define the relationship between data and model, including range, weights, 22 etc. 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. 23 23 """ 24 def __init__(self):25 """26 contains information about data and model to fit27 """28 ## data used for fitting29 self.fit_data = None30 self.theory_data = None31 self.residuals = None32 # original data: should not be modified33 self.original_data = None34 ## the current model35 self.model = None36 ## if 1 this fit problem will be selected to fit , if 037 ## it will not be selected for fit38 self.schedule = 039 ##list containing parameter name and value40 self.list_param = []41 self.list_param2fit = []42 ## smear object to smear or not data1D43 self.smearer_computed = False44 self.smearer_enable = False45 self.smearer_computer_value = None46 ## fitting range47 self.qmin = None48 self.qmax = None49 # fit weight50 self.weight = None51 self.result = None52 self.fit_tab_caption = None53 self.name_per_page = None54 55 24 def enable_smearing(self, flag=False): 56 25 """ … … 58 27 flag is 0 ingore smear value. 59 28 """ 60 self.smearer_enable = flag61 62 def set_smearer(self, smearer):63 """64 save reference of smear object on fitdata65 66 :param smear: smear object from DataLoader67 68 """69 self.smearer_computer_value = smearer70 29 71 30 def get_smearer(self): … … 73 32 return smear object 74 33 """ 75 if not self.smearer_enable:76 return None77 if not self.smearer_computed:78 #smeari_selection should be call only once per fitproblem79 self.smearer_computer_value = smear_selection(self.fit_data,80 self.model)81 self.smearer_computed = True82 return self.smearer_computer_value83 84 34 def save_model_name(self, name): 85 35 """ 86 36 """ 87 self.name_per_page = name88 37 89 38 def get_name(self): 90 39 """ 91 40 """ 92 return self.name_per_page93 41 94 42 def set_model(self, model): … … 98 46 :param name: name created for model 99 47 """ 100 self.model = model101 self.smearer_computer_value = smear_selection(self.fit_data,102 self.model)103 self.smearer_computed = True104 48 105 49 def get_model(self): … … 107 51 :return: saved model 108 52 """ 109 return self.model110 53 111 54 def set_residuals(self, residuals): … … 114 57 :param data: data selected 115 58 """ 116 self.residuals = residuals117 59 118 60 def get_residuals(self): … … 120 62 :return: residuals 121 63 """ 122 return self.residuals123 64 124 65 def set_theory_data(self, data): 125 66 """ 126 67 save a copy of the data select to fit 127 128 68 :param data: data selected 129 130 """ 131 self.theory_data = copy.deepcopy(data) 69 """ 132 70 133 71 def get_theory_data(self): 134 72 """ 135 :return: theory generated with the current model and data of this class 136 """ 137 return self.theory_data 73 :return: list of data dList 74 """ 138 75 139 76 def set_fit_data(self, data): 140 77 """ 141 Store data associated with this class 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 142 81 :param data: list of data selected 143 82 """ 144 self.original_data = None145 self.fit_data = None146 # original data: should not be modified147 self.original_data = data148 # fit data: used for fit and can be modified for convenience149 self.fit_data = copy.deepcopy(data)150 self.smearer_computer_value = smear_selection(self.fit_data, self.model)151 self.smearer_computed = True152 self.result = None153 83 154 84 def get_fit_data(self): 155 85 """ 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 86 """ 197 87 198 88 def set_model_param(self, name, value=None): … … 202 92 :param value: value of that parameter 203 93 """ 204 self.list_param.append([name, value]) 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 """ 205 105 206 106 def get_model_param(self): … … 208 108 return list of couple of parameter name and value 209 109 """ 210 return self.list_param211 110 212 111 def schedule_tofit(self, schedule=0): … … 214 113 set schedule to true to decide if this fit must be performed 215 114 """ 216 self.schedule = schedule217 115 218 116 def get_scheduled(self): … … 220 118 return true or false if a problem as being schedule for fitting 221 119 """ 222 return self.schedule223 120 224 121 def set_range(self, qmin=None, qmax=None): 225 122 """ 226 123 set fitting range 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 124 """ 232 125 233 126 def get_range(self): 234 127 """ 235 128 :return: fitting range 236 237 """ 238 return self.qmin, self.qmax 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 """ 239 140 240 141 def clear_model_param(self): … … 242 143 clear constraint info 243 144 """ 244 self.list_param = []245 145 246 146 def set_fit_tab_caption(self, caption): 247 147 """ 248 """249 self.fit_tab_caption = str(caption)148 store the caption of the page associated with object 149 """ 250 150 251 151 def get_fit_tab_caption(self): 252 152 """ 253 """254 return self.fit_tab_caption153 Return the caption of the page associated with object 154 """ 255 155 256 156 def set_graph_id(self, id): … … 258 158 Set graph id (from data_group_id at the time the graph produced) 259 159 """ 260 self.graph_id = id261 160 262 161 def get_graph_id(self): … … 264 163 Get graph_id 265 164 """ 266 return self.graph_id267 165 268 166 def set_result(self, result): 269 167 """ 270 168 """ 271 self.result = result272 169 273 170 def get_result(self): … … 275 172 get result 276 173 """ 277 return self.result 278 279 280 class FitProblemDictionary(dict): 174 175 176 class FitProblemDictionary(FitProblemComponent, dict): 281 177 """ 282 178 This module implements a dictionary of fitproblem objects 283 179 """ 284 180 def __init__(self): 181 FitProblemComponent.__init__(self) 285 182 dict.__init__(self) 286 183 ## the current model … … 309 206 self._smear_on = flag 310 207 if fid is None: 311 for value in self. values():208 for value in self.itervalues(): 312 209 value.enable_smearing(flag) 313 elif fid in self: 314 self[fid].enable_smearing(flag) 210 else: 211 if fid in self.iterkeys(): 212 self[fid].enable_smearing(flag) 315 213 316 214 def set_smearer(self, smearer, fid=None): … … 320 218 """ 321 219 if fid is None: 322 for value in self. values():220 for value in self.itervalues(): 323 221 value.set_smearer(smearer) 324 elif fid in self: 325 self[fid].set_smearer(smearer) 222 else: 223 if fid in self.iterkeys(): 224 self[fid].set_smearer(smearer) 326 225 327 226 def get_smearer(self, fid=None): … … 329 228 return smear object 330 229 """ 331 if fid in self :230 if fid in self.iterkeys(): 332 231 return self[fid].get_smearer() 333 232 … … 336 235 """ 337 236 if fid is None: 338 for value in self. values():237 for value in self.itervalues(): 339 238 value.save_model_name(name) 340 elif fid in self: 341 self[fid].save_model_name(name) 239 else: 240 if fid in self.iterkeys(): 241 self[fid].save_model_name(name) 342 242 343 243 def get_name(self, fid=None): … … 346 246 result = [] 347 247 if fid is None: 348 for value in self. values():248 for value in self.itervalues(): 349 249 result.append(value.get_name()) 350 elif fid in self: 351 result.append(self[fid].get_name()) 250 else: 251 if fid in self.iterkeys(): 252 result.append(self[fid].get_name()) 352 253 return result 353 254 … … 360 261 self.model = model 361 262 if fid is None: 362 for value in self. values():263 for value in self.itervalues(): 363 264 value.set_model(self.model) 364 elif fid in self: 365 self[fid].set_model(self.model) 265 else: 266 if fid in self.iterkeys(): 267 self[fid].set_model(self.model) 366 268 367 269 def get_model(self, fid): … … 369 271 :return: saved model 370 272 """ 371 if fid in self :273 if fid in self.iterkeys(): 372 274 return self[fid].get_model() 373 275 … … 389 291 :param data: data selected 390 292 """ 391 if fid in self :293 if fid in self.iterkeys(): 392 294 self[fid].set_residuals(residuals) 393 295 … … 396 298 :return: residuals 397 299 """ 398 if fid in self :300 if fid in self.iterkeys(): 399 301 return self[fid].get_residuals() 400 302 … … 404 306 :param data: data selected 405 307 """ 406 if fid in self :308 if fid in self.iterkeys(): 407 309 self[fid].set_theory_data(data) 408 310 … … 411 313 :return: list of data dList 412 314 """ 413 if fid in self :315 if fid in self.iterkeys(): 414 316 return self[fid].get_theory_data() 415 317 … … 420 322 :note: only data changes in the fit problem 421 323 """ 422 if data.id not in self :324 if data.id not in self.iterkeys(): 423 325 self[data.id] = FitProblem() 424 326 self[data.id].set_fit_data(data) … … 434 336 data = [] 435 337 for d in data: 436 if d is not None:437 if d.id not in self:338 if (d is not None): 339 if (d.id not in self.iterkeys()): 438 340 self[d.id] = FitProblem() 439 341 self[d.id].set_fit_data(d) … … 446 348 :param fid: key representing a fitproblem, usually extract from data id 447 349 """ 448 if fid in self :350 if fid in self.iterkeys(): 449 351 return self[fid].get_fit_data() 450 352 … … 456 358 """ 457 359 if fid is None: 458 for value in self. values():360 for value in self.itervalues(): 459 361 value.set_model_param(name, value) 460 elif fid in self: 461 self[fid].set_model_param(name, value) 362 else: 363 if fid in self.iterkeys(): 364 self[fid].set_model_param(name, value) 462 365 463 366 def get_model_param(self, fid): … … 465 368 return list of couple of parameter name and value 466 369 """ 467 if fid in self :370 if fid in self.iterkeys(): 468 371 return self[fid].get_model_param() 469 372 … … 486 389 """ 487 390 self.scheduled = schedule 488 for value in self. values():391 for value in self.itervalues(): 489 392 value.schedule_tofit(schedule) 490 393 … … 502 405 self.qmax = qmax 503 406 if fid is None: 504 for value in self. values():407 for value in self.itervalues(): 505 408 value.set_range(self.qmin, self.qmax) 506 elif fid in self: 507 self[fid].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) 508 412 509 413 def get_range(self, fid): … … 511 415 :return: fitting range 512 416 """ 513 if fid in self :417 if fid in self.iterkeys(): 514 418 return self[fid].get_range() 515 419 … … 519 423 """ 520 424 if fid is None: 521 for value in self. values():425 for value in self.itervalues(): 522 426 value.set_weight(flag=flag, is2d=is2d) 523 elif fid in self: 524 self[fid].set_weight(flag=flag, is2d=is2d) 427 else: 428 if fid in self.iterkeys(): 429 self[fid].set_weight(flag=flag, is2d=is2d) 525 430 526 431 def get_weight(self, fid=None): … … 528 433 return fit weight 529 434 """ 530 if fid in self :435 if fid in self.iterkeys(): 531 436 return self[fid].get_weight() 532 437 … … 536 441 """ 537 442 if fid is None: 538 for value in self. values():443 for value in self.itervalues(): 539 444 value.clear_model_param() 540 elif fid in self: 541 self[fid].clear_model_param() 445 else: 446 if fid in self.iterkeys(): 447 self[fid].clear_model_param() 542 448 543 449 def get_fit_problem(self): … … 545 451 return fitproblem contained in this dictionary 546 452 """ 547 return self. values()453 return self.itervalues() 548 454 549 455 def set_result(self, result, fid): 550 456 """ 551 457 """ 552 if fid in self :458 if fid in self.iterkeys(): 553 459 self[fid].set_result(result) 554 460 … … 564 470 get result 565 471 """ 566 if fid in self :472 if fid in self.iterkeys(): 567 473 return self[fid].get_result() 568 474 … … 584 490 """ 585 491 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 fit 504 """ 505 ## data used for fitting 506 self.fit_data = None 507 self.theory_data = None 508 self.residuals = None 509 # original data: should not be modified 510 self.original_data = None 511 ## the current model 512 self.model = None 513 ## if 1 this fit problem will be selected to fit , if 0 514 ## it will not be selected for fit 515 self.schedule = 0 516 ##list containing parameter name and value 517 self.list_param = [] 518 ## smear object to smear or not data1D 519 self.smearer_computed = False 520 self.smearer_enable = False 521 self.smearer_computer_value = None 522 ## fitting range 523 self.qmin = None 524 self.qmax = None 525 # fit weight 526 self.weight = None 527 self.result = None 528 529 def enable_smearing(self, flag=False): 530 """ 531 :param flag: bool.When flag is 1 get the computer smear value. When 532 flag is 0 ingore smear value. 533 """ 534 self.smearer_enable = flag 535 536 def set_smearer(self, smearer): 537 """ 538 save reference of smear object on fitdata 539 540 :param smear: smear object from DataLoader 541 542 """ 543 self.smearer_computer_value = smearer 544 545 def get_smearer(self): 546 """ 547 return smear object 548 """ 549 if not self.smearer_enable: 550 return None 551 if not self.smearer_computed: 552 #smeari_selection should be call only once per fitproblem 553 self.smearer_computer_value = smear_selection(self.fit_data, 554 self.model) 555 self.smearer_computed = True 556 return self.smearer_computer_value 557 558 def save_model_name(self, name): 559 """ 560 """ 561 self.name_per_page = name 562 563 def get_name(self): 564 """ 565 """ 566 return self.name_per_page 567 568 def set_model(self, model): 569 """ 570 associates each model with its new created name 571 :param model: model selected 572 :param name: name created for model 573 """ 574 self.model = model 575 self.smearer_computer_value = smear_selection(self.fit_data, 576 self.model) 577 self.smearer_computed = True 578 579 def get_model(self): 580 """ 581 :return: saved model 582 """ 583 return self.model 584 585 def set_residuals(self, residuals): 586 """ 587 save a copy of residual 588 :param data: data selected 589 """ 590 self.residuals = residuals 591 592 def get_residuals(self): 593 """ 594 :return: residuals 595 """ 596 return self.residuals 597 598 def set_theory_data(self, data): 599 """ 600 save a copy of the data select to fit 601 602 :param data: data selected 603 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 class 610 """ 611 return self.theory_data 612 613 def set_fit_data(self, data): 614 """ 615 Store data associated with this class 616 :param data: list of data selected 617 """ 618 self.original_data = None 619 self.fit_data = None 620 # original data: should not be modified 621 self.original_data = data 622 # fit data: used for fit and can be modified for convenience 623 self.fit_data = copy.deepcopy(data) 624 self.smearer_computer_value = smear_selection(self.fit_data, 625 self.model) 626 self.smearer_computed = True 627 self.result = None 628 629 def get_fit_data(self): 630 """ 631 :return: data associate with this class 632 """ 633 return self.fit_data 634 635 def get_origin_data(self): 636 """ 637 """ 638 return self.original_data 639 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 Data 645 """ 646 from sas.sasgui.perspectives.fitting.utils import get_weight 647 # send original data for weighting 648 self.weight = get_weight(data=self.original_data, is2d=is2d, flag=flag) 649 if is2d: 650 self.fit_data.err_data = self.weight 651 else: 652 self.fit_data.dy = self.weight 653 654 def get_weight(self): 655 """ 656 returns weight array 657 """ 658 return self.weight 659 660 def set_param2fit(self, list): 661 """ 662 Store param names to fit (checked) 663 :param list: list of the param names 664 """ 665 self.list_param2fit = list 666 667 def get_param2fit(self): 668 """ 669 return the list param names to fit 670 """ 671 return self.list_param2fit 672 673 def set_model_param(self, name, value=None): 674 """ 675 Store the name and value of a parameter of this fitproblem's model 676 :param name: name of the given parameter 677 :param value: value of that parameter 678 """ 679 self.list_param.append([name, value]) 680 681 def get_model_param(self): 682 """ 683 return list of couple of parameter name and value 684 """ 685 return self.list_param 686 687 def schedule_tofit(self, schedule=0): 688 """ 689 set schedule to true to decide if this fit must be performed 690 """ 691 self.schedule = schedule 692 693 def get_scheduled(self): 694 """ 695 return true or false if a problem as being schedule for fitting 696 """ 697 return self.schedule 698 699 def set_range(self, qmin=None, qmax=None): 700 """ 701 set fitting range 702 :param qmin: minimum value to consider for the fit range 703 :param qmax: maximum value to consider for the fit range 704 """ 705 self.qmin = qmin 706 self.qmax = qmax 707 708 def get_range(self): 709 """ 710 :return: fitting range 711 712 """ 713 return self.qmin, self.qmax 714 715 def clear_model_param(self): 716 """ 717 clear constraint info 718 """ 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_caption 730 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 = id 736 737 def get_graph_id(self): 738 """ 739 Get graph_id 740 """ 741 return self.graph_id 742 743 def set_result(self, result): 744 """ 745 """ 746 self.result = result 747 748 def get_result(self): 749 """ 750 get result 751 """ 752 return self.result
Note: See TracChangeset
for help on using the changeset viewer.