Skip to content

Commit

Permalink
fix: raise RuntimeError if fit_roi fails
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamCorrao committed Apr 11, 2024
1 parent 55f2c4d commit eadb120
Showing 1 changed file with 17 additions and 56 deletions.
73 changes: 17 additions & 56 deletions pdf_agents/peakfit.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,25 +194,14 @@ def fit_roi(self, xroi: tuple, fit_func: str, pos_percent_lim: float, maxcycles:

popt, pcov = curve_fit(self.voigt, x_roi, y_roi, p0=p0, bounds=bounds, max_nfev=maxcycles)

except RuntimeError: # if fitting fails, return None for parameters
popt = None

if popt is not None:
peak_amplitude, peak_position, peak_sigma, peak_gamma = popt
peak_fwhm = self.voigt_fwhm(peak_sigma, peak_gamma)
except RuntimeError:
raise RuntimeError("PeakFitAgent fit failed to converge")

ycalc = self.voigt(x_roi, *popt)
ydiff = y_roi - self.voigt(x_roi, *popt)
peak_amplitude, peak_position, peak_sigma, peak_gamma = popt
peak_fwhm = self.voigt_fwhm(peak_sigma, peak_gamma)

else:
peak_amplitude = None
peak_position = None
peak_sigma = None
peak_gamma = None
peak_fwhm = None
ycalc = None
ydiff = None
x_roi = None
ycalc = self.voigt(x_roi, *popt)
ydiff = y_roi - self.voigt(x_roi, *popt)

return peak_amplitude, peak_position, peak_sigma, peak_gamma, peak_fwhm, ycalc, ydiff, x_roi

Expand All @@ -234,23 +223,13 @@ def fit_roi(self, xroi: tuple, fit_func: str, pos_percent_lim: float, maxcycles:
popt, pcov = curve_fit(self.gaussian, x_roi, y_roi, p0=p0, bounds=bounds, max_nfev=maxcycles)

except RuntimeError:
# If fitting fails, return None for parameters
popt = None

if popt is not None:
peak_amplitude, peak_position, peak_width = popt
peak_fwhm = self.gaussian_fwhm(peak_width)
raise RuntimeError("PeakFitAgent fit failed to converge")

ycalc = self.gaussian(x_roi, *popt)
ydiff = y_roi - self.gaussian(x_roi, *popt)
peak_amplitude, peak_position, peak_width = popt
peak_fwhm = self.gaussian_fwhm(peak_width)

else:
peak_amplitude = None
peak_position = None
peak_fwhm = None
ycalc = None
ydiff = None
x_roi = None
ycalc = self.gaussian(x_roi, *popt)
ydiff = y_roi - self.gaussian(x_roi, *popt)

return peak_amplitude, peak_position, peak_fwhm, ycalc, ydiff, x_roi

Expand All @@ -272,26 +251,16 @@ def fit_roi(self, xroi: tuple, fit_func: str, pos_percent_lim: float, maxcycles:
popt, pcov = curve_fit(self.lorentzian, x_roi, y_roi, p0=p0, bounds=bounds, max_nfev=1000)

except RuntimeError:
# If fitting fails, return None for parameters
popt = None
print(f"Fitting failed for ROI = {x_roi[0]} - {x_roi[-1]}\n")

if popt is not None:
peak_amplitude, peak_position, peak_width = popt
peak_fwhm = self.lorentzian_fwhm(peak_width)
raise RuntimeError("PeakFitAgent fit failed to converge")

ycalc = self.lorentzian(x_roi, *popt)
ydiff = y_roi - self.lorentzian(x_roi, *popt)
peak_amplitude, peak_position, peak_width = popt
peak_fwhm = self.lorentzian_fwhm(peak_width)

else:
peak_amplitude = None
peak_position = None
peak_fwhm = None
ycalc = None
ydiff = None
x_roi = None
ycalc = self.lorentzian(x_roi, *popt)
ydiff = y_roi - self.lorentzian(x_roi, *popt)

return peak_amplitude, peak_position, peak_fwhm, ycalc, ydiff, x_roi

else:
raise NameError(
f"fit_func not recognized - must be 'voigt', 'gaussian', or 'lorentzian'. Input is {fit_func}"
Expand All @@ -304,8 +273,6 @@ def report(self) -> Dict[str, ArrayLike]:
) # this is what kmeans will likely consume - possibly better to handle this upstream?
peak_amplitudes = []
peak_positions = []
peak_sigmas = [] # not used for gaussian / lorentzian case, None is a placeholder
peak_gammas = [] # not used for gaussian / lorentzian case, None is a placeholder
peak_fwhms = []
ycalcs = []
ydiffs = []
Expand All @@ -320,8 +287,6 @@ def report(self) -> Dict[str, ArrayLike]:
peak_amps_poss_fwhms.append([peak_amplitude, peak_position, peak_fwhm])
peak_amplitudes.append(peak_amplitude)
peak_positions.append(peak_position)
peak_sigmas.append(peak_sigma)
peak_gammas.append(peak_gamma)
peak_fwhms.append(peak_fwhm)
ycalcs.append(ycalc)
ydiffs.append(ydiff)
Expand All @@ -336,8 +301,6 @@ def report(self) -> Dict[str, ArrayLike]:
peak_amps_poss_fwhms.append([peak_amplitude, peak_position, peak_fwhm])
peak_amplitudes.append(peak_amplitude)
peak_positions.append(peak_position)
peak_sigmas.append(None)
peak_gammas.append(None)
peak_fwhms.append(peak_fwhm)
ycalcs.append(ycalc)
ydiffs.append(ydiff)
Expand All @@ -358,8 +321,6 @@ def report(self) -> Dict[str, ArrayLike]:
peak_amps_poss_fwhms=np.array(peak_amps_poss_fwhms),
peak_amplitudes=np.array(peak_amplitudes),
peak_positions=np.array(peak_positions),
peak_sigmas=np.array(peak_sigmas),
peak_gammas=np.array(peak_gammas),
peak_fwhms=np.array(peak_fwhms),
ycalc=np.array(ycalcs),
ydiffs=np.array(ydiffs),
Expand Down

0 comments on commit eadb120

Please sign in to comment.