Spaces:
Sleeping
Sleeping
File size: 5,051 Bytes
f52e861 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | """Flatten stored job and test-run metrics into one export row.
Every image is kept. A failed pass flag or a no-conflict image is a row,
not a reason to drop it.
"""
from __future__ import annotations
import csv
import io
import json
from typing import Any, Dict, Iterable, List, Optional
COLUMNS = [
"source",
"id",
"filename",
"created_at",
"cvd_type",
"severity",
"de_before",
"de_after",
"de_improvement",
"conflicts_found",
"conflicts_resolved",
"conflict_resolution_rate",
"new_conflicts",
"unresolved_conflicts",
"new_conflict_pairs",
"unresolved_pairs",
"naturalness",
"no_conflict",
"pass_de",
"pass_resolution",
"pass_naturalness",
]
def _metrics(value: Any) -> dict:
if isinstance(value, str):
try:
parsed = json.loads(value)
except (TypeError, ValueError):
return {}
return parsed if isinstance(parsed, dict) else {}
return dict(value) if isinstance(value, dict) else {}
def _num(value: Any, digits: int = 6) -> Optional[float]:
if value is None or value == "":
return None
try:
return round(float(value), digits)
except (TypeError, ValueError):
return None
def _int(value: Any, default: int = 0) -> int:
if value is None or value == "":
return default
try:
return int(value)
except (TypeError, ValueError):
return default
def _pairs(raw: Any) -> List[List[int]]:
out = []
for pair in raw or []:
if isinstance(pair, (list, tuple)) and len(pair) == 2:
out.append([_int(pair[0]), _int(pair[1])])
return out
def _pair_text(pairs: List[List[int]]) -> str:
return ";".join(f"{a}-{b}" for a, b in pairs)
def _flag(metrics: dict, metrics_key: str, row: dict, row_key: str) -> bool:
if metrics_key in metrics:
return bool(metrics.get(metrics_key))
if row_key in row and row.get(row_key) is not None:
return bool(row.get(row_key))
return False
def metric_row(source: str, row: dict) -> dict:
"""One spreadsheet row. Caller decides which records to pass; this never filters."""
metrics = _metrics(row.get("metrics"))
diag = metrics.get("conflict_diagnostics") or {}
if not isinstance(diag, dict):
diag = {}
found = metrics.get("conflicts_found")
if found is None:
found = metrics.get("n_conflicts_total")
found_n = _int(found, 0)
resolved = metrics.get("n_conflicts_resolved")
resolved_pairs = _pairs(diag.get("resolved_pairs"))
if resolved is None:
resolved_n = len(resolved_pairs)
else:
resolved_n = _int(resolved, 0)
new_pairs = _pairs(diag.get("new_pairs"))
unresolved_pairs = _pairs(diag.get("unresolved_pairs"))
no_conflict = bool(metrics.get("already_accessible")) or found_n == 0
record_id = row.get("job_id") or row.get("run_id") or row.get("id") or ""
return {
"source": source,
"id": str(record_id),
"filename": row.get("filename") or "",
"created_at": row.get("created_at") or "",
"cvd_type": row.get("cvd_type") or "",
"severity": _num(row.get("severity")),
"de_before": _num(metrics.get("de_before_mean")),
"de_after": _num(metrics.get("de_after_mean")),
"de_improvement": _num(
metrics.get("delta_e_improvement", metrics.get("de_improvement"))
),
"conflicts_found": found_n,
"conflicts_resolved": resolved_n,
"conflict_resolution_rate": _num(metrics.get("conflict_resolution_rate")),
"new_conflicts": len(new_pairs),
"unresolved_conflicts": len(unresolved_pairs),
"new_conflict_pairs": _pair_text(new_pairs),
"unresolved_pairs": _pair_text(unresolved_pairs),
"naturalness": _num(metrics.get("naturalness_preservation")),
"no_conflict": no_conflict,
"pass_de": _flag(metrics, "pass_de_improvement", row, "pass_de"),
"pass_resolution": _flag(metrics, "pass_resolution_rate", row, "pass_resolution"),
"pass_naturalness": _flag(metrics, "pass_naturalness", row, "pass_naturalness"),
}
def to_csv(rows: Iterable[dict]) -> str:
buf = io.StringIO()
writer = csv.DictWriter(buf, fieldnames=COLUMNS, lineterminator="\n", extrasaction="ignore")
writer.writeheader()
for row in rows:
flat = {}
for key in COLUMNS:
value = row.get(key, "")
if isinstance(value, bool):
flat[key] = "true" if value else "false"
elif value is None:
flat[key] = ""
elif isinstance(value, str) and value.lstrip().startswith(("=", "+", "-", "@")):
# Uploaded filenames must remain text when opened in a spreadsheet.
flat[key] = "'" + value
else:
flat[key] = value
writer.writerow(flat)
return buf.getvalue()
def to_json(rows: Iterable[dict]) -> str:
return json.dumps(list(rows), indent=2)
|