Download scripts/lib-common.mjs from PeterKol/llm-questionnaire-protocol-sensitivity: direct link, hf CLI and curl.
- Browser
- Download file 7.17 kB
-
https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-common.mjs
- Command line
-
hf download hf://datasets/PeterKol/llm-questionnaire-protocol-sensitivity/scripts/lib-common.mjs
-
curl -L -o lib-common.mjs https://huggingface.co/datasets/PeterKol/llm-questionnaire-protocol-sensitivity/resolve/main/scripts/lib-common.mjs
7.17 kB
| /** | |
| * Shared pieces for the pilot run (2026-09-30). Outputs go to the bundle folder and to | |
| * <tmp>/pilot-2026-09-30/. | |
| * | |
| * Scoring: scoring-key.csv (scale and the five answer weights of every item) and scripts/score-from-key.mjs. | |
| * Up to 2026-10-05 the analysis called the product's own scoring code instead; the key was exported from that | |
| * code and the two were compared on every stored answer vector (README, "Scorer parity"). Item texts and | |
| * option labels come from items-en.csv (and items-en-licence-pending.csv, if present); the analysis does not | |
| * need them, only the model-calling scripts do. | |
| */ | |
| import fs from 'fs' | |
| import path from 'path' | |
| import crypto from 'crypto' | |
| import os from 'os' | |
| import { fileURLToPath } from 'url' | |
| import { loadKey, scoreFromKey } from './score-from-key.mjs' | |
| // Public bundle: paths are relative to the bundle (override with PROTOCOL_DIR). | |
| const HERE = path.dirname(fileURLToPath(import.meta.url)) | |
| export const BUNDLE = (process.env.PROTOCOL_DIR ? path.resolve(process.env.PROTOCOL_DIR) : path.resolve(HERE, '..')).replace(/\\/g, '/') | |
| export const VAULT = BUNDLE // name kept from the original scripts: the folder that holds runs/, runs-full/ and the result files | |
| export const SCRATCH = path.join(process.env.PROTOCOL_SCRATCH || os.tmpdir(), 'pilot-2026-09-30') | |
| export const HF_DIR = process.env.PROTOCOL_HF_DIR ? path.resolve(process.env.PROTOCOL_HF_DIR) : path.join(SCRATCH, 'hf') // the human dataset CSVs (see README) | |
| export const RUNS = path.join(VAULT, 'runs') | |
| fs.mkdirSync(RUNS, { recursive: true }) | |
| fs.mkdirSync(SCRATCH, { recursive: true }) | |
| // ---------- the scorer ---------- | |
| export const KEY_FILE = path.join(BUNDLE, 'scoring-key.csv') | |
| const KEY = loadKey(KEY_FILE) | |
| export const scoreSubmission = (slug, answers) => scoreFromKey(KEY, slug, answers) | |
| // ---------- the three tests ---------- | |
| export const TESTS = { | |
| big_five: { | |
| slug: 'big-five', pack: 'en.test.big-five.json', key: 'big_five', csv: 'big_five.csv', | |
| scales: ['O', 'C', 'E', 'A', 'N'], nItems: 50, | |
| }, | |
| riasec: { | |
| slug: 'riasec', pack: 'en.test.riasec.json', key: 'riasec', csv: 'riasec.csv', | |
| scales: ['R', 'I', 'A', 'S', 'E', 'C'], nItems: 60, | |
| }, | |
| dark_triad: { | |
| slug: 'dark-triad', pack: 'en.test.dark-triad.json', key: 'dark_triad', csv: 'dark_triad.csv', | |
| scales: ['narcissism', 'machiavellianism', 'psychopathy'], nItems: 18, | |
| }, | |
| } | |
| export const TEST_IDS = Object.keys(TESTS) | |
| /** Per item: scale, whether it is reverse-keyed (from the weights, not from a comment), and the text and option labels | |
| * exactly as the English pack served them to a person (null when the item-text files are absent). */ | |
| export function loadItems(testId) { | |
| const t = TESTS[testId] | |
| const k = KEY[t.slug] | |
| if (!k || k.items.length !== t.nItems) throw new Error(`${testId}: key has ${k ? k.items.length : 0} items, dataset has ${t.nItems}`) | |
| const texts = {} | |
| for (const f of ['items-en.csv', 'items-en-licence-pending.csv']) { | |
| const p = path.join(BUNDLE, f) | |
| if (!fs.existsSync(p)) continue | |
| const { head, rows } = readCsv(p) | |
| const c = Object.fromEntries(head.map((h, i) => [h, i])) | |
| for (const r of rows) if (r.length === head.length) texts[`${r[c.test]}/${r[c.item]}`] = { text: r[c.text], options: [0, 1, 2, 3, 4].map((o) => r[c[`option_${o}`]]) } | |
| } | |
| return k.items.map((it, i) => { | |
| const w = it.weights | |
| const tx = texts[`${t.slug}/${i + 1}`] | |
| return { i, text: tx ? tx.text : null, options: tx ? tx.options : null, scale: it.scale, reverse: w[0] > w[w.length - 1] } | |
| }) | |
| } | |
| // ---------- CSV ---------- | |
| export function readCsv(file) { | |
| const text = fs.readFileSync(file, 'utf8') | |
| const rows = [] | |
| let row = [], field = '', q = false | |
| for (let i = 0; i < text.length; i++) { | |
| const c = text[i] | |
| if (q) { | |
| if (c === '"') { if (text[i + 1] === '"') { field += '"'; i++ } else q = false } else field += c | |
| } else if (c === '"') q = true | |
| else if (c === ',') { row.push(field); field = '' } | |
| else if (c === '\n') { row.push(field.replace(/\r$/, '')); rows.push(row); row = []; field = '' } | |
| else field += c | |
| } | |
| if (field.length || row.length) { row.push(field); rows.push(row) } | |
| const head = rows.shift() | |
| return { head, rows } | |
| } | |
| export function loadHumans(testId) { | |
| const t = TESTS[testId] | |
| const { head, rows } = readCsv(path.join(HF_DIR, t.csv)) | |
| const idx = Object.fromEntries(head.map((h, i) => [h, i])) | |
| const qCols = Array.from({ length: t.nItems }, (_, k) => idx[`q${k + 1}`]) | |
| if (qCols.some((c) => c == null)) throw new Error(`${testId}: missing q columns`) | |
| return rows.filter((r) => r.length === head.length).map((r) => ({ | |
| id: Number(r[idx.response_id]), | |
| locale: r[idx.locale], | |
| ym: r[idx.year_month], | |
| duration: r[idx.duration_seconds] === '' ? null : Number(r[idx.duration_seconds]), | |
| top: r[idx.top_result], | |
| stored: Object.fromEntries(t.scales.map((s) => [s, r[idx[`score_${s}`]] === '' ? null : Number(r[idx[`score_${s}`]])])), | |
| q: qCols.map((c) => Number(r[c])), | |
| })) | |
| } | |
| // ---------- small stats ---------- | |
| export const mean = (a) => a.reduce((x, y) => x + y, 0) / a.length | |
| export const sd = (a) => { const m = mean(a); return Math.sqrt(a.reduce((s, x) => s + (x - m) ** 2, 0) / (a.length - 1)) } | |
| export const median = (a) => { const s = [...a].sort((x, y) => x - y); const n = s.length; return n % 2 ? s[(n - 1) / 2] : (s[n / 2 - 1] + s[n / 2]) / 2 } | |
| export const quantile = (sorted, p) => { const i = (sorted.length - 1) * p; const lo = Math.floor(i), hi = Math.ceil(i); return sorted[lo] + (sorted[hi] - sorted[lo]) * (i - lo) } | |
| export function pearson(x, y) { | |
| const mx = mean(x), my = mean(y) | |
| let sxy = 0, sxx = 0, syy = 0 | |
| for (let i = 0; i < x.length; i++) { sxy += (x[i] - mx) * (y[i] - my); sxx += (x[i] - mx) ** 2; syy += (y[i] - my) ** 2 } | |
| return sxx && syy ? sxy / Math.sqrt(sxx * syy) : NaN | |
| } | |
| /** mid-rank percentile of a value inside a sample (ties count half). */ | |
| export function midrankPct(sortedSample, v) { | |
| let lo = 0, hi = sortedSample.length | |
| while (lo < hi) { const m = (lo + hi) >> 1; if (sortedSample[m] < v) lo = m + 1; else hi = m } | |
| const below = lo | |
| hi = sortedSample.length | |
| let j = lo | |
| while (j < hi && sortedSample[j] === v) j++ | |
| return ((below + 0.5 * (j - below)) / sortedSample.length) * 100 | |
| } | |
| export const r1 = (x) => (x == null || Number.isNaN(x) ? null : Math.round(x * 10) / 10) | |
| export const r2 = (x) => (x == null || Number.isNaN(x) ? null : Math.round(x * 100) / 100) | |
| export const r3 = (x) => (x == null || Number.isNaN(x) ? null : Math.round(x * 1000) / 1000) | |
| export function sha256File(p) { return crypto.createHash('sha256').update(fs.readFileSync(p)).digest('hex') } | |
| /** What the numbers in an output file were computed from: the key, the scorer, the human CSVs. */ | |
| export function provenance() { | |
| const files = ['scoring-key.csv', 'scripts/score-from-key.mjs'] | |
| return { | |
| sha256: Object.fromEntries(files.map((f) => [f, sha256File(path.join(BUNDLE, f)).slice(0, 16)])), | |
| humanCsv: Object.fromEntries(Object.values(TESTS).map((t) => [t.csv, sha256File(path.join(HF_DIR, t.csv)).slice(0, 16)])), | |
| hfCommit: 'a39a58649a58202a02bb7eaf826609c2fe8bca84', | |
| } | |
| } | |