dosma.PolyFitter

class dosma.PolyFitter(deg: int, rcond: Optional[float] = None, y_bounds: Optional[Tuple[float]] = None, out_ufuncs: Optional[Union[Callable, Sequence[Callable]]] = None, out_bounds=None, r2_threshold: float = 'preferences', nan_to_num: Optional[float] = None, num_workers: Optional[int] = None, chunksize: Optional[int] = None, verbose: bool = False)[source]

The class using linear least squares to fit a polynomial to data.

This class is a wrapper around the dosma.utils.fits.polyfit() function that handles MedicalVolume data and supports additional post-processing on fitted parameters. Most attributes are shared with dosma.utils.fits.CurveFitter. See that class for details.

Unlike non-linear curve fitting, polynomial fitting can be done for multiple data sequences (i.e. multiple pixels/voxels) as a single least squares problem, which is the default in numpy. However, poor matrix conditioning can result in poor parameter estimates. As a result, this class also supports fitting each data sequence separately. If num_workers = None, data sequences will be fit as a single least squares problem. If num_workers = 0, data sequences will be fit separately and sequentially. If num_workers > 0, multiprocessing will be used to fit each data sequence separately and in parallel.

Parameters
  • deg (int) – Degree of the fitting polynomial. See numpy.polyfit().

  • rcond (float, optional) – Relative condition number of the fit. See numpy.polyfit().

  • y_bounds (tuple, optional) – Same as CurveFitter.

  • out_ufuncs (Callable, Sequence[Callable]) – Same as CurveFitter.

  • out_bounds (array-like, optional) – Same as CurveFitter.

  • r2_threshold (float) – Same as CurveFitter.

  • nan_to_num (float, optional) – Same as CurveFitter.

  • num_workers (int, optional) – Maximum number of workers to use for fitting. If None, all data sequences should be solved as a single least squares problem. If 0, each data sequence will be fit separately from one another. Defaults to None.

  • chunksize (int, optional) – Same as CurveFitter.

  • verbose (bool, optional) – Same as CurveFitter.

Examples

Fitting polynomial of degree 1 (\(y = a*x + b\)) to to independent variables x and dependent variables y.

>>> fitter = PolyFitter(deg=1)
>>> popt, r2 = fitter.fit(x, y)
>>> a_hat, b_hat = popt[..., 0], popt[..., 1]

Post-process b by taking the absolute value of b (i.e. \(|b|\)). Set all values not in domain \(0 \leq |b| \leq 100\) or with a goodness of fit less than 0.9 (\(r^2\) < 0.9) to np.nan:

>>> ufunc = lambda x: np.abs(x)
>>> out_bounds = ((-np.inf, np.inf), (0, 100))
>>> fitter = CurveFitter(
...     monoexponential, p0=(1.0, -0.2), out_ufuncs=[None, ufunc], out_bounds=out_bounds)
>>> popt, r2 = fitter.fit(x, y)
>>> a_hat, abs_b_hat = popt[..., 0], popt[..., 1]
__init__(deg: int, rcond: Optional[float] = None, y_bounds: Optional[Tuple[float]] = None, out_ufuncs: Optional[Union[Callable, Sequence[Callable]]] = None, out_bounds=None, r2_threshold: float = 'preferences', nan_to_num: Optional[float] = None, num_workers: Optional[int] = None, chunksize: Optional[int] = None, verbose: bool = False)[source]

Methods

__init__(deg[, rcond, y_bounds, out_ufuncs, ...])

fit(x, y[, mask, copy_headers])

Perform linear least squares fit.

Attributes