Skip to content

Commit d7c17fb

Browse files
authored
Suppress more numpy warnings from uncertainties (#1350)
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.
1 parent 5bb1fb4 commit d7c17fb

File tree

5 files changed

+56
-28
lines changed

5 files changed

+56
-28
lines changed

qiskit_experiments/curve_analysis/curve_analysis.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,11 @@ def _run_data_processing(
231231
)
232232
processed_values = self.options.data_processor(to_process)
233233
source["yval"] = unp.nominal_values(processed_values).flatten()
234-
source["yerr"] = unp.std_devs(processed_values).flatten()
234+
with np.errstate(invalid="ignore"):
235+
# For averaged data, the processed std dev will be NaN.
236+
# Setting std_devs to NaN will trigger floating point exceptions
237+
# which we can ignore. See https://stackoverflow.com/q/75656026
238+
source["yerr"] = unp.std_devs(processed_values).flatten()
235239
source["category"] = category
236240

237241
table = ScatterTable(data=source)

qiskit_experiments/curve_analysis/curve_data.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,13 @@ def ufloat_params(self) -> Dict[str, uncertainties.UFloat]:
235235
)
236236
else:
237237
# Invalid covariance matrix. Std dev is set to nan, i.e. not computed.
238-
ufloat_fitvals = uarray(
239-
nominal_values=[self.params[name] for name in self.var_names],
240-
std_devs=np.full(len(self.var_names), np.nan),
241-
)
238+
with np.errstate(invalid="ignore"):
239+
# Setting std_devs to NaN will trigger floating point exceptions
240+
# which we can ignore. See https://stackoverflow.com/q/75656026
241+
ufloat_fitvals = uarray(
242+
nominal_values=[self.params[name] for name in self.var_names],
243+
std_devs=np.full(len(self.var_names), np.nan),
244+
)
242245
# Combine fixed params and fitting variables into a single dictionary
243246
# Fixed parameter has zero std_dev
244247
ufloat_params = {}

qiskit_experiments/data_processing/nodes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def _process(self, data: np.ndarray) -> np.ndarray:
8585

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

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

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

786792
return probabilities
787793

test/data_processing/test_data_processing.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,13 @@ def test_json_trained(self):
418418
unp.nominal_values(loaded_out),
419419
)
420420

421-
np.testing.assert_array_almost_equal(
422-
unp.std_devs(ref_out),
423-
unp.std_devs(loaded_out),
424-
)
421+
with np.errstate(invalid="ignore"):
422+
# Setting std_devs to NaN will trigger floating point exceptions
423+
# which we can ignore. See https://stackoverflow.com/q/75656026
424+
np.testing.assert_array_almost_equal(
425+
unp.std_devs(ref_out),
426+
unp.std_devs(loaded_out),
427+
)
425428

426429

427430
class TestIQSingleAvg(BaseDataProcessorTest):

test/data_processing/test_nodes.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ class TestAveraging(BaseDataProcessorTest):
4343

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

4851
node = AverageData(axis=1)
4952
processed_data = node(data=datum)
@@ -85,16 +88,19 @@ def test_with_error(self):
8588

8689
def test_with_error_partly_non_error(self):
8790
"""Compute error propagation. Some elements have no error."""
88-
datum = unp.uarray(
89-
[
90-
[1, 2, 3, 4, 5, 6],
91-
[1, 2, 3, 4, 5, 6],
92-
],
93-
[
94-
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
95-
[np.nan, 0.2, 0.3, 0.4, 0.5, 0.6],
96-
],
97-
)
91+
with np.errstate(invalid="ignore"):
92+
# Setting std_devs to NaN will trigger floating point exceptions
93+
# which we can ignore. See https://stackoverflow.com/q/75656026
94+
datum = unp.uarray(
95+
[
96+
[1, 2, 3, 4, 5, 6],
97+
[1, 2, 3, 4, 5, 6],
98+
],
99+
[
100+
[0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
101+
[np.nan, 0.2, 0.3, 0.4, 0.5, 0.6],
102+
],
103+
)
98104

99105
node = AverageData(axis=1)
100106
processed_data = node(data=datum)
@@ -130,7 +136,10 @@ def test_iq_averaging(self):
130136
)
131137
iq_std = np.full_like(iq_data, np.nan)
132138

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

135144
avg_iq = AverageData(axis=0)
136145
processed_data = avg_iq(data=np.asarray(self.iq_experiment.data(0)["memory"]))
@@ -188,11 +197,14 @@ def test_simple(self):
188197
decimal=-8,
189198
)
190199

191-
np.testing.assert_array_almost_equal(
192-
unp.std_devs(processed),
193-
unp.std_devs(expected),
194-
decimal=-8,
195-
)
200+
with np.errstate(invalid="ignore"):
201+
# Setting std_devs to NaN will trigger floating point exceptions
202+
# which we can ignore. See https://stackoverflow.com/q/75656026
203+
np.testing.assert_array_almost_equal(
204+
unp.std_devs(processed),
205+
unp.std_devs(expected),
206+
decimal=-8,
207+
)
196208

197209

198210
class TestNormalize(QiskitExperimentsTestCase):

0 commit comments

Comments
 (0)