Skip to content

Commit f184e36

Browse files
committed
Suppress fit warning
1 parent bddec78 commit f184e36

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/ess/reflectometry/normalize.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3+
import warnings
4+
35
import scipp as sc
6+
from scipy.optimize import OptimizeWarning
47

58
from .types import (
69
FootprintCorrectedData,
@@ -51,19 +54,30 @@ def normalization_factor(
5154
def q_of_z_wavelength(wavelength, a, b):
5255
return a + b / wavelength
5356

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+
)
6781
return sc.DataArray(
6882
data=corr.data,
6983
coords={

0 commit comments

Comments
 (0)