Spaces:
Sleeping
Sleeping
Download server/rating.py from devmandan/qr-scan-difficulty-check: direct link, hf CLI and curl.
- Browser
- Download file 8.13 kB
-
https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/rating.py
- Command line
-
hf download hf://spaces/devmandan/qr-scan-difficulty-check/server/rating.py
-
curl -L -o rating.py https://huggingface.co/spaces/devmandan/qr-scan-difficulty-check/resolve/main/server/rating.py
8.13 kB
| """A picture's level on the scale, and how sure that level is. | |
| The level comes from d alone, with the cuts in web/data/levels.json, the file the page reads too: Easy below 30, | |
| Everyday from 30, Tough from 50 and Advanced from 60, a d on a cut going to the level above; the left pile is Easy and | |
| the right pile Advanced. d stays the Space's result; nothing here changes it. | |
| The confidence says how often, on the measured codes, the level from the five scanners' place (place.py, on each code's | |
| measured reads) is the level from all eight scanners, among codes with the same evidence: the left or the right pile; | |
| otherwise how many of the five read the picture at 1280 px (fewer than two, or more) and how far d is from the nearest | |
| cut (under 2 points, 2 to 4, or more), and with two or more reads also the picture's size class (big when its own long | |
| side is over 1280 px, so that its 1280-px frame is a real shrink) and, near a cut, the side of the cut d is on (below | |
| it, or on or above it). Size and side matter because the eight's d is offset from the five's: for big pictures it sits | |
| about 2 to 3 points higher near cuts 30 and 50, so just below them the eight's level is often the one above, while near | |
| 60 it is drawn toward the cut from both sides (higher just below it, lower just above it), so there the eight's level | |
| often differs on either side, and a big picture near the top cut counts as below it on either side (big_top_below). For | |
| small pictures the offset is smaller, and the riskier side is above the cut. levels.json holds each cell's codes (n), | |
| how many of them agree (m), the share (agree, to 3 decimals) and its grade: high from 0.9, medium from 0.7, otherwise | |
| low, on the unrounded share. A reading cut short by the time limit is always low: some of its reads are missing, as in | |
| none of the measured codes. A reading with decodes the many-codes rule left out ("not_run", reading_rules.MANY_CODES) | |
| is at most medium: those decodes count as misses, and no measured code had any. | |
| "why" names the main reason the confidence isn't high. A reading cut short gives "partial", even on the right pile (its | |
| unfinished reads count as misses, so they may be what put it there). Otherwise a pile always gives the pile, whatever | |
| the grade (a picture none of the five read may hold no code at all); off the piles, a d near a cut, or few reads, or | |
| "not_run" for a high grade capped at medium, and none for a high grade. The module refuses to load, loudly, without a | |
| well-formed levels.json. | |
| """ | |
| import json | |
| from bisect import bisect_right | |
| from itertools import product | |
| from pathlib import Path | |
| PATH = Path(__file__).resolve().parent.parent / "web" / "data" / "levels.json" | |
| READS, SIZES, SIDES = ("few", "two_plus"), ("big", "small"), ("below", "above") | |
| WHY = ("near_cut", "few_reads") # the reasons a cell off the piles can give | |
| def _no_constant(name: str): | |
| raise ValueError(f"{name} is not JSON") | |
| def _grade(m: int, n: int, grades: dict) -> str: | |
| return next((g for g, least in grades.items() if m / n >= least), "low") | |
| def _check(data: dict) -> dict: | |
| """The off-pile cell for each evidence (reads, size, distance bin, side), once levels.json is found sound.""" | |
| cuts, conf = data["cuts"], data["confidence"] | |
| grades, near, cells = conf["grades"], conf["near"], conf["cells"] | |
| if len(data["levels"]) != len(cuts) + 1 or cuts != sorted(cuts) or any(not lv["key"] for lv in data["levels"]): | |
| raise ValueError("levels and cuts don't match") | |
| if list(grades) != ["high", "medium"] or not 1 >= grades["high"] > grades["medium"] > 0 or near != sorted(near): | |
| raise ValueError("grades or near are malformed") | |
| if not (isinstance(conf["few_below"], int) and isinstance(conf["big_above"], int)): | |
| raise ValueError("few_below and big_above are whole numbers") | |
| if not isinstance(conf["big_top_below"], bool): | |
| raise ValueError("big_top_below is true or false") | |
| off = cells["off_piles"] | |
| every = [cells["pile_lo"], cells["pile_hi"], *off] | |
| for c in every: | |
| n, m = c["n"], c["m"] | |
| if not (isinstance(n, int) and isinstance(m, int) and 0 <= m <= n and n > 0): | |
| raise ValueError(f"a cell's n and m are malformed: {c}") | |
| if c["agree"] != round(m / n, 3) or c["grade"] != _grade(m, n, grades): | |
| raise ValueError(f"a cell's agree or grade isn't its share's: {c}") | |
| if [sum(c[k] for c in every) for k in ("m", "n")] != [conf["overall"]["m"], conf["overall"]["n"]]: | |
| raise ValueError("the cells don't add up to overall") | |
| lookup = {} | |
| for c in off: | |
| if (c["why"] is None) != (c["grade"] == "high") or c["why"] not in (None, *WHY): | |
| raise ValueError(f"a cell that isn't high needs a reason, and a high one has none: {c}") | |
| if not (set(c["reads"]) <= set(READS) and set(c["size"]) <= set(SIZES) and set(c["side"]) <= set(SIDES) | |
| and set(c["bins"]) <= set(range(len(near) + 1))): | |
| raise ValueError(f"a cell names evidence that doesn't exist: {c}") | |
| for e in product(c["reads"], c["size"], c["bins"], c["side"]): | |
| if lookup.setdefault(e, c) is not c: | |
| raise ValueError(f"two cells take the evidence {e}") | |
| if len(lookup) != len(READS) * len(SIZES) * (len(near) + 1) * len(SIDES): | |
| raise ValueError("some evidence has no cell") | |
| return lookup | |
| def load(path: Path = PATH) -> tuple[dict, dict]: | |
| """levels.json and its off-pile cells by evidence; a missing or malformed file raises RuntimeError.""" | |
| try: | |
| data = json.loads(path.read_text(), parse_constant=_no_constant) | |
| return data, _check(data) | |
| except (OSError, ValueError, KeyError, TypeError, AttributeError) as e: | |
| raise RuntimeError(f"rating: {path} is missing or malformed: {e!r}") from e | |
| LEVELS, BY_EVIDENCE = load() | |
| CUTS = LEVELS["cuts"] | |
| KEYS = [lv["key"] for lv in LEVELS["levels"]] | |
| CONFIDENCE = LEVELS["confidence"] | |
| def level(d: float) -> str: | |
| """The level's key for d: a d on a cut belongs to the level above it.""" | |
| return KEYS[bisect_right(CUTS, d)] | |
| def evidence(d: float, big: bool) -> tuple[int, str]: | |
| """The distance bin from d to the nearest cut, and the side of it d counts as on: below it, or on or above it (a d | |
| on the cut is above it); with big_top_below, a big picture near the top cut counts as below it on either side.""" | |
| cut = min(CUTS, key=lambda c: abs(d - c)) | |
| near = CONFIDENCE["near"] | |
| k = bisect_right(near, round(abs(d - cut), 1)) # d comes rounded to 0.1, and so does its distance | |
| below = d < cut or (CONFIDENCE["big_top_below"] and big and cut == CUTS[-1] and k < len(near)) | |
| return k, "below" if below else "above" | |
| def rating(place: dict, count1280: int, partial: bool, size: list[int], not_run: bool = False) -> dict: | |
| """{"level", "confidence": "high" | "medium" | "low", "agree": the evidence cell's share (None for a reading cut | |
| short), "n": its measured codes (None for a reading cut short), "why": "partial" | "pile_lo" | "pile_hi" | | |
| "near_cut" | "few_reads" | "not_run" | None, "big": whether the picture's long side is over 1280 px}, from | |
| place.place()'s {"d", "pile"}, how many of the five read the picture at 1280 px, whether the reading was cut | |
| short, the picture's [width, height], and whether the many-codes rule left any decode out.""" | |
| lv = level(place["d"]) | |
| big = max(size) > CONFIDENCE["big_above"] | |
| if partial: | |
| return {"level": lv, "confidence": "low", "agree": None, "n": None, "why": "partial", "big": big} | |
| if place["pile"]: | |
| why = "pile_" + place["pile"] | |
| cell = CONFIDENCE["cells"][why] | |
| else: | |
| reads = "few" if count1280 < CONFIDENCE["few_below"] else "two_plus" | |
| cell = BY_EVIDENCE[(reads, "big" if big else "small", *evidence(place["d"], big))] | |
| why = cell["why"] | |
| grade = cell["grade"] | |
| if not_run and grade == "high": # decodes not run count as misses: they may be what raised it | |
| grade, why = "medium", why or "not_run" | |
| return {"level": lv, "confidence": grade, "agree": cell["agree"], "n": cell["n"], "why": why, "big": big} | |