{"schemaVersion":1,"slug":"component-classifier-threshold","kind":"component","version":"0.1.0","files":[{"path":"Component.jsx","content":"'use client'\nimport React from 'react'\nimport Example from './classifier-threshold.jsx'\nimport './collection.css'\nexport default function Component(props) {\n  return <div className=\"lq-component\" data-family=\"visualization\"><Example {...props}/></div>\n}\n","sha256":"9702827a073668f2894683df721c7bf81e5fd1ad50d511971cef4e0e542632e9"},{"path":"classifier-threshold.jsx","content":"'use client'\nimport React, { useState } from 'react'\nimport { classifierSettings, classifierDefaults, classifierCells } from './startup.js'\nimport { phrase, number, palette } from './shared.jsx'\nconst scores = [\n  [0.94, 1],\n  [0.83, 0],\n  [0.79, 1],\n  [0.72, 1],\n  [0.63, 0],\n  [0.58, 1],\n  [0.43, 0],\n  [0.35, 1],\n  [0.22, 0],\n  [0.1, 0],\n]\nexport default function Component({\n  locale = 'en',\n  initialThreshold,\n  initialCell,\n  onSettingsChange,\n}) {\n  const initial = classifierSettings({ initialThreshold, initialCell }) ?? classifierDefaults\n  const t = (...v) => phrase(locale, ...v),\n    [threshold, setThreshold] = useState(initial.initialThreshold),\n    [cell, inspect] = useState(classifierCells.indexOf(initial.initialCell))\n  const buckets = [[], [], [], []]\n  scores.forEach(([score, label], i) => {\n    const predicted = Math.round(score * 100) >= threshold ? 1 : 0\n    buckets[(predicted ? 0 : 2) + (label ? 0 : 1)].push([i + 1, score])\n  })\n  const [tp, fp, fn] = buckets.map((b) => b.length),\n    precision = tp + fp ? number(locale, (100 * tp) / (tp + fp)) + '%' : '—',\n    recall = number(locale, (100 * tp) / (tp + fn)) + '%'\n  const names = t(\n    'True positive|False positive|False negative|True negative',\n    '真阳性|假阳性|假阴性|真阴性',\n    'Verdadero positivo|Falso positivo|Falso negativo|Verdadero negativo',\n    '真陽性|偽陽性|偽陰性|真陰性',\n  ).split('|')\n  return (\n    <section className=\"viz-study\">\n      <label>\n        {t('Positive cutoff', '阳性阈值', 'Umbral positivo', '陽性判定値')} · {threshold}%\n        <input\n          type=\"range\"\n          min=\"0\"\n          max=\"100\"\n          value={threshold}\n          onChange={(e) => {\n            const value = Number(e.target.value)\n            setThreshold(value)\n            onSettingsChange?.({ initialThreshold: value, initialCell: classifierCells[cell] })\n          }}\n        />\n      </label>\n      <div\n        role=\"group\"\n        aria-label={t('Confusion matrix', '混淆矩阵', 'Matriz de confusión', '混同行列')}\n        style={{ display: 'grid', gridTemplateColumns: 'repeat(2,minmax(0,1fr))', gap: 4 }}\n      >\n        {buckets.map((bucket, i) => (\n          <button\n            key={i}\n            aria-pressed={cell === i}\n            onClick={() => {\n              inspect(i)\n              onSettingsChange?.({ initialThreshold: threshold, initialCell: classifierCells[i] })\n            }}\n            style={{\n              borderInlineStart: '4px solid ' + (i === 0 || i === 3 ? palette.mid : palette.pale),\n            }}\n          >\n            {names[i]} · {bucket.length}\n          </button>\n        ))}\n      </div>\n      <output>\n        {t('Precision', '精确率', 'Precisión', '適合率')} {precision} ·{' '}\n        {t('Recall', '召回率', 'Exhaustividad', '再現率')} {recall}\n      </output>\n      <p>\n        {names[cell]} ·{' '}\n        {buckets[cell].length\n          ? buckets[cell]\n              .map(([id, score]) => 'S' + id + ' (' + Math.round(score * 100) + '%)')\n              .join(' / ')\n          : t('No matching records', '无匹配记录', 'Sin registros', '該当する記録はありません')}\n      </p>\n    </section>\n  )\n}\n","sha256":"e90a5b19dd259468df755add6a69a2400b0165e166b74cee508ce4d070d8cccf"},{"path":"collection.css","content":"/* Portable styles for the optional wrapper in Component.jsx. */\n.lq-component {\n  --cc-ink:#162632; --cc-muted:#526572; --cc-stroke:#cbd5dc; --cc-tint:#edf3f6;\n  --cc-blue:#426981; --cc-surface:#fff; --cc-inverse:#fff;\n  font:400 15px/1.55 system-ui,sans-serif; color:var(--cc-ink);\n  min-width:0; width:100%; overflow-wrap:anywhere; color-scheme:light;\n}\n.lq-component *, .lq-component *::before,.lq-component *::after { box-sizing:border-box; }\n.lq-component :is(h2,h3,h4,p,ul,ol,dl,form,fieldset,figure,table) { margin:0 0 .85em; }\n.lq-component :is(h2,h3) { font-size:clamp(20px,2vw,29px); line-height:1.18; letter-spacing:-.035em; }\n.lq-component h4 {font-size:1rem}\n.lq-component :is(ol,ul){padding-inline-start:1.4em}\n.lq-component label { display:inline-flex; flex-direction:column; gap:4px; max-width:100%; font-size:14px; font-weight:550; }\n.lq-component label:has(input[type=checkbox]),.lq-component label:has(input[type=radio]) {flex-direction:row;align-items:center}\n.lq-component :is(input,select,textarea,button){font:inherit;max-width:100%;accent-color:var(--cc-blue)}\n.lq-component :is(input:not([type=checkbox],[type=radio],[type=range],[type=file]),select,textarea){\n  color:var(--cc-ink); background:var(--cc-surface); border:1px solid #a9bac5; border-radius:6px; padding:7px 10px;min-width:0;\n}\n.lq-component textarea{min-height:64px}\n.lq-component :is(button,summary){cursor:pointer;}\n.lq-component button{\n  color:var(--cc-ink);background:var(--cc-tint);border:1px solid var(--cc-stroke);\n  border-radius:6px;min-height:35px;padding:5px 11px;font-weight:600;font-size:14px;\n}\n.lq-component button:disabled{opacity:.5;cursor:default}\n.lq-component button[aria-pressed=true],.lq-component [role=tab][aria-selected=true]{background:var(--cc-ink);color:var(--cc-inverse)}\n.lq-component :focus-visible{outline:2px solid var(--cc-blue);outline-offset:2px}\n.lq-component output {display:block;color:var(--cc-ink);font-weight:650;font-variant-numeric:tabular-nums}\n.lq-component :is(.collection-actions,.collection-row,.demo-actions){display:flex;align-items:end;gap:9px;flex-wrap:wrap;margin-block:12px}\n.lq-component :is(.collection-fields,.form-fields){display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,180px),1fr));gap:14px;margin-block:14px}\n.lq-component :is(.collection-output,.demo-output){padding:12px;border-inline-start:3px solid var(--cc-blue);background:var(--cc-tint);margin-block:14px}\n.lq-component fieldset {border:1px solid var(--cc-stroke);border-radius:8px;padding:12px;min-width:0}\n.lq-component .collection-demo>*,.lq-component .viz-study>*{max-width:100%}\n.lq-component :is(form,fieldset)>label {margin:0 10px 10px 0}\n.lq-component :is(button+button,label+button) {margin-inline-start:3px}\n.lq-component table{width:100%;border-collapse:collapse;font-variant-numeric:tabular-nums;overflow-wrap:anywhere}\n.lq-component :is(th,td){padding:6px;text-align:start;border-bottom:1px solid var(--cc-stroke)}\n.lq-component dialog{max-width:min(90vw,500px);border:1px solid var(--cc-stroke);border-radius:10px;padding:20px;color:var(--cc-ink);background:white}\n.lq-component dialog::backdrop{background:rgb(17 29 39/.45)}\n.lq-component :is(svg,canvas,img){max-width:100%}\n@media(hover:hover){.lq-component button:not(:disabled):hover{border-color:var(--cc-blue)}}\n@media(pointer:coarse){.lq-component button,.lq-component select,.lq-component input[type=range]{min-height:44px}}\n@media(prefers-reduced-motion:reduce){.lq-component *, .lq-component *::before,.lq-component *::after{animation-duration:0s!important;transition-duration:0s!important;scroll-behavior:auto!important}}\n.lq-component :is(.viz-study,.wf-demo,.collection-demo)>label {margin:0 16px 12px 0}\n.lq-component .viz-study>svg[role=img]{display:block;margin:6px 0 14px}\n.lq-component .viz-study>output{border-top:1px solid var(--cc-stroke);padding:12px 0 0;font-variant-numeric:tabular-nums}\n.lq-component :is(.wf-demo,.collection-demo) form{display:flex;align-items:end;flex-wrap:wrap;gap:9px 14px}\n.lq-component :is(.wf-demo,.collection-demo) label textarea{width:min(100%,34rem)}\n.lq-component :is(.wf-demo,.collection-demo)>:is(output,[role=status]){display:block;margin-top:12px;color:var(--cc-blue);font-weight:600}\n.lq-component .collection-fields>.collection-row,.lq-component .collection-fields>.collection-output,.lq-component .collection-fields>svg{grid-column:1/-1}\n","sha256":"9a629a50c65fc07377b3d164b571ed33b8bd54c392ff4dab06c4fd317c0864ab"},{"path":"shared.jsx","content":"import React from 'react'\n\nconst locales = ['en', 'zh-CN', 'es', 'ja']\nexport const phrase = (locale, ...translations) =>\n  translations[locales.includes(locale) ? locales.indexOf(locale) : 0]\nexport const number = (locale, value, digits = 1) =>\n  new Intl.NumberFormat(locales.includes(locale) ? locale : 'en', {\n    maximumFractionDigits: digits,\n  }).format(value)\nexport const clamp = (min, max, n) => Math.min(max, Math.max(min, n))\nexport const palette = {\n  ink: '#233847',\n  mid: '#526f84',\n  steel: '#7795a9',\n  pale: '#bdced9',\n  line: '#cfdae2',\n  paper: '#eef3f6',\n}\n\nexport function Bar({ fraction, label, shade = palette.mid }) {\n  const width = clamp(0, 1, fraction || 0) * 200\n  return (\n    <svg\n      viewBox=\"0 0 200 8\"\n      preserveAspectRatio=\"none\"\n      role=\"img\"\n      aria-label={label}\n      width=\"100%\"\n      height=\"8\"\n    >\n      <rect width=\"200\" height=\"8\" fill={palette.line} rx=\"2\" />\n      <rect width={width} height=\"8\" fill={shade} rx=\"2\" />\n    </svg>\n  )\n}\n\nexport function Spark({ lines, label, height = 100, minimum = 0, maximum }) {\n  const values = lines.flat(),\n    low = Math.min(minimum, ...values),\n    high = maximum ?? Math.max(low, ...values),\n    scale = Math.max(Math.abs(high), Math.abs(low)) || 1,\n    span = high / scale - low / scale || 1\n  const project = (series) =>\n    series\n      .map((value, i) =>\n        [\n          9 + (i * 342) / Math.max(1, series.length - 1),\n          height - 8 - ((value / scale - low / scale) / span) * (height - 16),\n        ].join(','),\n      )\n      .join(' ')\n  return (\n    <svg\n      viewBox={'0 0 360 ' + height}\n      width=\"100%\"\n      height={height}\n      preserveAspectRatio=\"none\"\n      role=\"img\"\n      aria-label={label}\n    >\n      {[0.25, 0.5, 0.75].map((f) => (\n        <line\n          key={f}\n          x1=\"6\"\n          x2=\"354\"\n          y1={height * f}\n          y2={height * f}\n          stroke={palette.line}\n          strokeDasharray=\"2 4\"\n        />\n      ))}\n      {lines.map((series, i) => (\n        <polyline\n          key={i}\n          points={project(series)}\n          fill=\"none\"\n          stroke={[palette.ink, palette.steel, palette.mid][i % 3]}\n          strokeWidth=\"2.3\"\n          strokeDasharray={i === 1 ? '5 3' : undefined}\n          vectorEffect=\"non-scaling-stroke\"\n        />\n      ))}\n    </svg>\n  )\n}\n","sha256":"d8aa85e92613057be69d4e9ac7cfa916de1af8f6492321c9291654315f7a042b"},{"path":"README.md","content":"# Classification threshold\n\nMove the cutoff to inspect misses and false alarms in labelled records.\n\nImport the default export from Component.jsx into a React client entry. The locale prop accepts en, zh-CN, es and ja. Use your host’s existing React dependency and test and build commands. The sample runs locally.\n\n## Optional startup props\n\ninitialThreshold accepts an integer percentage from 0 to 100. initialCell accepts true-positive, false-positive, false-negative or true-negative. The default configuration is {\"initialThreshold\":60,\"initialCell\":\"false-positive\"}. Invalid startup props use the default configuration.\n","sha256":"0ee637adfdff9d78bcd2cec029667da78cf967cb07be4d1a6f06eff6e65117ae"},{"path":"startup.js","content":"export const classifierCells = Object.freeze([\n  'true-positive',\n  'false-positive',\n  'false-negative',\n  'true-negative',\n])\nexport const classifierDefaults = Object.freeze({\n  initialThreshold: 60,\n  initialCell: 'false-positive',\n})\nexport function classifierSettings({ initialThreshold = 60, initialCell = 'false-positive' } = {}) {\n  if (\n    !Number.isInteger(initialThreshold) ||\n    initialThreshold < 0 ||\n    initialThreshold > 100 ||\n    !classifierCells.includes(initialCell)\n  )\n    return null\n  return { initialThreshold, initialCell }\n}\n","sha256":"1cca8b219557571b2b5e7ebfcfdf6b094b9f7ab7f7eb18ff9af87a89b94943cd"}]}
