Coverage for app / services / evaluation / spread.py: 98%

53 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1""" 

2Repeats — how much of a score is the configuration, and how much is chance. 

3 

4A model sampled twice on the same question does not answer the same way, so 

5one run's mean score is one draw. Two variants a point apart on one run may 

6not differ at all. A run with ``repeats > 1`` asks every case of every 

7variant that many times, one full pass after another, and this module reads 

8the spread out of the passes: 

9 

10* **Score spread** — each pass has its own mean score; the standard 

11 deviation and range of those means say how far the headline number moves 

12 run to run. It is the spread of the *mean*, not of individual cells, 

13 because the mean is what the leaderboard ranks. 

14* **Consistency** — the share of cases whose pass/fail verdict was the same 

15 on every repeat. A case that passes twice and fails once is *inconsistent*: 

16 the configuration cannot be relied on for it, whatever its average says. 

17 

18Everything here is over plain outcome objects (``repeat``, ``target_key``, 

19``case``, ``score.value``, ``passed``), so it runs on the runner's outcomes 

20and is testable without one. 

21""" 

22 

23from __future__ import annotations 

24 

25import statistics 

26from collections.abc import Iterable 

27from typing import Any 

28 

29#: The most passes one run may make. Each pass costs as much as a plain run, 

30#: and past a handful the spread estimate barely tightens. 

31MAX_REPEATS = 5 

32 

33#: Inconsistent cases listed on a run's summary; the count is always exact. 

34MAX_INCONSISTENT_LISTED = 20 

35 

36 

37def clamp_repeats(value: Any) -> int: 

38 """A usable repeat count from whatever a config holds.""" 

39 try: 

40 repeats = int(value or 1) 

41 except (TypeError, ValueError): 

42 return 1 

43 return max(1, min(repeats, MAX_REPEATS)) 

44 

45 

46def _case_key(outcome: Any) -> str: 

47 case = outcome.case 

48 return str(getattr(case, "case_id", None) or getattr(case, "name", "")) 

49 

50 

51def _score(outcome: Any) -> float | None: 

52 return outcome.score.value 

53 

54 

55def spread(outcomes: Iterable[Any]) -> dict[str, Any]: 

56 """Run-to-run spread of one set of outcomes (a variant's, or a run's). 

57 

58 ``repeats`` is how many passes the outcomes cover. With one, there is 

59 nothing to measure: every figure but ``repeats`` is None — never 0, which 

60 would claim a perfectly stable configuration. 

61 """ 

62 items = list(outcomes) 

63 passes: dict[int, list[float]] = {} 

64 for outcome in items: 

65 # Every pass is counted even when none of its cells scored, so a 

66 # pass lost to errors still reads as a repeat that happened. 

67 bucket = passes.setdefault(getattr(outcome, "repeat", 0), []) 

68 value = _score(outcome) 

69 if value is not None: 

70 bucket.append(value) 

71 repeats = len(passes) or 1 

72 empty = { 

73 "repeats": repeats, 

74 "score_sd": None, 

75 "score_min": None, 

76 "score_max": None, 

77 "consistency": None, 

78 "inconsistent": None, 

79 } 

80 if repeats < 2: 

81 return empty 

82 

83 means = [sum(v) / len(v) for v in passes.values() if v] 

84 verdicts: dict[tuple[str, str], set[bool]] = {} 

85 for outcome in items: 

86 if outcome.passed is None: 

87 continue 

88 key = (str(outcome.target_key), _case_key(outcome)) 

89 verdicts.setdefault(key, set()).add(bool(outcome.passed)) 

90 judged = len(verdicts) 

91 inconsistent = sum(1 for seen in verdicts.values() if len(seen) > 1) 

92 return { 

93 "repeats": repeats, 

94 "score_sd": round(statistics.stdev(means), 4) if len(means) > 1 else None, 

95 "score_min": round(min(means), 4) if means else None, 

96 "score_max": round(max(means), 4) if means else None, 

97 "consistency": round(1 - inconsistent / judged, 4) if judged else None, 

98 "inconsistent": inconsistent, 

99 } 

100 

101 

102def inconsistent_cases(outcomes: Iterable[Any]) -> list[dict[str, Any]]: 

103 """Every (variant, case) whose verdict changed between repeats. 

104 

105 Listed beside the results the way suspect labels are: a prompt to read 

106 those answers, most often a case on the edge of what the configuration 

107 can do — or a criterion loose enough that the judge reads it two ways. 

108 """ 

109 cells: dict[tuple[str, str], list[Any]] = {} 

110 for outcome in outcomes: 

111 if outcome.passed is None: 

112 continue 

113 cells.setdefault((str(outcome.target_key), _case_key(outcome)), []).append( 

114 outcome 

115 ) 

116 found = [] 

117 for (target_key, _), items in cells.items(): 

118 passed = sum(1 for o in items if o.passed) 

119 if 0 < passed < len(items): 

120 case = items[0].case 

121 found.append( 

122 { 

123 "variant_id": target_key, 

124 "case_id": getattr(case, "case_id", None), 

125 "case_name": getattr(case, "name", None), 

126 "passed": passed, 

127 "answers": len(items), 

128 } 

129 ) 

130 found.sort(key=lambda c: (str(c["case_name"] or ""), c["variant_id"])) 

131 return found[:MAX_INCONSISTENT_LISTED]