From f1d43065d39ec2845c4d757e0b76813b2a90a97a Mon Sep 17 00:00:00 2001 From: Remis Lape Date: Tue, 1 Sep 2026 14:15:07 +0100 Subject: [PATCH] Take log-bin edges from dcio Third part of M3. log_bin_edges keeps its signature and stays HJCFIT's documented entry point, but the arithmetic now comes from dcio.analysis.histogram, so there is one definition of it in the stack rather than one here and a diverging one in EKDIST. dcio is imported inside the function, as read_idealized_bursts already does, so it stays a soft dependency: the likelihood library still works with numpy alone, and only dwell-time histograms need dcio. The ImportError says how to install it rather than surfacing a bare ModuleNotFoundError from three frames down. ideal_pdf_scale_factor stays here. It looked like a duplicate of EKDIST's __exponential_scale_factor and is a different quantity: this one renormalises an ideal pdf onto the resolved intervals from the Q-matrix survival, EKDIST's normalises an exponential mixture already fitted to data. Co-Authored-By: Claude Opus 5 --- likelihood/python/_methods.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/likelihood/python/_methods.py b/likelihood/python/_methods.py index ae97f26..90c4415 100644 --- a/likelihood/python/_methods.py +++ b/likelihood/python/_methods.py @@ -283,6 +283,12 @@ def plot_time_intervals(series, start=0, ax = None): def log_bin_edges(intervals, tres, nbdec=None): """ Geometric bin edges for a dwell-time histogram. + Delegates to :func:`dcio.analysis.histogram.log_bin_edges`, which is the + one definition of this arithmetic in the DCPROGS stack. dcio is not a + dependency of HJCFIT -- it is imported here and in + :func:`HJCFIT.read_idealized_bursts`, and nowhere else, so the rest of + the library works without it. + Dwell times span decades, so bins are uniform in log time rather than in time: each is a fixed ratio wider than the last, and the first starts at the resolution. The number of bins per decade follows the DCprogs @@ -296,18 +302,16 @@ def log_bin_edges(intervals, tres, nbdec=None): intervals, 8 to 1000, 10 to 3000, 12 above. :returns: (edges, nbdec) - """ - from numpy import asarray, log, log10, ceil, arange - intervals = asarray(intervals, dtype=float) - if nbdec is None: - n = len(intervals) - nbdec = 5 if n <= 300 else 8 if n <= 1000 else 10 if n <= 3000 else 12 - - ratio = 10.0 ** (1.0 / nbdec) - tmax = 10.0 ** ceil(log10(intervals.max())) - nbin = int(log(tmax / tres) / log(ratio)) + 1 - return tres * ratio ** arange(nbin + 1), nbdec + :raises ImportError: if dcio is not installed. + """ + try: + from dcio.analysis.histogram import log_bin_edges as _log_bin_edges + except ImportError: + raise ImportError( + "dwell-time histograms need dcio for their binning: " + "pip install git+https://github.com/DCPROGS/dcio") + return _log_bin_edges(intervals, tres, nbdec) def ideal_pdf_scale_factor(tres, aa, initial_vectors):