sqil-core 0.0.1__py3-none-any.whl → 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqil_core/__init__.py +5 -2
- sqil_core/config.py +13 -0
- sqil_core/fit/__init__.py +16 -0
- sqil_core/fit/_core.py +936 -0
- sqil_core/fit/_fit.py +782 -0
- sqil_core/fit/_models.py +96 -0
- sqil_core/resonator/__init__.py +11 -0
- sqil_core/resonator/_resonator.py +807 -0
- sqil_core/utils/__init__.py +62 -5
- sqil_core/utils/_analysis.py +292 -0
- sqil_core/utils/{const.py → _const.py} +49 -38
- sqil_core/utils/_formatter.py +188 -0
- sqil_core/utils/_plot.py +107 -0
- sqil_core/utils/{read.py → _read.py} +179 -156
- sqil_core/utils/_utils.py +17 -0
- {sqil_core-0.0.1.dist-info → sqil_core-0.1.0.dist-info}/METADATA +32 -7
- sqil_core-0.1.0.dist-info/RECORD +19 -0
- {sqil_core-0.0.1.dist-info → sqil_core-0.1.0.dist-info}/WHEEL +1 -1
- {sqil_core-0.0.1.dist-info → sqil_core-0.1.0.dist-info}/entry_points.txt +1 -1
- sqil_core/utils/analysis.py +0 -68
- sqil_core/utils/formatter.py +0 -134
- sqil_core-0.0.1.dist-info/RECORD +0 -10
sqil_core/fit/_core.py
ADDED
@@ -0,0 +1,936 @@
|
|
1
|
+
import inspect
|
2
|
+
import warnings
|
3
|
+
from functools import wraps
|
4
|
+
|
5
|
+
import numpy as np
|
6
|
+
import scipy.optimize as spopt
|
7
|
+
from lmfit.model import ModelResult
|
8
|
+
|
9
|
+
from sqil_core.utils import print_fit_metrics as _print_fit_metrics
|
10
|
+
from sqil_core.utils import print_fit_params as _print_fit_params
|
11
|
+
from sqil_core.utils._utils import _count_function_parameters
|
12
|
+
|
13
|
+
|
14
|
+
class FitResult:
|
15
|
+
"""
|
16
|
+
Stores the result of a fitting procedure.
|
17
|
+
|
18
|
+
This class encapsulates the fitted parameters, their standard errors, optimizer output,
|
19
|
+
and fit quality metrics. It also provides functionality for summarizing the results and
|
20
|
+
making predictions using the fitted model.
|
21
|
+
|
22
|
+
Parameters
|
23
|
+
----------
|
24
|
+
params : dict
|
25
|
+
Array of fitted parameters.
|
26
|
+
std_err : dict
|
27
|
+
Array of standard errors of the fitted parameters.
|
28
|
+
fit_output : any
|
29
|
+
Raw output from the optimization routine.
|
30
|
+
metrics : dict, optional
|
31
|
+
Dictionary of fit quality metrics (e.g., R-squared, reduced chi-squared).
|
32
|
+
predict : callable, optional
|
33
|
+
Function of x that returns predictions based on the fitted parameters.
|
34
|
+
If not provided, an exception will be raised when calling it.
|
35
|
+
param_names : list, optional
|
36
|
+
List of parameter names, defaulting to a range based on the number of parameters.
|
37
|
+
metadata : dict, optional
|
38
|
+
Additional information that can be passed in the fit result.
|
39
|
+
|
40
|
+
Methods
|
41
|
+
-------
|
42
|
+
summary()
|
43
|
+
Prints a detailed summary of the fit results, including parameter values,
|
44
|
+
standard errors, and fit quality metrics.
|
45
|
+
_no_prediction()
|
46
|
+
Raises an exception when no prediction function is available.
|
47
|
+
"""
|
48
|
+
|
49
|
+
def __init__(
|
50
|
+
self,
|
51
|
+
params,
|
52
|
+
std_err,
|
53
|
+
fit_output,
|
54
|
+
metrics=None,
|
55
|
+
predict=None,
|
56
|
+
param_names=None,
|
57
|
+
metadata={},
|
58
|
+
):
|
59
|
+
self.params = params
|
60
|
+
self.std_err = std_err
|
61
|
+
self.output = fit_output
|
62
|
+
self.metrics = metrics
|
63
|
+
self.predict = predict or self._no_prediction
|
64
|
+
self.param_names = param_names or list(range(len(params)))
|
65
|
+
self.metadata = metadata
|
66
|
+
|
67
|
+
def __repr__(self):
|
68
|
+
return (
|
69
|
+
f"FitResult(\n"
|
70
|
+
f" params={self.params},\n"
|
71
|
+
f" std_err={self.std_err},\n"
|
72
|
+
f" metrics={self.metrics}\n)"
|
73
|
+
)
|
74
|
+
|
75
|
+
def summary(self):
|
76
|
+
"""Prints a detailed summary of the fit results."""
|
77
|
+
_print_fit_metrics(self.metrics)
|
78
|
+
_print_fit_params(
|
79
|
+
self.param_names,
|
80
|
+
self.params,
|
81
|
+
self.std_err,
|
82
|
+
self.std_err / self.params * 100,
|
83
|
+
)
|
84
|
+
|
85
|
+
def _no_prediction(self):
|
86
|
+
raise Exception("No predition function available")
|
87
|
+
|
88
|
+
|
89
|
+
def fit_output(fit_func):
|
90
|
+
"""
|
91
|
+
Decorator to standardize the output of fitting functions.
|
92
|
+
|
93
|
+
This decorator processes the raw output of various fitting libraries
|
94
|
+
(such as SciPy's curve_fit, least_squares leastsq, and minimize, as well as lmfit)
|
95
|
+
and converts it into a unified `FitResult` object. It extracts
|
96
|
+
optimized parameters, their standard errors, fit quality metrics,
|
97
|
+
and a prediction function.
|
98
|
+
|
99
|
+
Parameters
|
100
|
+
----------
|
101
|
+
fit_func : Callable
|
102
|
+
A function that performs fitting and returns raw fit output,
|
103
|
+
possibly along with metadata.
|
104
|
+
|
105
|
+
Returns
|
106
|
+
-------
|
107
|
+
Callable
|
108
|
+
A wrapped function that returns a `FitResult` object containing:
|
109
|
+
- `params` : list
|
110
|
+
Optimized parameter values.
|
111
|
+
- `std_err` : list or None
|
112
|
+
Standard errors of the fitted parameters.
|
113
|
+
- `metrics` : dict or None
|
114
|
+
Dictionary of fit quality metrics (e.g., reduced chi-squared).
|
115
|
+
- `predict` : Callable or None
|
116
|
+
A function that predicts values using the optimized parameters.
|
117
|
+
- `output` : object
|
118
|
+
The raw optimizer output from the fitting process.
|
119
|
+
- `param_names` : list or None
|
120
|
+
Names of the fitted parameters.
|
121
|
+
- `metadata` : dict
|
122
|
+
A dictionary containing extra information. Advanced uses include passing
|
123
|
+
functions that get evaluated after fit result has been processed.
|
124
|
+
See the documentation, Notebooks/The fit_output decorator
|
125
|
+
|
126
|
+
Raises
|
127
|
+
------
|
128
|
+
TypeError
|
129
|
+
If the fitting function's output format is not recognized.
|
130
|
+
|
131
|
+
Notes
|
132
|
+
-----
|
133
|
+
- If the fit function returns a tuple `(raw_output, metadata)`,
|
134
|
+
the metadata is extracted and applied to enhance the fit results.
|
135
|
+
In case of any conflicts, the metadata overrides the computed values.
|
136
|
+
|
137
|
+
Examples
|
138
|
+
--------
|
139
|
+
>>> @fit_output
|
140
|
+
... def my_fitting_function(x, y):
|
141
|
+
... return some_raw_fit_output
|
142
|
+
...
|
143
|
+
>>> fit_result = my_fitting_function(x_data, y_data)
|
144
|
+
>>> print(fit_result.params)
|
145
|
+
"""
|
146
|
+
|
147
|
+
@wraps(fit_func)
|
148
|
+
def wrapper(*args, **kwargs):
|
149
|
+
# Perform the fit
|
150
|
+
fit_result = fit_func(*args, **kwargs)
|
151
|
+
|
152
|
+
# Extract information from function arguments
|
153
|
+
x_data, y_data = _get_xy_data_from_fit_args(*args, **kwargs)
|
154
|
+
sigma = kwargs.get("sigma", None)
|
155
|
+
has_sigma = isinstance(sigma, (list, np.ndarray))
|
156
|
+
|
157
|
+
# Initilize variables
|
158
|
+
sqil_keys = ["params", "std_err", "metrics", "predict", "output", "param_names"]
|
159
|
+
sqil_dict = {key: None for key in sqil_keys}
|
160
|
+
metadata = {}
|
161
|
+
formatted = None
|
162
|
+
# Set the default parameters to an empty array instead of None
|
163
|
+
sqil_dict["params"] = []
|
164
|
+
|
165
|
+
# Check if the fit output is a tuple and separate it into raw_fit_ouput and metadata
|
166
|
+
if (
|
167
|
+
isinstance(fit_result, tuple)
|
168
|
+
and (len(fit_result) == 2)
|
169
|
+
and isinstance(fit_result[1], dict)
|
170
|
+
):
|
171
|
+
raw_fit_output, metadata = fit_result
|
172
|
+
else:
|
173
|
+
raw_fit_output = fit_result
|
174
|
+
sqil_dict["output"] = raw_fit_output
|
175
|
+
|
176
|
+
# Format the raw_fit_output into a standardized dict
|
177
|
+
# Scipy tuple (curve_fit, leastsq)
|
178
|
+
if _is_scipy_tuple(raw_fit_output):
|
179
|
+
formatted = _format_scipy_tuple(raw_fit_output, has_sigma=has_sigma)
|
180
|
+
|
181
|
+
# Scipy least squares
|
182
|
+
elif _is_scipy_least_squares(raw_fit_output):
|
183
|
+
formatted = _format_scipy_least_squares(raw_fit_output, has_sigma=has_sigma)
|
184
|
+
|
185
|
+
# Scipy minimize
|
186
|
+
elif _is_scipy_minimize(raw_fit_output):
|
187
|
+
residuals = None
|
188
|
+
predict = metadata.get("predict", None)
|
189
|
+
if predict and callable(predict):
|
190
|
+
residuals = y_data - metadata["predict"](x_data, *raw_fit_output.x)
|
191
|
+
formatted = _format_scipy_minimize(
|
192
|
+
raw_fit_output, residuals=residuals, has_sigma=has_sigma
|
193
|
+
)
|
194
|
+
|
195
|
+
# lmfit
|
196
|
+
elif _is_lmfit(raw_fit_output):
|
197
|
+
formatted = _format_lmfit(raw_fit_output)
|
198
|
+
|
199
|
+
# Custom fit output
|
200
|
+
elif isinstance(raw_fit_output, dict):
|
201
|
+
formatted = raw_fit_output
|
202
|
+
|
203
|
+
else:
|
204
|
+
raise TypeError(
|
205
|
+
"Couldn't recognize the output.\n"
|
206
|
+
+ "Are you using scipy? Did you forget to set `full_output=True` in your fit method?"
|
207
|
+
)
|
208
|
+
|
209
|
+
# Update sqil_dict with the formatted fit_output
|
210
|
+
if formatted is not None:
|
211
|
+
sqil_dict.update(formatted)
|
212
|
+
|
213
|
+
# Add/override fileds using metadata
|
214
|
+
sqil_dict.update(metadata)
|
215
|
+
|
216
|
+
# Process metadata
|
217
|
+
metadata = _process_metadata(metadata, sqil_dict)
|
218
|
+
# Remove fields already present in sqil_dict from metadata
|
219
|
+
filtered_metadata = {k: v for k, v in metadata.items() if k not in sqil_keys}
|
220
|
+
|
221
|
+
# Assign the optimized parameters to the prediction function
|
222
|
+
if sqil_dict["predict"] is not None:
|
223
|
+
params = sqil_dict["params"]
|
224
|
+
predict = sqil_dict["predict"]
|
225
|
+
n_inputs = _count_function_parameters(predict)
|
226
|
+
if n_inputs == 1 + len(params):
|
227
|
+
sqil_dict["predict"] = lambda x: predict(x, *params)
|
228
|
+
|
229
|
+
return FitResult(
|
230
|
+
params=sqil_dict.get("params", []),
|
231
|
+
std_err=sqil_dict.get("std_err", None),
|
232
|
+
fit_output=raw_fit_output,
|
233
|
+
metrics=sqil_dict.get("metrics", None),
|
234
|
+
predict=sqil_dict.get("predict", None),
|
235
|
+
param_names=sqil_dict.get("param_names", None),
|
236
|
+
metadata=filtered_metadata,
|
237
|
+
)
|
238
|
+
|
239
|
+
return wrapper
|
240
|
+
|
241
|
+
|
242
|
+
# TODO: make a function that handles the bounds for lmfit.
|
243
|
+
# Such a function will take the bounds as they are returned by @fit_input, i.e lower and upper bounds
|
244
|
+
# and it will iterated through the lmfit parameters to apply the bounds.
|
245
|
+
def fit_input(fit_func):
|
246
|
+
"""
|
247
|
+
Decorator to handle optional fitting inputs like initial guesses, bounds, and fixed parameters
|
248
|
+
for a fitting function.
|
249
|
+
|
250
|
+
- `guess` : list or np.ndarray, optional, default=None
|
251
|
+
The initial guess for the fit. If None it's not passed to the fit function.
|
252
|
+
- `bounds` : list or np.ndarray, optional, default=(-np.inf, np.inf)
|
253
|
+
The bounds on the fit parameters in the form [(min, max), (min, max), ...].
|
254
|
+
- `fixed_params` : list or np.ndarray, optional, default=None
|
255
|
+
Indices of the parameters that must remain fixed during the optimization.
|
256
|
+
For example fitting `f(x, a, b)`, if we want to fix the value of `a` we would pass
|
257
|
+
`fit_f(guess=[a_guess, b_guess], fixed_params=[0])`
|
258
|
+
- `fixed_bound_factor` : float, optional, default=1e-6
|
259
|
+
The relative tolerance allowed for parameters that must remain fixed (`fixed_params`).
|
260
|
+
|
261
|
+
IMPORTANT: This decorator requires the x and y input vectors to be named `x_data` and `y_data`.
|
262
|
+
The initial guess must be called `guess` and the bounds `bounds`.
|
263
|
+
|
264
|
+
Parameters
|
265
|
+
----------
|
266
|
+
fit_func : callable
|
267
|
+
The fitting function to be decorated. This function should accept `x_data` and `y_data` as
|
268
|
+
mandatory parameters and may optionally accept `guess` and `bounds` (plus any other additional
|
269
|
+
parameter).
|
270
|
+
|
271
|
+
Returns
|
272
|
+
-------
|
273
|
+
callable
|
274
|
+
A wrapper function that processes the input arguments and then calls the original fitting
|
275
|
+
function with the preprocessed inputs. This function also handles warnings if unsupported
|
276
|
+
parameters are passed to the fit function.
|
277
|
+
|
278
|
+
Notes
|
279
|
+
-----
|
280
|
+
- The parameters in `guess`, `bounds` and `fixed_params` must be in the same order as in the
|
281
|
+
modeled function definition.
|
282
|
+
- The decorator can fix certain parameters by narrowing their bounds based on an initial guess
|
283
|
+
and a specified `fixed_bound_factor`.
|
284
|
+
- The decorator processes bounds by setting them as `(-np.inf, np.inf)` if they are not specified (`None`).
|
285
|
+
|
286
|
+
Examples
|
287
|
+
-------
|
288
|
+
>>> @fit_input
|
289
|
+
... def my_fit_func(x_data, y_data, guess=None, bounds=None, fixed_params=None):
|
290
|
+
... # Perform fitting...
|
291
|
+
... return fit_result
|
292
|
+
>>> x_data = np.linspace(0, 10, 100)
|
293
|
+
>>> y_data = np.sin(x_data) + np.random.normal(0, 0.1, 100)
|
294
|
+
>>> result = my_fit_func(x_data, y_data, guess=[1, 1], bounds=[(0, 5), (-np.inf, np.inf)])
|
295
|
+
"""
|
296
|
+
|
297
|
+
@wraps(fit_func)
|
298
|
+
def wrapper(
|
299
|
+
x_data,
|
300
|
+
y_data,
|
301
|
+
guess=None,
|
302
|
+
bounds=None,
|
303
|
+
fixed_params=None,
|
304
|
+
fixed_bound_factor=1e-6,
|
305
|
+
**kwargs,
|
306
|
+
):
|
307
|
+
# Inspect function to check if it requires guess and bounds
|
308
|
+
func_params = inspect.signature(fit_func).parameters
|
309
|
+
|
310
|
+
# Check if the user passed parameters that are not supported by the fit fun
|
311
|
+
if guess and ("guess" not in func_params):
|
312
|
+
warnings.warn("The fit function doesn't allow any initial guess.")
|
313
|
+
if bounds and ("bounds" not in func_params):
|
314
|
+
warnings.warn("The fit function doesn't allow any bounds.")
|
315
|
+
if fixed_params and (not guess):
|
316
|
+
raise ValueError("Using fixed_params requires an initial guess.")
|
317
|
+
|
318
|
+
# Process bounds if the function accepts it
|
319
|
+
if bounds and ("bounds" in func_params):
|
320
|
+
processed_bounds = np.array(
|
321
|
+
[(-np.inf, np.inf) if b is None else b for b in bounds],
|
322
|
+
dtype=np.float64,
|
323
|
+
)
|
324
|
+
lower_bounds, upper_bounds = (
|
325
|
+
processed_bounds[:, 0],
|
326
|
+
processed_bounds[:, 1],
|
327
|
+
)
|
328
|
+
else:
|
329
|
+
lower_bounds, upper_bounds = None, None
|
330
|
+
|
331
|
+
# Fix parameters by setting a very tight bound
|
332
|
+
if (fixed_params is not None) and (guess is not None):
|
333
|
+
if bounds is None:
|
334
|
+
lower_bounds = -np.inf * np.ones(len(guess))
|
335
|
+
upper_bounds = np.inf * np.ones(len(guess))
|
336
|
+
for idx in fixed_params:
|
337
|
+
tolerance = (
|
338
|
+
abs(guess[idx]) * fixed_bound_factor
|
339
|
+
if guess[idx] != 0
|
340
|
+
else fixed_bound_factor
|
341
|
+
)
|
342
|
+
lower_bounds[idx] = guess[idx] - tolerance
|
343
|
+
upper_bounds[idx] = guess[idx] + tolerance
|
344
|
+
|
345
|
+
# Prepare arguments dynamically
|
346
|
+
fit_args = {"x_data": x_data, "y_data": y_data, **kwargs}
|
347
|
+
|
348
|
+
if guess is not None and "guess" in func_params:
|
349
|
+
fit_args["guess"] = guess
|
350
|
+
if (
|
351
|
+
(bounds is not None) or (fixed_params is not None)
|
352
|
+
) and "bounds" in func_params:
|
353
|
+
fit_args["bounds"] = (lower_bounds, upper_bounds)
|
354
|
+
|
355
|
+
# Call the wrapped function with preprocessed inputs
|
356
|
+
return fit_func(**fit_args)
|
357
|
+
|
358
|
+
return wrapper
|
359
|
+
|
360
|
+
|
361
|
+
def _process_metadata(metadata: dict, sqil_dict: dict):
|
362
|
+
"""Process metadata by computing values that cannot be calculated before having
|
363
|
+
the sqil_dict. For example use the standard errors to compute a different metric.
|
364
|
+
|
365
|
+
Treats items whose key starts with @ as functions that take sqil_dict as input.
|
366
|
+
So it evaluates them and renames the key removing the @.
|
367
|
+
"""
|
368
|
+
res = metadata.copy()
|
369
|
+
for key, value in metadata.items():
|
370
|
+
if key.startswith("@"):
|
371
|
+
res[key[1:]] = value(sqil_dict)
|
372
|
+
del res[key]
|
373
|
+
return res
|
374
|
+
|
375
|
+
|
376
|
+
def compute_adjusted_standard_errors(
|
377
|
+
pcov: np.ndarray,
|
378
|
+
residuals: np.ndarray,
|
379
|
+
red_chi2=None,
|
380
|
+
cov_rescaled=True,
|
381
|
+
sigma=None,
|
382
|
+
) -> np.ndarray:
|
383
|
+
"""
|
384
|
+
Compute adjusted standard errors for fitted parameters.
|
385
|
+
|
386
|
+
This function adjusts the covariance matrix based on the reduced chi-squared
|
387
|
+
value and calculates the standard errors for each parameter. It accounts for
|
388
|
+
cases where the covariance matrix is not available or the fit is nearly perfect.
|
389
|
+
|
390
|
+
Parameters
|
391
|
+
----------
|
392
|
+
pcov : np.ndarray
|
393
|
+
Covariance matrix of the fitted parameters, typically obtained from an
|
394
|
+
optimization routine.
|
395
|
+
residuals : np.ndarray
|
396
|
+
Residuals of the fit, defined as the difference between observed and
|
397
|
+
model-predicted values.
|
398
|
+
red_chi2 : float, optional
|
399
|
+
Precomputed reduced chi-squared value. If `None`, it is computed from
|
400
|
+
`residuals` and `sigma`.
|
401
|
+
cov_rescaled : bool, default=True
|
402
|
+
Whether the fitting process already rescales the covariance matrix with
|
403
|
+
the reduced chi-squared.
|
404
|
+
sigma : np.ndarray, optional
|
405
|
+
Experimental uncertainties. Only used if `cov_rescaled=False` AND
|
406
|
+
known experimental errors are available.
|
407
|
+
|
408
|
+
Returns
|
409
|
+
-------
|
410
|
+
np.ndarray
|
411
|
+
Standard errors for each fitted parameter. If the covariance matrix is
|
412
|
+
undefined, returns `None`.
|
413
|
+
|
414
|
+
Warnings
|
415
|
+
--------
|
416
|
+
- If the covariance matrix is not available (`pcov is None`), the function
|
417
|
+
issues a warning about possible numerical instability or a near-perfect fit.
|
418
|
+
- If the reduced chi-squared value is `NaN`, the function returns `NaN` for
|
419
|
+
all standard errors.
|
420
|
+
|
421
|
+
Notes
|
422
|
+
-----
|
423
|
+
- The covariance matrix is scaled by the reduced chi-squared value to adjust
|
424
|
+
for under- or overestimation of uncertainties.
|
425
|
+
- If `red_chi2` is not provided, it is computed internally using the residuals.
|
426
|
+
- If a near-perfect fit is detected (all residuals close to zero), the function
|
427
|
+
warns that standard errors may not be necessary.
|
428
|
+
|
429
|
+
Examples
|
430
|
+
--------
|
431
|
+
>>> pcov = np.array([[0.04, 0.01], [0.01, 0.09]])
|
432
|
+
>>> residuals = np.array([0.1, -0.2, 0.15])
|
433
|
+
>>> compute_adjusted_standard_errors(pcov, residuals)
|
434
|
+
array([0.2, 0.3])
|
435
|
+
"""
|
436
|
+
# Check for invalid covariance
|
437
|
+
if pcov is None:
|
438
|
+
if np.allclose(residuals, 0, atol=1e-10):
|
439
|
+
warnings.warn(
|
440
|
+
"Covariance matrix could not be estimated due to an almost perfect fit. "
|
441
|
+
"Standard errors are undefined but may not be necessary in this case."
|
442
|
+
)
|
443
|
+
else:
|
444
|
+
warnings.warn(
|
445
|
+
"Covariance matrix could not be estimated. This could be due to poor model fit "
|
446
|
+
"or numerical instability. Review the data or model configuration."
|
447
|
+
)
|
448
|
+
return None
|
449
|
+
|
450
|
+
# Calculate reduced chi-squared
|
451
|
+
n_params = len(np.diag(pcov))
|
452
|
+
if red_chi2 is None:
|
453
|
+
_, red_chi2 = compute_chi2(
|
454
|
+
residuals, n_params, cov_rescaled=cov_rescaled, sigma=sigma
|
455
|
+
)
|
456
|
+
|
457
|
+
# Rescale the covariance matrix
|
458
|
+
if np.isnan(red_chi2):
|
459
|
+
pcov_rescaled = np.nan
|
460
|
+
else:
|
461
|
+
pcov_rescaled = pcov * red_chi2
|
462
|
+
|
463
|
+
# Calculate standard errors for each parameter
|
464
|
+
if np.any(np.isnan(pcov_rescaled)):
|
465
|
+
standard_errors = np.full(n_params, np.nan, dtype=float)
|
466
|
+
else:
|
467
|
+
standard_errors = np.sqrt(np.diag(pcov_rescaled))
|
468
|
+
|
469
|
+
return standard_errors
|
470
|
+
|
471
|
+
|
472
|
+
def compute_chi2(residuals, n_params=None, cov_rescaled=True, sigma: np.ndarray = None):
|
473
|
+
"""
|
474
|
+
Compute the chi-squared (χ²) and reduced chi-squared (χ²_red) statistics.
|
475
|
+
|
476
|
+
This function calculates the chi-squared value based on residuals and an
|
477
|
+
estimated or provided uncertainty (`sigma`). If the number of model parameters
|
478
|
+
(`n_params`) is specified, it also computes the reduced chi-squared.
|
479
|
+
|
480
|
+
Parameters
|
481
|
+
----------
|
482
|
+
residuals : np.ndarray
|
483
|
+
The difference between observed and model-predicted values.
|
484
|
+
n_params : int, optional
|
485
|
+
Number of fitted parameters. If provided, the function also computes
|
486
|
+
the reduced chi-squared (χ²_red).
|
487
|
+
cov_rescaled : bool, default=True
|
488
|
+
Whether the covariance matrix has been already rescaled by the fit method.
|
489
|
+
If `True`, the function assumes proper uncertainty scaling. Otherwise,
|
490
|
+
it estimates uncertainty from the standard deviation of the residuals.
|
491
|
+
sigma : np.ndarray, optional
|
492
|
+
Experimental uncertainties. Should only be used when the fitting process
|
493
|
+
does not account for experimental errors AND known uncertainties are available.
|
494
|
+
|
495
|
+
Returns
|
496
|
+
-------
|
497
|
+
chi2 : float
|
498
|
+
The chi-squared statistic (χ²), which measures the goodness of fit.
|
499
|
+
red_chi2 : float (if `n_params` is provided)
|
500
|
+
The reduced chi-squared statistic (χ²_red), computed as χ² divided by
|
501
|
+
the degrees of freedom (N - p). If `n_params` is `None`, only χ² is returned.
|
502
|
+
|
503
|
+
Warnings
|
504
|
+
--------
|
505
|
+
- If the degrees of freedom (N - p) is non-positive, a warning is issued,
|
506
|
+
and χ²_red is set to NaN. This may indicate overfitting or an insufficient
|
507
|
+
number of data points.
|
508
|
+
- If any uncertainty value in `sigma` is zero, it is replaced with machine epsilon
|
509
|
+
to prevent division by zero.
|
510
|
+
|
511
|
+
Notes
|
512
|
+
-----
|
513
|
+
- If `sigma` is not provided and `cov_rescaled=False`, the function estimates
|
514
|
+
the uncertainty using the standard deviation of residuals.
|
515
|
+
- The reduced chi-squared value (χ²_red) should ideally be close to 1 for a good fit.
|
516
|
+
Values significantly greater than 1 indicate underfitting, while values much less
|
517
|
+
than 1 suggest overfitting.
|
518
|
+
|
519
|
+
Examples
|
520
|
+
--------
|
521
|
+
>>> residuals = np.array([0.1, -0.2, 0.15, -0.05])
|
522
|
+
>>> compute_chi2(residuals, n_params=2)
|
523
|
+
(0.085, 0.0425) # Example output
|
524
|
+
"""
|
525
|
+
# If the optimization does not account for th experimental sigma,
|
526
|
+
# approximate it with the std of the residuals
|
527
|
+
S = 1 if cov_rescaled else np.std(residuals)
|
528
|
+
# If the experimental error is provided, use that instead
|
529
|
+
if sigma is not None:
|
530
|
+
S = sigma
|
531
|
+
|
532
|
+
# Replace 0 elements of S with the machine epsilon to avoid divisions by 0
|
533
|
+
if not np.isscalar(S):
|
534
|
+
S_safe = np.where(S == 0, np.finfo(float).eps, S)
|
535
|
+
else:
|
536
|
+
S_safe = np.finfo(float).eps if S == 0 else S
|
537
|
+
|
538
|
+
# Compute chi squared
|
539
|
+
chi2 = np.sum((residuals / S_safe) ** 2)
|
540
|
+
# If number of parameters is not provided return just chi2
|
541
|
+
if n_params is None:
|
542
|
+
return chi2
|
543
|
+
|
544
|
+
# Reduced chi squared
|
545
|
+
dof = len(residuals) - n_params # degrees of freedom (N - p)
|
546
|
+
if dof <= 0:
|
547
|
+
warnings.warn(
|
548
|
+
"Degrees of freedom (dof) is non-positive. This may indicate overfitting or insufficient data."
|
549
|
+
)
|
550
|
+
red_chi2 = np.nan
|
551
|
+
else:
|
552
|
+
red_chi2 = np.sum(residuals**2) / dof
|
553
|
+
|
554
|
+
return chi2, red_chi2
|
555
|
+
|
556
|
+
|
557
|
+
def _is_scipy_tuple(result):
|
558
|
+
"""
|
559
|
+
Check whether the given result follows the expected structure of a SciPy optimization tuple.
|
560
|
+
"""
|
561
|
+
if isinstance(result, tuple):
|
562
|
+
if len(result) < 3:
|
563
|
+
raise TypeError(
|
564
|
+
"Fit result is a tuple, but couldn't recognize it.\n"
|
565
|
+
+ "Are you using scipy? Did you forget to set `full_output=True` in your fit method?"
|
566
|
+
)
|
567
|
+
popt = result[0]
|
568
|
+
cov_ish = result[1]
|
569
|
+
infodict = result[2]
|
570
|
+
keys_to_check = ["fvec"]
|
571
|
+
|
572
|
+
if cov_ish is not None:
|
573
|
+
cov_check = isinstance(cov_ish, np.ndarray) and cov_ish.ndim == 2
|
574
|
+
else:
|
575
|
+
cov_check = True
|
576
|
+
return (
|
577
|
+
isinstance(popt, np.ndarray)
|
578
|
+
and cov_check
|
579
|
+
and (all(key in infodict for key in keys_to_check))
|
580
|
+
)
|
581
|
+
return False
|
582
|
+
|
583
|
+
|
584
|
+
def _is_scipy_minimize(result):
|
585
|
+
"""
|
586
|
+
Check whether the given result follows the expected structure of a SciPy minimize.
|
587
|
+
"""
|
588
|
+
return (
|
589
|
+
isinstance(result, spopt.OptimizeResult)
|
590
|
+
and hasattr(result, "fun")
|
591
|
+
and np.isscalar(result.fun)
|
592
|
+
and hasattr(result, "jac")
|
593
|
+
)
|
594
|
+
|
595
|
+
|
596
|
+
def _is_scipy_least_squares(result):
|
597
|
+
"""
|
598
|
+
Check whether the given result follows the expected structure of a SciPy least_squares.
|
599
|
+
"""
|
600
|
+
return (
|
601
|
+
isinstance(result, spopt.OptimizeResult)
|
602
|
+
and hasattr(result, "cost")
|
603
|
+
and hasattr(result, "fun")
|
604
|
+
and hasattr(result, "jac")
|
605
|
+
)
|
606
|
+
|
607
|
+
|
608
|
+
def _is_lmfit(result):
|
609
|
+
"""
|
610
|
+
Check whether the given result follows the expected structure of a lmfit fit.
|
611
|
+
"""
|
612
|
+
return isinstance(result, ModelResult)
|
613
|
+
|
614
|
+
|
615
|
+
def _format_scipy_tuple(result, has_sigma=False):
|
616
|
+
"""
|
617
|
+
Formats the output of a SciPy fitting function into a standardized dictionary.
|
618
|
+
|
619
|
+
This function takes the tuple returned by SciPy optimization functions (e.g., `curve_fit`, `leastsq`)
|
620
|
+
and extracts relevant fitting parameters, standard errors, and reduced chi-squared values. It ensures
|
621
|
+
the result is structured consistently for further processing.
|
622
|
+
|
623
|
+
Parameters
|
624
|
+
----------
|
625
|
+
result : tuple
|
626
|
+
A tuple containing the fitting results from a SciPy function. Expected structure:
|
627
|
+
- `result[0]`: `popt` (optimized parameters, NumPy array)
|
628
|
+
- `result[1]`: `pcov` (covariance matrix, NumPy array or None)
|
629
|
+
- `result[2]`: `infodict` (dictionary containing residuals, required for error computation)
|
630
|
+
|
631
|
+
has_sigma : bool, optional
|
632
|
+
Indicates whether the fitting procedure considered experimental errors (`sigma`).
|
633
|
+
If `True`, the covariance matrix (`pcov`) does not need rescaling.
|
634
|
+
|
635
|
+
Returns
|
636
|
+
-------
|
637
|
+
dict
|
638
|
+
A dictionary containing:
|
639
|
+
- `"params"`: The optimized parameters (`popt`).
|
640
|
+
- `"std_err"`: The standard errors computed from the covariance matrix (`pcov`).
|
641
|
+
- `"metrics"`: A dictionary containing the reduced chi-squared (`red_chi2`).
|
642
|
+
"""
|
643
|
+
if not isinstance(result, tuple):
|
644
|
+
raise TypeError("Fit result must be a tuple")
|
645
|
+
|
646
|
+
std_err = None
|
647
|
+
popt, pcov, infodict = None, None, None
|
648
|
+
|
649
|
+
# Extract output parameters
|
650
|
+
length = len(result)
|
651
|
+
popt = result[0]
|
652
|
+
pcov = result[1] if length > 1 else None
|
653
|
+
infodict = result[2] if length > 2 else None
|
654
|
+
|
655
|
+
if infodict is not None:
|
656
|
+
residuals = infodict["fvec"]
|
657
|
+
_, red_chi2 = compute_chi2(
|
658
|
+
residuals, n_params=len(popt), cov_rescaled=has_sigma
|
659
|
+
)
|
660
|
+
if pcov is not None:
|
661
|
+
std_err = compute_adjusted_standard_errors(
|
662
|
+
pcov, residuals, cov_rescaled=has_sigma, red_chi2=red_chi2
|
663
|
+
)
|
664
|
+
|
665
|
+
return {"params": popt, "std_err": std_err, "metrics": {"red_chi2": red_chi2}}
|
666
|
+
|
667
|
+
|
668
|
+
def _format_scipy_least_squares(result, has_sigma=False):
|
669
|
+
"""
|
670
|
+
Formats the output of a SciPy least-squares optimization into a standardized dictionary.
|
671
|
+
|
672
|
+
This function processes the result of a SciPy least-squares fitting function (e.g., `scipy.optimize.least_squares`)
|
673
|
+
and structures the fitting parameters, standard errors, and reduced chi-squared values for consistent downstream use.
|
674
|
+
|
675
|
+
Parameters
|
676
|
+
----------
|
677
|
+
result : `scipy.optimize.OptimizeResult`
|
678
|
+
The result of a least-squares optimization (e.g., from `scipy.optimize.least_squares`).
|
679
|
+
It must contain the following fields:
|
680
|
+
- `result.x`: Optimized parameters (NumPy array)
|
681
|
+
- `result.fun`: Residuals (array of differences between the observed and fitted data)
|
682
|
+
- `result.jac`: Jacobian matrix (used to estimate covariance)
|
683
|
+
|
684
|
+
has_sigma : bool, optional
|
685
|
+
Indicates whether the fitting procedure considered experimental errors (`sigma`).
|
686
|
+
If `True`, the covariance matrix does not need rescaling.
|
687
|
+
|
688
|
+
Returns
|
689
|
+
-------
|
690
|
+
dict
|
691
|
+
A dictionary containing:
|
692
|
+
- `"params"`: Optimized parameters (`result.x`).
|
693
|
+
- `"std_err"`: Standard errors computed from the covariance matrix and residuals.
|
694
|
+
- `"metrics"`: A dictionary containing the reduced chi-squared (`red_chi2`).
|
695
|
+
"""
|
696
|
+
params = result.x
|
697
|
+
residuals = result.fun
|
698
|
+
cov = np.linalg.inv(result.jac.T @ result.jac)
|
699
|
+
_, red_chi2 = compute_chi2(residuals, n_params=len(params), cov_rescaled=has_sigma)
|
700
|
+
std_err = compute_adjusted_standard_errors(
|
701
|
+
cov, residuals, cov_rescaled=has_sigma, red_chi2=red_chi2
|
702
|
+
)
|
703
|
+
|
704
|
+
return {"params": params, "std_err": std_err, "metrics": {"red_chi2": red_chi2}}
|
705
|
+
|
706
|
+
|
707
|
+
def _format_scipy_minimize(result, residuals=None, has_sigma=False):
|
708
|
+
"""
|
709
|
+
Formats the output of a SciPy minimize optimization into a standardized dictionary.
|
710
|
+
|
711
|
+
This function processes the result of a SciPy minimization optimization (e.g., `scipy.optimize.minimize`)
|
712
|
+
and structures the fitting parameters, standard errors, and reduced chi-squared values for consistent downstream use.
|
713
|
+
|
714
|
+
Parameters
|
715
|
+
----------
|
716
|
+
result : `scipy.optimize.OptimizeResult`
|
717
|
+
The result of a minimization optimization (e.g., from `scipy.optimize.minimize`).
|
718
|
+
It must contain the following fields:
|
719
|
+
- `result.x`: Optimized parameters (NumPy array).
|
720
|
+
- `result.hess_inv`: Inverse Hessian matrix used to estimate the covariance.
|
721
|
+
|
722
|
+
residuals : array-like, optional
|
723
|
+
The residuals (differences between observed data and fitted model).
|
724
|
+
If not provided, standard errors will be computed based on the inverse Hessian matrix.
|
725
|
+
|
726
|
+
has_sigma : bool, optional
|
727
|
+
Indicates whether the fitting procedure considered experimental errors (`sigma`).
|
728
|
+
If `True`, the covariance matrix does not need rescaling.
|
729
|
+
|
730
|
+
Returns
|
731
|
+
-------
|
732
|
+
dict
|
733
|
+
A dictionary containing:
|
734
|
+
- `"params"`: Optimized parameters (`result.x`).
|
735
|
+
- `"std_err"`: Standard errors computed either from the Hessian matrix or based on the residuals.
|
736
|
+
- `"metrics"`: A dictionary containing the reduced chi-squared (`red_chi2`), if residuals are provided.
|
737
|
+
"""
|
738
|
+
params = result.x
|
739
|
+
cov = _get_covariance_from_scipy_optimize_result(result)
|
740
|
+
metrics = None
|
741
|
+
|
742
|
+
if residuals is None:
|
743
|
+
std_err = np.sqrt(np.abs(result.hess_inv.diagonal()))
|
744
|
+
else:
|
745
|
+
std_err = compute_adjusted_standard_errors(
|
746
|
+
cov, residuals, cov_rescaled=has_sigma
|
747
|
+
)
|
748
|
+
_, red_chi2 = compute_chi2(
|
749
|
+
residuals, n_params=len(params), cov_rescaled=has_sigma
|
750
|
+
)
|
751
|
+
metrics = {"red_chi2": red_chi2}
|
752
|
+
|
753
|
+
return {"params": params, "std_err": std_err, "metrics": metrics}
|
754
|
+
|
755
|
+
|
756
|
+
def _format_lmfit(result: ModelResult):
|
757
|
+
"""
|
758
|
+
Formats the output of an lmfit model fitting result into a standardized dictionary.
|
759
|
+
|
760
|
+
This function processes the result of an lmfit model fitting (e.g., from `lmfit.Model.fit`) and
|
761
|
+
structures the fitting parameters, their standard errors, reduced chi-squared, and a prediction function.
|
762
|
+
|
763
|
+
Parameters
|
764
|
+
----------
|
765
|
+
result : `lmfit.ModelResult`
|
766
|
+
The result of an lmfit model fitting procedure. It must contain the following fields:
|
767
|
+
- `result.params`: A dictionary of fitted parameters and their values.
|
768
|
+
- `result.redchi`: The reduced chi-squared value.
|
769
|
+
- `result.eval`: A method to evaluate the fitted model using independent variable values.
|
770
|
+
- `result.userkws`: Dictionary of user-supplied keywords that includes the independent variable.
|
771
|
+
- `result.model.independent_vars`: List of independent variable names in the model.
|
772
|
+
|
773
|
+
Returns
|
774
|
+
-------
|
775
|
+
dict
|
776
|
+
A dictionary containing:
|
777
|
+
- `"params"`: Optimized parameters (as a NumPy array).
|
778
|
+
- `"std_err"`: Standard errors of the parameters.
|
779
|
+
- `"metrics"`: A dictionary containing the reduced chi-squared (`red_chi2`).
|
780
|
+
- `"predict"`: A function that predicts the model's output given an input (using optimized parameters).
|
781
|
+
- `"param_names"`: List of parameter names.
|
782
|
+
|
783
|
+
Notes
|
784
|
+
-----
|
785
|
+
- lmfit already rescales standard errors by the reduced chi-squared, so no further adjustments are made.
|
786
|
+
- The independent variable name used in the fit is determined from `result.userkws` and `result.model.independent_vars`.
|
787
|
+
- The function creates a prediction function (`predict`) from the fitted model.
|
788
|
+
"""
|
789
|
+
params = np.array([param.value for param in result.params.values()])
|
790
|
+
param_names = list(result.params.keys())
|
791
|
+
std_err = np.array(
|
792
|
+
[
|
793
|
+
param.stderr if param.stderr is not None else np.nan
|
794
|
+
for param in result.params.values()
|
795
|
+
]
|
796
|
+
)
|
797
|
+
# Determine the independent variable name used in the fit
|
798
|
+
independent_var = result.userkws.keys() & result.model.independent_vars
|
799
|
+
independent_var = (
|
800
|
+
independent_var.pop() if independent_var else result.model.independent_vars[0]
|
801
|
+
)
|
802
|
+
fit_function = lambda x: result.eval(**{independent_var: x})
|
803
|
+
|
804
|
+
return {
|
805
|
+
"params": params,
|
806
|
+
"std_err": std_err,
|
807
|
+
"metrics": {"red_chi2": result.redchi},
|
808
|
+
"predict": fit_function,
|
809
|
+
"param_names": param_names,
|
810
|
+
}
|
811
|
+
|
812
|
+
|
813
|
+
def _get_covariance_from_scipy_optimize_result(
|
814
|
+
result: spopt.OptimizeResult,
|
815
|
+
) -> np.ndarray:
|
816
|
+
"""
|
817
|
+
Extracts the covariance matrix (or an approximation) from a scipy optimization result.
|
818
|
+
|
819
|
+
This function attempts to retrieve the covariance matrix of the fitted parameters from the
|
820
|
+
result object returned by a scipy optimization method. It first checks for the presence of
|
821
|
+
the inverse Hessian (`hess_inv`), which is used to estimate the covariance. If it's not available,
|
822
|
+
the function attempts to compute the covariance using the Hessian matrix (`hess`).
|
823
|
+
|
824
|
+
Parameters
|
825
|
+
----------
|
826
|
+
result : `scipy.optimize.OptimizeResult`
|
827
|
+
The result object returned by a scipy optimization function, such as `scipy.optimize.minimize` or `scipy.optimize.curve_fit`.
|
828
|
+
This object contains the optimization results, including the Hessian or its inverse.
|
829
|
+
|
830
|
+
Returns
|
831
|
+
-------
|
832
|
+
np.ndarray or None
|
833
|
+
The covariance matrix of the optimized parameters, or `None` if it cannot be computed.
|
834
|
+
If the inverse Hessian (`hess_inv`) is available, it will be returned directly.
|
835
|
+
If the Hessian matrix (`hess`) is available and not singular, its inverse will be computed and returned.
|
836
|
+
If neither is available, the function returns `None`.
|
837
|
+
|
838
|
+
Notes
|
839
|
+
-----
|
840
|
+
- If the Hessian matrix (`hess`) is singular or nearly singular, the covariance matrix cannot be computed.
|
841
|
+
- In some cases, the inverse Hessian (`hess_inv`) is directly available and provides the covariance without further computation.
|
842
|
+
"""
|
843
|
+
if hasattr(result, "hess_inv"):
|
844
|
+
hess_inv = result.hess_inv
|
845
|
+
|
846
|
+
# Handle different types of hess_inv
|
847
|
+
if isinstance(hess_inv, np.ndarray):
|
848
|
+
return hess_inv
|
849
|
+
elif hasattr(hess_inv, "todense"):
|
850
|
+
return hess_inv.todense()
|
851
|
+
|
852
|
+
if hasattr(result, "hess") and result.hess is not None:
|
853
|
+
try:
|
854
|
+
return np.linalg.inv(result.hess)
|
855
|
+
except np.linalg.LinAlgError:
|
856
|
+
pass # Hessian is singular, cannot compute covariance
|
857
|
+
|
858
|
+
return None
|
859
|
+
|
860
|
+
|
861
|
+
def _get_xy_data_from_fit_args(*args, **kwargs):
|
862
|
+
"""
|
863
|
+
Extracts x and y data from the given arguments and keyword arguments.
|
864
|
+
|
865
|
+
This helper function retrieves the x and y data (1D vectors) from the function's arguments or keyword arguments.
|
866
|
+
The function checks for common keyword names like "x_data", "xdata", "x", "y_data", "ydata", and "y", and returns
|
867
|
+
the corresponding data. If no keyword arguments are found, it attempts to extract the first two consecutive 1D
|
868
|
+
vectors from the positional arguments.
|
869
|
+
|
870
|
+
Parameters
|
871
|
+
----------
|
872
|
+
*args : variable length argument list
|
873
|
+
The positional arguments passed to the function, potentially containing the x and y data.
|
874
|
+
|
875
|
+
**kwargs : keyword arguments
|
876
|
+
The keyword arguments passed to the function, potentially containing keys such as "x_data", "x", "y_data", or "y".
|
877
|
+
|
878
|
+
Returns
|
879
|
+
-------
|
880
|
+
tuple of (np.ndarray, np.ndarray)
|
881
|
+
A tuple containing the x data and y data as 1D numpy arrays or lists. If no valid data is found, returns (None, None).
|
882
|
+
|
883
|
+
Raises
|
884
|
+
------
|
885
|
+
ValueError
|
886
|
+
If both x and y data cannot be found in the input arguments.
|
887
|
+
|
888
|
+
Notes
|
889
|
+
-----
|
890
|
+
- The function looks for the x and y data in the keyword arguments first, in the order of x_keys and y_keys.
|
891
|
+
- If both x and y data are not found in keyword arguments, the function will look for the first two consecutive
|
892
|
+
1D vectors in the positional arguments.
|
893
|
+
- If the data cannot be found, the function will return (None, None).
|
894
|
+
- The function validates that the extracted x and y data are 1D vectors (either lists or numpy arrays).
|
895
|
+
"""
|
896
|
+
# Possible keyword names for x and y data
|
897
|
+
x_keys = ["x_data", "xdata", "x"]
|
898
|
+
y_keys = ["y_data", "ydata", "y"]
|
899
|
+
|
900
|
+
# Validate if an object is a 1D vector
|
901
|
+
def is_valid_vector(obj):
|
902
|
+
return isinstance(obj, (list, np.ndarray)) and np.ndim(obj) == 1
|
903
|
+
|
904
|
+
x_data, y_data = None, None
|
905
|
+
|
906
|
+
# Look for x_data in keyword arguments
|
907
|
+
for key in x_keys:
|
908
|
+
if key in kwargs and is_valid_vector(kwargs[key]):
|
909
|
+
x_data = kwargs[key]
|
910
|
+
break
|
911
|
+
# Look for y_data in keyword arguments
|
912
|
+
for key in y_keys:
|
913
|
+
if key in kwargs and is_valid_vector(kwargs[key]):
|
914
|
+
y_data = kwargs[key]
|
915
|
+
break
|
916
|
+
|
917
|
+
# If both parameters were found, return them
|
918
|
+
if (x_data is not None) and (y_data is not None):
|
919
|
+
return x_data, y_data
|
920
|
+
|
921
|
+
# If the args have only 1 entry
|
922
|
+
if len(args) == 1 and is_valid_vector(args[0]):
|
923
|
+
if y_data is not None:
|
924
|
+
x_data = args[0]
|
925
|
+
else:
|
926
|
+
y_data = args[0]
|
927
|
+
|
928
|
+
# If x and y were not found, try finding the first two consecutive vectors in args
|
929
|
+
if x_data is None or y_data is None:
|
930
|
+
# Check pairs of consecutive elements
|
931
|
+
for i in range(len(args) - 1):
|
932
|
+
if is_valid_vector(args[i]) and is_valid_vector(args[i + 1]):
|
933
|
+
x_data, y_data = args[i], args[i + 1]
|
934
|
+
break
|
935
|
+
|
936
|
+
return x_data, y_data
|