Skip to content

Commit 1c87615

Browse files
Ignore warning about std_dev==0 in ufloat_params (backport #1556) (#1558)
As of Uncertainties 3.2.3, `ufloat()` warns when passed a standard deviation of 0 because that can lead to problems (see the discussion [here](lmfit/uncertainties#283). The recommendation is to use a plain float instead but we want `ufloat_params` to return all `UFloats` so that `.n` and `.s` always exist and the user doesn't need to handle type checking on curve data results, so here we just ignore the warning. A standard deviation of 0 is usually not a problem. Uncertainties propagates errors for all of its variables even when they have 0 standard deviation. If the propagation of one variable leads to a `nan` or `inf` value, the standard deviation will take on that value even though, since the variable's standard deviation was 0, the error propagation could have been skipped for that variable.<hr>This is an automatic backport of pull request #1556 done by [Mergify](https://mergify.com). Co-authored-by: Will Shanks <willshanks@us.ibm.com>
1 parent c7d41be commit 1c87615

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

qiskit_experiments/curve_analysis/curve_analysis.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,28 @@ def _run_curve_fit(
505505

506506
try:
507507
with np.errstate(all="ignore"):
508-
new = lmfit.minimize(
509-
fcn=lambda x: np.concatenate([p(x) for p in partial_weighted_residuals]),
510-
params=guess_params,
511-
method=self.options.fit_method,
512-
scale_covar=not valid_uncertainty,
513-
nan_policy="omit",
514-
**fit_option.fitter_opts,
515-
)
508+
with warnings.catch_warnings():
509+
# Temporary workaround to avoid lmfit generate an
510+
# Uncertainties warning about std_dev==0 when there are
511+
# fixed parameters in the fit.
512+
#
513+
# This warning filter can be removed after
514+
# https://github.com/lmfit/lmfit-py/pull/1000 is
515+
# released.
516+
warnings.filterwarnings(
517+
"ignore",
518+
message="Using UFloat objects with std_dev==0.*",
519+
)
520+
new = lmfit.minimize(
521+
fcn=lambda x: np.concatenate(
522+
[p(x) for p in partial_weighted_residuals]
523+
),
524+
params=guess_params,
525+
method=self.options.fit_method,
526+
scale_covar=not valid_uncertainty,
527+
nan_policy="omit",
528+
**fit_option.fitter_opts,
529+
)
516530
except Exception: # pylint: disable=broad-except
517531
continue
518532

qiskit_experiments/curve_analysis/curve_data.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""
1616
import dataclasses
1717
import itertools
18+
import warnings
1819
from typing import Any, Dict, Union, List, Tuple, Optional, Iterable
1920

2021
import numpy as np
@@ -132,7 +133,24 @@ def ufloat_params(self) -> Dict[str, uncertainties.UFloat]:
132133
uind = self.var_names.index(name)
133134
ufloat_params[name] = ufloat_fitvals[uind]
134135
except ValueError:
135-
ufloat_params[name] = uncertainties.ufloat(self.params[name], std_dev=0.0)
136+
with warnings.catch_warnings():
137+
# As of Uncertainties 3.2.3, ufloat() warns about std_dev=0
138+
# We want to return UFloats uniformly (not a mix of
139+
# UFloats and plain floats) so we need to ignore this
140+
# warning and trust the user not to use the results
141+
# with std_dev==0 in a way that causes problems.
142+
#
143+
# In Uncertainties 3.2.3, the module of the warning is
144+
# uncertainties.core. Once
145+
# https://github.com/lmfit/uncertainties/pull/305 is
146+
# released, the module will be curve_data.py in
147+
# qiskit_experiments and the uncertainties part can be
148+
# removed from the module expression.
149+
warnings.filterwarnings(
150+
"ignore",
151+
module=r"(qiskit_experiments|uncertainties)\.",
152+
)
153+
ufloat_params[name] = uncertainties.ufloat(self.params[name], std_dev=0.0)
136154

137155
setattr(self, "_ufloat_params", ufloat_params)
138156
return ufloat_params
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
fixes:
3+
- |
4+
Warnings from the `Uncertainties
5+
<https://uncertainties.readthedocs.io/en/latest/>`__ library related to a
6+
standard deviation of zero have been suppressed where a standard deviation
7+
of zero is expected and can not be avoided. One case involves the fixed fit
8+
parameters included in :class:`.CurveFitResult`. Since these are fixed
9+
parameters, they have no uncertainty. The other case involves the `lmfit
10+
<https://lmfit.github.io/lmfit-py/>`__ library's handling of fixed fit
11+
parameters and will be addressed by lmfit directly in the future. In some
12+
cases, Uncertainties will propagate the error in a variable and produce a
13+
non-finite result when the error should be zero since the variable zero
14+
error. For example, the operation ``y ** 0.5`` could lead to 0 with ``inf``
15+
standard deviation rather than 0 standard deviation when the nominal value
16+
and standard deviation of ``y`` are both 0.

0 commit comments

Comments
 (0)