calamari.fitting.windows.fit_windows#

calamari.fitting.windows.fit_windows(model, x, y, *, windows, yerr=None, derived=None, **fit_kwargs)#

Fit model separately over each x-window and tabulate the results.

The check that a fitted parameter does not depend on the fitting range: a Curie-Weiss theta that drifts as T_min rises is the classic sign of a neglected background (fit curie_weiss, whose chi0 term absorbs it, rather than inverse_curie_weiss, which cannot). Each window is an independent fit() of the points inside it; see the module docstring for why there is no verdict, threshold or default window.

Parameters:
  • model (str | ModelDefinition) – A registered model name or a definition, as for fit().

  • x (ArrayLike) – The full dataset, same length. Points are selected per window by x; a point whose x is NaN lies in no window. A non-finite y inside a window is kept, and that window’s fit fails – the data are not quietly cleaned.

  • y (ArrayLike) – The full dataset, same length. Points are selected per window by x; a point whose x is NaN lies in no window. A non-finite y inside a window is kept, and that window’s fit fails – the data are not quietly cleaned.

  • windows (Sequence[tuple[float, float]]) – Inclusive (x_lo, x_hi) bounds, one fit each, reported in the order given. Required: choosing the windows is the physics. Windows may overlap and need not be sorted.

  • yerr (ArrayLike | None) – Per-point errors. An array (same length as x) is sliced with each window, so every point keeps its own error; a scalar applies to every point. None (default) means assumed errors, as for fit().

  • derived (Mapping[str, Callable[[Mapping[str, float]], float]] | None) – Quantities to compute from each window’s parameters, tabulated with their propagated errors as <name> and <name>_err columns after the parameters, in the mapping’s order. Each callable gets a dict of every parameter (free and fixed) and returns a number; see derive() for the propagation and its limits. It is not forwarded to fit(). Make sure the quantity means what its name says: an effective moment from C needs chi fitted per mole of magnetic ion, which the fit cannot check.

  • **fit_kwargs (Any) – Forwarded unchanged to every fit() call: initial, fixed, limits and run_minos. Recorded in params['fit_kwargs'].

Return type:

FitWindowsResult

Returns:

calamari.fitting.windows.FitWindowsResult – .table (one row per window), .results (the aligned FitResult objects) and .params (the recipe).

Raises:
  • ValueError – For a malformed window (not a pair of numbers, a non-finite bound, x_lo >= x_hi), an empty windows, x and y of different lengths, an array yerr of the wrong length, or a derived name that would collide with another column (or ends in _err). Also when a derived function raises: the error is re-raised naming the quantity and the window, chained to the original.

  • TypeError – For a keyword fit() does not take, or a derived that is not a mapping of names to callables.

  • FitConstraintError – A forwarded fixed value conflicts with its limits; propagated from fit(), including in empty windows.

  • KeyError – For an unknown model, or an unknown parameter name in initial, fixed or limits.

Notes

Unknown models and parameter names raise UnknownModelError and FitParameterError, respectively (both also KeyError and CalamariError).

FT-1 holds per window: a window whose fit fails is a success=False row, not an exception. That includes a window with no more points than free parameters, which fit() refuses: with zero degrees of freedom the points determine the parameters exactly, and there is no fit to judge.

Examples

>>> import numpy as np
>>> from calamari.fitting import curie_weiss, fit_windows
>>> T = np.linspace(20, 300, 57)
>>> chi = curie_weiss(T, 0.5, -20.0, 2e-4)
>>> scan = fit_windows("curie_weiss", T, chi, windows=[(20, 300), (100, 300)])
>>> scan.table[["x_min", "n_points", "success"]]
   x_min  n_points  success
0   20.0        57     True
1  100.0        41     True
>>> [round(t, 3) for t in scan.table["theta"]]
[-20.0, -20.0]

A derived quantity gets a column and an error column of its own:

>>> from calamari import mu_eff_from_curie_constant
>>> scan = fit_windows(
...     "curie_weiss", T, chi, windows=[(20, 300), (100, 300)],
...     derived={"mu_eff": lambda p: mu_eff_from_curie_constant(p["C"])},
... )
>>> list(scan.table.columns[-2:])
['mu_eff', 'mu_eff_err']
>>> [round(v, 3) for v in scan.table["mu_eff"]]
[2.0, 2.0]