|
1 | 1 | # SPDX-License-Identifier: BSD-3-Clause |
2 | 2 | # Copyright (c) 2023 Scipp contributors (https://github.com/scipp) |
| 3 | +import warnings |
| 4 | + |
3 | 5 | import scipp as sc |
| 6 | +from scipy.optimize import OptimizeWarning |
4 | 7 |
|
5 | 8 | from .types import ( |
6 | 9 | FootprintCorrectedData, |
@@ -51,19 +54,30 @@ def normalization_factor( |
51 | 54 | def q_of_z_wavelength(wavelength, a, b): |
52 | 55 | return a + b / wavelength |
53 | 56 |
|
54 | | - p, _ = sc.curve_fit( |
55 | | - ["wavelength"], |
56 | | - q_of_z_wavelength, |
57 | | - sc.DataArray( |
58 | | - data=sample_q, |
59 | | - coords={"wavelength": wm}, |
60 | | - masks={ |
61 | | - **corr.masks, |
62 | | - "_sample_q_isnan": sc.isnan(sample_q), |
63 | | - }, |
64 | | - ), |
65 | | - p0={"a": sc.scalar(1, unit="1/angstrom")}, |
66 | | - ) |
| 57 | + with warnings.catch_warnings(): |
| 58 | + # `curve_fit` raises a warning if it fails to estimate variances. |
| 59 | + # We don't care here because we only need the parameter values and anyway |
| 60 | + # assume that the fit worked. |
| 61 | + # The warning can be caused by there being too few points to estimate |
| 62 | + # uncertainties because of masks. |
| 63 | + warnings.filterwarnings( |
| 64 | + "ignore", |
| 65 | + message="Covariance of the parameters could not be estimated", |
| 66 | + category=OptimizeWarning, |
| 67 | + ) |
| 68 | + p, _ = sc.curve_fit( |
| 69 | + ["wavelength"], |
| 70 | + q_of_z_wavelength, |
| 71 | + sc.DataArray( |
| 72 | + data=sample_q, |
| 73 | + coords={"wavelength": wm}, |
| 74 | + masks={ |
| 75 | + **corr.masks, |
| 76 | + "_sample_q_isnan": sc.isnan(sample_q), |
| 77 | + }, |
| 78 | + ), |
| 79 | + p0={"a": sc.scalar(-1e-3, unit="1/angstrom")}, |
| 80 | + ) |
67 | 81 | return sc.DataArray( |
68 | 82 | data=corr.data, |
69 | 83 | coords={ |
|
0 commit comments