From 2fb4e5d91e5ca1a1c6ba35a0c7d0b4211dd74a28 Mon Sep 17 00:00:00 2001 From: Leonardo Schwarz Date: Fri, 5 Jul 2024 12:27:38 +0200 Subject: [PATCH] remove a potential source of non-determinism i observed occasional test failures for the samples with a single point, but maybe it was simply due to the problem being ill-posed and different correct solutions being returned. hopefully this fixes it, but i am not sure --- src/depiction/calibration/models/linear_model.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/depiction/calibration/models/linear_model.py b/src/depiction/calibration/models/linear_model.py index 6266a0c..28f3fea 100644 --- a/src/depiction/calibration/models/linear_model.py +++ b/src/depiction/calibration/models/linear_model.py @@ -46,20 +46,18 @@ def zero(cls) -> LinearModel: return cls([0, 0]) @classmethod - def fit_lsq(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int | None = None) -> LinearModel: + def fit_lsq(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int = 2) -> LinearModel: """Fits a linear model to the given data using least squares regression.""" - if min_points is not None and x_arr.size < min_points: + if x_arr.size < min_points: return cls.fit_constant(y_arr=y_arr) model = sklearn.linear_model.LinearRegression() model.fit(x_arr[:, np.newaxis], y_arr[:, np.newaxis]) return LinearModel(coef=[model.intercept_[0], model.coef_[0, 0]]) @classmethod - def fit_siegelslopes( - cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int | None = None - ) -> LinearModel: + def fit_siegelslopes(cls, x_arr: NDArray[float], y_arr: NDArray[float], min_points: int = 2) -> LinearModel: """Fits a linear model to the given data using robust Siegel-Slopes regression.""" - if min_points is not None and x_arr.size < min_points: + if x_arr.size < min_points: return cls.fit_constant(y_arr) slope, intercept = scipy.stats.siegelslopes(y=y_arr, x=x_arr) return LinearModel(coef=[intercept, slope])