Download rl/tests/test_grading.py from d0rj/mathematics_dataset: direct link, hf CLI and curl.
- Browser
- Download file 7.39 kB
-
https://huggingface.co/datasets/d0rj/mathematics_dataset/resolve/main/rl/tests/test_grading.py
- Command line
-
hf download hf://datasets/d0rj/mathematics_dataset/rl/tests/test_grading.py
-
curl -L -o test_grading.py https://huggingface.co/datasets/d0rj/mathematics_dataset/resolve/main/rl/tests/test_grading.py
7.39 kB
| import hashlib | |
| import json | |
| import tomllib | |
| from fractions import Fraction | |
| from pathlib import Path | |
| import pytest | |
| from mathematics_rl.catalog import MODULES, contract_for | |
| from mathematics_rl.grading import extract_answer, grade_answer | |
| from mathematics_rl.parsing import ParseError, as_sympy, parse | |
| FIXTURES = Path(__file__).parent / "fixtures" | |
| ROWS = [ | |
| json.loads(line) | |
| for path in sorted(FIXTURES.glob("generated_*.ndjson")) | |
| for line in path.read_text(encoding="utf-8").splitlines() | |
| ] | |
| def grade(row, answer): | |
| return grade_answer(row["module"], row["question"], row["answer"], answer) | |
| def wrong_answer(row): | |
| contract = contract_for(row["module"], row["question"]) | |
| answer = row["answer"] | |
| if contract.kind == "boolean": | |
| return "False" if answer in ("True", "Истина") else "True" | |
| if contract.kind == "number": | |
| return str(Fraction(answer) + 1) | |
| if contract.kind == "roots": | |
| return str(max(Fraction(item) for item in answer.split(",")) + 1) | |
| if contract.kind == "ordered": | |
| return ", ".join(reversed(answer.split(", "))) | |
| if contract.kind == "clock": | |
| return "00:00" if not answer.startswith("12:00") else "00:01" | |
| if contract.kind in ("selection", "base"): | |
| return "0" if answer != "0" else "1" | |
| expression = as_sympy(parse(answer), positive=contract.positive) | |
| return str(expression + 1) | |
| def test_generated_reference_and_wrong_answer(row): | |
| actual = grade(row, row["answer"]) | |
| assert actual.status == "correct", (row, actual) | |
| assert grade(row, wrong_answer(row)).reward == 0, row | |
| def test_fixture_provenance_and_module_documentation(): | |
| provenance = tomllib.loads((FIXTURES / "provenance.toml").read_text(encoding="utf-8")) | |
| for language in ("en", "ru"): | |
| record = provenance[language] | |
| digest = hashlib.sha256((FIXTURES / record["file"]).read_bytes()).hexdigest() | |
| assert digest == record["sha256"] | |
| text = (FIXTURES.parents[1] / "MODULES.md").read_text(encoding="utf-8") | |
| assert all(f"`{module}`" in text for module in MODULES) | |
| def test_complete_bilingual_catalog_and_categories(): | |
| assert len(MODULES) == 70 | |
| assert ROWS, "Generate the committed regression fixtures first" | |
| for language in ("en", "ru"): | |
| selected = [row for row in ROWS if row["language"] == language] | |
| assert {row["module"] for row in selected} == set(MODULES) | |
| assert {row["category"] for row in selected} == { | |
| "train-easy", | |
| "train-medium", | |
| "train-hard", | |
| "interpolate", | |
| "extrapolate", | |
| } | |
| assert {contract_for(row["module"], row["question"]).kind for row in ROWS} == { | |
| "number", | |
| "expression", | |
| "roots", | |
| "factors", | |
| "ordered", | |
| "selection", | |
| "boolean", | |
| "clock", | |
| "base", | |
| "expanded", | |
| "surd", | |
| "power", | |
| } | |
| def test_equivalent_answers(module, question, reference, answer): | |
| assert grade_answer(module, question, reference, answer).reward == 1 | |
| def test_wrong_answers_and_reward_shortcuts(module, question, reference, answer): | |
| assert grade_answer(module, question, reference, answer).reward == 0 | |
| def test_unsafe_and_unbounded_expressions_fail(text): | |
| with pytest.raises((ParseError, ValueError)): | |
| as_sympy(parse(text)) | |
| def test_extraction_and_falsey_answers(): | |
| assert extract_answer(r"Reasoning: \boxed{2}. Final: \boxed{\frac{1}{2}}") == r"\frac{1}{2}" | |
| assert extract_answer("<think>wrong 5</think>\nAnswer: 0") == "0" | |
| assert grade_answer("arithmetic__add_or_sub", "Compute.", "0", "0").reward == 1 | |
| with pytest.raises(ParseError): | |
| extract_answer(r"\boxed{4}\n\boxed{") | |
| with pytest.raises(ParseError): | |
| extract_answer(" ") | |
| def test_invalid_reference_is_distinct_from_model_failure(): | |
| result = grade_answer("arithmetic__add_or_sub", "Compute.", "not a number", "0") | |
| assert result.status == "invalid_reference" | |
| def test_large_exact_simple_power_and_polynomial_allocation_bounds(): | |
| reference = "x**(1234567890123/98765432101)" | |
| assert ( | |
| grade_answer( | |
| "polynomials__simplify_power", "Simplify positive x.", reference, reference | |
| ).reward | |
| == 1 | |
| ) | |
| for candidate in ("x**1000000000", "(a+b+c+d+e+f)**64", "(x**2-1)/(x-1)"): | |
| result = grade_answer("polynomials__compose", "Compute.", "x+1", candidate) | |
| assert result.reward == 0 and result.status == "parse_error" | |