From 1f10cc2802d3828d2e48c9f8c6c6aa29c6c0f177 Mon Sep 17 00:00:00 2001 From: Naoki Kanazawa Date: Sun, 19 Nov 2023 08:48:51 +0900 Subject: [PATCH] fix handling of nan value in averaging --- qiskit_experiments/curve_analysis/curve_analysis.py | 2 +- qiskit_experiments/curve_analysis/utils.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/qiskit_experiments/curve_analysis/curve_analysis.py b/qiskit_experiments/curve_analysis/curve_analysis.py index 2634a8c984..27fd53eaa1 100644 --- a/qiskit_experiments/curve_analysis/curve_analysis.py +++ b/qiskit_experiments/curve_analysis/curve_analysis.py @@ -272,7 +272,7 @@ def _format_data( average = averaging_methods[self.options.average_method] model_names = self.model_names() formatted = [] - for (class_id, xv), g in groupby(sorted(curve_data.values, key=sort_by), key=sort_by): + for (_, xv), g in groupby(sorted(curve_data.values, key=sort_by), key=sort_by): g_values = np.array(list(g)) g_dict = dict(zip(columns, g_values.T)) avg_yval, avg_yerr, shots = average(g_dict["yval"], g_dict["yerr"], g_dict["shots"]) diff --git a/qiskit_experiments/curve_analysis/utils.py b/qiskit_experiments/curve_analysis/utils.py index 66a8855254..3cd8496849 100644 --- a/qiskit_experiments/curve_analysis/utils.py +++ b/qiskit_experiments/curve_analysis/utils.py @@ -18,6 +18,7 @@ import asteval import lmfit import numpy as np +import pandas as pd from qiskit.utils.deprecation import deprecate_func from qiskit.utils import detach_prefix from uncertainties import UFloat, wrap as wrap_function @@ -243,9 +244,9 @@ def shot_weighted_average( if len(yvals) == 1: return yvals[0], yerrs[0], shots[0] - if np.any(shots < -1): + if any(s is pd.NA for s in shots): # Shot number is unknown - return np.mean(yvals), np.nan, -1 + return np.mean(yvals), np.nan, pd.NA total_shots = np.sum(shots) weights = shots / total_shots @@ -276,7 +277,7 @@ def inverse_weighted_variance( if len(yvals) == 1: return yvals[0], yerrs[0], shots[0] - total_shots = np.sum(shots) if all(shots > 0) else -1 + total_shots = np.sum(shots) weights = 1 / yerrs**2 yvar = 1 / np.sum(weights) @@ -307,7 +308,7 @@ def sample_average( if len(yvals) == 1: return yvals[0], 0.0, shots[0] - total_shots = np.sum(shots) if all(shots > 0) else -1 + total_shots = np.sum(shots) avg_yval = np.mean(yvals) avg_yerr = np.sqrt(np.mean((avg_yval - yvals) ** 2) / len(yvals))