File size: 9,182 Bytes
6f28f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a2a176
 
 
 
 
 
 
6f28f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
378f397
6f28f9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// The four levels: their names and cuts from data/levels.json (the file the server rates with), what each means on
// the measured codes, the row of level names under the bell's axis, and the confidence bars.
import { count, css, el, join, pctText, sv } from './util.js';
import { t } from './copy.js';

const PHONES = { apple: 'apple-vision', google: 'google', huawei: 'hms-scankit' };
const GROUP = { // what a measured code is, by its set: {mix} in the levels' lines
  pristine: 'prints', everyday: 'prints', tough: 'prints', boofcv: 'photos', broken104: 'photos', qrdn: 'research', syn10k: 'research',
};
let LV;
let STATS;

export function initLevels(json) {
  LV = json;
}

/** A d on a cut goes to the level above; the left pile (-6) is Easy and the right pile (106) Advanced. */
export const levelOf = (d) => LV.cuts.filter((c) => d >= c).length;
export const levelName = (i) => LV.levels[i].name;
export const levelKey = (i) => LV.levels[i].key;
export const levelNames = () => LV.levels.map((lv) => lv.name);
export const indexOfKey = (key) => LV.levels.findIndex((lv) => lv.key === key);

/** The level across the cut nearest d; on a cut, the level below it. */
export function nearestOther(d) {
  const c = LV.cuts.reduce((a, b) => (Math.abs(b - d) < Math.abs(a - d) ? b : a));
  return levelName(d < c ? levelOf(c) : levelOf(c) - 1);
}

/** The score, cut down (never rounded) so a Tough picture never shows 60; the piles have words of their own. */
export function scoreText(place) {
  if (place.pile === 'lo') return t('verdict.score_lo');
  if (place.pile === 'hi') return t('verdict.score_hi');
  return t('verdict.score', { s: Math.floor(place.d) });
}
export function measuredScore(d) {
  if (d < 0) return t('panel.measured_level_lo');
  if (d > 100) return t('panel.measured_level_hi');
  return t('panel.measured_level', { s: Math.floor(d) });
}

/** What the scanners did with the measured codes at each level (each code's level from its eight-scanner d), at
 *  1280 pixels, and what the codes are; read once from bell.json. */
export function levelStats({ n, d, read1280, set }) {
  const st = LV.levels.map(() => ({ n: 0, apple: 0, google: 0, huawei: 0, all3: 0, any3: 0, zxing: 0, mix: {}, sets: {} }));
  const totals = {};
  for (let i = 0; i < n; i++) {
    const s = st[levelOf(d(i))];
    const ph = Object.values(PHONES).map((k) => read1280(i, k));
    s.n += 1;
    Object.keys(PHONES).forEach((k, j) => { s[k] += ph[j]; });
    s.all3 += ph.every(Boolean);
    s.any3 += ph.some(Boolean);
    s.zxing += read1280(i, 'zxing-java');
    const key = set(i);
    if (GROUP[key]) s.mix[GROUP[key]] = (s.mix[GROUP[key]] || 0) + 1;
    s.sets[key] = (s.sets[key] || 0) + 1;
    totals[key] = (totals[key] || 0) + 1;
  }
  for (const s of st) for (const k of [...Object.keys(PHONES), 'all3', 'any3', 'zxing']) s[k] /= s.n || 1;
  STATS = { st, totals };
}

/** What the codes mostly are: the kinds making up a tenth or more, largest first; "mostly" when some were left out. */
function mixText(mix) {
  const all = Object.values(mix).reduce((a, b) => a + b, 0);
  if (!all) return '';
  const parts = Object.entries(mix).filter(([, c]) => c / all >= 0.1).sort((a, b) => b[1] - a[1]);
  const names = join(parts.map(([k]) => t(`levels.mix.${k}`)));
  return Object.values(mix).some((c) => c > 0 && c / all < 0.1) ? t('levels.mix.mostly', { parts: names }) : names;
}

const num = (v) => pctText(v).replace('%', '');
function shares(m) {
  const each = [m.apple, m.google, m.huawei];
  const [a, b] = [num(Math.min(...each)), num(Math.max(...each))];
  return { each3: a === b ? `${a}%` : `${a}–${b}%`, all3: pctText(m.all3), any3: pctText(m.any3), zxing: pctText(m.zxing), google: pctText(m.google) };
}

export const gist = (i) => t(`levels.${levelKey(i)}.gist`);

/** The levels list: the measured codes all eight scanners place at level i. */
function listLine(i) {
  const s = STATS.st[i];
  return s.n ? t(`levels.${levelKey(i)}.line`, { n: count(s.n), mix: mixText(s.mix), ...shares(s) }) : '';
}

/** Beside a picture: the measured codes in pictures of its size class that the five place at its level. */
export function yoursLine(i, big) {
  const m = LV.meaning?.[big ? 'big' : 'small']?.[levelKey(i)];
  if (!m || !m.n) return '';
  return t(`levels.${levelKey(i)}.line_yours`, {
    class: t(big ? 'levels.class.big' : 'levels.class.small'), frame: t(big ? 'levels.frame.big' : 'levels.frame.small'),
    n: count(m.n), mix: mixText(m.mix || {}), ...shares(m),
  });
}

