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
thetathat drifts asT_minrises is the classic sign of a neglected background (fitcurie_weiss, whosechi0term absorbs it, rather thaninverse_curie_weiss, which cannot). Each window is an independentfit()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 forfit().x (
ArrayLike) – The full dataset, same length. Points are selected per window byx; a point whosexis NaN lies in no window. A non-finiteyinside 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 byx; a point whosexis NaN lies in no window. A non-finiteyinside 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 asx) is sliced with each window, so every point keeps its own error; a scalar applies to every point.None(default) means assumed errors, as forfit().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>_errcolumns after the parameters, in the mapping’s order. Each callable gets a dict of every parameter (free and fixed) and returns a number; seederive()for the propagation and its limits. It is not forwarded tofit(). Make sure the quantity means what its name says: an effective moment fromCneedschifitted per mole of magnetic ion, which the fit cannot check.**fit_kwargs (
Any) – Forwarded unchanged to everyfit()call:initial,fixed,limitsandrun_minos. Recorded inparams['fit_kwargs'].
- Return type:
- Returns:
calamari.fitting.windows.FitWindowsResult–.table(one row per window),.results(the alignedFitResultobjects) and.params(the recipe).- Raises:
ValueError – For a malformed window (not a pair of numbers, a non-finite bound,
x_lo >= x_hi), an emptywindows,xandyof different lengths, an arrayyerrof the wrong length, or aderivedname that would collide with another column (or ends in_err). Also when aderivedfunction 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 aderivedthat 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,fixedorlimits.
Notes
Unknown models and parameter names raise
UnknownModelErrorandFitParameterError, respectively (both alsoKeyErrorandCalamariError).FT-1 holds per window: a window whose fit fails is a
success=Falserow, not an exception. That includes a window with no more points than free parameters, whichfit()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]