Skip to content

Commit

Permalink
another round of tweaks to pass tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmarshall committed Nov 18, 2024
1 parent 13ec928 commit 3f69abb
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/pygama/pargen/AoE_cal.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,19 @@ def aoe_peak_guess(func, hist, bins, var, **kwargs):
mu = bin_centers[np.argmax(hist)]
pars, _ = pgf.gauss_mode_width_max(hist, bins, var, mode_guess=mu, n_bins=5)
bin_centres = pgh.get_bin_centers(bins)
_, fallback_sigma, _ = pgh.get_gaussian_guess(hist, bins)

if pars is None or (
pars[0] > bins[-1]
or pars[0] < bins[0]
or pars[-1] < (0.5 * np.nanmax(hist))
or pars[-1] > (2 * np.nanmax(hist))
or pars[1] > 2 * fallback_sigma
or pars[1] < 0.5 * fallback_sigma
):
i_0 = np.argmax(hist)
mu = bin_centres[i_0]
(_, sigma, _) = pgh.get_gaussian_guess(hist, bins)
sigma = fallback_sigma
else:
mu = pars[0]
sigma = pars[1]
Expand Down Expand Up @@ -236,7 +240,11 @@ def guess(bands, means, mean_errs):
class SigmaFit:
@staticmethod
def func(x, a, b, c):
return np.sqrt(a + (b / (x + 10**-99)) ** c)
return np.where(
(x > 0) & ((a + (b / (x + 10**-99)) ** c) > 0),
np.sqrt(a + (b / (x + 10**-99)) ** c),
np.nan,
)

@staticmethod
def string_func(input_param):
Expand Down Expand Up @@ -283,14 +291,14 @@ def unbinned_aoe_fit(
if not isinstance(aoe, np.ndarray):
aoe = np.array(aoe)

bin_width = (
2
* (np.nanpercentile(aoe, 75) - np.nanpercentile(aoe, 25))
* len(aoe) ** (-1 / 3)
)
nbins = int(np.ceil((np.nanmax(aoe) - np.nanmin(aoe)) / bin_width))
# bin_width = (
# 2
# * (np.nanpercentile(aoe, 75) - np.nanpercentile(aoe, 25))
# * len(aoe) ** (-1 / 3)
# )
# nbins = int(np.ceil((np.nanmax(aoe) - np.nanmin(aoe)) / bin_width))
hist, bins, var = pgh.get_hist(
aoe[(aoe < np.nanpercentile(aoe, 99)) & (aoe > np.nanpercentile(aoe, 1))],
aoe[(aoe < np.nanpercentile(aoe, 99)) & (aoe > np.nanpercentile(aoe, 10))],
bins=500,
)

Expand Down

0 comments on commit 3f69abb

Please sign in to comment.