Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/jmrplens/phonometry. Standards-conformant acoustics library for Python: measurement and metrology, docs.jmrp.io/phonometry
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443#!/usr/bin/env python3r"""Fail on an annotation the reader cannot read off the page.
Two defects, and they are not the same defect.
**A curve drawn behind the letters.** The strokes of the letters and thestroke of the curve are the same weight, and on the dark page close to thesame lightness. The answer the corpus uses, at well over a hundred call sites under``scripts/figures``, is an opaque chip::
bbox={"boxstyle": "round,pad=0.5", "facecolor": COLOR_PANEL, "edgecolor": COLOR_GRID}
which is *not* the translucent one a text box in an empty corner takes(``{"boxstyle": "round", "facecolor": COLOR_GRID, "alpha": 0.6}``): over a linethat one loses its contrast on the dark page. ``pad`` follows the neighbours: 0.5for most, 0.4 where the box is tight against something, 0.3 for a small labelpinned near a point. The chip is given a ``zorder`` above the curves.
(Neither of those is given as a figure on purpose. Both were, twice, and bothwere wrong both times, because the count moves with every label edited anddepends on whether a bare ``facecolor=COLOR_PANEL`` keyword counts beside thedict literals. A number nobody can recompute the same way twice is worse thanthe rule it was standing in for.)
A chip is not free, and where it can be avoided it should be. It is opaque, sowhatever it lands on is gone: a curve or a filled band passing behind it readsas passing behind it, but a plotted point, a marker or the end of a range barunder it is a datum the reader no longer has. Move the label first, and reachfor the chip when there is nowhere for it to go.
**Something drawn over the letters.** Here a chip is no answer at all, becausethe chip is being painted over with them: the label needs a ``zorder`` abovewhatever covers it. A chip *and* a zorder is the whole convention, and a chipwith no zorder is the case the first rule passes and the reader fails.
Where the number comes from---------------------------
:mod:`figure_annotation_audit` measures both during the generation run, byrendering the figure with pieces of it taken away and counting the pixels thatmove. This script reads that recording and applies the rule; the reason it ispixels rather than geometry, and the reason the measurement has to happenwhile the artists are alive, are both written down there.
The two measures answer different questions and were calibrated separately.Each threshold is where the classes actually part, on labels classified by eyeat 3x and 8x dpi in both themes, and neither number is borrowed from the other.
**Behind**, against 94 labels (57 struck, 37 clean), and the boundary is onepixel wide:
* at **45 px** and above, every one of the 50 labels that fire is a real defect -- precision 1.00, recall 0.88. The largest label judged clean scores 44, the smallest judged struck above it scores 45, and the reported example (``g_weighting_response``, "0 dB @ 10 Hz") scores 49, which is what bounds the threshold from above.* between **32 and 44 px** the two classes interleave completely, so those print as an advisory and do not fail. They are real contacts, but which of them a reader minds is a coin toss, and a gate that tells someone to change something that looks fine is a gate nobody believes.
Zero false alarms in 37 clean examples bounds the false-alarm rate near 9 % with95 % confidence, not at zero. The costs are lopsided, which is why it gatesanyway: a false positive adds a chip that was not strictly needed -- invisible,and nobody argues about it -- while a false negative ships a label a readercannot read.
A one-pixel boundary invites the question of whether the count drifts, sincethe corpus is known not to be bit-identical across machines and``check_figures.py`` compares it by tolerance for that reason. Measured, itdoes not: displacing every plotted vertex of four figures by one ULP up, onedown, and randomly per coordinate moved none of thirteen counts, the twositting at 44 px included. Floating-point drift changes where a curve is by afraction of a pixel; a struck-pixel count changes when it moves by one.
**Covered** gates lower, at **20 px**, and has no advisory band, because itseparates where the other one interleaves. When the threshold was chosen, thelabels losing any letter ink at all fell into two groups with nothing betweenthem: every one at or above 21 px was real, and the largest clean one scored18. Both ends were looked at twice. ``detailed_impact_paths`` at 18 px is agrey dotted rule clipping the "t" of "paths", and it reads; the label that satat 21 px, ``two_tone_separation_es``, printed a black note straight over thered "Hz" of "212 Hz" and destroyed it. That one has since been moved by thisbranch and now records clean, which is why the group counts are not quotedhere: they described the corpus as it was before the gate was applied to it.Twenty is the round number inside the gap, with twopixels of margin over the largest clean example -- wider than behind's one.
It gates lower because it is a different measurement, not a stricter mood:covered pixels are ink the reader never receives, so they destroy a characteroutright, while a struck pixel is a character still fully drawn and merelycompeting with a curve. It also counts only letters, which the audit explains:counting the chip as well puts a clean label at 384 px and no thresholdsurvives that.
:data:`REQUIRED_DPI` is part of the rule, not a detail: the count scales withthe square of the resolution, so a recording taken at any other dpi is notcomparable and is refused rather than rescaled.
Running it----------
The measurement comes from a generation run, so the check needs one::
make graphs # records into build/figure-annotations make figure-annotations # reads it
``make graphs`` empties the directory first. Reading a directory some other runfilled would be answering about the wrong tree, so the check requires therecording to cover every committed figure *in both languages* -- Spanish proseis longer than English and grows into curves the English strings clear, so thetwo drawings are measured separately and recorded under separate keys(``foo`` and ``foo_es``). ``--partial`` is for a targeted re-render of onefigure: it checks what that run produced and reports no exemption as stale,because a run that did not draw a figure cannot say anything about it."""
from __future__ import annotations
import argparseimport jsonimport pathlibimport sysfrom typing import TYPE_CHECKING, Any
if TYPE_CHECKING: from collections.abc import Iterator
_SCRIPTS = pathlib.Path(__file__).resolve().parentif str(_SCRIPTS) not in sys.path: sys.path.insert(0, str(_SCRIPTS))
import figure_annotation_audit as audit
#: Struck pixels at :data:`REQUIRED_DPI` that fail the build, per defect. The#: two measures count different things -- a curve painted under a character#: that is nonetheless fully drawn, against ink the reader never receives at#: all -- so each carries the threshold its own classes part at.GATE_PX = {audit.BEHIND: 45, audit.COVERED: 20}
#: Where the advisory band under a gate begins. Only the behind measure has#: one: between 32 and 44 px an unreadable label and a clean one interleave#: completely, so those are reported for a person to judge and do not fail.#: The covered measure has no such band -- its classes separate, 18 px clean#: against 21 px unreadable -- so under its gate there is nothing to say.ADVISORY_PX = {audit.BEHIND: 32}
#: The resolution the thresholds are pinned to (``figure.dpi`` in the figure#: theme). The count scales with its square, so a recording taken anywhere else#: means a different rule.REQUIRED_DPI = audit.REFERENCE_DPI
#: Figures allowed to keep an annotation the rules fire on, each with the#: reason. Checked in both directions: an entry whose label no longer fires is#: deleted, so the file cannot silently cover a figure that changed.EXEMPTIONS = _SCRIPTS / "figure_annotation_exemptions.txt"
#: The committed figures, which say what a full generation run produces.IMAGES = _SCRIPTS.parent / ".github" / "images"
#: What to do about each kind, printed under the labels that fire it. The two#: defects need opposite fixes, which is why they are counted apart: a chip#: answers a curve behind the letters and does nothing at all for a label#: something is painted over.ADVICE = { audit.BEHIND: ( 'give each one bbox={"boxstyle": "round,pad=0.5", "facecolor": ' 'COLOR_PANEL, "edgecolor": COLOR_GRID} with a zorder above the curves ' "(pad as its neighbours use)" ), audit.COVERED: ( "raise each one above what is drawn over it, with a zorder higher than " "that artist's (a chip does not help here: it is being painted over " "too)" ),}
#: How each kind reads in the failure report.HEADLINE = { audit.BEHIND: "struck by a curve and carry no chip", audit.COVERED: "painted over by something drawn after them",}
def committed_figures() -> set[str]: """The drawings a full run measures, read off the committed images.
Read off the images rather than off the registry, because a generator's name is not its output name: ``generate_filter_responses`` writes ten files, none of them called ``filter_responses``, and the recording is keyed by what is written. Both languages count and only the light pass does: a figure ships as four files and the two dark ones are the same drawing in other colours, so ``foo`` and ``foo_es`` are the two keys a full run owes. The hand-drawn plates are then subtracted -- they are written by ``scripts/generate_diagrams.py`` from an SVG canvas, never pass through the figure saver, and have no matplotlib artist to measure -- and so are the clips, which come from the separate ``make animations``. """ from diagrams.registry import DIAGRAMS
found = set() for path in IMAGES.iterdir(): if path.suffix not in (".svg", ".webp") or path.stem.endswith("_dark"): continue stem = path.stem.removesuffix("_es") if stem.startswith("anim_") or stem in DIAGRAMS: continue found.add(path.stem) return found
def read_exemptions(path: pathlib.Path) -> dict[tuple[str, str], str]: """Parse the committed exemptions into ``(key, label) -> reason``.""" entries: dict[tuple[str, str], str] = {} if not path.exists(): return entries for number, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1): if not line.strip() or line.lstrip().startswith("#"): continue entries.update([_parse_exemption(path, number, line)]) return entries
def _parse_exemption( path: pathlib.Path, number: int, line: str) -> tuple[tuple[str, str], str]: """One ``key: "label": reason`` line, or a :class:`SystemExit` saying so.""" stem, _, rest = line.partition(": ") label, reason = "", "" if rest.startswith('"'): label, end = json.JSONDecoder().raw_decode(rest) reason = rest[end:].removeprefix(":").strip() if not stem or not label or not reason: msg = f"{path}:{number}: expected 'stem: \"label\": reason'" raise SystemExit(msg) return (stem, label), reason
def kind_of(hit: dict[str, Any]) -> str: """Which defect *hit* is. An old recording predates the distinction.""" return str(hit.get("kind", audit.BEHIND))
def struck( recorded: dict[str, list[dict[str, Any]]], floors: dict[str, int]) -> Iterator[tuple[str, dict[str, Any]]]: """Every recorded label at or above its own kind's floor, worst first.
A kind *floors* says nothing about is silent, which is what gives the covered measure an empty advisory band without a special case. """ ranked = sorted( ((key, hit) for key, hits in recorded.items() for hit in hits), key=lambda found: -found[1]["pixels"], ) for key, hit in ranked: floor = floors.get(kind_of(hit)) if floor is not None and hit["pixels"] >= floor: yield key, hit
def describe(key: str, hit: dict[str, Any]) -> str: """One report line: the drawing, the label, the count and what does it.""" text = json.dumps(hit["text"], ensure_ascii=False) verb = "struck by" if kind_of(hit) == audit.BEHIND else "under" by = ", ".join(f"{name} ({pixels} px)" for name, pixels in hit["struck_by"]) return f" {key}: {text} - {hit['pixels']} px" + (f", {verb} {by}" if by else "")
def wrong_dpi(recorded: dict[str, list[dict[str, Any]]]) -> set[float]: """The resolutions in *recorded* that the thresholds do not describe.
Read off the measurements, so a figure that recorded none says nothing about its own resolution. That is the right silence: a figure with no ink on any letter has no count to compare with a threshold, and a contact real enough to matter leaves pixels at any resolution, which then carries the dpi that produced them. """ return { hit["dpi"] for hits in recorded.values() for hit in hits if hit["dpi"] != REQUIRED_DPI }
def report( recorded: dict[str, list[dict[str, Any]]], exemptions: dict[tuple[str, str], str], *, partial: bool,) -> int: """Fail on an un-exempt unreadable label, and on an exemption that stopped being true.""" failures = [ (key, hit) for key, hit in struck(recorded, GATE_PX) if (key, hit["text"]) not in exemptions ] problems = _failure_report(failures) if failures else [] if not partial: problems.extend(_stale_report(recorded, exemptions)) if problems: print("::error::an annotation on a figure cannot be read") print("\n".join(problems)) return 1 _print_advisory(recorded) print( f"No unreadable annotation at or above {_gates()} " f"({len(recorded)} drawing(s) measured, {len(exemptions)} exempt)." ) return 0
def _gates() -> str: """The thresholds as the summary line says them, one clause per defect.""" return " or ".join( f"{pixels} px {kind}" for kind, pixels in sorted(GATE_PX.items()) )
def _failure_report(failures: list[tuple[str, dict[str, Any]]]) -> list[str]: """What to print for the labels that fail the gate, one block per kind.""" lines: list[str] = [] for kind in (audit.BEHIND, audit.COVERED): of_kind = [(key, hit) for key, hit in failures if kind_of(hit) == kind] if not of_kind: continue figures = len({key for key, _ in of_kind}) lines += [ f"{len(of_kind)} annotation(s) across {figures} drawing(s) are " f"{HEADLINE[kind]}:", *(describe(key, hit) for key, hit in of_kind), f" -> {ADVICE[kind]}, or, if it must stay as it is, add it to " f"{EXEMPTIONS.name} with the reason.", ] return lines
def _stale_report( recorded: dict[str, list[dict[str, Any]]], exemptions: dict[tuple[str, str], str],) -> list[str]: """What to print for exemptions that no longer describe anything.
An exemption is a decision about a label that *is* unreadable. Once the label is fixed, renamed or gone, the line covers nothing and has to go, or the file rots into a list of figures nobody is allowed to regress on.
Only reached on a full run, which by then is known to cover every committed figure in both languages -- so a key missing from the recording is a drawing that is no longer made, and its exemption is as stale as a fixed one. """ firing = {(key, hit["text"]) for key, hit in struck(recorded, GATE_PX)} stale = [(key, label) for key, label in exemptions if (key, label) not in firing] if not stale: return [] return [ f"{len(stale)} exemption(s) are no longer true (fixed, moved or gone):", *(f" {key}: {json.dumps(label, ensure_ascii=False)}" for key, label in stale), f" -> delete them from {EXEMPTIONS.name}.", ]
def _print_advisory(recorded: dict[str, list[dict[str, Any]]]) -> None: """List the contacts below the gate, which are for a person to judge.""" band = [ (key, hit) for key, hit in struck(recorded, ADVISORY_PX) if hit["pixels"] < GATE_PX[kind_of(hit)] ] if not band: return spans = ", ".join( f"{floor} and {GATE_PX[kind] - 1} px {kind}" for kind, floor in sorted(ADVISORY_PX.items()) ) print( f"{len(band)} annotation(s) score between {spans}. Advisory: in that " "band an unreadable label and a clean one are indistinguishable by " "count, so this does not fail." ) for key, hit in band: print(describe(key, hit))
def main(argv: list[str] | None = None) -> int: parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) parser.add_argument( "--audit", default=audit.DEFAULT_DIR, metavar="DIR", help=f"where the generation run recorded (default: {audit.DEFAULT_DIR})", ) parser.add_argument( "--partial", action="store_true", help="the run generated only some of the figures: check what it drew, " "and neither require full coverage nor report an exemption as stale", ) args = parser.parse_args(argv)
directory = pathlib.Path(args.audit) recorded = audit.load(str(directory)) if directory.is_dir() else {} if not recorded: print( f"::error::no figure-annotation recording in {directory}. It is " "written by the generation run itself: run `make graphs` with " f"{audit.AUDIT_ENV} set, which is what the graphs target does, " "and check again." ) return 1
unexpected = wrong_dpi(recorded) if unexpected: print( f"::error::the recording was taken at {sorted(unexpected)} dpi and " f"the thresholds are pinned to {REQUIRED_DPI}. A struck-pixel count " "scales with the square of the resolution, so the two are not " "comparable; regenerate with the figure theme's rcParams." ) return 1
if not args.partial: missing = committed_figures() - set(recorded) if missing: print( f"::error::the recording in {directory} covers {len(recorded)} " f"drawing(s) and misses {len(missing)} that `make graphs` " f"produces (every figure in both languages, the `_es` twin " f"included), so it is not a full run: " f"{', '.join(sorted(missing)[:5])}..." ) print(" -> run `make graphs` (it empties the directory first).") return 1
return report(recorded, read_exemptions(EXEMPTIONS), partial=args.partial)
if __name__ == "__main__": sys.exit(main())