Download scripts/scoring.py from yflantmy/DecisionRobust: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/datasets/yflantmy/DecisionRobust/resolve/main/scripts/scoring.py
- Command line
-
hf download hf://datasets/yflantmy/DecisionRobust/scripts/scoring.py
-
curl -L -o scoring.py https://huggingface.co/datasets/yflantmy/DecisionRobust/resolve/main/scripts/scoring.py
2.5 kB
| """Native-answer scoring for the public panels; Python standard library only. | |
| score(record, answers) -> {prediction, correct}; invalid answers raise ValueError. | |
| answers maps question IDs to {'choice': label}, {'noul': probability}, or {'score': number}. | |
| Only task.question_ids are read. No model calls are made. | |
| """ | |
| import math | |
| def number(x): | |
| if isinstance(x, bool) or not isinstance(x, (int, float)) or not math.isfinite(x): | |
| raise ValueError('Expected a finite numeric model output') | |
| return float(x) | |
| def score(record, answers): | |
| task = record['task'] | |
| qids = task['question_ids'] | |
| mode = task['mode'] | |
| try: | |
| if mode == 'choice': | |
| if len(qids) != 1: | |
| raise ValueError('Choice expects one scoring target') | |
| pred = answers[qids[0]]['choice'] | |
| options = record['clean']['questions'][qids[0]]['criteria'] | |
| if pred not in options: | |
| raise ValueError('Choice label is not in the options') | |
| elif mode == 'score': | |
| if len(qids) != 1: | |
| raise ValueError('Score expects one scoring target') | |
| pred = number(answers[qids[0]]['score']) | |
| return {'prediction': pred, 'correct': abs(pred - record['gt']) <= 0.5} | |
| elif mode == 'noul': | |
| probs = [number(answers[q]['noul']) for q in qids] | |
| if any(p < 0 or p > 1 for p in probs): | |
| raise ValueError('Noul probability must be in [0,1]') | |
| labels = task['labels'] | |
| if 'positive_label' in task: | |
| if len(probs) != 1 or len(labels) != 2: | |
| raise ValueError('Binary Noul expects one output and two labels') | |
| positive = task['positive_label'] | |
| negative = next(v for v in labels if v != positive) | |
| pred = positive if probs[0] >= 0.5 else negative | |
| else: | |
| if len(probs) != len(labels): | |
| raise ValueError('Multi-Noul question and label counts differ') | |
| total = sum(probs) | |
| norm = [p / total for p in probs] if total else [1 / len(probs)] * len(probs) | |
| pred = labels[max(range(len(labels)), key=lambda i: norm[i])] | |
| else: | |
| raise ValueError('Unknown mode') | |
| except (KeyError, TypeError, StopIteration) as error: | |
| raise ValueError('Missing or malformed target model answer') from error | |
| return {'prediction': pred, 'correct': pred == record['gt']} | |