least_squares
least_squares
¶
Levenberg-Marquardt model fitting for osipy.
This module provides non-linear least squares optimization using
xp-compatible operations. Works on any FittableModel — the fitter
never knows about modality-specific context (time, AIF, b-values).
NO scipy dependency - uses custom vectorized Levenberg-Marquardt implementation that processes all voxels in parallel via batch operations. Works identically on CPU (NumPy) and GPU (CuPy).
References
Marquardt (1963). An Algorithm for Least-Squares Estimation of Nonlinear Parameters. SIAM Journal on Applied Mathematics.
LevenbergMarquardtFitter
¶
Bases: BaseFitter
Vectorized Levenberg-Marquardt fitter.
Processes all voxels simultaneously using batched array operations.
GPU/CPU is automatic via xp = get_array_module() — no manual
use_gpu flag needed.
Supports analytical Jacobians via FittableModel.compute_jacobian_batch().
Falls back to numerical finite differences when the model returns None.
| PARAMETER | DESCRIPTION |
|---|---|
max_iterations
|
Maximum number of LM iterations per batch.
TYPE:
|
tolerance
|
Convergence tolerance for relative cost change.
TYPE:
|
chunk_size
|
Number of voxels per processing chunk for memory management.
Defaults to
TYPE:
|
r2_threshold
|
Minimum R-squared value for a valid fit.
TYPE:
|
Examples:
>>> from osipy.common.fitting.least_squares import LevenbergMarquardtFitter
>>> fitter = LevenbergMarquardtFitter(max_iterations=100)
>>> result = fitter.fit_image(bound_model, data_4d, mask=brain_mask)
fit_batch
¶
Fit a batch of voxels using vectorized Levenberg-Marquardt.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Model with independent variables bound.
TYPE:
|
observed_batch
|
Observed data, shape
TYPE:
|
bounds_override
|
Per-parameter bound overrides.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
params
|
Fitted parameters, shape
TYPE:
|
r2
|
R-squared values, shape
TYPE:
|
converged
|
Convergence flags, shape
TYPE:
|