diff --git a/CHANGELOG.md b/CHANGELOG.md index 89f06f7..6e95544 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The decisions are in [docs/plan.md](docs/plan.md) §12. ## Unreleased +- `time_series.equalise_brightness` refuses a factor or scaled value that overflows float64 + (for example a time point whose mean is many orders of magnitude below the reference's) + instead of returning infinite pixels (`TimeSeriesError`, contract §3.7). Values that were + finite are unchanged. + - **Brightness of a time series** (plan N72, contract §3.7): new module `time_series` with `frame_means`, `brightness_trend`, `equalise_brightness`, `match_histograms` and `TimeSeriesError`, moved from the OpticalFlow3D Profiler (`brightness.py`) at the owner's diff --git a/docs/contract.md b/docs/contract.md index 013ab13..bfda625 100644 --- a/docs/contract.md +++ b/docs/contract.md @@ -894,7 +894,7 @@ matched = match_histograms(stack, reference) |---|---| | `frame_means` | `mean(stack[t])` over every axis but time, in float64. | | `brightness_trend` | `slope / means[reference]`, the least-squares slope of the means against the time index `0..T-1` (`numpy.polyfit`, degree 1), divided by the reference mean. NaN when the reference mean is not above 0 or `T = 1`. A check: it changes nothing. | -| `equalise_brightness` | `stack[t] * factor[t]` in float64 with `factor[t] = means[reference] / means[t]` (the reference's factor is exactly 1): Fiji Bleach Correction's "Simple Ratio" without a background. Returns the scaled series and the factors. Refuses a time point whose mean is not above 0, naming it. A constant camera offset is scaled too. | +| `equalise_brightness` | `stack[t] * factor[t]` in float64 with `factor[t] = means[reference] / means[t]` (the reference's factor is exactly 1): Fiji Bleach Correction's "Simple Ratio" without a background. Returns the scaled series and the factors. Refuses a time point whose mean is not above 0, and a factor or scaled value that is not finite (float64 overflow), naming them. A constant camera offset is scaled too. | | `match_histograms` | For every `t` but the reference, `p' = interp(F_t(p), F_ref, v_ref)`: `F_t` the empirical cumulative distribution of time point `t` at each of its distinct values, `v_ref` the reference's distinct values and `F_ref` their cumulative distribution, with NumPy's linear interpolation: Fiji Bleach Correction's "Histogram Matching". Float64; the reference time point is returned unchanged. Monotonic, non-linear. | | `reference` | A whole-number index `0 <= reference < T` (a boolean or a float is refused, never converted). The caller chooses it; the profiler uses the central frame of each optical-flow window. | diff --git a/src/morphobricks_normalization/time_series.py b/src/morphobricks_normalization/time_series.py index 3b16608..2efa15c 100644 --- a/src/morphobricks_normalization/time_series.py +++ b/src/morphobricks_normalization/time_series.py @@ -129,8 +129,9 @@ def equalise_brightness(stack: np.ndarray, reference: int) -> tuple[np.ndarray, Raises ------ TimeSeriesError - For an invalid series or reference, or a time point whose mean is not above 0 (it - cannot be scaled to a positive mean). + For an invalid series or reference, a time point whose mean is not above 0 (it + cannot be scaled to a positive mean), or a factor or scaled value that is not finite + (float64 overflow): never returned as infinite pixels. """ means = frame_means(stack) _checked(np.asarray(stack), reference) @@ -141,10 +142,21 @@ def equalise_brightness(stack: np.ndarray, reference: int) -> tuple[np.ndarray, "so they cannot be scaled to the reference's mean; use histogram matching or no " "correction." ) - factors = means[reference] / means - factors[reference] = 1.0 - shape = (len(factors),) + (1,) * (np.ndim(stack) - 1) - return np.asarray(stack, dtype=np.float64) * factors.reshape(shape), factors + with np.errstate(over="ignore"): + factors = means[reference] / means + factors[reference] = 1.0 + shape = (len(factors),) + (1,) * (np.ndim(stack) - 1) + scaled = np.asarray(stack, dtype=np.float64) * factors.reshape(shape) + bad = np.flatnonzero( + ~np.isfinite(factors) | ~np.isfinite(scaled).reshape(len(factors), -1).all(axis=1) + ) + if bad.size: + raise TimeSeriesError( + f"Scaling time point(s) {', '.join(map(str, bad))} to the reference's mean gives " + "values float64 cannot hold (a mean far below the reference's); use histogram " + "matching or no correction." + ) + return scaled, factors def match_histograms(stack: np.ndarray, reference: int) -> np.ndarray: diff --git a/tests/test_time_series.py b/tests/test_time_series.py index 2f3015a..e1c897f 100644 --- a/tests/test_time_series.py +++ b/tests/test_time_series.py @@ -47,6 +47,8 @@ def test_equalise_brightness_matches_every_mean_to_the_reference(): assert np.array_equal(scaled[3], STACK[3].astype(np.float64)) # the reference unchanged with pytest.raises(TimeSeriesError, match="not above 0"): equalise_brightness(np.zeros((3, 4, 4)), 1) + with pytest.raises(TimeSeriesError, match="float64 cannot hold"): + equalise_brightness(np.array([[5e-324], [1.0]]), 1) # factor overflows to infinity def test_match_histograms_gives_every_time_point_the_reference_distribution():