Upload 2 files
Browse files- quread/trends.py +28 -0
quread/trends.py
CHANGED
|
@@ -274,6 +274,34 @@ def compute_drift_alerts(
|
|
| 274 |
return alerts
|
| 275 |
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
def alerts_to_csv(alerts: List[Dict[str, Any]]) -> str:
|
| 278 |
out = io.StringIO()
|
| 279 |
writer = csv.writer(out)
|
|
|
|
| 274 |
return alerts
|
| 275 |
|
| 276 |
|
| 277 |
+
def compute_snapshot_delta(
|
| 278 |
+
series: np.ndarray,
|
| 279 |
+
metric_key: str,
|
| 280 |
+
*,
|
| 281 |
+
from_index: int,
|
| 282 |
+
to_index: int,
|
| 283 |
+
) -> Tuple[np.ndarray, np.ndarray, int, int]:
|
| 284 |
+
arr = np.asarray(series, dtype=float)
|
| 285 |
+
if arr.ndim != 2:
|
| 286 |
+
raise ValueError("series must be a 2D array of shape [snapshots, qubits].")
|
| 287 |
+
points = int(arr.shape[0])
|
| 288 |
+
if points < 2:
|
| 289 |
+
raise ValueError("Need at least 2 snapshots to compute delta.")
|
| 290 |
+
|
| 291 |
+
i = int(np.clip(int(from_index), 0, points - 1))
|
| 292 |
+
j = int(np.clip(int(to_index), 0, points - 1))
|
| 293 |
+
if i == j:
|
| 294 |
+
if j == points - 1:
|
| 295 |
+
i = max(0, j - 1)
|
| 296 |
+
else:
|
| 297 |
+
j = min(points - 1, i + 1)
|
| 298 |
+
|
| 299 |
+
raw_delta = arr[j] - arr[i]
|
| 300 |
+
risk_series = _risk_transform(arr, _resolve_metric(metric_key))
|
| 301 |
+
risk_delta = risk_series[j] - risk_series[i]
|
| 302 |
+
return np.asarray(raw_delta, dtype=float), np.asarray(risk_delta, dtype=float), int(i), int(j)
|
| 303 |
+
|
| 304 |
+
|
| 305 |
def alerts_to_csv(alerts: List[Dict[str, Any]]) -> str:
|
| 306 |
out = io.StringIO()
|
| 307 |
writer = csv.writer(out)
|