Datasets:
Download code/evaluation-code/v0.2-0c2690ae/tools/evaluation/scoring.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 5.83 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/scoring.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/scoring.py
-
curl -L -o scoring.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/scoring.py
5.83 kB
| """Private scoring of parsed answers against truth. | |
| The rule comes from one place, ``embodied_pipeline.prover.scoring`` (the | |
| question pack carries it as ``answer.scoring``; an item set without it is | |
| looked up there by ``version_id``). Its tolerance is ``absolute`` (time, | |
| angle) or ``max(floor, relative * magnitude)``, where ``magnitude`` defaults to | |
| the truth's distance to the origin (point2), the truth vector length (vector2) | |
| or the truth value (scalar); a truth row may override it explicitly when the | |
| version defines magnitude differently. | |
| - choice: pass on the exact option index. | |
| A tolerance is the scale of an answer, not a correct/incorrect cut (system.md | |
| Section 9): ``score`` is 1 within the tolerance and decays as a Gaussian | |
| ``exp(-(e - tol)**2 / (2 * sigma**2))`` beyond it, ``sigma`` provisionally | |
| equal to the tolerance; choices score 1 or 0. ``passed`` (within tolerance) | |
| stays as a diagnostic. | |
| Unparsed, truncated (token budget exhausted before an answer) and failed | |
| requests are reported as such, never converted to an automatic number. | |
| ``pass_rate`` uses all items as denominator (a missing answer | |
| does not pass); ``answered_pass_rate`` uses parsed items only. | |
| """ | |
| import math | |
| import statistics | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[2])) | |
| from embodied_pipeline.prover import scoring as rules # noqa: E402 | |
| def rule_for(item): | |
| """The item's rule: the pack's ``answer.scoring`` when present, else the same rule looked up by version.""" | |
| s = item.get('scoring') or {} | |
| return s if 'kind' in s else rules.rule(item['version_id'], None) | |
| def tolerance_for(spec, rule, truth, magnitude=None): | |
| kind = spec['type'] | |
| if kind == 'choice': | |
| return None, None | |
| tol = (rule or {}).get('tolerance') | |
| if tol is None: | |
| raise ValueError(f'scoring rule has no tolerance: {rule!r}') | |
| if kind == 'time': | |
| return rules.tau(tol, 0.0), None | |
| if magnitude is None: | |
| magnitude = abs(float(truth)) if kind == 'scalar' else math.hypot(float(truth[0]), float(truth[1])) | |
| return rules.tau(tol, magnitude), magnitude | |
| def curve(error, tol, sigma=None): | |
| """Scoring curve: 1 within ``tol``, Gaussian decay beyond; ``sigma`` defaults to ``tol``.""" | |
| sigma = tol if sigma is None else sigma | |
| if error <= tol + 1e-12: | |
| return 1.0 | |
| return math.exp(-(error - tol) ** 2 / (2 * sigma ** 2)) if sigma > 0 else 0.0 | |
| def score_one(spec, scoring, truth, parsed, magnitude=None, n_options=None): | |
| """One scored row; ``parsed`` comes from ``prompts.parse`` or ``None``. | |
| For choices ``sector_distance`` counts steps around the ring of | |
| ``n_options`` equal sectors (diagnostic, not part of the pass rule). | |
| """ | |
| kind = spec['type'] | |
| if scoring.get('kind') == 'log_ratio': | |
| from tools.evaluation import np_answers | |
| factor = scoring.get('factor', scoring.get('tolerance', {}).get('factor')) | |
| return dict(np_answers.score(dict(kind='log_ratio',factor=factor),truth,parsed),answer_type=kind) | |
| tol, mag = tolerance_for(spec, scoring, truth, magnitude) | |
| row = {'answer_type': kind, 'tolerance': tol, 'magnitude': mag} | |
| if not parsed or parsed.get('status') != 'PARSED': | |
| status = parsed.get('status', 'UNPARSED') if parsed else 'ERROR' | |
| return {**row, 'status': status, 'passed': False, 'score': 0.0, 'error': None} | |
| value = parsed['value'] | |
| if kind == 'choice': | |
| circular = None | |
| if n_options and isinstance(truth, int): | |
| d = abs(value - truth) % n_options | |
| circular = min(d, n_options - d) | |
| return {**row, 'status': 'SCORED', 'passed': value == truth, 'score': float(value == truth), | |
| 'error': 0.0 if value == truth else 1.0, 'sector_distance': circular} | |
| if kind in ('point2', 'vector2'): | |
| error = math.hypot(value[0] - truth[0], value[1] - truth[1]) | |
| else: | |
| error = abs(value - float(truth)) | |
| rel = error / mag if mag else None | |
| return {**row, 'status': 'SCORED', 'passed': error <= tol + 1e-12, 'score': curve(error, tol), 'error': error, | |
| 'relative_error': rel} | |
| def summarize(rows, keys=('version_id',)): | |
| """Group scored rows; each group reports counts, pass rates and errors.""" | |
| groups = {} | |
| for row in rows: | |
| label = tuple(row.get(k) for k in keys) | |
| groups.setdefault(label, []).append(row) | |
| out = [] | |
| for label, every in sorted(groups.items(), key=lambda kv: tuple(str(x) for x in kv[0])): | |
| # NO_TOLERANCE rows (no registered tolerance) have an error but no pass/fail; they stay out of rates. | |
| items = [r for r in every if r['status'] != 'NO_TOLERANCE'] | |
| scored = [r for r in items if r['status'] == 'SCORED'] | |
| errors = [r['error'] for r in scored if r.get('error') is not None | |
| and r['answer_type'] not in ('choice', 'sector', 'options', 'boolean', 'order', 'map')] | |
| passed = sum(1 for r in items if r['passed']) | |
| out.append({ | |
| **dict(zip(keys, label)), | |
| 'items': len(items), | |
| 'no_tolerance': len(every) - len(items), | |
| 'scored': len(scored), | |
| 'unparsed': sum(1 for r in items if r['status'] == 'UNPARSED'), | |
| 'truncated': sum(1 for r in items if r['status'] == 'TRUNCATED'), | |
| 'errors': sum(1 for r in items if r['status'] == 'ERROR'), | |
| 'passed': passed, | |
| 'pass_rate': passed / len(items) if items else None, | |
| 'answered_pass_rate': passed / len(scored) if scored else None, | |
| 'mean_score': sum(r.get('score', 0.0) for r in items) / len(items) if items else None, | |
| 'median_error': statistics.median(errors) if errors else None, | |
| 'mean_error': statistics.fmean(errors) if errors else None, | |
| }) | |
| return out | |