def find_good_peaks(self, xvals, peakids, acceptance, bad_peak_to_consider, use_poisson, fit_ws, peak_width_estimate): prog_reporter = Progress(self, start=0.1, end=1.0, nreports=2 + len(peakids)) actual_peaks = [] skipped = 0 cost_idx = 1 if use_poisson else 0 prog_reporter.report('Starting fit') logger.notice('Fitting null hypothesis') peak_table, refit_peak_table, cost = FitGaussianPeaks( InputWorkspace=fit_ws, PeakGuessTable=self.generate_peak_guess_table(xvals, []), CentreTolerance=1.0, EstimatedPeakSigma=peak_width_estimate, MinPeakSigma=self._min_sigma, MaxPeakSigma=self._max_sigma, GeneralFitTolerance=0.1, RefitTolerance=0.001, StoreInADS=False) old_cost = cost.column(cost_idx)[0] for idx, peak_idx in enumerate(peakids): peak_table, refit_peak_table, cost = FitGaussianPeaks( InputWorkspace=fit_ws, PeakGuessTable=self.generate_peak_guess_table( xvals, actual_peaks + [peak_idx]), CentreTolerance=1.0, EstimatedPeakSigma=peak_width_estimate, MinPeakSigma=self._min_sigma, MaxPeakSigma=self._max_sigma, GeneralFitTolerance=0.1, RefitTolerance=0.001, StoreInADS=False) new_cost = cost.column(cost_idx)[0] if use_poisson: # if p_new > p_old, but uses logs cost_change = new_cost - old_cost good_peak_condition = cost_change > np.log(acceptance) else: cost_change = abs(new_cost - old_cost) / new_cost good_peak_condition = (new_cost <= old_cost) and (cost_change > acceptance) if skipped > bad_peak_to_consider: break msg = '' if good_peak_condition: msg = '** peak found, ' skipped = 0 actual_peaks.append(peak_idx) old_cost = new_cost else: skipped += 1 prog_reporter.report('Iteration {}, {} peaks found'.format( idx + 1, len(actual_peaks))) msg += '{} peaks in total. cost={:.2}, cost change={:.5}' logger.information( msg.format(len(actual_peaks), new_cost, cost_change)) peak_table, refit_peak_table, cost = FitGaussianPeaks( InputWorkspace=fit_ws, PeakGuessTable=self.generate_peak_guess_table(xvals, actual_peaks), CentreTolerance=1.0, EstimatedPeakSigma=peak_width_estimate, MinPeakSigma=self._min_sigma, MaxPeakSigma=self._max_sigma, GeneralFitTolerance=0.1, RefitTolerance=0.001, StoreInADS=False) prog_reporter.report('Fitting done') logger.notice( 'Fitting done, {} good peaks and {} refitted peak found'.format( peak_table.rowCount(), refit_peak_table.rowCount())) return actual_peaks, peak_table, refit_peak_table
def test_algorithm_does_not_need_refitting_when_given_good_data(self): peak_table, refit_peak_table, fit_cost = FitGaussianPeaks( InputWorkspace=self.data_ws, PeakGuessTable=self.peak_guess_table) self.assertEqual(0, refit_peak_table.rowCount()) self.assertEqual(2, peak_table.rowCount())