Spaces:
Running
Running
Download source/src/challenge.ts from Giuliocc/windtunnel-lab: direct link, hf CLI and curl.
- Browser
- Download file 4.04 kB
-
https://huggingface.co/spaces/Giuliocc/windtunnel-lab/resolve/main/source/src/challenge.ts
- Command line
-
hf download hf://spaces/Giuliocc/windtunnel-lab/source/src/challenge.ts
-
curl -L -o challenge.ts https://huggingface.co/spaces/Giuliocc/windtunnel-lab/resolve/main/source/src/challenge.ts
4.04 kB
| export interface Challenge { | |
| revision: string; | |
| rows: { | |
| row_id: string; | |
| question_id: string; | |
| option_ids: string[]; | |
| observed: number[]; | |
| baseline: number[]; | |
| weight: number; | |
| }[]; | |
| } | |
| export function distributionMetrics(predicted: number[], observed: number[]) { | |
| let totalVariation = 0; | |
| let halfBrier = .5; | |
| for (const [index, p] of predicted.entries()) { | |
| const q = observed[index]; | |
| totalVariation += .5 * Math.abs(p - q); | |
| halfBrier += .5 * p * p - p * q; | |
| } | |
| return { total_variation: totalVariation, half_multiclass_brier: halfBrier }; | |
| } | |
| export function scoreSubmission(challenge: Challenge, input: unknown) { | |
| const submission = input as { challenge_revision?: unknown; predictions?: unknown }; | |
| if (!submission || typeof submission !== 'object' || Object.keys(submission).length !== 2 | |
| || submission.challenge_revision !== challenge.revision || !Array.isArray(submission.predictions) | |
| || submission.predictions.length > challenge.rows.length) { | |
| throw new Error('Use a prediction file for this exact challenge revision.'); | |
| } | |
| const rows = new Map(challenge.rows.map(row => [row.row_id, row])); | |
| const scoredItems = new Map<string, { count: number; weight: number; submitted: number; reference: number; submittedBrier: number; referenceBrier: number }>(); | |
| const seen = new Set<string>(); | |
| let scored = 0; | |
| for (const prediction of submission.predictions) { | |
| if (!prediction || typeof prediction !== 'object' || Object.keys(prediction).length !== 2 | |
| || !Object.hasOwn(prediction, 'probabilities') || typeof prediction.row_id !== 'string') { | |
| throw new Error('Each prediction needs a row ID and a probability vector or null.'); | |
| } | |
| const row = rows.get(prediction.row_id); | |
| if (!row || seen.has(row.row_id)) throw new Error('Duplicate or unknown benchmark row ID.'); | |
| seen.add(row.row_id); | |
| const values = prediction.probabilities; | |
| if (values === null) continue; | |
| if (!Array.isArray(values) || values.length !== row.option_ids.length | |
| || values.some(p => typeof p !== 'number' || !Number.isFinite(p) || p < 0 || p > 1) | |
| || Math.abs(values.reduce((sum, p) => sum + p, 0) - 1) > 1e-6) { | |
| throw new Error(`Invalid probability vector for ${row.row_id}.`); | |
| } | |
| const trial = distributionMetrics(values, row.observed); | |
| const reference = distributionMetrics(row.baseline, row.observed); | |
| const item = scoredItems.get(row.question_id) ?? { count: 0, weight: 0, submitted: 0, reference: 0, submittedBrier: 0, referenceBrier: 0 }; | |
| item.count += 1; | |
| item.weight += row.weight; | |
| item.submitted += row.weight * trial.total_variation; | |
| item.reference += row.weight * reference.total_variation; | |
| item.submittedBrier += row.weight * trial.half_multiclass_brier; | |
| item.referenceBrier += row.weight * reference.half_multiclass_brier; | |
| scoredItems.set(row.question_id, item); | |
| scored += 1; | |
| } | |
| const byItem = [...scoredItems].sort(([a], [b]) => a.localeCompare(b)).map(([question_id, item]) => ({ | |
| question_id, rows: item.count, | |
| submission: { total_variation: item.submitted / item.weight, half_multiclass_brier: item.submittedBrier / item.weight }, | |
| reference: { total_variation: item.reference / item.weight, half_multiclass_brier: item.referenceBrier / item.weight }, | |
| })); | |
| const mean = (kind: 'submission' | 'reference', metric: 'total_variation' | 'half_multiclass_brier') => | |
| byItem.reduce((sum, item) => sum + item[kind][metric], 0) / byItem.length; | |
| return { | |
| challenge_revision: challenge.revision, scored_rows: scored, expected_rows: rows.size, | |
| coverage: scored / rows.size, complete: scored === rows.size, | |
| metrics: byItem.length ? { | |
| submission: { total_variation: mean('submission', 'total_variation'), half_multiclass_brier: mean('submission', 'half_multiclass_brier') }, | |
| reference: { total_variation: mean('reference', 'total_variation'), half_multiclass_brier: mean('reference', 'half_multiclass_brier') }, | |
| } : null, | |
| by_item: byItem, | |
| }; | |
| } | |