Skip to content

Commit

Permalink
Suppress more numpy warnings from uncertainties (#1350)
Browse files Browse the repository at this point in the history
This PR is a follow up to
#1070. It
suppresses numpy warnings during uncertainties array creation in more
places in the code. Something changed recently in the numpy code so that
it generates warnings when it used to suppress them, in particular when
using `numpy.vectorize` which wraps user-level Python code inside of
numpy C code and seems to lose context about warning state. See
numpy/numpy#21416 for more information.
  • Loading branch information
wshanks authored Dec 27, 2023
1 parent 5bb1fb4 commit d7c17fb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
6 changes: 5 additions & 1 deletion qiskit_experiments/curve_analysis/curve_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,11 @@ def _run_data_processing(
)
processed_values = self.options.data_processor(to_process)
source["yval"] = unp.nominal_values(processed_values).flatten()
source["yerr"] = unp.std_devs(processed_values).flatten()
with np.errstate(invalid="ignore"):
# For averaged data, the processed std dev will be NaN.
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
source["yerr"] = unp.std_devs(processed_values).flatten()
source["category"] = category

table = ScatterTable(data=source)
Expand Down
11 changes: 7 additions & 4 deletions qiskit_experiments/curve_analysis/curve_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,13 @@ def ufloat_params(self) -> Dict[str, uncertainties.UFloat]:
)
else:
# Invalid covariance matrix. Std dev is set to nan, i.e. not computed.
ufloat_fitvals = uarray(
nominal_values=[self.params[name] for name in self.var_names],
std_devs=np.full(len(self.var_names), np.nan),
)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
ufloat_fitvals = uarray(
nominal_values=[self.params[name] for name in self.var_names],
std_devs=np.full(len(self.var_names), np.nan),
)
# Combine fixed params and fitting variables into a single dictionary
# Fixed parameter has zero std_dev
ufloat_params = {}
Expand Down
10 changes: 8 additions & 2 deletions qiskit_experiments/data_processing/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ def _process(self, data: np.ndarray) -> np.ndarray:

reduced_array = np.mean(data, axis=ax)
nominals = unp.nominal_values(reduced_array)
errors = unp.std_devs(reduced_array)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
errors = unp.std_devs(reduced_array)

if np.any(np.isnan(errors)):
# replace empty elements with SEM
Expand Down Expand Up @@ -781,7 +784,10 @@ def _process(self, data: np.ndarray) -> np.ndarray:
p_mean = alpha_posterior[0] / alpha_sum
p_var = p_mean * (1 - p_mean) / (alpha_sum + 1)

probabilities[idx] = ufloat(nominal_value=p_mean, std_dev=np.sqrt(p_var))
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
probabilities[idx] = ufloat(nominal_value=p_mean, std_dev=np.sqrt(p_var))

return probabilities

Expand Down
11 changes: 7 additions & 4 deletions test/data_processing/test_data_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,13 @@ def test_json_trained(self):
unp.nominal_values(loaded_out),
)

np.testing.assert_array_almost_equal(
unp.std_devs(ref_out),
unp.std_devs(loaded_out),
)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
np.testing.assert_array_almost_equal(
unp.std_devs(ref_out),
unp.std_devs(loaded_out),
)


class TestIQSingleAvg(BaseDataProcessorTest):
Expand Down
46 changes: 29 additions & 17 deletions test/data_processing/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ class TestAveraging(BaseDataProcessorTest):

def test_simple(self):
"""Simple test of averaging. Standard error of mean is generated."""
datum = unp.uarray([[1, 2], [3, 4], [5, 6]], np.full((3, 2), np.nan))
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
datum = unp.uarray([[1, 2], [3, 4], [5, 6]], np.full((3, 2), np.nan))

node = AverageData(axis=1)
processed_data = node(data=datum)
Expand Down Expand Up @@ -85,16 +88,19 @@ def test_with_error(self):

def test_with_error_partly_non_error(self):
"""Compute error propagation. Some elements have no error."""
datum = unp.uarray(
[
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
],
[
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
[np.nan, 0.2, 0.3, 0.4, 0.5, 0.6],
],
)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
datum = unp.uarray(
[
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
],
[
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
[np.nan, 0.2, 0.3, 0.4, 0.5, 0.6],
],
)

node = AverageData(axis=1)
processed_data = node(data=datum)
Expand Down Expand Up @@ -130,7 +136,10 @@ def test_iq_averaging(self):
)
iq_std = np.full_like(iq_data, np.nan)

self.create_experiment_data(unp.uarray(iq_data, iq_std), single_shot=True)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
self.create_experiment_data(unp.uarray(iq_data, iq_std), single_shot=True)

avg_iq = AverageData(axis=0)
processed_data = avg_iq(data=np.asarray(self.iq_experiment.data(0)["memory"]))
Expand Down Expand Up @@ -188,11 +197,14 @@ def test_simple(self):
decimal=-8,
)

np.testing.assert_array_almost_equal(
unp.std_devs(processed),
unp.std_devs(expected),
decimal=-8,
)
with np.errstate(invalid="ignore"):
# Setting std_devs to NaN will trigger floating point exceptions
# which we can ignore. See https://stackoverflow.com/q/75656026
np.testing.assert_array_almost_equal(
unp.std_devs(processed),
unp.std_devs(expected),
decimal=-8,
)


class TestNormalize(QiskitExperimentsTestCase):
Expand Down

0 comments on commit d7c17fb

Please sign in to comment.