Spaces:
Runtime error
Runtime error
Deploy b63376f: Catch silent failures: bar grouping by gap distribution, misread category names, off-grid and lost-decimal tick labels
Browse files- EVAL_LOG.md +27 -0
- README.md +4 -4
- plotscribe/calibrate.py +23 -0
- plotscribe/extract_bar.py +10 -0
- plotscribe/pipeline.py +27 -3
- tests/test_calibrate.py +23 -0
EVAL_LOG.md
CHANGED
|
@@ -225,3 +225,30 @@ Failures found and fixed:
|
|
| 225 |
| clean lines 100 (oracle): median / p90 chart median | 0.19% / 0.39% | 0.13% / 0.25% |
|
| 226 |
| perturbed 200: silent / status ok / recall | 6.5% / 55% / 79% | 6.5% / 57.5% / 80% |
|
| 227 |
| PlotQA test RNSS / held-out RNSS | 99.5 / 97.3 | 99.5 / 97.1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
| clean lines 100 (oracle): median / p90 chart median | 0.19% / 0.39% | 0.13% / 0.25% |
|
| 226 |
| perturbed 200: silent / status ok / recall | 6.5% / 55% / 79% | 6.5% / 57.5% / 80% |
|
| 227 |
| PlotQA test RNSS / held-out RNSS | 99.5 / 97.3 | 99.5 / 97.1 |
|
| 228 |
+
|
| 229 |
+
## Stage 10 — silent failures on perturbed charts
|
| 230 |
+
|
| 231 |
+
Of 13 perturbed charts reported `ok` but wrong (6.5%), 7 were bar charts with
|
| 232 |
+
misread categories.
|
| 233 |
+
|
| 234 |
+
Fixes:
|
| 235 |
+
1. Grouped bars separated by edge strokes / JPEG erosion became one category per
|
| 236 |
+
bar (and labels were split across them). The within-group gap tolerance is now
|
| 237 |
+
taken from the gap distribution (largest jump between small and large gaps).
|
| 238 |
+
2. Category names that look misread (stray symbols, unbalanced brackets, fused
|
| 239 |
+
words, 1-2 letter fragments at the edges) lower the status when >= 1/3 of them.
|
| 240 |
+
3. Linear tick values must be multiples of their spacing (a digit misread on every
|
| 241 |
+
label, "0" -> "6", still fits a line).
|
| 242 |
+
4. Rejected labels exactly 10^k smaller than the fit (decimal points lost on the
|
| 243 |
+
inliers) lower the confidence.
|
| 244 |
+
5. Status also drops when the x calibration is very weak or most legend colours are
|
| 245 |
+
missing from the plot.
|
| 246 |
+
|
| 247 |
+
| 200 charts | before | after |
|
| 248 |
+
|---|---|---|
|
| 249 |
+
| perturbed: silent failures | 6.5% | 0.5% |
|
| 250 |
+
| perturbed: point recall | 80% | 84% |
|
| 251 |
+
| perturbed: status ok | 57.5% | 48.5% |
|
| 252 |
+
| clean: silent failures / median error | 0% / 0.11% | 0% / 0.11% |
|
| 253 |
+
| clean: status ok | 100% | 97% (6 line charts with misread rotated category names, correctly flagged) |
|
| 254 |
+
| PlotQA test / held-out RNSS | 99.5 / 97.1 | 99.5 / 97.1 |
|
README.md
CHANGED
|
@@ -164,12 +164,12 @@ whose data lies outside the plotted y range (cut off by the frame).
|
|
| 164 |
|---|---|---|
|
| 165 |
| chart type correct | 100% | 91% |
|
| 166 |
| series count correct | 99% | 88% |
|
| 167 |
-
| point recall | 98.7% |
|
| 168 |
| median of per-chart median error | **0.11%** | 0.26% |
|
| 169 |
| p90 of per-chart median error | 0.24% | 9.2% |
|
| 170 |
-
| status `ok` |
|
| 171 |
| abstained (`uncalibrated`/`failed`) | 0% | 21% |
|
| 172 |
-
| **silent failures** (status `ok` but median error > 2% or recall < 50%) | **0%** |
|
| 173 |
|
| 174 |
The CI target (median error < 1% on clean charts) is met for every chart type.
|
| 175 |
On clean charts the worst single bar value is 0.9% off (150 bar charts); for lines
|
|
@@ -177,7 +177,7 @@ median error is 0.13% (100 clean line charts).
|
|
| 177 |
|
| 178 |
## Known limitations
|
| 179 |
|
| 180 |
-
- **Perturbed styles**: 21% of perturbed charts abstain and
|
| 181 |
wrong. Weak spots: fivethirtyeight (no spines, panel = figure colour),
|
| 182 |
frameless legends drawn over data, bmh/classic low-resolution JPEGs, and
|
| 183 |
heavily overlapping category labels.
|
|
|
|
| 164 |
|---|---|---|
|
| 165 |
| chart type correct | 100% | 91% |
|
| 166 |
| series count correct | 99% | 88% |
|
| 167 |
+
| point recall | 98.7% | 84% |
|
| 168 |
| median of per-chart median error | **0.11%** | 0.26% |
|
| 169 |
| p90 of per-chart median error | 0.24% | 9.2% |
|
| 170 |
+
| status `ok` | 97% | 48.5% |
|
| 171 |
| abstained (`uncalibrated`/`failed`) | 0% | 21% |
|
| 172 |
+
| **silent failures** (status `ok` but median error > 2% or recall < 50%) | **0%** | 0.5% |
|
| 173 |
|
| 174 |
The CI target (median error < 1% on clean charts) is met for every chart type.
|
| 175 |
On clean charts the worst single bar value is 0.9% off (150 bar charts); for lines
|
|
|
|
| 177 |
|
| 178 |
## Known limitations
|
| 179 |
|
| 180 |
+
- **Perturbed styles**: 21% of perturbed charts abstain and 0.5% are silently
|
| 181 |
wrong. Weak spots: fivethirtyeight (no spines, panel = figure colour),
|
| 182 |
frameless legends drawn over data, bmh/classic low-resolution JPEGs, and
|
| 183 |
heavily overlapping category labels.
|
plotscribe/calibrate.py
CHANGED
|
@@ -221,6 +221,29 @@ def fit_numeric(axis: str, px: list[float], values: list[float] | list[list[floa
|
|
| 221 |
if kind == "log" and len(fits) > 1 and int(fits[1][1].sum()) == cal.n_inliers and rms(fits[1]) < 2 * tol:
|
| 222 |
conf *= 0.6
|
| 223 |
cal.warnings.append(f"{axis}: log vs linear ambiguous")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
cal.confidence = conf
|
| 225 |
return cal
|
| 226 |
|
|
|
|
| 221 |
if kind == "log" and len(fits) > 1 and int(fits[1][1].sum()) == cal.n_inliers and rms(fits[1]) < 2 * tol:
|
| 222 |
conf *= 0.6
|
| 223 |
cal.warnings.append(f"{axis}: log vs linear ambiguous")
|
| 224 |
+
if kind == "linear" and cal.n_inliers >= 3:
|
| 225 |
+
# Tick labels are multiples of their spacing (0, 20, 40 - not 26, 46, 66: a
|
| 226 |
+
# digit misread the same way on every label still fits a perfect line).
|
| 227 |
+
v = np.sort(t[inl])
|
| 228 |
+
step = float(np.min(np.diff(v)))
|
| 229 |
+
frac = np.abs(v / step - np.round(v / step)) if step > 0 else np.zeros(0)
|
| 230 |
+
if np.any((frac > 0.05) & (np.abs(frac - 0.5) > 0.05)):
|
| 231 |
+
conf *= 0.5
|
| 232 |
+
cal.warnings.append(f"{axis}: tick values are not multiples of their spacing (digits misread?)")
|
| 233 |
+
# Rejected labels at exactly 10^k times the fitted value: decimal points lost
|
| 234 |
+
# on the inliers ("0.4" read as "4"; the fit is then 10^k too large).
|
| 235 |
+
hits: dict[int, int] = {}
|
| 236 |
+
for k in np.flatnonzero(~inl):
|
| 237 |
+
pred = (p[k] - c) / d
|
| 238 |
+
for val in cl[k]:
|
| 239 |
+
if val != 0 and pred / val > 0:
|
| 240 |
+
e = np.log10(pred / val)
|
| 241 |
+
if abs(e - round(e)) < 0.015 and round(e) > 0:
|
| 242 |
+
hits[round(e)] = hits.get(round(e), 0) + 1
|
| 243 |
+
break
|
| 244 |
+
if hits and max(hits.values()) >= 2:
|
| 245 |
+
conf *= 0.4
|
| 246 |
+
cal.warnings.append(f"{axis}: rejected labels are a power of 10 off the fit (decimal point lost?)")
|
| 247 |
cal.confidence = conf
|
| 248 |
return cal
|
| 249 |
|
plotscribe/extract_bar.py
CHANGED
|
@@ -185,6 +185,16 @@ def category_clusters(bars: list[Bar]) -> list[float]:
|
|
| 185 |
return []
|
| 186 |
bs = sorted(bars, key=lambda b: b.x0)
|
| 187 |
tol = 0.15 * float(np.median([b.width for b in bs]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
groups: list[list[Bar]] = [[bs[0]]]
|
| 189 |
right = bs[0].x1
|
| 190 |
for b in bs[1:]:
|
|
|
|
| 185 |
return []
|
| 186 |
bs = sorted(bars, key=lambda b: b.x0)
|
| 187 |
tol = 0.15 * float(np.median([b.width for b in bs]))
|
| 188 |
+
# Bars of one group may be separated by edge strokes or JPEG erosion: when the
|
| 189 |
+
# gaps fall into two clearly separated sizes, the small ones are within groups.
|
| 190 |
+
right = np.maximum.accumulate([b.x1 for b in bs])
|
| 191 |
+
gaps = np.sort(np.maximum([b.x0 - r for b, r in zip(bs[1:], right[:-1])], 0.0))
|
| 192 |
+
if gaps.size >= 2:
|
| 193 |
+
upper_med = np.array([np.median(gaps[k + 1:]) for k in range(gaps.size - 1)])
|
| 194 |
+
ok = ((gaps[1:] + 1) >= 1.5 * (gaps[:-1] + 1)) & (gaps[:-1] < 0.5 * upper_med)
|
| 195 |
+
if ok.any():
|
| 196 |
+
i = int(np.argmax(np.where(ok, np.diff(gaps), -1.0)))
|
| 197 |
+
tol = max(tol, (gaps[i] + gaps[i + 1]) / 2)
|
| 198 |
groups: list[list[Bar]] = [[bs[0]]]
|
| 199 |
right = bs[0].x1
|
| 200 |
for b in bs[1:]:
|
plotscribe/pipeline.py
CHANGED
|
@@ -485,6 +485,7 @@ def separate_series(res: PipelineResult, ink: np.ndarray, leg: legend_mod.Legend
|
|
| 485 |
else:
|
| 486 |
colours.append(np.asarray(e.lab, np.float32))
|
| 487 |
res.warn("series", f"legend entry '{e.label}' has no matching colour in the plot")
|
|
|
|
| 488 |
names.append(e.label or f"series_{k + 1}")
|
| 489 |
from_legend.append(True)
|
| 490 |
for j, c in enumerate(clusters):
|
|
@@ -845,6 +846,23 @@ def check_clipping(res: PipelineResult, series: list[Series]) -> None:
|
|
| 845 |
res.extra["status"] = "low_confidence"
|
| 846 |
|
| 847 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 848 |
def status_from_calibration(res: PipelineResult) -> None:
|
| 849 |
"""Mark the result uncalibrated / low-confidence instead of emitting wrong numbers."""
|
| 850 |
status = "ok"
|
|
@@ -858,11 +876,17 @@ def status_from_calibration(res: PipelineResult) -> None:
|
|
| 858 |
status = "low_confidence"
|
| 859 |
res.warn("axes", "plot area uncertain; values may be wrong")
|
| 860 |
x = res.x_cal
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
if x is not None and x.kind == "categorical" and x.categories:
|
| 862 |
-
junk = sum(
|
| 863 |
-
if junk >=
|
| 864 |
status = "low_confidence" if status == "ok" else status
|
| 865 |
-
res.warn("calibrate", "x:
|
| 866 |
if res.chart_type in ("line", "scatter") and (res.x_cal is None or (res.x_cal.kind != "categorical"
|
| 867 |
and res.x_cal.n_inliers < 2)):
|
| 868 |
status = "uncalibrated"
|
|
|
|
| 485 |
else:
|
| 486 |
colours.append(np.asarray(e.lab, np.float32))
|
| 487 |
res.warn("series", f"legend entry '{e.label}' has no matching colour in the plot")
|
| 488 |
+
res.extra["legend_unmatched"] = res.extra.get("legend_unmatched", 0) + 1
|
| 489 |
names.append(e.label or f"series_{k + 1}")
|
| 490 |
from_legend.append(True)
|
| 491 |
for j, c in enumerate(clusters):
|
|
|
|
| 846 |
res.extra["status"] = "low_confidence"
|
| 847 |
|
| 848 |
|
| 849 |
+
_LABEL_PUNCT = set(" .,-%/&'+:()$#")
|
| 850 |
+
|
| 851 |
+
|
| 852 |
+
def misread_name(c: str) -> bool:
|
| 853 |
+
"""Category name that looks like an OCR misread: placeholder, stray symbols,
|
| 854 |
+
unbalanced brackets, two words fused ("iwDat"), or 1-2 letter fragments bled in
|
| 855 |
+
from a neighbouring label ("a Fig", "Mango kK")."""
|
| 856 |
+
if c.startswith("category_") or not any(ch.isalnum() for ch in c):
|
| 857 |
+
return True
|
| 858 |
+
if any(not (ch.isalnum() or ch in _LABEL_PUNCT) for ch in c) or c.count("(") != c.count(")"):
|
| 859 |
+
return True
|
| 860 |
+
toks = c.split()
|
| 861 |
+
if any(a.islower() and b.isupper() for t in toks for a, b in zip(t, t[1:])):
|
| 862 |
+
return True
|
| 863 |
+
return len(toks) > 1 and any(len(t) <= 2 and t.isalpha() and not t.isupper() for t in (toks[0], toks[-1]))
|
| 864 |
+
|
| 865 |
+
|
| 866 |
def status_from_calibration(res: PipelineResult) -> None:
|
| 867 |
"""Mark the result uncalibrated / low-confidence instead of emitting wrong numbers."""
|
| 868 |
status = "ok"
|
|
|
|
| 876 |
status = "low_confidence"
|
| 877 |
res.warn("axes", "plot area uncertain; values may be wrong")
|
| 878 |
x = res.x_cal
|
| 879 |
+
if status == "ok" and x is not None and x.kind != "categorical" and x.n_inliers >= 2 and x.confidence < 0.3:
|
| 880 |
+
status = "low_confidence"
|
| 881 |
+
n_leg = len(res.extra.get("legend_info", {}).get("entries", []))
|
| 882 |
+
if status == "ok" and n_leg and res.extra.get("legend_unmatched", 0) >= max(2, n_leg / 2):
|
| 883 |
+
status = "low_confidence"
|
| 884 |
+
res.warn("series", "most legend colours were not found in the plot; series may be mixed up")
|
| 885 |
if x is not None and x.kind == "categorical" and x.categories:
|
| 886 |
+
junk = sum(misread_name(c) for c in x.categories)
|
| 887 |
+
if junk >= max(2.0, len(x.categories) / 3):
|
| 888 |
status = "low_confidence" if status == "ok" else status
|
| 889 |
+
res.warn("calibrate", f"x: {junk} of {len(x.categories)} category labels look misread")
|
| 890 |
if res.chart_type in ("line", "scatter") and (res.x_cal is None or (res.x_cal.kind != "categorical"
|
| 891 |
and res.x_cal.n_inliers < 2)):
|
| 892 |
status = "uncalibrated"
|
tests/test_calibrate.py
CHANGED
|
@@ -45,3 +45,26 @@ def test_rescale_roundtrip():
|
|
| 45 |
r = cal.rescaled(2.0)
|
| 46 |
assert float(r.to_value(100)) == pytest.approx(float(cal.to_value(200)))
|
| 47 |
assert np.isclose(float(cal.to_px(cal.to_value(123.0))), 123.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
r = cal.rescaled(2.0)
|
| 46 |
assert float(r.to_value(100)) == pytest.approx(float(cal.to_value(200)))
|
| 47 |
assert np.isclose(float(cal.to_px(cal.to_value(123.0))), 123.0)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_misread_digit_on_every_label_is_flagged():
|
| 51 |
+
# "0" read as "6": 26, 46, 66 fits a line but is off the tick grid.
|
| 52 |
+
cal = fit_numeric("y", [500, 400, 300, 200], [26, 46, 66, 86])
|
| 53 |
+
assert cal.confidence < 0.6
|
| 54 |
+
clean = fit_numeric("y", [500, 400, 300, 200], [20, 40, 60, 80])
|
| 55 |
+
assert clean.confidence == 1.0
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_lost_decimal_points_are_flagged():
|
| 59 |
+
# "0.1".."0.4" read as 1..4; the correctly read 0.6, 0.7 are 10x off the fit.
|
| 60 |
+
px = [400, 300, 200, 100, -100, -200]
|
| 61 |
+
cal = fit_numeric("y", px, [1, 2, 3, 4, 0.6, 0.7])
|
| 62 |
+
assert cal.n_inliers == 4 and cal.confidence < 0.3
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def test_misread_category_names():
|
| 66 |
+
from plotscribe.pipeline import misread_name
|
| 67 |
+
for ok in ["Banana", "Q1 2020", "Isle of Man", "Korea, Rep.", "R&D", "A"]:
|
| 68 |
+
assert not misread_name(ok), ok
|
| 69 |
+
for bad in ["Plum |!", "a Fig", "Mango kK", "iwDat", "Peach)", "category_3"]:
|
| 70 |
+
assert misread_name(bad), bad
|