Skip to content

nca.terminal

Vectorized terminal phase regression.

The elimination rate constant lambda_z is the negative slope of the linear regression of ln c on t over the points of the terminal phase (Gabrielsson & Weiner 2016, ch. 2.8; Phoenix WinNonlin NCA). Which points form the terminal phase is decided by TerminalPhase.method; BEST_FIT evaluates every window of consecutive points that ends at the last measurable point and takes the largest adjusted R², preferring more points within a tolerance, which is the rule of Phoenix.

All windows of all rows are evaluated at once: with suffix sums of x, y, x², xy and y² over the packed points the statistics of the window starting at index s are closed-form expressions of the sums from s to the end, so window_statistics returns (N, n) arrays without a loop over rows or windows.

RESIDUAL_FLOOR module-attribute

RESIDUAL_FLOOR = 16.0 * float(np.finfo(np.float64).eps)

Multiple of n * eps * syy below which a residual sum of squares is zero.

The residual of a window is syy_c - slope * sxy_c, a difference of two quantities which are equal for a perfect fit, so what is left there is the rounding of the sums themselves and its sign is arbitrary: clamping it at zero reports either 0 or the square root of that noise, and which one depends on the platform and on whether the python core or the rust kernel computed it.

The floor is taken against the uncentred syy and not against syy_c, because every window of a row shares one origin, the row's first sample, so window_statistics can evaluate them all from one set of suffix sums. For a short window late in the curve that makes syy and sy * sy / n both large, of the order of the whole drop of the curve, while their difference syy_c is small - the cancellation, and therefore the noise, is set by the uncentred sums and not by the centred one. A floor written against syy_c leaves the three and four point windows of a noise free curve reporting a standard error of up to 4e-08, which is what measurement showed.

The multiple is 16 rather than tighter or looser from measurement over 120 000 windows of 20 000 curves. The ratio |ss_res| / (n * eps * syy) reaches 1.20 for a noise free curve, at three points and falling with the window, while a curve with 1 % noise stays above 1.9e+05 from four points up. The two regimes therefore overlap only at three points, where a window has one degree of freedom and three noisy samples can fall nearly collinear by chance: at this floor that zeroes 2 windows in 120 000 of a noisy batch, whose residual really is at the rounding floor, and leaves no exact fit reporting a standard error. A multiple of 4 would do the same with a margin of only 3x over the worst exact fit measured, so 16 buys robustness to another time grid for the same cost.

TerminalFit dataclass

TerminalFit(
    slope,
    intercept,
    r2,
    r2_adj,
    se_slope,
    n_points,
    t_first,
    t_last,
    start,
    flags,
    candidates=None,
)

Result of the terminal regression per row, NaN (and start = -1) without a fit.

Attributes:

Name Type Description
slope ndarray

slope of ln c against t (-lambda_z)

intercept ndarray

intercept of the regression, ln c at t = 0

r2 ndarray

coefficient of determination

r2_adj ndarray

adjusted coefficient of determination

se_slope ndarray

standard error of the slope

n_points ndarray

number of points of the regression

t_first ndarray

time of the first point of the regression

t_last ndarray

time of the last point of the regression

start ndarray

packed index of the first point of the window

flags ndarray

NCAFlag bits POSITIVE_SLOPE, TOO_FEW_POINTS and TERMINAL_LOW_CONCENTRATION

candidates DataFrame | None

every candidate window of every row with the columns row (the index of the row), start_time, n, r2_adj and slope, None unless TerminalPhase.keep_candidates asked for it (candidate_table)

window_statistics

window_statistics(x, y, valid)

Regression statistics of every window from an index to the end of the row.

Parameters:

Name Type Description Default
x ndarray

regressor (N, n)

required
y ndarray

response (N, n)

required
valid ndarray

which points enter the regression (N, n)

required

Returns:

Type Description
dict[str, ndarray]

Arrays (N, n) keyed n, slope, intercept, r2, r2_adj,

dict[str, ndarray]

se_slope; column s describes the window s..end. Windows with

dict[str, ndarray]

fewer than 3 points are NaN.

terminal_fit

terminal_fit(
    tp,
    cp,
    n_valid,
    tmax_idx,
    phase,
    manual_mask=None,
    exclude=None,
    windows=None,
)

Terminal log-linear regression of every row.

Parameters:

Name Type Description Default
tp ndarray

packed times (N, n)

required
cp ndarray

packed values (N, n)

required
n_valid ndarray

valid points per row

required
tmax_idx ndarray

packed index of the maximum per row

required
phase TerminalPhase

the selection rule and its parameters

required
manual_mask ndarray | None

packed points of the regression for TerminalMethod.MANUAL

None
exclude ndarray | None

packed points which may not enter the regression (N, n), the values below the limit of quantification a BLQ rule kept or imputed (pkpdutils.nca.options.BLQRules)

None
windows ndarray | None

the terminal window of single rows (N, 2), NaN for a row without one (TerminalPhase.windows). A row with a window regresses the points whose time lies in [t_first, t_last], every other row follows phase.method.

None

Returns:

Type Description
TerminalFit

The fit per row, with the table of every candidate window

TerminalFit

(candidate_table) when phase.keep_candidates is set.

Raises:

Type Description
ValueError

phase.method is TerminalMethod.MANUAL and manual_mask is None.

candidate_table

candidate_table(tp, y, regressable, tmax_idx, phase)

Every window the selection of the terminal phase may choose from.

A candidate is a window which starts at a point of the regression, holds at least phase.min_points regressable points and, with phase.exclude_cmax, starts after the maximum: the windows BEST_FIT ranks by the adjusted R², and the same set for the other rules, which pick one of them by a different criterion. A window whose first point cannot be regressed is left out, since its statistics are those of the window starting at the next regressable point (_collect). The slope is reported as it is, so a window of a still rising curve is in the table with a positive slope, which BEST_FIT never chooses.

Parameters:

Name Type Description Default
tp ndarray

packed times (N, n)

required
y ndarray

the logarithms of the values (N, n), NaN where there is none

required
regressable ndarray

the points which may enter a regression (N, n)

required
tmax_idx ndarray

packed index of the maximum per row

required
phase TerminalPhase

the selection rule and its parameters

required

Returns:

Type Description
DataFrame

One row per candidate window with the columns row (the index of the

DataFrame

row of the batch), start_time (the time of the first point of the

DataFrame

window), n (points of the window), r2_adj and slope; the rows

DataFrame

are ordered by row and by the start time within a row.