/** Beside a picture on the right pile: the measured codes the five put there, over both size classes. */
export function pileLine() {
  const m = LV.meaning?.pile_hi;
  if (!m || !m.n) return '';
  return t('levels.advanced.line_pile_hi', { n: count(m.n), mix: mixText(m.mix || {}), ...shares(m) });
}

/** The score's integer band for level i: below 30, 30 to 49, 50 to 59, 60 and up. */
function band(i) {
  const c = LV.cuts;
  if (i === 0) return t('levels.band.below', { c: c[0] });
  if (i === c.length) return t('levels.band.above', { c: c[c.length - 1] });
  return t('levels.band.between', { a: c[i - 1], b: c[i] - 1 });
}

/** The prints line-up: how many clean, everyday and tough prints sit at Easy, Everyday and Tough. */
function prints() {
  const at = (set, key) => count(STATS.st[indexOfKey(key)]?.sets[set] || 0);
  const of = (set) => count(STATS.totals[set] || 0);
  return {
    clean_easy: at('pristine', 'easy'), clean_n: of('pristine'), everyday_everyday: at('everyday', 'everyday'),
    everyday_n: of('everyday'), tough_tough: at('tough', 'tough'), tough_n: of('tough'),
  };
}

/** "What the four levels mean", under the bell. */
export function renderLevelsList(box) {
  if (!box) return; // a page cached from before the levels: nothing to fill
  const summary = box.querySelector('summary');
  summary.textContent = t('levels.more_summary');
  box.querySelector('.levels-intro').textContent = t('levels.phones_defined');
  box.querySelector('.levels-list').replaceChildren(...LV.levels.flatMap((lv, i) => [
    el('dt', {}, [el('span', { class: 'lv-name', text: lv.name }), el('span', { class: 'lv-band', text: band(i) })]),
    el('dd', { text: [gist(i), listLine(i)].filter(Boolean).join(' ') }),
  ]));
  box.querySelector('.levels-relation').textContent = t('levels.relation', prints());
  box.hidden = !summary.textContent;
}

/** The footer's sentences on levels, confidence, the checks and the selector, with their numbers from the data. */
export function fillHow() {
  const conf = LV.confidence;
  const hi = conf.cells.pile_hi;
  const [c1, c2, c3] = LV.cuts;
  const fill = (id, key, vars) => {
    const node = document.getElementById(id);
    if (!node) return;
    node.textContent = t(key, vars);
    node.hidden = !node.textContent;
  };
  fill('howDashed', 'how.index_html.dashed');
  fill('howPileHi', 'how.index_html.pile_hi_add', { hi_n: count(hi.n), hi_pct: pctText(hi.m != null ? hi.m / hi.n : hi.agree) });
  fill('howLevels', 'how.index_html.levels', { c1, c2, c3, ...prints() });
  fill('howConfidence', 'how.index_html.confidence', {
    agree_m: count(conf.overall.m), agree_n: count(conf.overall.n), high: pctText(conf.grades.high), medium: pctText(conf.grades.medium),
  });
  fill('howChecks', 'how.index_html.checks');
  fill('howDetails', 'how.index_html.details');
  fill('howSelector', 'how.index_html.selector');
}

/** Under the bell's axis: a notch at each cut, each level's name centred under its band (pushed apart where a band is
 *  narrower than its name), and the band where the cursor is, underlined in ink with its name in bold. */
export function drawLevelAxis(svg, x, base, W, d, [lo, hi]) {
  const on = levelOf(d);
  const last = LV.levels.length - 1;
  const edges = [lo, ...LV.cuts, hi];
  const g = sv('g', { class: 'levels', 'aria-hidden': 'true' }, svg);
  for (const c of LV.cuts) sv('line', { x1: x(c), x2: x(c), y1: base - 0.5, y2: base + 9, stroke: css('--ink-3') }, g);
  sv('line', {
    x1: x(edges[on]) + (on ? 1.5 : 0), x2: x(edges[on + 1]) - (on < last ? 1.5 : 0), y1: base + 1.5, y2: base + 1.5,
    stroke: css('--ink'), 'stroke-width': 2,
  }, g);
  const names = LV.levels.map((lv, i) => sv('text', {
    x: x((edges[i] + edges[i + 1]) / 2), y: base + 25, 'text-anchor': 'middle',
    class: `level-name${i === on ? ' on' : ''}${W < 520 ? ' narrow' : ''}`, text: lv.name,
  }, g));
  const boxes = names.map((n) => n.getBBox());
  let right = -Infinity;
  for (const b of boxes) { // left to right: no name closer than 8 px to the one before
    b.x += Math.max(0, right + 8 - b.x);
    right = b.x + b.width;
  }
  for (let i = boxes.length - 1; i >= 0 && right > W - 2; i--) { // then the last one back inside the drawing
    const limit = i < boxes.length - 1 ? boxes[i + 1].x - 8 - boxes[i].width : W - 2 - boxes[i].width;
    boxes[i].x = Math.min(boxes[i].x, limit);
  }
  names.forEach((n, i) => n.setAttribute('x', boxes[i].x + boxes[i].width / 2));
}

/** Confidence as one to three filled bars; the word always stands beside them. */
export const bars = (grade) => el('span', { class: `conf conf-${grade}`, 'aria-hidden': 'true' }, [el('i'), el('i'), el('i')]);