Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pargen bug fixes #582

Merged
merged 13 commits into from
May 10, 2024
19 changes: 13 additions & 6 deletions src/pygama/pargen/energy_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ def hpge_cal_energy_peak_tops(
(Polynomial(self.pars) - i).roots()
for i in (peaks_kev[0] * 0.9, peaks_kev[-1] * 1.1)
)
euc_min = euc_min[0]
euc_max = euc_max[0]
euc_min = np.nanmin(euc_min)
euc_max = np.nanmax(euc_max)

if euc_min < 0:
euc_min = 0
Expand Down Expand Up @@ -831,8 +831,8 @@ def hpge_fit_energy_peaks(
(Polynomial(self.pars) - i).roots()
for i in (peaks_kev[0] * 0.9, peaks_kev[-1] * 1.1)
)
euc_min = euc_min[0]
euc_max = euc_max[0]
euc_min = np.nanmin(euc_min)
euc_max = np.nanmax(euc_max)
if euc_min < 0:
euc_min = 0
if euc_max > np.nanmax(e_uncal) * 1.1:
Expand Down Expand Up @@ -2484,16 +2484,23 @@ def hpge_fit_energy_cal_func(
for n in range(len(energy_scale_pars) - 1):
d_mu_d_es += energy_scale_pars[n + 1] * mus ** (n + 1)
e_weights = np.sqrt(d_mu_d_es * mu_vars)
mask = np.isfinite(e_weights)
poly_pars = (
Polynomial.fit(mus, energies_kev, deg=deg, w=1 / e_weights).convert().coef
Polynomial.fit(
mus[mask], energies_kev[mask], deg=deg, w=1 / e_weights[mask]
)
.convert()
.coef
)
if fixed is not None:
for idx, val in fixed.items():
if val is True or val is None:
pass
else:
poly_pars[idx] = val
c = cost.LeastSquares(mus, energies_kev, e_weights, poly_wrapper)
c = cost.LeastSquares(
mus[mask], energies_kev[mask], e_weights[mask], poly_wrapper
)
m = Minuit(c, *poly_pars)
if fixed is not None:
for idx in list(fixed):
Expand Down
3 changes: 3 additions & 0 deletions src/pygama/pargen/lq_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@ def get_cut_lq_dep(self, df: pd.DataFrame(), lq_param: str, cal_energy_param: st
raise (e)
log.error("LQ cut determination failed")
self.cut_val = np.nan
c = cost.UnbinnedNLL(np.array([0]), gaussian.pdf)
m = Minuit(c, np.full(2, np.nan))
self.cut_fit_pars = pars = m.values

self.update_cal_dicts(
{
Expand Down