Fit data in sequence according to given strategies. The param_dict is extended to cover elemental parameters. Use app.precessEvents() for multi-threading.
(self)
| 493 | self.residual = self.fit_y - y0 |
| 494 | |
| 495 | def fit_multiple(self): |
| 496 | """ |
| 497 | Fit data in sequence according to given strategies. |
| 498 | The param_dict is extended to cover elemental parameters. |
| 499 | Use app.precessEvents() for multi-threading. |
| 500 | """ |
| 501 | # app = QApplication.instance() |
| 502 | self.define_range() |
| 503 | self.get_background() |
| 504 | |
| 505 | # PC = ParamController(self.param_dict, self.param_model.element_list) |
| 506 | # self.param_dict = PC.params |
| 507 | |
| 508 | if self.param_model.param_new["non_fitting_values"]["escape_ratio"] > 0: |
| 509 | self.es_peak = trim_escape_peak(self.io_model.data, self.param_model.param_new, self.y0.size) |
| 510 | y0 = self.y0 - self.bg - self.es_peak |
| 511 | else: |
| 512 | y0 = self.y0 - self.bg |
| 513 | |
| 514 | t0 = time.time() |
| 515 | self.fit_info = ( |
| 516 | "Spectrum fitting of the sum spectrum (incident energy " |
| 517 | f"{self.param_model.param_new['coherent_sct_energy']['value']})." |
| 518 | ) |
| 519 | # app.processEvents() |
| 520 | # logger.info('-------- '+self.fit_info+' --------') |
| 521 | |
| 522 | # Parameters should be initialized only once |
| 523 | init_params = True |
| 524 | for k, v in self.all_strategy.items(): |
| 525 | if v: |
| 526 | strat_name = fit_strategy_list[v - 1] |
| 527 | # self.fit_info = 'Fit with {}: {}'.format(k, strat_name) |
| 528 | |
| 529 | logger.info(self.fit_info) |
| 530 | strategy = extract_strategy(self.param_model.param_new, strat_name) |
| 531 | # register the strategy and extend the parameter list |
| 532 | # to cover all given elements |
| 533 | register_strategy(strat_name, strategy) |
| 534 | set_parameter_bound(self.param_model.param_new, strat_name) |
| 535 | |
| 536 | self.fit_data(self.x0, y0, init_params=init_params) |
| 537 | init_params = False |
| 538 | |
| 539 | self.update_param_with_result() |
| 540 | |
| 541 | # The following is a patch for rare cases when fitting results in negative |
| 542 | # areas for some emission lines. These are typically non-existent lines, but |
| 543 | # they should not be automatically eliminated from the list. To prevent |
| 544 | # elimination, set the area to some small positive value. |
| 545 | for key, val in self.param_model.param_new.items(): |
| 546 | if key.endswith("_area") and val["value"] <= 0.0: |
| 547 | _small_value_for_area = 0.1 |
| 548 | logger.warning( |
| 549 | f"Fitting resulted in negative value for '{key}' ({val['value']}). \n" |
| 550 | f" In order to continue using the emission line in future computations, " |
| 551 | f"the fitted area is set to a small value ({_small_value_for_area}).\n Delete " |
| 552 | f"the emission line from the list if you know it is not present in " |
no test coverage detected