diff --git a/tasks/class-test/class-test-distractors-d1-e0000/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-e0000/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..c907d803e2bf3dd22ea7fe04f9642a3f35cd6c9c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,D +Q2,D +Q3,B +Q4,D +Q5,D +Q6,A diff --git a/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..eb231f6fbfa846c26b1fabd22ae11e9d0888aeab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/environment/workdir/responses.csv @@ -0,0 +1,57 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S004,10P,D,C,B,A,D,C +S084,10R,A,D,B,A,D,D +S211,10R,D,D,D,A,D,D +S370,10Q,D,D,A,B,A,D +S263,10Q,D,D,B,A,B,A +S292,10P,D,C,A,C,D,A +S360,10R,A,D,B,A,D,A +S251,10R,D,D,C,A,D,A +S204,10R,D,D,B,C,D,D +S392,10P,D,D,B,D,B,C +S277,10P,D,D,B,C,B,A +S237,10Q,D,D,A,B,D,A +S107,10R,D,D,A,C,D,C +S332,10Q,D,D,B,D,D,D +S055,10Q,D,D,D,D,D,A +S236,10Q,D,D,B,C,D,A +S221,10P,D,D,B,A,A,A +S166,10R,D,C,B,D,A,A +S165,10P,A,D,A,C,D,C +S274,10P,A,D,A,A,D,D +S327,10R,D,D,A,B,D,D +S005,10Q,D,D,B,A,B,A +S024,10Q,D,D,A,C,D,D +S083,10Q,D,D,C,A,B,A +S153,10R,D,D,B,B,B,A +S149,10P,D,C,D,C,A,A +S311,10P,B,D,B,A,D,D +S152,10Q,A,D,C,D,D,C +S270,10R,B,D,C,D,D,A +S361,10Q,D,D,D,C,A,D +S117,10P,A,C,B,D,D,A +S290,10Q,D,D,B,A,D,D +S199,10R,B,D,D,C,D,D +S247,10P,B,D,B,D,D,D +S287,10Q,D,D,B,B,A,A +S337,10Q,D,D,A,D,D,B +S330,10R,D,D,B,C,D,B +S378,10Q,D,D,B,B,D,B +S301,10Q,D,B,B,A,D,D +S397,10P,D,D,B,A,D,C +S222,10R,A,A,B,C,D,B +S123,10P,D,C,B,A,D,D +S195,10P,D,D,B,D,D,A +S282,10Q,D,D,D,B,D,D +S375,10R,D,D,A,A,D,A +S059,10Q,D,D,B,B,B,A +S354,10R,A,D,A,A,D,B +S261,10P,D,D,B,A,D,D +S350,10P,D,D,B,D,A,A +S209,10Q,D,D,D,A,D,C +S235,10R,B,D,D,A,A,A +S303,10Q,D,D,D,B,D,A +S284,10Q,D,D,B,A,B,A +S227,10P,D,D,B,A,D,B +S275,10P,D,D,D,A,B,D +S190,10Q,D,D,B,A,B,A diff --git a/tasks/class-test/class-test-distractors-d1-e0000/instruction.md b/tasks/class-test/class-test-distractors-d1-e0000/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-e0000/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-e0000/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-e0000/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-e0000/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-e0000/task.toml b/tasks/class-test/class-test-distractors-d1-e0000/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3ce373794ce854cfadaa6d5b24d7047ee01cc12d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10000 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-e0000/tests/checks.json b/tasks/class-test/class-test-distractors-d1-e0000/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..92ca0564819416df7bf85b17ded6eda576a8ef8b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/tests/checks.json @@ -0,0 +1,143 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 8, + 5, + 0, + 43 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 1, + 1, + 6, + 48 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A", + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 11, + 31, + 4, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 24, + 9, + 12, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 8, + 10, + 0, + 38 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 25, + 6, + 7, + 18 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-e0000/tests/test.sh b/tasks/class-test/class-test-distractors-d1-e0000/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-e0000/tests/verify.py b/tasks/class-test/class-test-distractors-d1-e0000/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0000/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-e0012/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-e0012/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..1d5ae2a0e5af6ef9ecda5c287dc228c143d7bd4f --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,B +Q2,B +Q3,C +Q4,C +Q5,C +Q6,C diff --git a/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..c65e3d1b6b568620a122f917256b1f7e2931a37c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/environment/workdir/responses.csv @@ -0,0 +1,42 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S384,10Q,A,B,B,C,D,B +S016,10R,D,C,C,C,A,D +S219,10P,B,D,C,C,C,D +S207,10R,B,C,A,C,C,B +S254,10Q,A,C,C,C,C,B +S218,10R,A,D,B,D,C,B +S379,10Q,B,D,C,C,D,A +S228,10P,B,B,A,C,C,B +S305,10Q,B,C,C,C,C,B +S056,10Q,D,C,A,C,C,B +S240,10R,C,D,A,D,C,C +S128,10P,B,B,C,C,C,B +S099,10R,B,B,C,C,C,C +S088,10P,B,C,C,C,C,B +S290,10R,D,B,C,C,C,B +S248,10Q,B,B,C,C,C,A +S054,10Q,B,C,C,D,C,B +S268,10R,D,B,B,D,C,B +S101,10R,B,B,A,D,C,B +S044,10R,B,B,B,C,C,B +S257,10Q,B,D,A,C,C,C +S359,10R,D,B,C,C,A,B +S235,10Q,B,D,C,D,D,B +S041,10Q,B,D,C,C,C,C +S253,10Q,C,B,A,D,D,B +S279,10Q,B,B,C,D,C,B +S031,10Q,C,D,C,C,A,B +S026,10R,B,B,C,C,C,B +S093,10Q,B,C,C,C,A,B +S215,10R,C,C,C,C,C,A +S119,10R,D,B,C,C,C,C +S315,10R,B,B,C,C,D,D +S133,10P,D,D,C,C,C,B +S291,10Q,B,B,C,C,A,B +S166,10Q,B,B,B,C,C,B +S300,10P,B,B,C,C,C,B +S298,10R,B,B,B,C,C,B +S050,10Q,B,D,C,D,C,B +S269,10R,B,C,A,C,D,B +S350,10P,B,C,C,C,D,B +S169,10Q,C,B,C,C,C,B diff --git a/tasks/class-test/class-test-distractors-d1-e0012/instruction.md b/tasks/class-test/class-test-distractors-d1-e0012/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-e0012/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-e0012/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-e0012/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-e0012/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-e0012/task.toml b/tasks/class-test/class-test-distractors-d1-e0012/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..5231bd17c920d89737f17fb30b494034cc323e9f --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10012 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-e0012/tests/checks.json b/tasks/class-test/class-test-distractors-d1-e0012/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d47df275200088e4915049e9288670cc8f09afa2 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/tests/checks.json @@ -0,0 +1,146 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 3, + 26, + 5, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 0, + 20, + 11, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 8, + 6, + 27, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 0, + 32, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A", + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 5, + 0, + 29, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 3, + 30, + 5, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-e0012/tests/test.sh b/tasks/class-test/class-test-distractors-d1-e0012/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-e0012/tests/verify.py b/tasks/class-test/class-test-distractors-d1-e0012/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-e0012/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0000/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0000/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..d8075be782e7719776f6aac44bab7688c4866574 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,D +Q2,B +Q3,D +Q4,C +Q5,D +Q6,C diff --git a/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..9569eb2278394a46cc5bfc984953b7aac4b40e6c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/environment/workdir/responses.csv @@ -0,0 +1,37 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S144,10Q,C,C,C,C,D,A +S077,10R,D,B,A,C,D,A +S096,10Q,D,C,D,C,B,C +S180,10P,D,D,D,B,D,B +S117,10Q,C,B,A,B,B,B +S133,10R,D,D,A,C,D,B +S234,10P,C,B,D,A,B,C +S205,10R,D,C,C,C,D,C +S021,10Q,D,B,D,C,D,B +S286,10Q,C,B,C,A,A,C +S092,10Q,D,C,D,B,D,B +S216,10P,D,C,A,C,D,D +S389,10R,C,D,D,C,D,D +S068,10Q,C,A,B,C,D,B +S113,10R,A,A,A,C,D,B +S228,10P,C,D,A,B,A,B +S184,10Q,C,C,B,D,D,A +S100,10R,D,B,A,C,D,B +S162,10P,D,B,D,B,D,B +S055,10Q,D,C,D,C,D,B +S146,10P,D,C,D,D,D,D +S264,10Q,A,D,A,D,A,B +S089,10Q,C,B,A,A,D,C +S305,10P,D,B,A,C,D,B +S321,10P,D,B,A,C,D,A +S095,10Q,C,D,A,C,D,D +S209,10R,D,C,D,C,D,A +S231,10P,C,D,B,C,D,C +S172,10P,D,C,D,B,A,D +S185,10P,D,C,D,C,B,B +S255,10P,C,C,D,D,D,B +S087,10Q,C,C,B,D,D,B +S342,10P,D,D,C,C,B,D +S140,10Q,A,C,A,C,D,C +S062,10P,A,B,B,C,A,C +S147,10Q,C,C,A,B,D,A diff --git a/tasks/class-test/class-test-distractors-d1-t0000/instruction.md b/tasks/class-test/class-test-distractors-d1-t0000/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0000/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0000/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0000/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0000/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0000/task.toml b/tasks/class-test/class-test-distractors-d1-t0000/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..58fc33eb0fc89c8bcfd782955cdde973fb7e4f01 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 0 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0000/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0000/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f2a587aaffb899d44ef4fdfe0416d54f69c42672 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/tests/checks.json @@ -0,0 +1,141 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 4, + 0, + 14, + 18 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 2, + 11, + 15, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 14, + 5, + 4, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 3, + 7, + 21, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 5, + 5, + 0, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 6, + 16, + 8, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0000/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0000/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0000/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0000/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0000/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0012/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0012/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..fea68ff2162dec30056e6f067b9ee8d6fcc8ad56 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,C +Q2,C +Q3,D +Q4,C +Q5,B +Q6,A diff --git a/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..97cf3dc4f1359d2c4a82abc79dd4a1d9fb5394fa --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/environment/workdir/responses.csv @@ -0,0 +1,57 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S078,10P,B,D,D,C,B,A +S279,10Q,C,D,A,C,B,A +S319,10R,C,A,A,A,A,A +S377,10P,C,D,D,C,B,A +S380,10P,C,C,A,D,B,A +S177,10R,C,C,A,C,B,C +S066,10Q,D,B,B,C,B,A +S027,10P,C,D,D,D,A,D +S193,10Q,A,C,D,C,A,D +S140,10Q,C,D,D,A,A,A +S149,10Q,C,B,D,A,B,C +S117,10R,C,D,A,A,B,A +S141,10P,A,D,D,C,B,A +S382,10R,C,D,D,B,B,C +S385,10R,B,C,C,C,B,C +S207,10R,B,D,D,C,B,A +S363,10R,C,D,D,C,B,A +S153,10Q,C,B,D,D,A,C +S075,10P,B,C,D,C,A,A +S364,10R,D,B,D,C,B,C +S085,10Q,C,D,D,B,B,A +S309,10P,C,D,D,C,A,A +S169,10R,C,B,A,C,B,D +S297,10Q,B,C,D,C,B,C +S225,10Q,C,D,B,B,A,A +S330,10P,B,D,A,C,B,A +S082,10Q,C,B,D,A,B,D +S349,10P,C,C,D,B,B,A +S014,10Q,C,D,D,C,B,A +S223,10Q,C,C,D,B,B,D +S118,10Q,C,A,D,C,A,A +S112,10R,A,D,D,C,A,C +S394,10P,C,D,D,D,B,D +S357,10Q,B,A,A,C,C,A +S209,10P,C,D,D,C,B,D +S128,10Q,C,A,D,C,B,D +S369,10Q,C,B,D,C,A,A +S098,10P,A,D,D,A,A,D +S190,10P,B,D,D,B,B,A +S242,10P,C,A,A,C,A,A +S266,10R,C,B,D,B,B,D +S214,10R,A,B,D,C,B,D +S137,10P,B,C,D,C,B,A +S178,10Q,C,B,D,C,B,A +S328,10Q,C,B,D,C,B,C +S331,10P,C,C,D,C,B,C +S186,10Q,A,C,D,B,B,D +S201,10P,C,A,A,C,B,A +S088,10P,B,C,D,C,B,A +S397,10R,B,C,D,C,B,A +S061,10P,A,D,D,C,A,A +S139,10R,C,D,D,A,A,D +S296,10P,C,C,B,C,A,A +S047,10R,D,D,A,C,B,C +S125,10P,A,D,D,C,A,A +S006,10R,C,C,D,C,B,C diff --git a/tasks/class-test/class-test-distractors-d1-t0012/instruction.md b/tasks/class-test/class-test-distractors-d1-t0012/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0012/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0012/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0012/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0012/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0012/task.toml b/tasks/class-test/class-test-distractors-d1-t0012/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c6727d95ad4dafd0aa347deed72c608e391e65df --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 12 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0012/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0012/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d67f611f639295282f325a1705dcdf406254b7d5 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/tests/checks.json @@ -0,0 +1,143 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 8, + 11, + 34, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 6, + 11, + 15, + 24 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 11, + 3, + 1, + 41 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 7, + 8, + 37, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 17, + 38, + 1, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 31, + 0, + 12, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q3", + "Q5", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0012/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0012/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0012/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0012/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0012/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0024/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0024/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..f39468c35929128941b3fe85b9176959e25c60d1 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,B +Q2,C +Q3,B +Q4,D +Q5,C +Q6,A diff --git a/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..126bfd9cd3c1499eb0fcf9d3a06c84943ed71b9c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/environment/workdir/responses.csv @@ -0,0 +1,36 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S270,10P,B,C,A,D,A,A +S107,10Q,B,C,A,D,C,A +S190,10P,B,A,D,D,C,A +S102,10R,B,B,B,D,A,A +S372,10P,D,A,B,D,C,A +S130,10P,A,C,B,D,C,A +S275,10R,A,C,A,D,C,C +S218,10Q,B,C,A,D,C,C +S264,10P,B,C,B,D,A,A +S147,10P,B,C,B,D,C,A +S124,10Q,A,C,B,D,A,A +S267,10P,B,B,B,B,C,C +S362,10P,B,C,B,D,C,A +S329,10Q,B,C,B,D,A,A +S273,10R,B,C,B,C,A,A +S208,10Q,D,C,D,D,A,A +S085,10P,B,A,B,C,C,A +S344,10P,B,A,B,D,A,A +S070,10R,B,B,D,A,A,A +S173,10R,B,C,D,D,A,C +S158,10R,B,B,B,A,A,C +S296,10Q,A,C,C,C,A,A +S314,10R,D,B,C,D,A,C +S053,10Q,B,C,B,C,A,C +S090,10R,A,C,B,D,A,A +S089,10P,A,A,C,D,A,A +S105,10R,A,B,C,D,C,A +S386,10Q,B,C,B,D,A,A +S378,10R,B,C,D,C,C,A +S007,10Q,C,C,B,D,A,A +S116,10Q,B,C,B,D,C,A +S383,10Q,D,C,B,D,C,A +S040,10P,B,C,B,D,C,A +S073,10Q,A,B,B,D,C,C +S152,10P,B,D,B,D,A,A diff --git a/tasks/class-test/class-test-distractors-d1-t0024/instruction.md b/tasks/class-test/class-test-distractors-d1-t0024/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0024/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0024/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0024/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0024/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0024/task.toml b/tasks/class-test/class-test-distractors-d1-t0024/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8209d62edce3744660e36a5d95f106a88ba8c2bc --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 24 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0024/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0024/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..2cff94bb1d43af9376f931437b1474d8be414710 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/tests/checks.json @@ -0,0 +1,150 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 8, + 22, + 1, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 5, + 7, + 22, + 1 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 4, + 22, + 4, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 2, + 1, + 5, + 27 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 19, + 0, + 16, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "B", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 27, + 0, + 8, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "B", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q4", + "Q5", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0024/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0024/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0024/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0024/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0024/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0036/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0036/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..475f99a41d9a7e3fef35c42d41f411bd4c61898e --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,B +Q2,A +Q3,B +Q4,B +Q5,C +Q6,D diff --git a/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..1a284248e5f2110eb00ce9997138b3a3766239b9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/environment/workdir/responses.csv @@ -0,0 +1,39 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S025,10P,D,D,C,B,C,B +S363,10P,D,A,D,D,C,D +S097,10Q,B,A,B,B,C,D +S252,10Q,D,A,B,C,C,D +S341,10P,B,A,D,B,C,C +S255,10P,B,A,B,B,C,C +S366,10R,B,A,B,D,C,B +S376,10R,B,A,B,B,B,B +S130,10P,D,C,C,C,C,C +S289,10R,D,C,C,B,C,D +S229,10P,D,A,D,B,B,B +S308,10R,D,A,B,B,C,D +S211,10Q,B,A,B,C,C,D +S380,10P,B,A,A,D,B,B +S348,10Q,B,D,B,D,C,D +S220,10Q,B,A,C,D,B,A +S215,10Q,B,C,A,D,C,D +S274,10R,B,A,B,B,B,A +S243,10P,C,A,B,C,B,C +S388,10R,B,B,B,B,C,B +S062,10P,C,D,D,B,B,B +S231,10P,B,B,B,D,C,B +S306,10P,B,D,B,C,C,B +S069,10Q,D,A,B,D,C,B +S329,10R,B,A,B,B,B,B +S015,10Q,B,C,B,D,B,C +S109,10R,B,A,B,D,C,C +S020,10P,B,D,D,B,C,D +S327,10R,B,D,C,D,C,C +S336,10P,C,A,B,D,C,B +S156,10Q,D,A,A,B,B,D +S335,10P,C,A,B,B,C,B +S323,10R,C,A,B,B,C,C +S128,10Q,B,A,B,C,C,D +S008,10P,C,A,B,D,B,B +S171,10R,B,A,B,C,C,D +S396,10P,C,D,B,B,C,B +S207,10Q,B,B,B,D,C,D diff --git a/tasks/class-test/class-test-distractors-d1-t0036/instruction.md b/tasks/class-test/class-test-distractors-d1-t0036/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0036/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0036/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0036/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0036/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0036/task.toml b/tasks/class-test/class-test-distractors-d1-t0036/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..deb931687ac5b831c6daa599492e501103f22f1d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 36 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0036/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0036/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..04d5ac17e9dd158556359af236ed9d3dca36be26 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/tests/checks.json @@ -0,0 +1,143 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 0, + 22, + 7, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 24, + 3, + 4, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 3, + 25, + 5, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 17, + 7, + 14 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 0, + 11, + 27, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "A", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 2, + 15, + 8, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0036/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0036/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0036/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0036/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0036/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0048/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0048/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..7a16996c32c73a04f6e7e7afdd58f33a97e5e6ba --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,C +Q2,D +Q3,C +Q4,B +Q5,B +Q6,A diff --git a/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..c6b66266bec2b311370e8be774375fd614b4ca1c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/environment/workdir/responses.csv @@ -0,0 +1,33 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S066,10Q,C,D,B,B,B,A +S266,10Q,C,B,B,B,A,A +S261,10Q,C,D,B,B,A,A +S186,10Q,C,C,B,B,C,A +S329,10Q,C,D,C,B,B,A +S336,10P,D,C,B,B,B,A +S372,10Q,C,A,D,D,B,C +S037,10Q,C,D,C,B,B,A +S322,10P,C,D,C,B,B,A +S205,10R,C,D,C,A,D,B +S362,10P,D,A,D,B,D,A +S378,10R,C,D,B,A,C,B +S252,10R,D,C,B,A,B,C +S332,10P,B,D,B,B,A,A +S317,10P,C,B,B,C,B,B +S148,10R,C,D,C,B,A,B +S352,10R,C,A,C,B,B,A +S288,10Q,C,D,B,D,B,C +S079,10P,C,C,B,B,B,C +S087,10P,C,A,B,D,C,A +S053,10R,C,A,B,B,B,A +S286,10R,C,D,B,B,C,C +S015,10R,B,C,C,C,B,B +S307,10R,C,A,A,B,B,A +S158,10Q,C,D,D,B,D,B +S227,10Q,C,C,D,B,C,A +S051,10R,C,A,C,B,B,B +S164,10Q,B,D,A,B,C,B +S370,10Q,D,D,C,C,B,A +S110,10Q,C,D,C,B,A,A +S328,10R,C,D,B,D,B,B +S157,10P,C,D,D,B,B,A diff --git a/tasks/class-test/class-test-distractors-d1-t0048/instruction.md b/tasks/class-test/class-test-distractors-d1-t0048/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0048/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0048/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0048/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0048/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0048/task.toml b/tasks/class-test/class-test-distractors-d1-t0048/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..a2df440a9d1e7cb837c1164f31404d9b46a4fa8f --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 48 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0048/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0048/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d867028317222cb153ad5de9d09d33de4e8996e0 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/tests/checks.json @@ -0,0 +1,139 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 0, + 3, + 25, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 7, + 2, + 6, + 17 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 2, + 15, + 10, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 3, + 22, + 3, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 5, + 18, + 6, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 18, + 9, + 5, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0048/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0048/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0048/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0048/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0048/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0060/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0060/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..8253a4c46dec6ab147bb7e1ccc34aee650c988a7 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,B +Q2,D +Q3,B +Q4,D +Q5,B +Q6,C diff --git a/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..f3e74dc51cc31a0319656604ef61c5cc9086ea5d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/environment/workdir/responses.csv @@ -0,0 +1,52 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S271,10P,B,B,A,B,B,C +S042,10P,D,D,C,A,B,A +S319,10R,C,D,B,B,B,C +S106,10R,D,D,B,C,B,D +S190,10R,B,C,B,A,D,D +S374,10P,B,C,C,A,D,D +S186,10R,D,D,D,A,B,D +S259,10P,B,D,B,D,B,C +S238,10P,D,B,A,B,D,D +S046,10R,D,D,C,B,D,D +S202,10R,C,B,B,A,B,D +S258,10Q,C,D,B,D,D,D +S326,10Q,C,D,C,B,D,B +S142,10P,B,D,B,C,B,D +S152,10P,C,B,C,B,B,D +S132,10Q,C,D,B,D,A,A +S355,10Q,C,D,A,D,B,D +S376,10Q,D,B,B,C,B,C +S300,10P,C,C,C,D,D,D +S155,10R,D,C,D,D,B,A +S234,10Q,C,C,C,B,B,C +S346,10P,D,C,C,D,B,B +S030,10Q,D,C,B,D,D,B +S368,10P,B,D,B,D,B,B +S062,10P,D,C,B,D,B,D +S298,10Q,D,C,B,B,D,D +S158,10R,C,D,B,D,B,C +S137,10P,D,D,C,B,B,A +S291,10R,C,B,B,A,B,D +S349,10Q,D,D,B,B,B,B +S069,10R,C,C,B,A,B,D +S354,10P,C,B,C,C,B,D +S336,10Q,D,D,C,B,B,D +S345,10P,B,D,A,C,B,D +S251,10P,D,C,B,B,A,B +S089,10P,A,B,A,D,B,D +S314,10Q,D,D,B,D,D,D +S063,10Q,D,B,B,D,D,D +S269,10P,B,D,B,A,D,A +S184,10P,D,D,C,B,D,C +S196,10P,B,D,B,D,B,B +S178,10R,C,B,C,D,B,A +S352,10P,D,D,D,B,B,C +S231,10Q,D,C,C,D,B,B +S171,10Q,D,C,B,B,B,D +S037,10Q,D,D,B,A,B,A +S183,10R,B,D,B,B,B,D +S312,10R,D,D,B,C,D,C +S389,10R,C,C,D,B,B,B +S386,10Q,D,B,B,B,B,D +S017,10Q,C,D,B,B,A,D diff --git a/tasks/class-test/class-test-distractors-d1-t0060/instruction.md b/tasks/class-test/class-test-distractors-d1-t0060/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0060/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0060/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0060/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0060/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0060/task.toml b/tasks/class-test/class-test-distractors-d1-t0060/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..1ef7063867dadd4cc2e84a5e531766c55e4953b2 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 60 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0060/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0060/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..5092570e58953cd2fcabdc1332041029b4e1deb6 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/tests/checks.json @@ -0,0 +1,144 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 1, + 10, + 16, + 24 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 0, + 11, + 14, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 5, + 28, + 14, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 9, + 19, + 6, + 17 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 3, + 34, + 0, + 14 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 7, + 9, + 9, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1", + "Q4", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0060/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0060/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0060/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0060/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0060/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d1-t0072/environment/Dockerfile b/tasks/class-test/class-test-distractors-d1-t0072/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..90af884cd72f4b34cc7c3b44a9e6681c65532427 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test. + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..fd5f53a694c4d074e26262e7ff867f86d38ca9a1 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/key.csv @@ -0,0 +1,7 @@ +item,key +Q1,C +Q2,B +Q3,B +Q4,B +Q5,A +Q6,B diff --git a/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..4602235b14e7b8e3d87d0b72fd181d8866d6b7f3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/environment/workdir/responses.csv @@ -0,0 +1,36 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6 +S337,10P,C,C,B,C,B,B +S136,10P,C,A,B,A,A,C +S230,10R,C,C,B,A,C,B +S344,10R,C,C,B,A,A,B +S295,10Q,C,A,B,A,B,B +S195,10R,C,B,C,C,A,B +S266,10P,C,B,B,B,A,C +S135,10P,C,C,B,D,A,C +S137,10Q,C,D,B,B,D,B +S198,10P,C,A,B,C,B,B +S226,10R,D,A,B,C,D,B +S089,10R,C,B,B,B,A,B +S205,10R,C,D,B,B,D,B +S258,10Q,A,A,B,C,C,B +S001,10Q,A,D,B,C,A,B +S066,10P,C,B,C,C,D,B +S375,10Q,C,B,C,C,A,D +S172,10Q,A,C,B,C,C,D +S011,10Q,C,B,B,C,B,C +S124,10P,C,C,B,C,B,D +S345,10Q,C,A,B,B,B,B +S081,10Q,D,B,B,C,A,B +S355,10Q,C,B,B,B,A,A +S380,10Q,D,A,B,A,B,B +S142,10P,C,C,B,D,A,B +S180,10P,C,B,B,D,A,C +S315,10Q,C,C,C,D,A,B +S202,10P,C,D,B,B,A,C +S208,10R,C,C,B,C,D,B +S392,10Q,D,D,B,B,B,B +S224,10R,C,C,B,B,A,B +S107,10Q,C,D,C,C,A,B +S164,10P,C,C,C,C,D,B +S029,10R,C,C,B,C,A,B +S194,10P,C,C,B,A,B,B diff --git a/tasks/class-test/class-test-distractors-d1-t0072/instruction.md b/tasks/class-test/class-test-distractors-d1-t0072/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..7bebf5bfbbcb97180cc9d1f4ef26e94e4218e8f9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/instruction.md @@ -0,0 +1,13 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 6 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q6`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d1-t0072/solution/oracle.py b/tasks/class-test/class-test-distractors-d1-t0072/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d1-t0072/solution/solve.sh b/tasks/class-test/class-test-distractors-d1-t0072/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d1-t0072/task.toml b/tasks/class-test/class-test-distractors-d1-t0072/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..b4af499e4696667435b6038394452da4963114da --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 72 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d1-t0072/tests/checks.json b/tasks/class-test/class-test-distractors-d1-t0072/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ebede940d1078b079bc67e223fc89c7b92f8fa1a --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/tests/checks.json @@ -0,0 +1,144 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 3, + 0, + 28, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 7, + 9, + 13, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 29, + 6, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 6, + 9, + 16, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 17, + 9, + 3, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 1, + 25, + 6, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q3", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q2", + "Q4" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d1-t0072/tests/test.sh b/tasks/class-test/class-test-distractors-d1-t0072/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d1-t0072/tests/verify.py b/tasks/class-test/class-test-distractors-d1-t0072/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d1-t0072/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-e0004/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-e0004/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..eb10b7c33df4b482e3fd4b5f490ae574bfe931d8 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,A +Q2,C +Q3,D +Q4,D +Q5,D +Q6,D +Q7,C +Q8,A diff --git a/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..3794dbecd4a9546c25d169ef6cdee99e9b394e3d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/environment/workdir/responses.csv @@ -0,0 +1,49 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S306,10R,B,D,D,B,D,D,,A +S249,10P,,D,D,B,A,D,D,A +S164,10P,D,D,B,D,B,D,C,D +S084,10R,C,D,D,D,D,D,C,A +S338,10P,C,D,D,D,D,A,B,A +S094,10R,A,B,B,D,D,D,B, +S398,10Q,,C,C,D,B,D,C,A +S072,10Q,C,D,B,,D,A,B,D +S253,10P,A,D,D,C,D,D,D,A +S211,10P,,C,B,D,B,A,B,A +S025,10R,D,B,D,D,D,A,B,A +S024,10P,B,C,D,D,D,A,C,A +S044,10Q,C,C,,B,D,A,C,A +S290,10P,D,A,C,D,D,D,D,D +S346,10P,B,C,D,C,A,D,D,A +S199,10P,C,A,B,D,D,D,A,A +S218,10R,A,C,D,D,,A,D,A +S255,10P,C,D,D,D,D,D,D,A +S145,10Q,D,A,D,D,D,D,D,A +S151,10P,C,D,,D,D,D,D,A +S086,10Q,C,C,B,D,D,A,C,D +S334,10R,C,C,D,B,D,A,D,A +S222,10R,C,D,D,D,A,A,B,D +S210,10P,C,D,D,,D,D,D,A +S161,10Q,A,C,D,D,,D,D,A +S181,10Q,B,D,D,C,D,A,D,D +S260,10P,C,C,D,D,D,D,D,A +S042,10Q,A,A,D,,D,D,D,C +S236,10R,D,D,,D,A,A,C,A +S191,10R,,D,B,D,A,D,C,D +S142,10Q,C,C,,D,D,D,C,A +S358,10P,B,C,D,D,D,A,D,D +S254,10R,B,D,,D,D,D,B,A +S156,10R,C,C,,D,D,A,D,A +S328,10Q,,D,C,B,D,D,, +S362,10R,A,D,D,D,D,D,,A +S382,10R,C,C,D,C,D,D,D, +S213,10R,C,C,D,B,D,D,C,A +S163,10Q,C,D,D,D,A,D,B,A +S259,10Q,C,C,D,D,D,D,C,A +S127,10Q,C,D,B,D,A,D,C, +S028,10P,A,B,C,D,,A,C,A +S216,10R,C,D,B,B,C,D,C, +S192,10R,C,C,D,D,A,A,C,D +S266,10R,C,C,D,D,D,A,D,C +S004,10P,C,D,D,D,D,A,D,D +S008,10P,D,B,C,D,A,A,D, +S215,10R,A,D,D,D,D,D,D,D diff --git a/tasks/class-test/class-test-distractors-d2-e0004/instruction.md b/tasks/class-test/class-test-distractors-d2-e0004/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-e0004/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-e0004/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-e0004/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-e0004/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-e0004/task.toml b/tasks/class-test/class-test-distractors-d2-e0004/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9559e3faccfc50bf76787c428f3eee7ed81d210e --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10004 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-e0004/tests/checks.json b/tasks/class-test/class-test-distractors-d2-e0004/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f3ce45eeca236e43d0d356e9499af935759f8dda --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/tests/checks.json @@ -0,0 +1,297 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 8, + 6, + 23, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 16.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 4, + 4, + 18, + 22 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 9, + 5, + 28 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 58.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 7, + 4, + 34 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 70.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 9, + 3, + 1, + 32 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 66.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 19, + 0, + 0, + 29 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "B", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 60.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 1, + 8, + 15, + 21 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 31.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 29, + 0, + 2, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [ + "B", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 60.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q3", + "Q4", + "Q5", + "Q6", + "Q7", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q7" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-e0004/tests/test.sh b/tasks/class-test/class-test-distractors-d2-e0004/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-e0004/tests/verify.py b/tasks/class-test/class-test-distractors-d2-e0004/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0004/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-e0016/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-e0016/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..22e34397a591116e1b26d2ff55a481b83c0d2876 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,C +Q2,A +Q3,C +Q4,B +Q5,D +Q6,B +Q7,B +Q8,A diff --git a/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..d751bcf5d6920732f84e529d6f9b1a39e5a83b2a --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/environment/workdir/responses.csv @@ -0,0 +1,73 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S332,10Q,B,A,B,A,B,A,B,A +S047,10P,D,A,D,,C,A,B,A +S286,10R,A,A,C,B,C,D,C,A +S063,10P,C,A,A,C,D,B,B,D +S056,10R,C,A,B,C,C,D,B,A +S093,10P,C,D,C,C,D,C,B,A +S089,10P,D,A,C,C,D,A,B,A +S383,10R,D,D,A,C,C,C,B,A +S224,10Q,D,B,B,B,C,C,A, +S124,10Q,D,A,C,A,D,B,B,A +S103,10P,,A,C,B,C,B,B,A +S242,10R,,A,C,B,C,C,B,D +S356,10Q,D,A,C,C,D,C,B,D +S359,10Q,D,A,C,A,D,C,D,A +S130,10Q,C,A,A,B,C,B,B,A +S179,10R,D,B,C,A,,C,B,A +S072,10R,D,A,,B,,C,B, +S217,10Q,D,A,B,B,D,C,A, +S121,10R,C,A,C,C,C,B,D,A +S142,10Q,D,A,C,A,D,C,B,A +S098,10Q,B,A,C,A,D,C,B,B +S008,10R,C,D,C,A,D,B,B,A +S044,10P,D,A,B,B,C,B,B, +S091,10R,C,D,C,C,D,C,B,A +S304,10Q,D,A,A,B,,B,B,D +S132,10R,D,,,A,D,C,B,A +S394,10Q,C,B,C,C,C,C,B,B +S399,10Q,D,A,A,B,D,D,D,A +S360,10P,A,A,B,A,,B,D,B +S245,10P,C,B,A,B,C,C,C,A +S060,10P,C,A,C,B,,C,D,A +S096,10R,B,A,C,C,C,B,B,A +S209,10R,D,A,C,B,C,C,B,A +S259,10P,A,A,C,A,D,C,B,D +S007,10Q,D,A,B,B,C,D,B,A +S195,10P,D,A,A,B,D,C,B,A +S018,10Q,,D,A,B,C,A,B,A +S267,10Q,D,A,A,B,C,C,C, +S123,10Q,,A,,C,D,B,B,D +S014,10P,D,B,C,C,C,C,D, +S194,10Q,D,A,C,A,,D,B,D +S077,10R,D,B,C,C,C,D,D,D +S202,10R,A,B,C,A,,C,D,A +S017,10Q,D,A,C,A,C,C,B,A +S336,10Q,C,A,C,B,C,C,B,A +S296,10Q,C,A,C,C,D,B,B,A +S133,10Q,D,A,C,C,,C,B,A +S151,10Q,B,A,C,B,D,C,B,D +S326,10R,C,A,D,C,D,C,D,A +S283,10P,C,D,B,B,D,A,B,D +S079,10R,D,B,C,C,,B,D,A +S237,10P,D,D,B,C,D,C,B,A +S053,10Q,D,A,A,,C,C,D,A +S227,10R,D,A,C,B,C,B,B,A +S166,10R,D,D,C,B,D,C,D,A +S262,10Q,C,A,A,B,D,C,B,A +S316,10R,D,A,B,C,D,B,B,A +S120,10R,A,D,A,B,,B,B,B +S191,10R,D,A,A,C,D,C,B,A +S108,10R,D,A,C,B,D,C,B,A +S292,10R,D,A,B,B,,B,A,B +S312,10R,A,A,D,B,C,A,D,A +S126,10P,D,A,C,A,C,B,C,A +S361,10R,B,A,A,B,D,C,B,A +S069,10R,C,A,C,B,D,C,B,A +S305,10Q,,A,C,C,C,A,D,B +S295,10R,A,D,C,C,C,A,A,D +S344,10P,C,A,C,B,C,B,B,B +S197,10Q,D,A,C,C,D,B,B,A +S075,10Q,D,B,A,B,D,C,B,A +S334,10Q,D,A,C,C,C,D,A,A +S114,10P,A,A,A,B,D,C,B,B diff --git a/tasks/class-test/class-test-distractors-d2-e0016/instruction.md b/tasks/class-test/class-test-distractors-d2-e0016/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-e0016/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-e0016/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-e0016/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-e0016/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-e0016/task.toml b/tasks/class-test/class-test-distractors-d2-e0016/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..df3ef18fcf45027b5dde4c8c83335dcfd030e591 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10016 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-e0016/tests/checks.json b/tasks/class-test/class-test-distractors-d2-e0016/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..427b638d23db618fad01c66b4671203f94a2c9ee --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/tests/checks.json @@ -0,0 +1,292 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 8, + 5, + 17, + 37 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 23.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 52, + 9, + 0, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 72.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 16, + 11, + 39, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 54.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 14, + 32, + 24, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 44.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 0, + 1, + 29, + 31 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "A", + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 43.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 8, + 20, + 37, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 27.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 5, + 49, + 4, + 14 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 68.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 47, + 8, + 0, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 65.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q4", + "Q5", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-e0016/tests/test.sh b/tasks/class-test/class-test-distractors-d2-e0016/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-e0016/tests/verify.py b/tasks/class-test/class-test-distractors-d2-e0016/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-e0016/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0004/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0004/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..51619e9d6499923a59288b68c0a93ff90b397e14 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,D +Q2,D +Q3,D +Q4,A +Q5,D +Q6,B +Q7,A +Q8,C diff --git a/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..0cbacd3eeacadd562ec779bfd23068ea46cbf445 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/environment/workdir/responses.csv @@ -0,0 +1,58 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S087,10Q,A,D,,D,A,B,C,D +S396,10P,D,A,D,A,C,A,A,D +S023,10P,D,D,,A,D,B,A,D +S367,10R,D,B,D,A,D,B,A,C +S061,10Q,B,D,C,A,D,B,A,D +S045,10R,A,A,,A,D,B,C,C +S058,10P,A,A,C,D,B,B,C,C +S041,10Q,A,A,C,A,D,B,D,B +S025,10Q,A,D,D,A,D,C,D,C +S135,10R,D,A,B,D,D,D,C,D +S245,10R,D,A,B,A,D,B,A,D +S258,10P,D,D,D,,B,A,A,D +S243,10P,D,C,,,D,B,A,C +S370,10Q,D,A,D,A,D,B,D,D +S186,10P,B,D,D,C,D,B,C,D +S382,10R,D,D,D,,,B,A,B +S345,10Q,D,D,D,A,B,A,A,C +S093,10Q,B,D,C,A,D,C,D,A +S234,10P,D,D,,C,A,D,A,B +S210,10P,C,A,,A,D,B,A,D +S198,10R,D,A,D,,A,B,A,D +S152,10Q,D,D,D,A,D,B,A,D +S241,10Q,A,D,D,A,D,B,A,B +S157,10P,D,D,B,C,D,B,A,B +S099,10Q,A,D,C,A,B,B,A,C +S227,10P,D,D,C,C,B,B,A,B +S014,10R,D,A,D,B,D,B,D,C +S013,10Q,A,D,,A,B,A,A,A +S172,10P,C,D,D,A,D,B,C,D +S256,10P,D,D,C,A,D,,C,A +S019,10R,D,D,D,D,D,A,A,D +S263,10Q,B,A,D,C,A,A,D,D +S159,10P,A,D,B,A,A,C,A,B +S379,10Q,D,A,D,A,A,B,C,D +S080,10Q,D,D,C,A,D,C,,B +S230,10P,D,D,C,A,D,B,B,D +S338,10Q,D,D,C,,A,A,A,D +S248,10P,D,D,C,A,A,D,A,D +S144,10R,D,C,C,A,C,B,C,D +S054,10R,D,D,D,,D,B,A,C +S158,10P,B,D,D,B,D,B,A,D +S031,10Q,D,D,B,A,D,B,A,D +S170,10R,D,D,D,D,A,D,A,D +S162,10R,D,D,D,C,D,B,C,A +S181,10Q,D,D,B,A,D,C,C,D +S202,10Q,D,A,C,D,D,C,A,D +S107,10R,D,D,D,,A,B,C,D +S071,10R,D,D,D,A,A,B,A,D +S195,10R,D,D,D,C,D,B,A,B +S342,10Q,D,A,C,A,D,B,C,C +S315,10R,A,A,B,B,B,B,A,D +S076,10R,D,D,C,A,A,D,C,D +S347,10Q,D,B,B,D,B,A,C,C +S391,10R,D,D,D,D,,B,A,A +S340,10R,D,B,D,A,D,D,A,D +S116,10Q,A,A,B,D,B,B,A,C +S201,10R,C,D,D,A,D,,A,D diff --git a/tasks/class-test/class-test-distractors-d2-t0004/instruction.md b/tasks/class-test/class-test-distractors-d2-t0004/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0004/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0004/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0004/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0004/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0004/task.toml b/tasks/class-test/class-test-distractors-d2-t0004/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9ae870acfab0feae6a57bd989933e4af36fb951d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 4 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0004/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0004/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..512dad4a4da9053f58b3913f1de6ebeffacf62e2 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/tests/checks.json @@ -0,0 +1,287 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 11, + 5, + 3, + 38 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 66.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 16, + 3, + 2, + 36 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 63.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 9, + 15, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 45.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 31, + 3, + 7, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 54.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 12, + 9, + 2, + 32 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 56.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 8, + 35, + 6, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 61.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 34, + 1, + 15, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 59.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 5, + 9, + 12, + 31 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 21.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q5", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0004/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0004/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0004/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0004/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0004/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0016/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0016/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..4a9d436773620d8a6931a3488005aac1ea405739 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,C +Q2,C +Q3,B +Q4,B +Q5,A +Q6,A +Q7,A +Q8,C diff --git a/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..9b1875e674c931c8e05dfe3b2de285cd5bf8d62b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/environment/workdir/responses.csv @@ -0,0 +1,76 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S284,10P,C,C,A,D,A,A,D,B +S043,10R,C,B,B,B,A,,A,A +S020,10R,D,C,B,B,C,B,C,C +S073,10P,D,C,B,C,A,A,D,C +S109,10R,C,A,D,B,D,B,D,A +S147,10P,D,C,D,D,B,B,C,B +S045,10Q,C,B,A,B,A,B,A,A +S207,10P,A,B,B,B,A,D,A, +S320,10P,C,B,D,B,A,D,A,C +S278,10R,B,C,A,C,D,D,,C +S079,10Q,C,B,C,C,A,A,C,B +S315,10R,C,C,D,B,C,D,A,C +S176,10R,C,B,,B,A,D,D, +S270,10P,C,C,D,B,A,D,A,A +S319,10R,C,A,D,C,C,D,C,C +S396,10Q,C,B,B,B,B,A,,C +S129,10R,C,A,A,B,A,D,C,C +S009,10Q,A,A,D,B,A,D,B,C +S004,10R,C,B,,B,A,B,A,C +S205,10P,C,C,D,,A,A,D,B +S272,10Q,C,,D,C,A,D,A,C +S113,10R,C,C,B,B,A,A,C,C +S172,10Q,C,A,B,C,A,D,A,C +S094,10Q,C,C,D,B,A,D,A,A +S351,10P,D,C,D,B,A,A,A,C +S338,10R,C,B,C,B,A,D,C,C +S353,10Q,C,C,A,C,A,D,A,C +S201,10Q,A,C,B,B,A,C,A,B +S083,10R,C,C,D,B,C,D,A,A +S310,10R,C,,B,B,A,A,,C +S052,10Q,C,C,C,C,C,A,B,C +S304,10R,C,,D,D,A,A,A,A +S076,10R,C,A,D,C,A,,A,C +S030,10R,A,C,D,,A,D,C,B +S379,10P,D,,,D,A,D,A,C +S360,10Q,A,B,A,B,A,D,A,C +S226,10Q,C,A,A,B,A,D,A,C +S391,10P,C,C,B,B,A,D,A,C +S398,10R,A,A,B,B,C,A,C,C +S105,10Q,B,C,B,C,B,B,C,B +S198,10R,C,A,B,B,C,A,B,B +S342,10P,D,C,D,D,A,B,B,B +S008,10Q,C,C,B,B,C,A,A,A +S022,10P,C,C,D,D,A,C,A,C +S335,10Q,C,C,A,B,A,B,A,C +S294,10R,A,C,D,B,C,D,A,C +S258,10R,C,C,D,B,A,C,A,A +S065,10R,B,C,D,,D,,A,B +S192,10R,A,C,B,,A,,A,C +S151,10P,D,C,D,B,C,B,A,C +S221,10Q,,C,C,B,C,D,A,B +S346,10Q,C,C,D,B,A,D,C,C +S087,10P,D,,B,D,A,D,A,C +S212,10R,D,A,A,D,B,D,,C +S337,10P,C,C,B,C,A,D,,A +S374,10R,A,C,D,B,A,A,A,B +S375,10P,C,A,A,B,A,D,,C +S137,10P,D,A,B,,A,B,,C +S369,10P,B,B,A,B,A,C,A,B +S018,10Q,C,C,D,B,B,C,C,A +S237,10R,C,C,D,B,A,C,D,B +S329,10R,C,C,D,B,B,A,A,C +S216,10Q,C,C,,B,A,C,A,B +S322,10R,C,C,D,,A,,A,B +S364,10Q,C,A,B,B,B,A,C,C +S058,10R,C,A,D,D,A,D,B,C +S291,10Q,C,B,D,B,B,B,D, +S155,10P,C,B,B,C,C,D,B,B +S133,10P,C,C,B,B,A,C,A,C +S131,10R,C,,B,B,A,D,A,C +S200,10P,A,C,D,B,C,A,D,A +S202,10R,C,A,D,B,B,A,A,C +S274,10P,,A,D,C,A,D,A,B +S293,10R,B,C,D,B,C,A,A,C +S390,10P,C,A,D,B,A,B,C,A diff --git a/tasks/class-test/class-test-distractors-d2-t0016/instruction.md b/tasks/class-test/class-test-distractors-d2-t0016/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0016/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0016/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0016/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0016/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0016/task.toml b/tasks/class-test/class-test-distractors-d2-t0016/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3fb149e1c4e2ddc86970c384e0051d8b650fb032 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 16 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0016/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0016/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f9352d93ba404f0eb81d4c4961590feb2c4ce34a --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/tests/checks.json @@ -0,0 +1,288 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 10, + 5, + 48, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 64.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 17, + 13, + 39, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 52.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 11, + 22, + 4, + 34 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 29.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 47, + 13, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 62.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 49, + 9, + 14, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 65.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 19, + 12, + 8, + 31 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 25.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 40, + 6, + 14, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 53.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 13, + 18, + 41, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q2", + "Q4", + "Q5", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0016/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0016/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0016/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0016/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0016/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0028/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0028/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..f19ec6cf323fea318fd29c8d81bf8dfce9735180 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,D +Q2,C +Q3,A +Q4,B +Q5,A +Q6,C +Q7,A +Q8,B diff --git a/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..b258e35b79bdc7eef75b2b665b681f283f20d089 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/environment/workdir/responses.csv @@ -0,0 +1,92 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S371,10R,D,D,D,B,C,C,D,B +S028,10Q,D,B,B,A,A,C,A,B +S172,10Q,D,C,D,B,B,C,,B +S259,10R,C,C,C,B,A,C,A,B +S345,10P,C,B,C,B,A,C,A,B +S073,10Q,D,B,D,A,A,C,A,D +S182,10Q,C,B,D,B,A,C,A,B +S352,10Q,D,C,D,B,A,C,B,B +S302,10P,B,C,A,B,A,C,A,C +S387,10R,C,D,D,B,A,C,D,B +S120,10R,D,B,D,B,C,C,A,B +S305,10P,D,C,A,B,A,C,D,B +S289,10R,D,C,D,B,A,B,A, +S035,10P,C,C,A,D,B,D,C,C +S360,10Q,D,D,D,B,A,C,A,C +S256,10R,D,D,D,B,A,C,B,B +S290,10R,D,B,D,A,C,D,D,B +S113,10Q,C,C,B,B,C,C,D,C +S080,10Q,D,C,A,D,A,C,A,B +S186,10Q,D,D,D,B,C,B,A,B +S349,10Q,D,C,D,B,C,C,A,B +S209,10R,A,C,C,A,B,C,C,B +S134,10R,C,C,D,A,A,C,A,B +S261,10P,D,B,B,B,A,A,A,B +S284,10Q,D,C,A,B,A,C,B,B +S399,10Q,D,C,D,D,B,C,A,D +S376,10Q,D,D,D,D,A,B,D,C +S058,10Q,D,B,D,A,A,C,C,B +S015,10P,D,C,C,B,B,C,A,B +S118,10R,C,C,A,D,A,C,A,B +S183,10R,C,C,B,C,B,C,A,B +S092,10Q,D,C,D,A,B,C,C,B +S233,10P,B,C,D,B,B,C,B,B +S347,10P,D,C,A,A,A,C,B,B +S300,10Q,B,C,B,D,A,B,B, +S034,10Q,D,D,D,B,A,C,A,B +S327,10P,C,C,D,B,B,C,D,B +S238,10Q,B,C,A,D,A,D,B,B +S061,10Q,C,C,A,B,A,C,A,B +S230,10Q,C,C,D,B,B,C,B,B +S297,10Q,D,C,A,B,A,C,A,B +S216,10Q,D,C,D,C,C,C,A,B +S091,10R,D,B,C,B,A,C,B,C +S075,10Q,D,D,,C,B,C,B,B +S025,10Q,D,C,D,C,C,D,A, +S220,10P,D,C,A,B,B,B,A,D +S361,10Q,D,D,,B,A,C,A,B +S106,10P,C,C,B,B,B,B,A,C +S150,10P,D,D,D,B,A,D,A,B +S029,10P,D,C,D,D,A,C,B,B +S027,10R,D,C,B,B,A,C,A,B +S287,10P,B,C,A,B,A,B,D, +S307,10R,D,B,A,B,A,B,A,C +S328,10R,C,B,,C,,C,C,B +S232,10P,D,C,A,B,C,C,A,B +S069,10P,C,B,B,C,B,C,A, +S022,10Q,D,B,D,B,A,C,C,B +S140,10R,B,C,D,B,C,C,A,D +S017,10Q,D,C,D,B,C,D,B,B +S311,10P,D,C,A,D,A,D,C,B +S231,10Q,D,C,D,C,B,B,B,B +S202,10R,D,C,C,B,C,C,A,B +S293,10R,D,D,A,C,A,C,C,B +S050,10P,D,B,D,D,A,D,A,D +S358,10Q,B,B,C,B,A,C,B,B +S207,10P,D,,A,D,A,C,D,B +S329,10Q,C,C,D,B,A,C,C,B +S043,10R,D,D,D,B,A,C,A,B +S122,10P,D,B,B,C,C,C,C,B +S030,10P,C,C,B,B,A,B,A,B +S109,10Q,D,C,A,B,C,C,A,B +S128,10R,D,C,A,D,A,D,A,B +S165,10R,C,C,B,B,A,C,A,C +S127,10Q,D,D,A,B,B,B,A,B +S332,10Q,C,C,A,B,A,C,D,B +S036,10R,C,D,D,C,A,C,A,B +S342,10P,D,B,A,B,A,B,D,B +S083,10P,D,B,D,B,A,C,D,B +S062,10P,D,D,C,B,,C,A,D +S154,10R,B,B,C,C,A,D,A,D +S212,10R,C,C,D,B,A,C,B,B +S322,10R,C,C,D,B,A,C,A,B +S194,10P,C,B,D,A,B,C,C,B +S068,10P,D,C,D,B,A,B,B,B +S372,10R,D,B,B,B,A,D,C,C +S142,10P,C,C,A,A,B,C,A,B +S152,10R,D,B,D,A,B,D,A,B +S263,10R,D,C,D,D,A,B,B,B +S090,10Q,C,D,D,D,A,C,A, +S074,10Q,B,D,C,B,A,C,A,B +S223,10Q,C,B,A,D,A,C,B,B diff --git a/tasks/class-test/class-test-distractors-d2-t0028/instruction.md b/tasks/class-test/class-test-distractors-d2-t0028/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0028/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0028/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0028/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0028/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0028/task.toml b/tasks/class-test/class-test-distractors-d2-t0028/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..0b552e30d28fdbbcffca666f7e9fcf834f00ae48 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 28 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0028/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0028/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ec7a3f7a0c605018be3648614ed6d509c6c89b19 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/tests/checks.json @@ -0,0 +1,290 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 1, + 9, + 26, + 55 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 60.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 0, + 23, + 50, + 17 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 54.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 24, + 12, + 10, + 42 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 26.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 11, + 54, + 11, + 15 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 59.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 56, + 19, + 14, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 61.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 1, + 14, + 64, + 12 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 70.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 48, + 18, + 12, + 12 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 52.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 0, + 68, + 10, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 74.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q5", + "Q6", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0028/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0028/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0028/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0028/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0028/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0040/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0040/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..ffda8d22403b75fc2bf1492c776087a5336ef5f0 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,B +Q2,A +Q3,B +Q4,A +Q5,B +Q6,A +Q7,B +Q8,C diff --git a/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..b1e6bfd38b7c6050fa98c8dff1a3eb3f07fcca01 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/environment/workdir/responses.csv @@ -0,0 +1,69 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S325,10Q,D,B,B,B,C,C,D,C +S169,10P,B,A,B,C,C,A,,C +S246,10P,B,D,B,A,C,C,A,B +S332,10Q,B,A,B,A,A,D,B,C +S366,10R,B,C,B,C,C,D,B,C +S231,10R,D,A,B,B,D,,B,C +S186,10Q,B,A,D,C,C,D,B,C +S162,10Q,D,A,D,C,C,A,A,A +S324,10Q,D,A,D,B,A,A,A,C +S322,10P,B,A,B,A,B,D,B,C +S356,10P,B,B,B,B,B,D,,A +S285,10Q,D,A,B,B,C,C,B,C +S064,10R,D,D,B,A,B,C,A,A +S308,10P,C,A,B,A,C,A,C,D +S031,10R,B,B,B,C,C,A,A,A +S193,10R,C,A,B,B,C,D,B,D +S191,10Q,D,B,B,A,A,D,B,D +S361,10Q,B,A,B,A,A,C,B,C +S281,10P,B,A,B,A,B,A,B,C +S123,10Q,D,A,B,B,C,A,B,B +S152,10R,B,A,D,B,B,A,B,C +S038,10Q,C,B,B,A,B,A,B,D +S354,10R,D,A,D,B,C,A,B,C +S358,10R,D,A,D,A,A,C,B,C +S037,10P,D,A,A,B,C,A,,C +S250,10P,B,A,D,B,,D,B,C +S120,10P,B,A,D,B,C,D,C,B +S217,10R,B,A,D,A,C,A,C,C +S376,10R,C,A,B,B,C,A,A,C +S316,10R,B,A,D,B,C,A,B,C +S067,10Q,D,D,D,B,B,A,,B +S196,10P,B,A,B,B,C,D,B,C +S004,10R,D,A,B,C,B,D,B,C +S216,10Q,A,A,D,A,A,A,B,C +S185,10Q,C,B,B,A,C,A,B,C +S127,10P,C,A,D,A,,D,B,C +S254,10R,B,A,B,B,D,D,B,A +S300,10P,B,A,B,D,C,A,B,C +S065,10R,C,A,B,C,B,D,C,B +S393,10Q,B,A,B,B,C,A,B,D +S370,10P,B,A,B,D,C,C,C,C +S225,10Q,B,D,D,B,B,,D,C +S373,10P,B,A,B,A,B,D,B,D +S277,10P,B,A,B,A,B,D,B,A +S115,10P,B,A,B,B,C,A,,C +S014,10R,B,B,B,D,A,A,B,C +S108,10Q,C,B,B,B,A,D,B,C +S346,10Q,B,A,D,C,C,,D,C +S243,10Q,C,A,D,B,C,A,B,D +S337,10Q,B,A,B,A,C,,C,C +S375,10R,B,A,B,B,C,A,B,C +S380,10P,B,B,D,C,C,C,B,C +S221,10R,C,B,D,B,B,A,B,C +S241,10R,B,A,A,A,B,C,C,C +S283,10P,B,A,B,B,C,,A,C +S286,10R,D,A,B,B,C,A,B,C +S078,10R,B,C,D,C,C,A,C,C +S271,10P,B,A,B,B,B,D,C,D +S329,10R,B,A,B,A,C,D,A,C +S291,10P,B,A,A,A,C,C,A,C +S323,10R,B,A,D,B,C,D,B,B +S341,10Q,C,A,B,C,C,A,C,C +S106,10Q,B,A,B,B,D,D,A,B +S034,10P,B,D,B,D,B,C,A,C +S081,10Q,B,A,B,C,B,A,B,C +S349,10Q,B,A,B,A,B,A,C,D +S289,10Q,B,A,D,B,C,A,B,C +S133,10Q,B,B,B,B,C,A,,D diff --git a/tasks/class-test/class-test-distractors-d2-t0040/instruction.md b/tasks/class-test/class-test-distractors-d2-t0040/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0040/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0040/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0040/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0040/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0040/task.toml b/tasks/class-test/class-test-distractors-d2-t0040/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..4fe72fa9337f0911fc43ff266b5778fa987a0cf7 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 40 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0040/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0040/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..c3ce3ee3372af431ed7c3db962303ac6c5fe7df6 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/tests/checks.json @@ -0,0 +1,295 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 1, + 42, + 11, + 14 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 61.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 50, + 11, + 2, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 73.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 3, + 44, + 0, + 21 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 64.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 21, + 31, + 12, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 30.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 8, + 18, + 37, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 26.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 31, + 0, + 11, + 21 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 45.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 11, + 37, + 11, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 54.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 6, + 7, + 45, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 66.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q3", + "Q5", + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0040/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0040/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0040/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0040/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0040/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0052/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0052/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..d27821faffcbed9c282773d202eab93ff10db93b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,B +Q2,D +Q3,A +Q4,D +Q5,D +Q6,A +Q7,B +Q8,A diff --git a/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..5231044e48be351a3c3aeba4cf1571f761a07e6c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/environment/workdir/responses.csv @@ -0,0 +1,92 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S387,10Q,A,C,,C,D,B,B,D +S088,10R,B,D,A,C,D,C,B,B +S363,10P,A,D,A,C,B,D,B,C +S295,10Q,C,D,A,C,D,,B,D +S275,10R,A,D,D,D,D,C,D,B +S239,10Q,D,D,A,D,D,A,C,A +S098,10Q,D,D,A,D,,B,B,C +S328,10R,A,,B,D,D,C,B,C +S385,10P,A,C,D,D,D,B,B,D +S255,10R,A,D,B,B,D,B,B,B +S298,10P,A,D,,C,D,A,B,D +S313,10R,B,A,A,D,B,A,B,D +S089,10P,B,A,D,D,D,B,B,B +S074,10P,A,D,A,C,D,A,B,B +S392,10R,A,D,,D,B,A,B,A +S134,10R,A,D,A,B,D,B,A,A +S253,10Q,,D,A,C,,D,D,B +S125,10Q,A,D,A,C,,B,B,B +S191,10P,D,D,A,C,A,B,B,A +S336,10Q,A,B,A,C,D,D,B,A +S049,10R,A,C,A,C,D,D,B,A +S209,10Q,B,D,A,D,D,A,D,C +S071,10P,B,D,A,D,D,B,D,A +S349,10R,B,D,D,D,A,A,B,D +S158,10P,A,D,A,D,D,A,A,D +S273,10P,A,B,A,D,D,B,B,B +S023,10R,A,D,A,B,B,B,B,B +S176,10R,C,D,D,C,D,,B,D +S380,10Q,A,D,B,C,D,B,B,C +S291,10Q,A,D,B,C,D,A,D,A +S050,10P,C,B,A,C,D,B,B,C +S020,10R,A,D,A,D,B,D,B,B +S003,10Q,D,D,B,D,B,A,A,A +S231,10Q,B,A,D,D,A,A,B,B +S103,10R,,D,D,D,D,,C,A +S244,10P,A,D,B,D,D,A,B,C +S034,10Q,B,B,A,C,D,,B,A +S022,10Q,A,D,,D,D,B,B,B +S141,10P,A,D,B,B,D,D,B,B +S317,10R,A,D,A,D,D,A,B,A +S019,10Q,B,A,A,D,C,B,B,A +S283,10R,D,D,A,B,,B,D,B +S128,10P,A,D,D,D,D,B,B,B +S351,10Q,A,D,A,C,D,B,B,B +S199,10Q,A,D,A,D,D,D,B,D +S001,10R,B,C,A,D,,B,B,D +S357,10Q,B,D,A,B,B,B,B,A +S218,10Q,,D,,C,C,B,D,B +S330,10Q,D,D,A,D,B,D,A,B +S155,10R,A,D,A,D,D,B,B,B +S398,10Q,A,C,A,D,B,B,D,B +S013,10R,A,C,A,B,D,B,D, +S381,10P,D,D,A,D,D,B,A,C +S060,10P,A,D,A,D,D,B,B,B +S326,10P,A,D,D,D,D,C,B,D +S180,10R,A,A,D,C,D,B,D,A +S367,10R,B,C,B,C,B,B,B, +S260,10P,B,D,,D,D,B,D, +S316,10P,,A,A,D,D,A,B,B +S319,10Q,D,D,A,B,D,B,B,A +S261,10P,B,D,A,D,D,B,C,A +S301,10R,A,D,B,C,D,B,B,B +S272,10R,B,D,B,D,A,B,B, +S347,10R,A,,B,D,D,A,B,A +S350,10R,A,D,A,D,A,B,B,D +S290,10R,A,A,A,D,D,B,B,C +S202,10R,B,D,D,B,B,B,B,D +S032,10Q,A,D,A,B,D,B,B,B +S396,10R,B,D,A,C,B,C,C,B +S171,10Q,A,D,A,D,D,B,B,A +S248,10Q,A,D,A,C,D,B,B,C +S036,10Q,B,D,A,C,D,C,B,D +S132,10P,B,,B,B,D,B,C,D +S055,10P,D,A,D,C,D,B,B,B +S116,10P,B,D,,B,D,A,B,D +S252,10P,A,D,A,C,D,B,B,B +S391,10R,A,D,D,B,B,B,B,A +S311,10R,A,D,D,B,D,B,B,B +S024,10P,,D,D,C,,B,B,A +S014,10P,A,D,A,D,,,B,C +S314,10R,A,,A,B,B,A,D,B +S222,10R,A,D,D,D,D,A,B,B +S161,10R,A,,A,B,D,B,B,A +S111,10Q,B,B,A,B,B,B,B,B +S344,10R,B,D,A,B,B,B,A,B +S333,10P,A,D,B,D,A,A,A,C +S226,10P,A,B,B,D,D,A,B,B +S268,10R,B,D,A,B,B,B,C,B +S207,10Q,B,,A,C,D,B,B,C +S053,10Q,A,D,A,B,D,B,D,D +S216,10P,B,D,B,D,D,B,B,B diff --git a/tasks/class-test/class-test-distractors-d2-t0052/instruction.md b/tasks/class-test/class-test-distractors-d2-t0052/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0052/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0052/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0052/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0052/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0052/task.toml b/tasks/class-test/class-test-distractors-d2-t0052/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..e3e29a2a56a0cb625db2e91d0fb499d95c899213 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 52 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0052/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0052/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..548ba9cc4e75dfd41b8ba849738a81e89b361182 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/tests/checks.json @@ -0,0 +1,289 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 49, + 25, + 3, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 27.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 8, + 6, + 7, + 64 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 70.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 53, + 15, + 0, + 16 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 58.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 20, + 28, + 43 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 47.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 6, + 17, + 2, + 59 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 64.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 20, + 52, + 6, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 22.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 7, + 65, + 6, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 71.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 22, + 35, + 13, + 17 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 24.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q3", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1", + "Q6", + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0052/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0052/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0052/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0052/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0052/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0064/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0064/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..149fc107726a76952072fee2e3efb521953e0c8b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,D +Q2,D +Q3,A +Q4,C +Q5,B +Q6,B +Q7,D +Q8,C diff --git a/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..c02b813483a6d98609eedac311ec022c5276410d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/environment/workdir/responses.csv @@ -0,0 +1,70 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S087,10R,D,,A,B,B,B,D,D +S323,10P,A,D,A,C,B,D,D,B +S134,10P,D,C,A,D,B,B,D,C +S359,10Q,C,D,A,D,B,B,B,D +S156,10P,D,C,B,C,D,B,D,C +S227,10P,B,D,B,D,D,D,, +S309,10Q,B,D,C,D,B,D,A,C +S230,10R,A,B,C,D,B,D,D,C +S388,10Q,D,C,D,C,B,A,D,D +S063,10P,C,D,D,D,A,B,C, +S123,10Q,C,D,D,C,B,B,A,B +S342,10P,A,C,A,C,B,B,B,B +S294,10R,A,C,A,D,B,B,B,C +S185,10Q,B,,,,A,B,B,B +S044,10P,B,B,A,B,D,B,B,C +S089,10Q,C,B,A,D,B,B,A,D +S184,10P,D,C,,B,D,D,C,C +S223,10P,D,D,A,D,C,A,D,C +S287,10Q,C,,D,D,D,A,A,C +S392,10Q,A,B,A,,B,B,A, +S014,10Q,,,A,B,B,A,C,C +S015,10P,A,D,A,C,B,D,A,B +S143,10Q,,D,A,C,D,D,B,C +S358,10R,B,B,A,D,B,B,D,D +S106,10Q,C,,B,B,A,D,D,C +S038,10R,A,C,A,B,B,B,D,B +S187,10R,B,B,A,C,B,D,D,C +S073,10Q,D,C,A,C,B,B,A,C +S121,10R,A,,A,D,B,A,B,B +S322,10R,B,D,B,B,B,B,A,D +S052,10P,C,B,B,,A,B,B,C +S236,10Q,D,,A,B,B,D,D,D +S193,10Q,D,D,B,D,D,D,B, +S055,10P,A,D,A,C,B,A,D,C +S265,10P,B,D,C,B,B,B,A,C +S331,10Q,B,D,,B,B,D,A,C +S234,10Q,A,D,A,A,B,B,C, +S238,10R,B,D,A,C,B,B,D,B +S195,10R,A,B,A,D,B,A,D,C +S293,10P,B,B,A,D,C,D,B,D +S059,10R,B,D,C,B,B,B,A,B +S243,10Q,C,B,A,D,B,A,D,D +S262,10Q,C,D,C,A,B,A,A, +S042,10P,C,D,D,D,B,B,A,C +S297,10P,D,D,A,C,B,,D,C +S003,10R,B,C,B,C,B,B,D,C +S040,10P,D,D,A,C,B,D,,C +S298,10R,B,D,C,D,C,D,C, +S152,10Q,C,C,A,D,B,B,B,C +S370,10R,A,C,C,C,B,,C,C +S332,10P,,B,A,C,B,D,A,B +S228,10R,C,D,A,A,B,D,A,C +S254,10P,A,B,A,C,C,B,A,B +S008,10P,A,D,A,,D,D,A,C +S046,10Q,C,B,A,B,D,D,D,D +S283,10Q,B,C,,C,C,D,A,C +S338,10P,B,D,B,D,B,B,C,B +S397,10P,C,D,A,B,B,B,D,D +S315,10R,C,,A,C,A,B,C,C +S149,10P,A,C,C,B,B,B,C,B +S247,10P,B,C,A,A,B,,A,C +S205,10R,A,D,A,B,B,B,D,B +S053,10R,A,D,D,D,D,B,D,C +S137,10P,B,D,B,D,D,,D,C +S387,10P,D,B,C,C,B,,B, +S058,10P,B,D,A,C,D,B,A,C +S274,10P,C,D,A,,D,D,A,D +S139,10P,B,B,A,D,C,B,A,B +S366,10P,C,B,,D,B,A,D,D diff --git a/tasks/class-test/class-test-distractors-d2-t0064/instruction.md b/tasks/class-test/class-test-distractors-d2-t0064/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0064/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0064/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0064/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0064/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0064/task.toml b/tasks/class-test/class-test-distractors-d2-t0064/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..16cc314ebd5e3e54ab37097de1dce4325751109e --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 64 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0064/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0064/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..2b20c384ed0c2e7ce03c24112c6804672aaa8872 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/tests/checks.json @@ -0,0 +1,285 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 17, + 20, + 17, + 12 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 17.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 0, + 16, + 14, + 31 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 44.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 40, + 9, + 9, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 58.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 4, + 15, + 21, + 24 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 30.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 5, + 45, + 6, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 65.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 10, + 33, + 0, + 21 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 47.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 22, + 12, + 9, + 24 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 34.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 0, + 15, + 33, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 47.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q2", + "Q6", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1", + "Q4" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0064/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0064/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0064/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0064/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0064/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d2-t0076/environment/Dockerfile b/tasks/class-test/class-test-distractors-d2-t0076/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..07c1492d285a0578da7818bd332bdcf1744dde9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/distractor_notes.md @@ -0,0 +1,7 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. diff --git a/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..b77166ee6f83225d6893058198715827b88602d7 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/key.csv @@ -0,0 +1,9 @@ +item,key +Q1,B +Q2,D +Q3,C +Q4,A +Q5,D +Q6,C +Q7,B +Q8,D diff --git a/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..ed17e72c48ce48634849c1b36aa521c41b2fd01a --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/environment/workdir/responses.csv @@ -0,0 +1,51 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S129,10R,B,A,C,A,B,C,A,D +S229,10P,B,D,B,A,D,C,B,D +S180,10P,D,C,C,A,B,C,D,D +S181,10R,B,A,C,,D,C,A,B +S118,10Q,B,A,C,A,D,D,B,D +S215,10R,B,B,D,B,D,C,D,D +S396,10Q,A,B,C,D,D,C,B,D +S282,10R,B,D,B,D,D,C,B,D +S095,10P,B,B,D,D,D,C,B,B +S154,10Q,B,C,B,A,D,C,A,D +S037,10R,B,B,B,A,D,C,B,D +S274,10Q,A,D,D,A,D,C,C,D +S368,10Q,D,B,D,D,D,D,B,B +S342,10R,B,B,C,A,,C,A,A +S211,10R,D,D,C,A,D,C,D,A +S346,10P,D,B,C,D,D,C,B,D +S080,10P,B,B,C,A,A,C,B,D +S347,10Q,B,B,B,,,C,C,B +S217,10P,B,B,C,A,D,D,C,D +S096,10R,A,D,B,B,D,C,B,D +S102,10R,A,D,C,A,D,D,B,D +S397,10P,A,B,C,D,D,C,B,C +S166,10P,A,B,C,A,B,C,B,D +S157,10R,D,D,C,D,B,C,B,A +S385,10Q,B,D,C,B,B,C,B,D +S059,10R,D,B,B,B,D,C,B,D +S265,10R,B,B,C,B,D,D,B,D +S127,10Q,D,B,C,D,D,C,B,B +S066,10P,D,D,D,D,C,,B,D +S353,10Q,B,A,C,A,A,D,B,D +S093,10Q,A,D,C,A,D,C,B,B +S196,10R,B,B,B,A,C,D,B,D +S081,10R,B,B,C,D,,B,B,D +S010,10P,,A,C,A,D,C,B,D +S253,10Q,B,B,C,B,D,C,B,D +S035,10P,D,B,B,D,D,D,D,D +S223,10P,B,B,C,D,D,D,B,D +S009,10Q,A,B,C,B,D,C,C,D +S021,10Q,B,D,C,A,D,C,B,D +S079,10Q,B,A,C,A,D,D,B,D +S007,10Q,B,B,B,A,C,D,B,D +S315,10Q,B,B,C,D,D,C,B,D +S098,10Q,B,A,B,A,A,C,B,B +S117,10R,D,D,D,A,A,D,B,D +S262,10R,B,B,C,A,D,D,D,D +S237,10Q,B,B,D,D,D,C,B,C +S317,10P,B,D,D,,D,C,D,B +S256,10R,D,D,D,A,B,C,B,A +S276,10P,B,B,D,A,,D,A,D +S125,10Q,A,B,D,D,D,D,D,C diff --git a/tasks/class-test/class-test-distractors-d2-t0076/instruction.md b/tasks/class-test/class-test-distractors-d2-t0076/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..a8a7fb31c7645a0c1c6bbdb284d3c7cb7da427c3 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/instruction.md @@ -0,0 +1,15 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 8 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D. + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d2-t0076/solution/oracle.py b/tasks/class-test/class-test-distractors-d2-t0076/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d2-t0076/solution/solve.sh b/tasks/class-test/class-test-distractors-d2-t0076/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d2-t0076/task.toml b/tasks/class-test/class-test-distractors-d2-t0076/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8131c2db3e7b0bbe44ab6a1cc7e3100791b8eb74 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 76 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d2-t0076/tests/checks.json b/tasks/class-test/class-test-distractors-d2-t0076/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..e228d6927f18a627601a06fe92cbfff79e4013cb --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/tests/checks.json @@ -0,0 +1,291 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 9, + 29, + 0, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 58.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 7, + 27, + 2, + 14 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 28.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 11, + 28, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 56.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 25, + 7, + 0, + 15 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 4, + 6, + 3, + 33 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 66.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 0, + 1, + 33, + 15 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "A", + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 66.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 5, + 34, + 4, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 68.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q8.counts", + "type": "list", + "value": [ + 4, + 8, + 3, + 35 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q8.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q8.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q8.key_pct", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q3", + "Q4", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-distractors-d2-t0076/tests/test.sh b/tasks/class-test/class-test-distractors-d2-t0076/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d2-t0076/tests/verify.py b/tasks/class-test/class-test-distractors-d2-t0076/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d2-t0076/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-e0008/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-e0008/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5bcfb6d54d3b27d7de40a4ab304a424fa8f04332 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,D,4 +Q2,D,5 +Q3,A,4 +Q4,A,5 +Q5,B,4 +Q6,D,4 +Q7,C,4 diff --git a/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..f5ec35323e7bb8a1e59a98c9ad1f62653eceb5d4 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/environment/workdir/responses.csv @@ -0,0 +1,97 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S064,10P,present,A,C,C,A,C,D,D +S109,10P,present,D,D,C,A,B,C,C +S164,10R,present,D,A,D,A,D,D,C +S016,10P,present,D,D,A,A,B,D,C +S362,10Q,present,D,D,B,D,B,, +S385,10Q,present,A,C,A,A,B,D,C +S056,10R,present,D,D,A,A,B,B,C +S356,10P,present,A,E,A,A,B,C, +S319,10R,present,D,A,C,A,B,A,D +S310,10Q,present,A,C,D,C,D,D,C +S259,10R,present,A,E,C,D,B,D,B +S139,10P,present,A,D,C,A,B,D,D +S377,10R,present,B,D,,A,D,D,C +S379,10R,present,D,D,A,C,B,B,C +S250,10R,present,D,D,C,A,B,B,C +S291,10P,present,B,D,C,A,B,D,A +S475,10P,absent,,,,,,, +S107,10R,present,D,D,A,A,B,D,D +S300,10P,present,D,D,A,A,C,D,C +S245,10P,present,D,D,D,D,B,C,D +S127,10R,present,D,D,C,A,C,C,B +S126,10Q,present,D,A,C,A,B,B,C +S318,10R,present,D,D,A,C,B,C,C +S102,10Q,present,D,D,A,A,B,C,C +S167,10R,present,D,D,C,A,B,A,C +S196,10R,present,D,D,D,C,D,C,C +S092,10R,present,D,D,C,A,C,D, +S387,10P,present,A,E,C,A,B,D,C +S228,10Q,present,A,D,B,C,B,D,A +S154,10R,present,A,D,A,A,C,B,C +S156,10R,present,B,C,,A,B,B,C +S489,10Q,absent,,,,,,, +S013,10Q,present,D,B,C,A,B,D,C +S062,10R,present,D,A,C,D,B,D,B +S134,10P,present,A,E,A,A,C,A,C +S199,10P,present,D,E,C,A,B,D,D +S152,10R,present,D,E,C,D,B,B,C +S350,10R,present,A,D,A,A,B,D,C +S183,10P,present,D,D,D,B,C,D,C +S085,10P,present,D,D,A,A,B,D,C +S253,10R,present,B,D,B,A,C,D,C +S140,10R,present,A,C,C,,D,C,B +S248,10P,present,D,D,B,B,B,D,D +S298,10P,present,A,E,D,A,B,D,C +S390,10P,present,B,D,C,C,B,D,D +S219,10Q,present,D,D,C,A,B,D,A +S153,10R,present,D,,C,A,B,D,C +S168,10P,present,A,D,C,A,C,D,C +S255,10Q,present,D,D,C,A,B,D, +S095,10Q,present,D,,C,A,C,B,C +S236,10Q,present,D,D,A,A,B,D,B +S018,10R,present,D,E,A,B,C,C,D +S177,10P,present,D,D,C,A,D,C,C +S231,10Q,present,B,D,A,C,B,D, +S028,10Q,present,B,D,D,B,B,D,C +S433,10R,absent,,,,,,, +S337,10R,present,B,D,A,A,B,B, +S364,10Q,present,D,D,C,A,D,D,C +S309,10Q,present,B,D,C,A,C,D,C +S162,10R,present,D,D,D,A,C,D,C +S333,10Q,present,D,E,D,C,B,D,C +S316,10P,present,B,D,C,A,B,D,D +S314,10R,present,B,D,B,A,B,A,C +S302,10P,present,B,E,A,A,B,D,C +S283,10P,present,D,D,C,A,D,D, +S015,10P,present,D,E,D,A,B,D,C +S077,10Q,present,A,E,A,C,B,D,C +S294,10R,present,D,E,B,A,B,D,C +S217,10R,present,A,B,,A,B,D,C +S160,10P,present,A,E,D,A,B,D,C +S176,10P,present,D,D,A,A,B,B, +S129,10R,present,A,E,C,B,B,D,B +S019,10P,present,B,C,D,A,B,D,A +S125,10R,present,B,D,D,A,B,D,C +S469,10P,absent,,,,,,, +S274,10R,present,D,D,C,A,B,D,D +S289,10P,present,D,D,A,A,B,D,C +S175,10R,present,D,C,C,D,B,A,B +S150,10P,present,D,E,C,C,B,D,C +S226,10P,present,D,D,A,A,C,C,D +S384,10R,present,A,D,A,A,D,D,C +S041,10R,present,D,D,B,C,B,D,C +S005,10P,present,B,D,A,A,B,D,C +S468,10R,absent,,,,,,, +S115,10P,present,D,A,A,A,D,B,C +S295,10P,present,D,C,D,D,D,D,C +S306,10P,present,B,D,A,A,B,D,C +S357,10P,present,A,,B,A,B,B, +S353,10P,present,A,D,C,A,C,D,C +S184,10R,present,D,C,C,D,B,D, +S088,10Q,present,A,E,A,A,B,D,D +S052,10R,present,A,D,D,A,B,D,B +S233,10R,present,A,E,C,A,B,B,A +S166,10R,present,D,E,C,D,B,A,A +S361,10R,present,A,D,,A,B,C,A +S058,10P,present,B,D,D,A,B,B,B diff --git a/tasks/class-test/class-test-distractors-d3-e0008/instruction.md b/tasks/class-test/class-test-distractors-d3-e0008/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-e0008/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-e0008/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-e0008/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-e0008/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-e0008/task.toml b/tasks/class-test/class-test-distractors-d3-e0008/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..45d8bbc30d88a68fbd93a8cdfcec5f399efb2692 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10008 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-e0008/tests/checks.json b/tasks/class-test/class-test-distractors-d3-e0008/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..97ae02fd44a47e033df3e88e8e2388e019b54878 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/tests/checks.json @@ -0,0 +1,305 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 25, + 17, + 0, + 49 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 53.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 5, + 2, + 9, + 53, + 19 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 58.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 27, + 8, + 36, + 16 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 29.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 65, + 5, + 11, + 9, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 71.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 0, + 65, + 15, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 71.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 6, + 14, + 12, + 58 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 63.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 7, + 9, + 52, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 57.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 91, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-e0008/tests/test.sh b/tasks/class-test/class-test-distractors-d3-e0008/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-e0008/tests/verify.py b/tasks/class-test/class-test-distractors-d3-e0008/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0008/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-e0020/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-e0020/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..35bf323de396fb8ddf0b3a8fac8348d403341e55 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,A,5 +Q2,D,4 +Q3,D,4 +Q4,D,5 +Q5,B,4 +Q6,B,4 +Q7,E,5 diff --git a/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..5ad8cf4d1aada45d974e135901df193a460e20d0 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/environment/workdir/responses.csv @@ -0,0 +1,64 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S153,10P,present,A,D,D,D,B,A,A +S311,10Q,present,A,C,D,D,A,B,E +S268,10Q,present,C,D,D,E,B,B,E +S286,10P,present,A,C,D,A,A,B,E +S269,10R,present,,B,A,D,B,B,E +S133,10P,present,A,C,D,D,B,A,E +S038,10Q,present,A,D,,E,A,D,E +S036,10P,present,A,C,,D,A,A,E +S196,10P,present,A,B,,B,D,,E +S155,10Q,present,A,D,D,,A,B,C +S140,10P,present,C,C,A,D,A,D,E +S241,10R,present,A,C,D,D,,B,E +S246,10P,present,B,D,D,,A,B,A +S120,10P,present,A,D,A,D,C,B,C +S091,10Q,present,A,B,D,D,B,A,E +S320,10P,present,A,B,D,D,A,D,E +S070,10Q,present,,D,,B,B,B,E +S372,10Q,present,E,D,C,D,A,B,E +S134,10R,present,A,C,D,D,C,B,E +S015,10P,present,A,C,D,D,A,B,E +S149,10Q,present,E,D,D,D,A,B,E +S381,10P,present,D,C,D,D,B,B,B +S245,10R,present,A,D,C,D,C,B,E +S234,10R,present,A,C,,E,D,B,D +S283,10Q,present,E,D,,A,D,D, +S122,10Q,present,C,D,D,E,,A,E +S240,10Q,present,E,C,D,B,A,B,E +S075,10R,present,D,B,D,D,D,A,E +S100,10P,present,D,D,,B,A,D,E +S299,10R,present,B,,D,D,C,B,E +S035,10R,present,A,D,D,D,A,B,E +S040,10R,present,A,D,D,D,C,B,E +S021,10Q,present,A,C,D,D,C,A,B +S333,10Q,present,A,D,C,A,B,B,C +S055,10R,present,A,C,A,B,B,B,E +S018,10R,present,C,B,D,D,B,B,E +S295,10Q,present,A,,C,B,A,A,E +S361,10Q,present,D,D,D,D,B,B,E +S478,10R,absent,,,,,,, +S068,10R,present,A,D,D,E,A,B,E +S323,10P,present,A,D,D,B,A,B,E +S288,10R,present,A,A,D,E,A,B, +S025,10R,present,B,D,D,A,A,D,E +S303,10P,present,A,A,D,D,D,A,E +S395,10Q,present,A,D,D,D,,D,E +S159,10R,present,A,C,D,B,A,B,E +S006,10R,present,B,D,D,A,D,D,E +S244,10P,present,D,C,D,D,D,B,E +S114,10R,present,A,D,D,D,D,B,E +S126,10R,present,A,A,D,E,C,,E +S384,10R,present,A,,D,D,A,B,E +S322,10Q,present,A,D,D,D,A,B,E +S031,10Q,present,,A,D,D,A,B,E +S451,10P,absent,,,,,,, +S291,10R,present,,D,,A,B,B,E +S087,10R,present,D,C,,D,B,D,E +S078,10R,present,C,A,B,D,D,B,A +S238,10R,present,B,B,B,D,B,B,A +S148,10P,present,,,,E,C,B,E +S232,10P,present,,D,D,E,B,B,E +S274,10P,present,A,,A,E,C,B,E +S213,10Q,present,A,D,D,B,C,A, +S378,10R,present,A,A,D,D,A,B, diff --git a/tasks/class-test/class-test-distractors-d3-e0020/instruction.md b/tasks/class-test/class-test-distractors-d3-e0020/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-e0020/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-e0020/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-e0020/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-e0020/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-e0020/task.toml b/tasks/class-test/class-test-distractors-d3-e0020/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..5abe283a3dcdc702082f6dfa77ad7f6fa8edc7bb --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "eval"] +isced_f = "0111" +family = "distractors" +split = "eval" +seed = 10020 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-e0020/tests/checks.json b/tasks/class-test/class-test-distractors-d3-e0020/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..9b20ab56a378a82cc307ace48c08339d8f1f9135 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/tests/checks.json @@ -0,0 +1,308 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 35, + 5, + 5, + 6, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 57.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 6, + 7, + 16, + 27 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 44.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 5, + 2, + 4, + 40 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 65.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 6, + 9, + 0, + 34, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 55.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 24, + 15, + 10, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 24.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 10, + 40, + 0, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 65.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 4, + 2, + 3, + 1, + 47 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "B", + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 77.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q3", + "Q4", + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 61, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-e0020/tests/test.sh b/tasks/class-test/class-test-distractors-d3-e0020/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-e0020/tests/verify.py b/tasks/class-test/class-test-distractors-d3-e0020/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-e0020/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0008/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0008/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..2b75307cc2a6a29df6ec8331075ee1232e02efe6 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,D,4 +Q2,A,4 +Q3,A,5 +Q4,D,5 +Q5,A,5 +Q6,D,5 +Q7,C,4 diff --git a/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..4c35b893f5de65f6bf408a4f8b8e2ba81f987330 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/environment/workdir/responses.csv @@ -0,0 +1,59 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S381,10Q,present,D,C,E,D,A,D,B +S304,10Q,present,D,B,E,D,C,D,A +S054,10P,present,B,A,E,A,C,D,B +S105,10P,present,A,C,D,B,C,B,A +S301,10R,present,D,C,E,E,B,A,A +S342,10R,present,D,D,E,D,A,E,C +S340,10Q,present,D,A,C,D,A,D,C +S284,10P,present,B,C,E,D,A,D,C +S157,10R,present,B,B,E,A,A,E,B +S316,10P,present,,C,D,D,A,D,C +S259,10Q,present,A,D,E,D,C,D,B +S022,10P,present,C,C,E,E,B,D,D +S176,10R,present,D,C,A,A,A,A,B +S246,10Q,present,D,A,E,D,A,D,D +S053,10Q,present,D,D,C,A,B,D,A +S281,10P,present,D,B,E,A,E,E,B +S429,10P,absent,,,,,,, +S232,10R,present,B,D,E,D,E,E,B +S247,10P,present,B,C,D,D,A,D, +S317,10P,present,C,D,,D,E,D,B +S434,10R,absent,,,,,,, +S365,10R,present,A,B,E,D,A,D,C +S210,10Q,present,D,B,E,A,A,D,A +S082,10R,present,D,D,A,D,A,D,B +S156,10R,present,D,C,C,A,A,D,B +S235,10Q,present,,B,E,B,E,D,A +S120,10P,present,D,C,D,A,A,D,B +S240,10P,present,D,C,E,D,C,D,A +S080,10R,present,D,D,E,B,B,A, +S392,10P,present,D,C,D,E,E,E,D +S354,10R,present,B,B,A,D,A,D,A +S038,10P,present,C,C,A,A,E,D,C +S318,10Q,present,A,B,A,D,A,D,A +S016,10P,present,D,D,D,A,A,D,C +S466,10P,absent,,,,,,, +S262,10R,present,D,B,E,A,A,E,A +S277,10P,present,D,A,E,E,A,D,C +S007,10Q,present,B,A,A,D,B,E,C +S180,10P,present,D,A,C,D,E,D,B +S271,10P,present,D,C,D,B,A,E,B +S263,10P,present,D,C,A,A,A,B,B +S108,10R,present,D,A,E,D,A,D,B +S409,10Q,absent,,,,,,, +S162,10P,present,D,B,A,E,A,B,A +S133,10R,present,D,D,D,D,B,D,A +S255,10Q,present,D,A,D,D,B,E,B +S070,10P,present,A,A,A,D,B,D,B +S306,10P,present,D,D,A,B,E,D,A +S083,10P,present,D,C,A,D,A,A,B +S264,10R,present,,A,E,B,A,D,B +S135,10R,present,,C,,A,A,D,C +S149,10P,present,D,D,A,E,B,D,A +S257,10P,present,D,D,E,A,A,D,B +S066,10R,present,A,B,E,D,B,D,B +S067,10P,present,D,C,D,D,B,D,B +S377,10Q,present,D,C,E,D,A,D,B +S433,10Q,absent,,,,,,, +S061,10R,present,D,B,E,D,A,D,B diff --git a/tasks/class-test/class-test-distractors-d3-t0008/instruction.md b/tasks/class-test/class-test-distractors-d3-t0008/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0008/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0008/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0008/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0008/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0008/task.toml b/tasks/class-test/class-test-distractors-d3-t0008/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..5e9ca86dc3e3abcb11c778f0027317411f323f0d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 8 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0008/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0008/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..992d650991476a59c6d23757d9966e2db8effada --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/tests/checks.json @@ -0,0 +1,309 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 6, + 7, + 3, + 33 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 62.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 10, + 12, + 19, + 12 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 18.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 12, + 0, + 4, + 10, + 25 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 22.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 14, + 6, + 0, + 27, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 50.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 29, + 11, + 5, + 0, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 4, + 3, + 0, + 37, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 69.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 14, + 24, + 10, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 18.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q3", + "Q4", + "Q5", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 53, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0008/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0008/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0008/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0008/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0008/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0020/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0020/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..35c334fed34fec75acb887e821e361558fd427d2 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,D,4 +Q2,A,4 +Q3,B,5 +Q4,C,5 +Q5,B,4 +Q6,B,5 +Q7,C,5 diff --git a/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..b4a0fd50571d3ad86c95ad837ac94a6b785370b8 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/environment/workdir/responses.csv @@ -0,0 +1,73 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S084,10P,present,B,D,B,A,,A,C +S094,10P,present,D,D,B,,B,B,C +S059,10R,present,D,A,B,C,A,C,C +S120,10Q,present,D,C,B,C,A,B,A +S455,10R,absent,,,,,,, +S427,10P,absent,,,,,,, +S208,10R,present,A,A,B,A,B,B,D +S188,10R,present,B,A,,C,C,B,C +S219,10Q,present,B,A,B,C,B,B,C +S399,10P,present,D,B,E,B,B,B,C +S227,10R,present,B,A,B,C,C,B,C +S078,10R,present,D,D,B,A,,B,C +S160,10R,present,B,A,E,C,A,,C +S350,10R,present,B,D,B,C,B,B,E +S367,10Q,present,B,A,B,C,A,B,C +S189,10Q,present,D,C,C,A,B,C,C +S490,10R,absent,,,,,,, +S029,10Q,present,A,B,E,,B,B,C +S118,10P,present,B,A,B,A,,,C +S174,10Q,present,D,B,B,B,A,B,C +S226,10R,present,B,A,B,A,B,A,C +S359,10R,present,B,C,B,C,,B,C +S076,10R,present,B,A,,C,,B, +S322,10R,present,B,A,B,C,B,B,E +S229,10Q,present,A,A,B,A,B,B,C +S364,10P,present,B,D,E,B,D,B,C +S106,10Q,present,D,C,B,C,B,B,A +S262,10P,present,A,,D,C,B,,E +S249,10R,present,B,A,B,C,B,A,C +S296,10R,present,B,D,B,C,B,B,A +S289,10Q,present,B,B,B,A,B,B,C +S201,10P,present,D,A,B,C,B,B,C +S058,10P,present,D,A,,C,B,B,C +S287,10Q,present,B,A,,C,A,B,B +S375,10R,present,A,A,B,C,B,B,C +S266,10R,present,B,D,D,C,B,B,B +S335,10R,present,B,A,B,C,,B,D +S294,10R,present,B,A,B,B,,B,A +S099,10R,present,D,A,B,C,B,B,C +S127,10Q,present,B,C,B,C,B,B, +S107,10P,present,B,D,B,C,B,,C +S341,10P,present,B,A,C,C,B,C,C +S398,10P,present,B,D,B,A,D,A,C +S225,10P,present,D,C,B,C,B,B,C +S307,10P,present,A,D,B,C,B,A,E +S088,10Q,present,D,A,D,C,C,A,C +S243,10P,present,A,A,B,C,B,B,C +S109,10P,present,B,D,,C,B,,C +S232,10R,present,B,A,B,C,D,B,C +S237,10R,present,B,B,B,C,B,B,C +S268,10P,present,B,A,,C,D,B,C +S073,10R,present,D,A,B,A,B,B,C +S145,10P,present,B,B,B,A,D,A,C +S026,10P,present,C,A,B,C,A,B,C +S168,10R,present,B,A,B,B,A,B,D +S080,10R,present,A,B,B,C,D,C,A +S202,10Q,present,D,A,B,C,B,B,B +S154,10R,present,B,A,B,C,,A,C +S138,10Q,present,B,A,E,C,B,B, +S001,10P,present,D,,B,C,B,B,E +S221,10R,present,A,A,B,C,D,B,E +S308,10R,present,B,A,,C,C,B,C +S479,10P,absent,,,,,,, +S024,10Q,present,C,A,B,B,B,B,C +S239,10Q,present,B,A,,C,D,,B +S125,10P,present,B,B,,A,B,B,C +S317,10R,present,D,A,B,C,B,B,C +S132,10P,present,B,A,B,C,B,B,C +S376,10Q,present,A,B,C,C,B,A,C +S224,10R,present,B,,,C,B,B,C +S095,10Q,present,D,,B,A,A,B,B +S207,10P,present,B,A,B,B,B,B, diff --git a/tasks/class-test/class-test-distractors-d3-t0020/instruction.md b/tasks/class-test/class-test-distractors-d3-t0020/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0020/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0020/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0020/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0020/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0020/task.toml b/tasks/class-test/class-test-distractors-d3-t0020/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..70f401e52df2d0f711642e7d94d0ef3f6db0dbfc --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 20 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0020/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0020/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ed717a0d67b1dd20d61812101c7c0521c1fdc934 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/tests/checks.json @@ -0,0 +1,314 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 10, + 38, + 2, + 18 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 26.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 38, + 9, + 6, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 55.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 47, + 3, + 3, + 5 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A", + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 69.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 13, + 7, + 46, + 0, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "D", + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 67.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 9, + 39, + 4, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 57.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 9, + 49, + 4, + 0, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "D", + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 72.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 5, + 5, + 45, + 3, + 6 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 66.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q3", + "Q4", + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 68, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0020/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0020/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0020/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0020/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0020/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0032/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0032/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..c86035592c479d57fea2b02fcab8d3ee76e86b84 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,D,4 +Q2,B,5 +Q3,D,4 +Q4,D,4 +Q5,D,4 +Q6,A,4 +Q7,C,5 diff --git a/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..8f329e4f89dfcacdb3b990e0beafb72c74cdebf4 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/environment/workdir/responses.csv @@ -0,0 +1,56 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S198,10P,present,C,D,D,C,B,A,C +S118,10R,present,D,D,D,D,D,A,B +S374,10R,present,D,A,C,C,D,,B +S360,10R,present,D,B,D,D,D,B,E +S100,10R,present,D,B,D,D,D,A,C +S393,10R,present,D,B,B,D,D,A,B +S140,10R,present,D,B,A,D,D,A,B +S029,10P,present,D,B,C,D,D,A,B +S474,10R,absent,,,,,,, +S170,10R,present,,A,D,D,D,B,B +S108,10Q,present,D,B,,D,D,B,A +S115,10R,present,D,B,D,D,D,A, +S280,10R,present,D,B,D,D,A,B, +S224,10Q,present,C,A,D,D,D,A,B +S380,10P,present,,B,D,D,D,A,B +S309,10P,present,D,B,D,A,D,A,B +S067,10P,present,,B,B,D,B,A,C +S049,10Q,present,,A,D,D,D,B, +S353,10R,present,A,D,C,B,D,A,B +S071,10Q,present,D,D,A,D,D,A,B +S272,10Q,present,D,B,B,C,A,A,B +S163,10P,present,D,B,C,A,B,A,B +S046,10P,present,A,B,D,D,B,D,C +S079,10Q,present,D,A,D,D,A,B,B +S326,10P,present,D,D,D,D,A,B,B +S366,10R,present,D,D,A,D,D,C,B +S321,10Q,present,D,A,D,D,A,B,C +S196,10Q,present,D,B,A,D,D,B,B +S203,10R,present,C,B,D,B,D,,B +S088,10Q,present,D,A,D,D,C,B,B +S076,10R,present,D,B,D,D,D,B,D +S216,10R,present,A,B,D,D,D,B,C +S478,10R,absent,,,,,,, +S171,10Q,present,D,B,D,D,B,B,C +S359,10P,present,D,B,D,D,B,B,B +S181,10P,present,C,D,C,D,D,A,B +S176,10Q,present,A,B,C,A,A,B,D +S395,10R,present,A,A,C,D,B,A,B +S236,10R,present,A,B,C,D,B,A,C +S342,10P,present,C,A,D,D,D,B,C +S310,10R,present,D,D,D,D,D,B,A +S111,10P,present,D,A,D,D,D,B,C +S183,10P,present,D,A,D,D,A,B,B +S120,10R,present,D,B,D,A,B,A,B +S373,10R,present,D,B,D,D,B,A,C +S060,10R,present,D,A,D,D,D,A,B +S370,10R,present,D,B,D,D,D,A,B +S143,10Q,present,D,B,B,B,A,A,B +S202,10R,present,D,B,D,C,A,A,E +S116,10P,present,D,A,A,C,D,B,B +S392,10P,present,C,B,D,D,D,B,B +S063,10R,present,D,B,D,D,A,B,B +S260,10Q,present,A,D,D,D,B,A,B +S090,10P,present,D,A,D,B,B,B,B +S188,10Q,present,D,A,D,C,D,B,B diff --git a/tasks/class-test/class-test-distractors-d3-t0032/instruction.md b/tasks/class-test/class-test-distractors-d3-t0032/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0032/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0032/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0032/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0032/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0032/task.toml b/tasks/class-test/class-test-distractors-d3-t0032/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d1da1de968ef1ace4432ac06ca4b6331d33eae34 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 32 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0032/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0032/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d06a64eeb8daf9c0d42ce030f29c1039e37f053c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/tests/checks.json @@ -0,0 +1,312 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 7, + 0, + 6, + 36 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 67.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 15, + 29, + 0, + 9, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "C", + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 5, + 4, + 8, + 35 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 66.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 4, + 4, + 6, + 39 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 73.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 10, + 12, + 1, + 30 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 56.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 25, + 24, + 1, + 1 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 47.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 2, + 33, + 11, + 2, + 2 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "A", + "D", + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 20.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q5", + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 53, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0032/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0032/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0032/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0032/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0032/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0044/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0044/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..3de9d97f30a90e903a2797a470a5c1d5fee9c351 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,A,5 +Q2,B,5 +Q3,D,4 +Q4,D,5 +Q5,C,4 +Q6,A,5 +Q7,A,4 diff --git a/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..042b765a6796c17cdc3e4958ee8e798f5329964c --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/environment/workdir/responses.csv @@ -0,0 +1,78 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S370,10Q,present,D,B,B,D,C,C,A +S391,10R,present,A,E,B,D,C,B,A +S362,10R,present,,,B,D,C,E,B +S321,10P,present,A,B,B,E,C,E,A +S086,10R,present,A,B,D,D,C,B,A +S084,10R,present,B,B,D,D,C,E,A +S334,10Q,present,A,D,B,D,C,E,D +S042,10P,present,A,E,D,B,C,E,A +S065,10Q,present,B,B,D,D,C,E,A +S154,10P,present,A,D,B,D,C,D,A +S331,10Q,present,E,B,,D,B,D,A +S311,10Q,present,,E,D,D,B,E,A +S166,10P,present,A,B,D,C,C,A,A +S369,10R,present,B,D,D,B,C,E,A +S341,10P,present,A,B,D,D,B,D,A +S318,10R,present,A,B,B,D,C,C,C +S114,10R,present,A,D,D,D,A,E,A +S299,10Q,present,A,E,B,C,C,D,D +S133,10Q,present,E,B,B,D,B,A,A +S253,10P,present,,B,D,D,A,A,A +S216,10P,present,E,B,B,C,C,E,A +S332,10Q,present,D,D,D,D,B,D,A +S113,10P,present,A,B,B,B,B,E,C +S277,10P,present,A,B,B,D,C,E,A +S213,10P,present,,A,B,D,C,E,A +S010,10P,present,D,B,B,D,C,E,D +S147,10R,present,A,B,B,D,C,D,A +S142,10P,present,A,B,B,D,A,E,A +S050,10P,present,A,B,D,D,A,A,A +S088,10R,present,B,B,B,D,B,D,C +S196,10Q,present,E,A,B,D,C,A,A +S080,10Q,present,E,B,B,D,C,C,A +S363,10P,present,A,B,D,D,C,D,D +S160,10Q,present,A,E,D,A,C,B,A +S239,10R,present,A,D,B,D,A,A,A +S402,10Q,absent,,,,,,, +S291,10P,present,E,E,D,D,C,C,A +S087,10P,present,A,B,D,D,B,A,D +S240,10Q,present,A,B,D,D,C,A,A +S051,10P,present,A,D,B,C,A,E,A +S348,10P,present,A,B,,C,B,B,A +S231,10R,present,A,A,B,C,A,D,A +S242,10R,present,A,B,B,D,C,A,A +S294,10R,present,A,B,D,D,B,A,A +S459,10Q,absent,,,,,,, +S023,10Q,present,A,B,B,D,A,E,A +S235,10Q,present,A,E,B,D,B,B,A +S357,10R,present,A,E,B,C,C,E,A +S271,10Q,present,E,D,B,C,A,B,B +S138,10Q,present,A,B,B,D,C,D,B +S225,10Q,present,B,E,D,D,B,E,A +S221,10Q,present,A,E,B,D,C,E,A +S290,10R,present,E,E,,B,C,E,A +S296,10R,present,B,E,,B,C,D,A +S233,10Q,present,B,B,D,C,B,A,A +S257,10P,present,A,D,D,C,B,E,D +S352,10R,present,A,B,D,D,C,E,A +S437,10P,absent,,,,,,, +S286,10Q,present,D,E,B,C,B,E,A +S070,10R,present,,B,B,D,A,D,A +S099,10Q,present,E,D,D,D,C,,B +S121,10Q,present,A,E,D,E,A,B,A +S302,10P,present,A,B,D,D,C,D,C +S385,10P,present,A,B,B,D,C,E,D +S122,10Q,present,D,B,B,C,,E,A +S152,10P,present,A,D,,D,C,,C +S056,10P,present,A,B,D,B,A,B,D +S324,10Q,present,A,B,D,D,C,E,A +S076,10R,present,,,D,D,A,D,A +S355,10R,present,,D,D,D,C,A,B +S272,10P,present,B,,B,B,C,D,A +S194,10Q,present,A,B,B,D,A,E,C +S266,10P,present,A,E,,D,A,B,A +S368,10P,present,A,D,D,D,B,A,A +S284,10Q,present,A,D,D,D,B,E,A +S180,10R,present,E,E,D,D,A,A,A +S247,10R,present,D,B,D,D,B,E,A diff --git a/tasks/class-test/class-test-distractors-d3-t0044/instruction.md b/tasks/class-test/class-test-distractors-d3-t0044/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0044/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0044/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0044/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0044/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0044/task.toml b/tasks/class-test/class-test-distractors-d3-t0044/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..327fcaa66f717c556c0fc7c1d1d3f20325f6a47b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 44 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0044/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0044/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..6909058f4b8266f04216ab762aa95fbf2737446d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/tests/checks.json @@ -0,0 +1,314 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 43, + 8, + 0, + 6, + 10 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 58.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 3, + 38, + 0, + 14, + 16 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 51.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 0, + 35, + 0, + 33 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [ + "A", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 44.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 1, + 7, + 12, + 52, + 2 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A", + "E" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 70.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 16, + 18, + 39, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 52.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 14, + 9, + 4, + 15, + 30 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 18.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 55, + 5, + 6, + 8 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 74.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q3", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 74, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0044/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0044/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0044/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0044/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0044/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0056/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0056/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..334637204086ab3c0c98902b9befc292fb482ac7 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,A,5 +Q2,A,4 +Q3,A,4 +Q4,B,4 +Q5,C,4 +Q6,C,5 +Q7,A,4 diff --git a/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..dce570555441e963cb88db1016ea8f0ff2045a9d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/environment/workdir/responses.csv @@ -0,0 +1,65 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S441,10P,absent,,,,,,, +S163,10P,present,B,A,A,A,C,A,B +S378,10Q,present,A,D,A,B,A,C,D +S051,10Q,present,A,A,A,A,C,D,A +S172,10R,present,A,A,A,B,A,C,A +S309,10P,present,A,D,D,B,C,B,A +S266,10Q,present,A,A,A,A,C,B,A +S296,10Q,present,A,C,A,B,A,B,A +S043,10R,present,A,A,B,B,C,C,A +S216,10Q,present,A,A,A,B,C,B,D +S024,10P,present,A,A,A,B,A,B,B +S115,10Q,present,A,A,B,A,C,B,B +S339,10R,present,A,A,A,B,C,B,A +S342,10R,present,,D,C,B,C,C,A +S122,10R,present,A,,C,B,C,A,A +S019,10Q,present,A,D,A,D,C,B,A +S011,10Q,present,A,C,B,B,C,,B +S185,10P,present,A,,A,B,C,C,A +S440,10P,absent,,,,,,, +S004,10Q,present,A,A,A,A,A,A,A +S204,10R,present,D,A,A,B,C,C,D +S336,10R,present,A,A,A,A,C,B,A +S167,10P,present,A,D,A,B,A,B,A +S153,10P,present,A,D,A,B,C,B,A +S237,10R,present,E,,A,B,A,B,B +S362,10R,present,B,A,A,A,C,C,A +S007,10P,present,A,C,A,A,C,B,A +S222,10R,present,A,A,A,A,A,B,B +S344,10P,present,B,C,A,,A,B,A +S467,10R,absent,,,,,,, +S114,10R,present,E,A,C,A,C,B,D +S094,10P,present,A,A,A,A,C,B,B +S465,10R,absent,,,,,,, +S144,10R,present,B,D,D,B,C,E,A +S130,10Q,present,A,A,C,B,A,,C +S092,10R,present,A,A,A,B,C,,C +S238,10P,present,A,A,B,B,C,B,B +S286,10Q,present,E,A,A,B,C,B,A +S337,10P,present,A,A,B,B,C,B,A +S026,10P,present,A,C,A,A,C,B,A +S218,10R,present,A,C,A,B,C,A,A +S143,10Q,present,A,A,C,B,C,D,A +S366,10R,present,A,A,A,A,C,A,A +S438,10R,absent,,,,,,, +S287,10P,present,A,A,C,A,C,,A +S067,10Q,present,A,A,A,A,C,C,A +S166,10R,present,A,A,A,B,C,E,A +S083,10Q,present,A,A,D,A,C,B,B +S373,10R,present,A,A,A,B,A,D,A +S070,10R,present,A,A,A,B,C,B,A +S259,10P,present,A,,C,B,C,E,A +S131,10Q,present,A,D,A,A,A,D,A +S152,10R,present,B,A,A,B,C,B,A +S025,10Q,present,A,C,B,B,C,B,C +S371,10R,present,A,A,A,B,C,D,D +S364,10P,present,A,D,A,B,C,C,A +S009,10P,present,A,D,A,A,C,,A +S154,10R,present,A,C,D,,C,B,A +S140,10Q,present,A,A,C,A,C,B,A +S173,10P,present,B,A,A,B,A,B,B +S341,10Q,present,A,A,A,A,C,B,A +S111,10P,present,A,A,A,B,C,E,D +S099,10Q,present,A,A,A,,C,D,D +S313,10Q,present,B,D,A,B,A,B,C diff --git a/tasks/class-test/class-test-distractors-d3-t0056/instruction.md b/tasks/class-test/class-test-distractors-d3-t0056/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0056/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0056/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0056/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0056/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0056/task.toml b/tasks/class-test/class-test-distractors-d3-t0056/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..772477bcb323791ed964cb9f43ba37bce857ba94 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 56 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0056/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0056/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..23dd6e4ab9d03ba3fdf4f243e9c690c992375e00 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/tests/checks.json @@ -0,0 +1,308 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 47, + 7, + 0, + 1, + 3 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 79.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 36, + 0, + 8, + 11 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 61.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 41, + 6, + 8, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 69.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 20, + 35, + 0, + 1 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "C", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 59.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 14, + 0, + 45, + 0 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "B", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 76.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 5, + 30, + 9, + 6, + 4 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 15.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 38, + 10, + 4, + 7 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 64.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q4", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 59, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0056/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0056/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0056/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0056/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0056/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0068/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0068/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..57b70bc876d943cb787b163d698c35f53b18dbd0 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,A,4 +Q2,C,5 +Q3,E,5 +Q4,D,4 +Q5,E,5 +Q6,D,4 +Q7,E,5 diff --git a/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..05f26a9ddc1ff717d57bc26b36e770825f49f89b --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/environment/workdir/responses.csv @@ -0,0 +1,49 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S348,10R,present,A,E,C,C,E,C,D +S230,10Q,present,A,C,E,C,A,D,A +S142,10Q,present,B,C,E,B,E,C,E +S152,10P,present,D,C,E,C,E,D,E +S266,10Q,present,A,E,E,D,E,D,E +S248,10Q,present,D,A,E,D,B,D,A +S281,10P,present,A,C,E,C,B,D,B +S336,10Q,present,B,C,E,D,B,D,E +S460,10R,absent,,,,,,, +S091,10Q,present,B,E,B,C,E,D,D +S401,10Q,absent,,,,,,, +S289,10Q,present,D,C,,D,A,D,D +S144,10Q,present,A,C,D,D,A,D,E +S014,10Q,present,B,E,E,,E,D,E +S365,10P,present,A,C,A,C,E,A,D +S218,10R,present,B,E,E,D,E,D,E +S119,10P,present,C,E,E,B,D,C,E +S156,10R,present,A,C,E,B,,C,B +S268,10R,present,D,C,C,C,D,D,D +S182,10P,present,B,E,E,D,B,D,E +S121,10Q,present,A,C,A,C,B,D,E +S273,10Q,present,A,A,D,D,E,C,E +S246,10P,present,B,C,D,D,E,D,E +S172,10Q,present,D,C,E,C,E,A,B +S102,10R,present,B,C,,C,B,C,B +S187,10P,present,A,C,B,B,B,A,D +S124,10P,present,A,E,E,D,E,A,E +S012,10P,present,A,C,E,D,E,D,E +S181,10Q,present,A,E,B,C,E,C,E +S068,10R,present,B,A,E,D,B,D,E +S141,10P,present,D,A,E,,E,A,E +S274,10R,present,A,A,A,,A,C,B +S065,10P,present,B,C,B,D,E,C,E +S135,10Q,present,A,A,B,C,E,D,E +S259,10P,present,A,C,E,C,A,D,E +S002,10R,present,B,E,E,C,E,A,D +S047,10R,present,D,C,A,D,B,D,D +S286,10P,present,B,C,E,C,E,D,A +S311,10R,present,A,C,E,D,E,D,E +S383,10P,present,A,E,E,C,B,A,E +S080,10R,present,D,E,C,D,E,D,B +S202,10R,present,B,C,E,D,E,C,E +S106,10R,present,A,E,E,C,D,D,E +S105,10P,present,D,A,E,B,E,D,D +S208,10R,present,A,C,,B,E,D,E +S494,10Q,absent,,,,,,, +S377,10P,present,B,A,C,,E,D,E +S389,10R,present,A,C,E,C,E,D,B diff --git a/tasks/class-test/class-test-distractors-d3-t0068/instruction.md b/tasks/class-test/class-test-distractors-d3-t0068/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0068/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0068/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0068/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0068/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0068/task.toml b/tasks/class-test/class-test-distractors-d3-t0068/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..60a15cafc2a0fdc4514fe43a5e1b004560a9bbd9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 68 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0068/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0068/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..62c7ed7aa0c5990d91a30bbce074a6caa25cdc2d --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/tests/checks.json @@ -0,0 +1,314 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 21, + 14, + 1, + 9 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 46.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 8, + 0, + 24, + 0, + 13 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "B", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 53.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "E", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 4, + 5, + 4, + 3, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 57.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 0, + 6, + 18, + 17 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [ + "A" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 37.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 5, + 10, + 0, + 3, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 57.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 7, + 0, + 10, + 28 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 62.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 3, + 7, + 0, + 9, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [ + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 57.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q4", + "Q5", + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 45, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0068/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0068/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0068/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0068/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0068/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-distractors-d3-t0080/environment/Dockerfile b/tasks/class-test/class-test-distractors-d3-t0080/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/distractor_notes.md b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/distractor_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..aaf2dce028715e233b927676bf3eba756d219f77 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/distractor_notes.md @@ -0,0 +1,9 @@ +# Distractor analysis: definitions + +For each item, the key is the correct option and the other options are its distractors. `N` is the number of students who sat the test (an empty cell means the student left the item out; such students still count in `N`). + +- The count of an option is the number of students who chose it. +- A distractor is non-functioning if it was chosen by under 5% of the `N` students (count divided by `N` below 0.05). +- A distractor beats the key if it was chosen by more students than the key was. +- Rows with status `absent` did not sit the test: they are not part of `N` or of any count. +- Some items have five options (A to E); `key.csv` gives the number of options of each item. diff --git a/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/key.csv b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..3eb294b4c6f46ea4948f4b8a9a971b9871d5e8b7 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/key.csv @@ -0,0 +1,8 @@ +item,key,n_options +Q1,E,5 +Q2,B,4 +Q3,A,4 +Q4,D,5 +Q5,D,4 +Q6,C,4 +Q7,C,4 diff --git a/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/responses.csv b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..d3076d28cfb573ae0bb66d1cbeae0dc7d950b371 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/environment/workdir/responses.csv @@ -0,0 +1,99 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7 +S187,10Q,present,A,B,B,E,D,D,C +S299,10R,present,E,B,D,E,D,C,C +S382,10Q,present,A,B,B,D,D,D,C +S444,10Q,absent,,,,,,, +S071,10R,present,A,B,A,C,D,C,B +S010,10Q,present,E,B,A,,D,C,D +S319,10P,present,E,B,B,B,D,D,C +S113,10R,present,A,B,D,B,D,C,A +S020,10Q,present,E,B,A,E,B,C,B +S150,10R,present,C,B,A,D,D,D,C +S151,10P,present,E,,B,B,D,D,C +S222,10Q,present,E,B,B,B,A,C,C +S169,10Q,present,E,B,B,B,D,C,C +S213,10R,present,E,B,D,B,A,C,D +S267,10R,present,E,B,D,B,C,C,D +S327,10Q,present,E,B,D,D,D,C,C +S266,10R,present,E,B,B,A,C,D,C +S237,10P,present,E,B,D,B,D,D,A +S255,10P,present,E,B,D,E,B,C,C +S028,10Q,present,E,D,B,B,D,C,D +S377,10P,present,E,B,B,B,D,D,C +S398,10Q,present,E,D,B,D,D,C,C +S370,10R,present,E,D,B,B,D,C,C +S023,10Q,present,E,B,B,D,C,D,C +S161,10Q,present,C,B,B,E,C,C,D +S042,10R,present,E,D,A,B,A,C,C +S193,10Q,present,E,D,C,E,A,A,C +S147,10R,present,E,D,B,B,D,C,C +S260,10P,present,E,B,A,B,D,C,C +S116,10P,present,E,,B,D,A,C,C +S219,10R,present,E,B,C,E,D,C,C +S258,10R,present,E,B,B,B,B,C,A +S422,10P,absent,,,,,,, +S288,10R,present,E,B,A,A,C,D,C +S035,10R,present,E,B,C,D,D,C,C +S384,10R,present,E,D,B,E,B,B,A +S303,10Q,present,C,D,C,D,A,C,C +S019,10P,present,E,B,B,B,D,C,D +S221,10P,present,E,B,B,D,C,D,C +S041,10P,present,E,B,B,A,B,C,C +S155,10P,present,E,D,B,D,B,C,A +S271,10Q,present,A,B,B,E,B,D, +S346,10Q,present,E,B,B,A,B,C,C +S309,10Q,present,C,B,B,D,A,C,D +S296,10P,present,C,B,A,E,D,D,C +S347,10P,present,E,B,B,B,D,C,C +S375,10R,present,E,B,B,C,D,C,C +S363,10P,present,E,,B,B,D,C,D +S049,10Q,present,E,D,A,A,B,C,C +S018,10R,present,E,D,C,D,D,B,A +S082,10P,present,E,B,,B,D,C,C +S036,10R,present,B,B,B,D,B,C,D +S378,10P,present,E,D,B,E,A,A,C +S324,10Q,present,E,D,D,B,D,D,C +S248,10Q,present,E,D,B,C,D,D,C +S044,10P,present,A,D,A,B,D,D,C +S311,10Q,present,C,B,D,A,D,C,B +S194,10Q,present,E,D,B,B,C,D,C +S097,10Q,present,E,D,C,C,D,C,C +S005,10R,present,E,B,C,E,D,D,D +S188,10P,present,E,B,C,D,D,C,C +S229,10R,present,E,D,C,C,C,C,C +S379,10R,present,C,D,B,D,B,C,C +S356,10Q,present,E,D,A,D,D,C,D +S326,10Q,present,E,D,A,B,D,C,C +S204,10R,present,E,B,C,B,D,C,C +S232,10Q,present,E,B,B,D,D,D,A +S179,10Q,present,A,B,B,B,D,C,C +S256,10Q,present,E,B,D,B,B,C,C +S032,10R,present,E,,C,E,D,C,C +S070,10Q,present,E,D,D,B,D,C,D +S192,10Q,present,E,B,A,B,D,D,D +S148,10R,present,E,B,B,B,D,C,D +S125,10R,present,E,D,D,B,D,C,B +S389,10Q,present,A,D,B,A,D,C,A +S086,10P,present,B,,,D,D,C,C +S228,10P,present,E,D,A,D,B,C,C +S149,10Q,present,E,,C,A,A,A,B +S108,10P,present,E,D,C,E,B,C,D +S069,10R,present,E,B,A,B,C,A,C +S353,10R,present,E,B,,D,D,C,C +S283,10Q,present,E,B,B,B,C,C,C +S209,10R,present,A,D,B,A,D,C,C +S286,10R,present,E,B,B,A,D,D,C +S397,10P,present,E,B,D,E,D,C,C +S399,10Q,present,E,D,B,A,C,D,C +S053,10R,present,E,B,B,,B,C,C +S246,10Q,present,E,B,B,D,D,D,C +S154,10Q,present,E,B,B,A,D,C,B +S099,10R,present,A,D,D,B,D,D,C +S331,10P,present,E,D,C,C,D,C,C +S381,10Q,present,E,B,D,B,D,C,A +S440,10Q,absent,,,,,,, +S233,10R,present,E,B,B,A,D,D,C +S247,10Q,present,E,B,A,B,D,C,C +S052,10P,present,E,B,B,D,D,C,C +S217,10P,present,E,B,C,B,C,D,C +S181,10P,present,A,B,D,D,A,C,C diff --git a/tasks/class-test/class-test-distractors-d3-t0080/instruction.md b/tasks/class-test/class-test-distractors-d3-t0080/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..0240204b12b49327b59e3227f0cd433b64f712c9 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/instruction.md @@ -0,0 +1,17 @@ +# Class test: distractor analysis + +`responses.csv` holds the option each student chose on each of the 7 items of a multiple-choice test (three classes, rows in no particular order). `key.csv` gives the correct option of each item. `distractor_notes.md` gives the definitions: follow them exactly. Analyse all classes together. + +Write `answer.json` in this folder, a JSON object with: + +- `items`: an object keyed by item (`Q1` to `Q7`). Each value has + - `counts`: a list of integers, the count of each option in the order A, B, C, D (and E for a five-option item). + - `non_functioning`: a list of the option letters of the item's non-functioning distractors (any order; an empty list if none). + - `omitted`: the number of students who left the item out, an integer. + - `key_pct`: the percentage of the `N` students who chose the key, 1 decimal. + - `top_distractor`: the letter of the distractor chosen by the most students. +- `has_non_functioning`: list of the items that have at least one non-functioning distractor (any order). +- `key_beaten`: list of the items where at least one distractor beats the key (any order). +- `n_students`: `N`, an integer. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-distractors-d3-t0080/solution/oracle.py b/tasks/class-test/class-test-distractors-d3-t0080/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-distractors-d3-t0080/solution/solve.sh b/tasks/class-test/class-test-distractors-d3-t0080/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-distractors-d3-t0080/task.toml b/tasks/class-test/class-test-distractors-d3-t0080/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..040adbca61118cbf4d397d0b5def1804864345e2 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "distractors", "isced-0111", "train"] +isced_f = "0111" +family = "distractors" +split = "train" +seed = 80 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-distractors-d3-t0080/tests/checks.json b/tasks/class-test/class-test-distractors-d3-t0080/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..cacea0f56c93bd04a17459311f6c365949cfc1dd --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/tests/checks.json @@ -0,0 +1,306 @@ +[ + { + "path": "items.Q1.counts", + "type": "list", + "value": [ + 11, + 2, + 7, + 0, + 75 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q1.non_functioning", + "type": "set", + "value": [ + "B", + "D" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q1.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q1.key_pct", + "type": "number", + "value": 78.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q1.top_distractor", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "items.Q2.counts", + "type": "list", + "value": [ + 0, + 59, + 0, + 30 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q2.non_functioning", + "type": "set", + "value": [ + "A", + "C" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q2.omitted", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q2.key_pct", + "type": "number", + "value": 62.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q2.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "items.Q3.counts", + "type": "list", + "value": [ + 16, + 45, + 15, + 16 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q3.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q3.omitted", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "items.Q3.key_pct", + "type": "number", + "value": 16.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q3.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q4.counts", + "type": "list", + "value": [ + 13, + 36, + 6, + 23, + 15 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q4.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q4.omitted", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "items.Q4.key_pct", + "type": "number", + "value": 24.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q4.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q5.counts", + "type": "list", + "value": [ + 10, + 15, + 12, + 58 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q5.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q5.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q5.key_pct", + "type": "number", + "value": 61.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q5.top_distractor", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "items.Q6.counts", + "type": "list", + "value": [ + 4, + 2, + 63, + 26 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q6.non_functioning", + "type": "set", + "value": [ + "A", + "B" + ], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q6.omitted", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "items.Q6.key_pct", + "type": "number", + "value": 66.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q6.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "items.Q7.counts", + "type": "list", + "value": [ + 9, + 6, + 64, + 15 + ], + "item": "int", + "weight": 1.0 + }, + { + "path": "items.Q7.non_functioning", + "type": "set", + "value": [], + "item": "str", + "weight": 1.0 + }, + { + "path": "items.Q7.omitted", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "items.Q7.key_pct", + "type": "number", + "value": 67.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "items.Q7.top_distractor", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "has_non_functioning", + "type": "set", + "value": [ + "Q1", + "Q2", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "key_beaten", + "type": "set", + "value": [ + "Q3", + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 95, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-distractors-d3-t0080/tests/test.sh b/tasks/class-test/class-test-distractors-d3-t0080/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-distractors-d3-t0080/tests/verify.py b/tasks/class-test/class-test-distractors-d3-t0080/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-distractors-d3-t0080/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..ab86812476add02ff33cc2b2d23aaffe90da332d --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/environment/workdir/scores.csv @@ -0,0 +1,24 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S047,0,1,1,1,0,0,0,0 +S110,1,1,1,1,0,1,0,0 +S144,0,1,1,1,0,1,1,1 +S387,1,1,1,1,1,0,0,1 +S153,1,1,1,1,0,1,1,0 +S132,0,0,1,0,0,0,0,0 +S309,0,1,1,1,0,0,0,0 +S287,0,1,1,1,0,0,0,1 +S351,0,1,1,1,0,0,0,1 +S344,0,1,1,0,0,0,0,0 +S160,0,1,1,0,1,0,0,1 +S301,0,1,1,1,1,1,0,1 +S119,1,1,1,1,0,0,0,0 +S146,1,1,0,1,0,0,1,0 +S241,0,1,1,1,1,0,1,1 +S058,0,0,1,1,1,0,1,0 +S050,0,1,1,1,0,0,0,0 +S088,0,1,1,1,0,1,0,1 +S312,0,0,0,0,1,1,0,0 +S276,0,1,1,0,0,0,1,0 +S126,0,0,1,1,1,0,0,0 +S346,1,1,1,1,1,1,1,0 +S175,0,1,1,1,1,1,1,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/instruction.md b/tasks/class-test/class-test-item-analysis-d1-e0001/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..4cf74fca8cef9a1b42cd4088369235b40ce4b370 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 23 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-e0001/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-e0001/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/task.toml b/tasks/class-test/class-test-item-analysis-d1-e0001/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7552bc12c70a206311a1d62b149370d80a8d1e70 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10001 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..fe49229a2d8b84593430b8d2a2e916878eac7419 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/checks.json @@ -0,0 +1,129 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.261, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.826, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.913, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.167, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.783, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.391, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.348, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.348, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.391, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-e0001/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0001/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..6f8b28b0753603bbad5ea3599e8437d8cf6645bf --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/environment/workdir/scores.csv @@ -0,0 +1,23 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S119,1,0,0,0,0,1,0,0 +S375,1,0,1,1,1,1,0,1 +S177,1,0,0,1,1,0,1,1 +S110,1,0,1,0,1,1,0,1 +S140,1,0,0,0,0,1,0,0 +S163,0,0,0,0,0,1,0,1 +S316,1,1,0,1,1,0,0,1 +S114,1,1,0,0,0,0,0,0 +S031,1,0,0,0,0,1,0,1 +S134,1,1,0,1,1,0,0,1 +S171,1,1,1,1,1,1,1,1 +S279,1,1,1,1,1,0,1,0 +S045,1,0,0,0,1,0,0,0 +S290,1,1,1,0,1,0,0,0 +S262,1,0,0,0,0,1,0,0 +S098,1,0,0,0,0,1,0,1 +S384,1,0,1,1,1,1,0,1 +S149,1,1,1,0,0,1,0,0 +S095,1,0,0,0,1,0,0,1 +S227,1,0,1,1,1,0,0,1 +S351,1,1,1,1,1,0,1,1 +S106,1,1,1,1,1,0,1,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/instruction.md b/tasks/class-test/class-test-item-analysis-d1-e0013/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..ca98ab75ef621acfc4255261fb85701e256817ff --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 22 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-e0013/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-e0013/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/task.toml b/tasks/class-test/class-test-item-analysis-d1-e0013/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..1787abe3d6517fc772af0dd51188b7cafefcf460 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10013 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..1907b9f73a7b549edcb6cbc721f23f886385370d --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/checks.json @@ -0,0 +1,130 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.955, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.167, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.409, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": -0.167, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.227, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q1", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-e0013/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-e0013/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..06bf92c76738075891d56389f33e2f7d97e252d7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/environment/workdir/scores.csv @@ -0,0 +1,23 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S308,0,1,1,1,1,0,0,1 +S342,0,0,1,0,0,0,0,1 +S020,0,0,1,1,1,0,0,0 +S141,0,0,1,0,1,0,0,1 +S315,0,1,1,1,0,0,0,0 +S077,0,0,1,1,1,0,1,0 +S035,1,0,1,0,1,1,0,1 +S219,1,1,0,1,0,1,0,0 +S316,0,0,1,1,1,0,0,0 +S247,1,0,1,1,1,0,0,1 +S260,0,1,1,1,0,0,0,1 +S208,0,0,1,1,0,0,0,1 +S102,1,1,1,1,1,1,1,1 +S225,1,1,1,1,1,1,0,1 +S370,1,1,1,1,1,0,0,1 +S154,0,1,1,1,0,1,0,1 +S285,1,1,1,1,0,0,0,0 +S111,0,1,1,0,1,0,1,1 +S006,1,1,1,1,0,1,0,0 +S011,0,0,1,1,1,1,1,1 +S333,1,0,1,1,1,0,1,1 +S331,0,0,1,1,0,0,0,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0001/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..ca98ab75ef621acfc4255261fb85701e256817ff --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 22 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0001/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0001/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0001/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..975a76cb87e874329cef5d47b0fb19e682cdd6f1 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 1 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..59d6c94eedb81bcf712d893d0d05fbd71bbca081 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/checks.json @@ -0,0 +1,130 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.409, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.955, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.818, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.591, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.318, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.227, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.682, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.167, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q3", + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0001/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0001/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..8080a7441c43dcd9e2aa72b89fe64fc00b140662 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/environment/workdir/scores.csv @@ -0,0 +1,19 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S279,1,0,1,0,0,0,0,1 +S396,1,1,1,1,1,1,0,0 +S359,1,1,1,1,1,1,1,1 +S088,1,0,1,1,1,1,0,0 +S385,0,0,0,0,0,1,0,0 +S160,1,0,1,0,0,1,1,0 +S151,0,0,0,0,0,1,0,0 +S246,1,0,0,1,1,1,0,1 +S099,0,1,0,1,0,1,0,0 +S118,1,0,1,0,1,0,1,0 +S311,0,0,0,0,0,1,0,0 +S244,0,0,1,0,1,0,0,1 +S284,1,1,1,1,1,1,1,0 +S362,0,1,1,1,1,1,0,0 +S377,0,0,1,1,1,1,0,1 +S122,0,0,0,0,0,1,0,1 +S017,0,0,0,0,0,1,0,0 +S214,1,0,1,0,1,1,1,0 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0013/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..70e6d57fb7864772c87e3ec7d1d6834b32c71729 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 18 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0013/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0013/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0013/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9e8b6fec97db21cad8311225f428347da6401185 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 13 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..9025b007262873fb8c5cf1d35b17560dfa422767 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/checks.json @@ -0,0 +1,130 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.278, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.611, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.278, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q6", + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0013/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0013/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..e5ec68225b154b27543d9d7c1736c2123360db37 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/environment/workdir/scores.csv @@ -0,0 +1,26 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S003,1,0,1,0,1,1,0,1 +S223,0,0,1,0,1,1,1,0 +S261,1,1,1,0,1,1,0,1 +S189,1,0,1,1,1,1,0,0 +S340,1,1,1,1,1,0,1,1 +S008,1,1,1,1,0,1,1,1 +S279,1,1,1,0,0,1,0,1 +S387,1,1,1,1,0,1,1,1 +S366,1,1,1,0,1,0,0,1 +S390,1,0,1,0,0,0,0,1 +S068,0,1,1,0,0,1,0,1 +S096,1,1,1,0,0,1,1,1 +S024,0,0,1,0,0,0,1,1 +S060,0,0,0,0,0,1,0,0 +S395,0,1,1,1,0,1,1,1 +S193,1,1,1,1,1,1,0,1 +S045,1,1,1,1,1,1,0,0 +S186,1,0,1,0,0,1,0,1 +S203,0,0,1,0,1,0,0,0 +S211,0,0,1,0,1,0,0,0 +S336,1,1,1,1,0,1,0,1 +S181,1,1,1,1,1,1,0,0 +S113,0,1,1,0,0,0,1,0 +S127,1,1,1,1,1,1,1,0 +S358,1,0,1,1,1,1,1,0 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0025/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..3384fb4eec4bc4e47bb0043a714d710161eee001 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 25 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0025/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0025/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0025/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8189bcbc19e352fa67c15530e179aaa48af77594 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 25 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..cbd5469d790dc85259b068cb910da500ed149de4 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/checks.json @@ -0,0 +1,130 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.68, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.857, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.857, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.96, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.143, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.44, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.857, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.52, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.143, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.72, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.571, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.286, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.429, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q3", + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0025/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0025/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..0afb6c07bd99c293cddeea10d68e2cf632f23353 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/environment/workdir/scores.csv @@ -0,0 +1,20 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S315,0,1,0,0,1,1,0,1 +S174,1,1,0,0,1,1,0,1 +S155,0,0,0,0,0,1,0,0 +S381,1,0,1,0,1,1,1,1 +S131,0,0,0,0,1,0,1,0 +S165,1,0,1,1,1,1,0,0 +S068,1,0,0,0,1,0,0,0 +S344,0,0,0,0,1,1,0,0 +S377,1,0,1,0,1,0,1,1 +S146,0,0,1,0,1,0,0,0 +S162,1,1,1,0,1,0,1,1 +S047,1,0,1,0,0,1,1,1 +S288,1,0,1,0,1,0,1,1 +S116,0,0,0,1,1,1,0,0 +S048,0,0,0,0,1,0,0,1 +S372,0,0,0,0,1,0,0,0 +S096,1,1,1,1,1,1,1,1 +S001,1,0,0,1,1,1,1,1 +S197,1,0,0,0,1,0,0,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0037/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..06ec4289a9631e2b878ac3defe2a67d942b66e02 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 19 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0037/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0037/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0037/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..1c9c79035cfa02afc32da37de857b21b52911110 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 37 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d5f580f1b409df03870b597d84524df4c35f4836 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/checks.json @@ -0,0 +1,129 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.579, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.211, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.421, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.211, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.895, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.421, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.579, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0037/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0037/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..ad40bb97167a29cf8a3dc529f3f0cb2ad5f28164 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/environment/workdir/scores.csv @@ -0,0 +1,22 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S353,0,0,1,1,1,1,1,0 +S307,0,1,0,0,1,0,1,1 +S115,0,1,0,0,1,0,0,1 +S188,0,1,0,0,0,0,0,0 +S391,1,1,1,0,0,1,1,1 +S380,0,1,0,0,1,0,1,1 +S066,1,1,0,0,0,1,0,1 +S314,1,1,0,1,0,1,1,0 +S161,0,1,1,0,1,1,0,1 +S037,0,0,0,0,0,1,0,1 +S257,1,1,1,1,0,1,1,1 +S364,0,1,1,1,1,1,1,0 +S297,0,0,0,0,1,0,1,0 +S040,0,1,1,1,1,0,1,0 +S100,1,1,1,0,1,0,1,1 +S300,1,1,1,1,1,1,0,1 +S293,0,0,0,0,0,0,1,1 +S355,1,1,1,1,1,0,1,0 +S090,1,1,1,1,1,0,1,1 +S372,1,1,1,1,1,1,1,0 +S365,1,1,0,0,0,0,0,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0049/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..59bb0a1bbce335e4c2ebeb36ffa8bfef1c8dd54f --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 21 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0049/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0049/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0049/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c4bbbfad82745157cbdb1fcd2c20e0bfce05e242 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 49 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..8ac82c62583fe3e9b15d2b26478885d1989e633a --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/checks.json @@ -0,0 +1,129 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.476, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.81, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.524, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.429, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.619, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.476, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.619, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0049/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0049/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..91b93b03cd6f2a54c68b2be9fe53e4944b15cb4f --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/environment/workdir/scores.csv @@ -0,0 +1,20 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S329,0,0,1,0,0,0,1,1 +S292,0,0,1,1,0,0,1,0 +S324,1,1,1,1,1,1,1,1 +S269,1,1,1,1,0,0,0,1 +S041,0,0,1,0,0,0,1,1 +S268,1,1,1,1,0,0,1,1 +S106,0,0,1,0,0,1,1,0 +S138,0,1,1,1,0,0,1,0 +S194,0,1,1,0,0,1,0,0 +S167,0,1,1,0,0,1,1,1 +S137,0,1,1,1,0,0,1,1 +S319,1,1,0,1,1,0,1,1 +S227,0,1,1,1,0,0,1,1 +S337,0,0,0,0,0,1,0,0 +S164,1,0,1,1,0,1,1,0 +S310,1,0,1,0,0,0,1,1 +S241,0,0,1,1,0,0,1,1 +S374,1,0,1,0,0,0,0,0 +S343,0,1,1,0,1,0,1,1 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0061/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..06ec4289a9631e2b878ac3defe2a67d942b66e02 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 19 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0061/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0061/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0061/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d0f91a685d12ad2c96638265e55e4d982b9e63b2 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 61 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..065965afd04748eacf9d16b9b1e0dba85f94a1dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/checks.json @@ -0,0 +1,130 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.368, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.895, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.158, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.316, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.789, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.632, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q3", + "Q6" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0061/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0061/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ab76a27149d2310fdcfd7a5e785e7acd6f50dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/item_analysis_notes.md @@ -0,0 +1,16 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise. A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/scores.csv b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/scores.csv new file mode 100644 index 0000000000000000000000000000000000000000..b2eb008a9efa4f7293fa8aaa58e5b8b113fa3531 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/environment/workdir/scores.csv @@ -0,0 +1,20 @@ +student_id,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8 +S382,0,1,0,0,0,1,0,1 +S352,0,1,0,1,0,1,1,0 +S111,0,1,0,1,1,1,1,1 +S155,1,1,0,1,0,1,1,0 +S276,0,1,0,0,0,1,0,1 +S025,0,1,0,0,0,0,0,1 +S249,1,1,0,1,1,1,1,0 +S120,1,1,1,1,1,1,1,0 +S378,1,1,1,1,0,1,1,1 +S285,0,1,1,1,0,1,1,0 +S037,0,1,1,1,1,1,1,1 +S208,1,0,1,0,0,1,1,1 +S314,0,1,0,1,1,1,1,0 +S078,0,1,0,1,1,1,1,1 +S083,0,1,1,0,0,1,1,0 +S133,0,1,1,0,0,0,0,0 +S265,1,1,0,1,1,1,1,0 +S344,0,1,0,1,0,0,1,0 +S331,0,1,0,1,0,0,0,0 diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/instruction.md b/tasks/class-test/class-test-item-analysis-d1-t0073/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..06ec4289a9631e2b878ac3defe2a67d942b66e02 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/instruction.md @@ -0,0 +1,13 @@ +# Class test: item analysis + +`scores.csv` holds the item scores (1 right, 0 wrong) of 19 students on a 8-item test. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q8`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d1-t0073/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d1-t0073/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/task.toml b/tasks/class-test/class-test-item-analysis-d1-t0073/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3c7aa2932f4676435519b88712965d7be5a05d27 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 73 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/tests/checks.json b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..bbb3d28e66c645b3ca6041a3b4201bd0af5783c1 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/checks.json @@ -0,0 +1,129 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.316, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.947, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.368, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.684, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.368, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.789, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.737, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.421, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/tests/test.sh b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d1-t0073/tests/verify.py b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d1-t0073/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..163db1191064ee12e5137da05b489362f2007251 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,C +Q2,A +Q3,A +Q4,C +Q5,D +Q6,B +Q7,D +Q8,C +Q9,A +Q10,B diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..99d00c950ade816b570dfc95ff5c1117f8c6f8dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/environment/workdir/responses.csv @@ -0,0 +1,42 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S079,8Y,A,B,A,B,D,D,D,C,A,B +S159,8X,A,C,C,B,A,D,D,D,A,D +S057,8Y,D,A,A,B,D,B,D,A,A,A +S089,8X,,A,A,A,D,B,B,C,A,C +S243,8X,D,A,A,C,D,B,D,A,A,B +S146,8X,C,C,A,A,C,B,C,B,A,A +S187,8Y,D,A,A,D,D,D,C,B,A,B +S376,8X,D,,D,D,B,,A,A,A,A +S123,8X,B,B,A,B,D,A,C,C,A,D +S116,8X,C,A,B,A,C,B,D,A,A,A +S369,8Y,A,B,D,A,C,C,A,B,A,C +S017,8Y,A,A,C,D,,C,A,C,A,B +S250,8X,D,C,B,A,D,B,C,A,A,C +S385,8Y,B,A,A,A,D,B,C,C,A,B +S204,8Y,B,C,D,D,D,C,D,C,A,A +S259,8Y,,A,C,A,A,A,,A,A,C +S379,8Y,B,A,A,A,D,B,C,C,A,B +S029,8Y,B,B,D,D,B,B,C,B,A,C +S310,8Y,A,C,A,,D,A,D,C,A,B +S049,8Y,D,A,A,D,D,C,C,C,A,B +S126,8Y,C,A,A,C,D,B,D,C,A,B +S065,8X,,A,A,D,D,B,C,C,A,C +S201,8Y,A,B,A,B,D,B,B,C,A,B +S178,8X,B,A,A,A,D,B,D,C,A,B +S313,8Y,B,B,D,,D,B,D,C,A,B +S045,8X,A,D,C,D,D,A,B,,A,C +S062,8Y,A,D,C,A,D,A,D,,A,D +S144,8X,B,A,D,C,D,B,D,C,A,A +S001,8X,D,B,B,A,D,A,B,C,A,D +S370,8Y,B,C,A,A,D,B,D,C,A,B +S326,8X,B,A,A,D,D,D,A,B,A,D +S053,8X,B,C,D,B,B,B,C,A,D,A +S162,8Y,C,A,B,B,D,A,C,D,A,B +S163,8X,D,D,D,A,D,C,B,D,A,A +S203,8X,B,A,B,C,D,B,A,C,,B +S215,8X,C,A,A,D,D,D,C,C,A,B +S249,8Y,D,D,B,C,D,B,D,D,A,C +S252,8X,B,D,A,B,D,D,D,D,A,C +S092,8Y,,A,B,,D,B,D,A,A,B +S325,8X,C,,D,A,B,B,B,A,A,C +S217,8X,D,B,A,D,D,B,B,C,A,C diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/instruction.md b/tasks/class-test/class-test-item-analysis-d2-e0005/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-e0005/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-e0005/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/task.toml b/tasks/class-test/class-test-item-analysis-d2-e0005/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..4e0638128f88698eff07d9564acef69eda5245ee --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10005 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..6b7c4809c11b9b2831e58d6073b1f7842d1602dd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/checks.json @@ -0,0 +1,184 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.146, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.091, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.463, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.818, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.488, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.909, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.122, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.756, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.537, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.39, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.364, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.488, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.818, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.951, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.091, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.415, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q1", + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q1", + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.635, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-e0005/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0005/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..55718286e345c28edcad93e3e9560d29f4fcc256 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,C +Q2,A +Q3,B +Q4,A +Q5,D +Q6,B +Q7,C +Q8,D +Q9,B +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..1879b1ec3c8640c2c7c138e231eab20b37bb5935 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/environment/workdir/responses.csv @@ -0,0 +1,39 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S199,8Y,A,C,C,A,B,B,C,D,C,C +S079,8X,C,A,A,A,D,B,C,D,B,C +S215,8Y,B,A,,A,B,B,C,D,D,B +S380,8X,C,A,A,A,D,B,C,D,C,C +S254,8X,D,A,,D,D,B,C,C,B,C +S163,8Y,B,A,B,A,D,C,C,B,D,C +S218,8X,B,,C,C,A,D,A,B,A, +S319,8X,C,A,B,A,D,B,C,D,B,C +S129,8Y,B,D,B,C,A,,C,C,B, +S009,8X,D,A,B,A,D,B,C,D,B,C +S026,8Y,B,D,A,B,D,B,C,C,B,C +S124,8Y,A,C,B,,B,C,B,A,A,C +S391,8Y,D,A,B,B,D,A,C,D,B,B +S037,8Y,B,A,A,C,D,B,C,D,B,C +S091,8X,B,B,B,C,D,B,C,D,A,C +S259,8X,B,A,B,A,D,B,C,C,B,C +S275,8X,D,A,D,B,A,B,C,A,B,A +S049,8Y,D,A,D,A,D,B,C,D,B,C +S376,8Y,A,A,B,A,D,B,C,,D,C +S152,8X,D,A,B,A,D,B,C,D,D,A +S288,8Y,A,A,B,A,B,B,C,A,B,C +S095,8Y,D,B,B,A,A,B,C,,,C +S315,8Y,A,A,D,,D,B,C,B,B,C +S133,8X,B,A,A,A,D,B,C,D,B,D +S033,8Y,D,A,B,A,D,B,C,D,B,C +S076,8X,A,A,B,B,C,D,,C,C,C +S108,8X,,A,A,A,D,B,C,D,B,C +S020,8X,C,B,,C,D,B,C,C,D,C +S211,8X,D,A,A,A,D,B,C,D,B,C +S312,8Y,C,A,D,A,D,B,C,D,B,B +S336,8Y,B,A,B,A,D,B,C,D,B,A +S227,8X,D,C,B,A,A,D,C,D,B,C +S216,8X,A,D,C,A,D,B,C,A,B,B +S064,8Y,D,D,C,,A,B,A,D,B,C +S192,8X,A,A,B,B,A,D,C,B,B,B +S294,8Y,A,B,B,A,B,A,C,D,A,B +S201,8X,D,A,B,A,D,B,,D,A,C +S374,8Y,C,A,B,A,D,B,C,D,B,C diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/instruction.md b/tasks/class-test/class-test-item-analysis-d2-e0017/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-e0017/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-e0017/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/task.toml b/tasks/class-test/class-test-item-analysis-d2-e0017/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3bc1772cfe9854daa18aa63114fdc3122d0abd13 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10017 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..7aeaad8ff9b900e1dd95e5662e678bdc3aed67ac --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/checks.json @@ -0,0 +1,182 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.158, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.684, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.632, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.658, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.763, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.868, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.579, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.632, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.684, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.641, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-e0017/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-e0017/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..67844e9884096d284e9c57ec9516fa0b6ce67e1c --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,C +Q2,D +Q3,C +Q4,D +Q5,C +Q6,B +Q7,C +Q8,D +Q9,A +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..c9d6d7a67d3ee77711844299e501469efd703d7b --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/environment/workdir/responses.csv @@ -0,0 +1,46 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S145,8Y,C,D,C,D,D,A,C,D,A,C +S387,8X,D,C,D,B,A,D,C,C,A, +S342,8Y,D,D,C,D,B,C,C,D,A,C +S391,8Y,B,D,D,D,D,C,C,C,A,C +S261,8X,B,D,C,D,C,,D,B,A,C +S267,8Y,C,A,C,D,C,C,C,B,A,C +S271,8X,B,C,B,B,D,A,,A,A, +S238,8Y,A,B,D,D,C,A,A,D,A,D +S273,8Y,C,D,C,D,C,C,A,D,A,C +S311,8Y,C,D,C,B,C,A,D,D,A,D +S026,8X,D,B,C,D,C,D,C,D,A,C +S229,8X,C,D,B,B,C,B,C,B,A,C +S146,8X,A,C,A,D,D,D,B,D,A,C +S252,8Y,C,D,C,B,C,C,C,D,A,C +S365,8Y,D,D,C,A,,A,C,D,A,C +S373,8Y,C,A,C,D,A,B,C,D,A,C +S382,8Y,B,D,C,D,C,A,C,D,A,C +S379,8Y,B,B,A,A,C,A,B,A,A,C +S182,8X,C,D,C,C,D,C,C,D,A,C +S260,8Y,B,D,C,D,A,A,C,D,A,C +S073,8Y,D,D,C,D,C,D,C,D,A,A +S274,8X,C,B,C,D,C,C,C,C,A,C +S165,8Y,C,A,C,D,C,D,A,B,A,D +S251,8X,,D,C,C,D,A,C,D,A,B +S299,8Y,C,B,C,A,B,,A,C,A,C +S117,8Y,C,D,C,A,C,B,A,A,A,C +S399,8X,B,D,A,D,C,D,,C,A,C +S276,8X,C,D,C,D,C,B,C,A,A,C +S240,8Y,C,D,C,D,C,A,C,D,A,C +S256,8X,B,D,C,D,C,D,C,B,A,C +S047,8X,B,D,C,D,A,D,C,D,A,C +S287,8X,C,D,C,D,C,C,C,D,A,A +S061,8Y,B,,D,D,A,A,C,D,A,B +S321,8Y,D,B,C,D,B,A,C,D,A,B +S036,8X,D,C,B,D,C,D,D,D,B,C +S069,8X,C,D,C,A,C,A,C,B,A,C +S120,8Y,C,B,D,D,,A,C,C,A,C +S189,8X,C,,C,D,C,C,C,D,A, +S043,8Y,C,D,C,A,C,B,C,C,A,C +S024,8X,C,C,C,,C,B,C,C,A,B +S208,8Y,D,B,,D,C,A,C,B,A,D +S306,8Y,C,D,C,D,D,C,C,D,A,C +S003,8X,C,D,C,D,C,D,C,D,A,C +S214,8Y,D,B,C,D,B,,D,B,A,B +S193,8X,D,A,,B,A,A,D,B,A,D diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0005/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0005/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0005/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0005/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3d1fac3211ddde90e895c5cb174a137d3cab20cf --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 5 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..38d30b9513851c5ae68f18f9361d66db4a64e586 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/checks.json @@ -0,0 +1,182 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 12, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.489, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.711, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.833, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.578, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.133, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.689, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.533, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.978, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.083, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.576, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0005/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0005/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5d56819efb16e54b1f2bddc663cfc4e759c853de --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,A +Q3,B +Q4,D +Q5,A +Q6,B +Q7,C +Q8,C +Q9,C +Q10,A diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..141c972f5b2e029bdb3f69b8da0de71f7e90c23b --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/environment/workdir/responses.csv @@ -0,0 +1,31 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S196,8X,B,,B,C,A,B,B,C,C,A +S204,8X,,B,B,A,B,B,C,C,D,C +S240,8X,D,A,B,A,A,B,A,C,C,A +S173,8Y,C,A,A,C,A,B,A,C,C,B +S033,8X,B,B,B,D,A,B,C,C,C,A +S285,8Y,A,D,B,C,A,B,C,C,C,A +S042,8X,D,A,D,C,A,B,D,C,B,A +S370,8Y,A,A,C,A,B,D,B,A,A,A +S213,8X,A,D,D,B,B,B,C,C,, +S316,8X,B,A,B,D,C,B,C,C,C,A +S074,8X,A,A,A,,A,B,D,D,C,A +S129,8Y,D,A,C,B,D,B,C,B,D, +S307,8Y,A,C,B,C,C,B,C,B,D,C +S206,8X,A,C,A,B,B,A,B,B,B,D +S302,8Y,D,A,D,B,A,B,B,C,B,D +S205,8X,B,A,A,A,A,B,C,C,B,B +S303,8X,C,A,B,C,A,B,C,A,D,A +S146,8X,D,A,B,C,B,B,B,C,D,B +S022,8Y,,A,C,B,C,B,D,B,D,D +S365,8Y,A,A,B,A,A,B,C,C,D,A +S014,8Y,D,A,B,B,C,B,C,B,D,B +S059,8Y,C,A,B,B,A,B,B,C,C,B +S082,8Y,B,B,B,C,A,B,C,C,C,A +S377,8X,A,A,D,B,A,B,C,D,A,D +S202,8Y,A,B,B,B,A,B,C,C,C, +S085,8X,C,A,D,C,A,B,B,A,A,C +S172,8X,B,D,B,B,D,B,C,C,C,A +S106,8Y,B,A,B,B,D,B,A,C,B,A +S194,8Y,B,A,B,A,A,B,A,C,B,A +S161,8Y,A,B,A,C,D,B,A,C,B,A diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0017/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0017/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0017/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0017/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..35dbe1de8e67f0817ad39d5e180ec0940efd581c --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 17 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ad1126264bbd0b9e81089e5bac309e7d6986095a --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/checks.json @@ -0,0 +1,183 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.267, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.633, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": -0.125, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.567, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.067, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.567, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.933, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.367, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q1", + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.628, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0017/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0017/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..dc5488064d895afbaab762bb74522100e9201b43 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,C +Q3,C +Q4,D +Q5,D +Q6,A +Q7,B +Q8,D +Q9,A +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..186d410e3a06acda66f02752fefe93c7b3900130 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/environment/workdir/responses.csv @@ -0,0 +1,39 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S326,8Y,B,C,C,D,D,A,B,D,A,C +S036,8X,A,D,A,A,D,C,,C,B,B +S398,8X,B,C,D,D,D,A,A,D,A,C +S041,8Y,C,C,C,B,D,D,B,A,,A +S297,8Y,B,C,A,D,D,B,B,D,A,B +S393,8X,B,C,C,A,D,A,B,D,A,C +S317,8Y,B,C,B,A,D,A,C,D,B,A +S022,8X,D,C,,B,D,D,C,A,A,B +S132,8Y,B,B,D,B,D,A,A,D,A,D +S048,8X,B,C,B,C,D,A,A,B,D, +S177,8X,B,A,A,C,D,D,A,D,A,C +S331,8Y,C,C,D,B,D,C,A,C,A,B +S212,8X,B,C,A,D,D,A,D,D,A,C +S095,8X,C,D,D,C,D,C,B,A,A,D +S021,8Y,B,C,D,C,D,A,B,C,C,A +S248,8Y,D,A,C,C,D,B,A,A,B,B +S063,8Y,B,C,D,,D,A,B,A,D,C +S240,8Y,B,C,D,C,D,C,C,D,A,B +S258,8X,B,C,,C,D,A,,A,C,C +S137,8X,C,C,D,C,D,A,C,A,C,B +S158,8X,C,B,C,,D,,A,D,A,A +S254,8X,D,B,D,A,D,B,D,A,D,B +S261,8Y,B,A,,C,C,A,C,C,D,A +S350,8X,B,C,,D,D,A,B,D,A,C +S119,8Y,D,C,A,D,D,A,B,D,D,D +S367,8Y,A,C,A,C,D,A,,C,A,C +S321,8X,A,C,A,B,D,B,D,D,D,C +S334,8X,B,C,B,D,D,A,D,D,B,D +S381,8X,C,D,D,A,D,C,C,C,,C +S352,8X,,D,A,A,D,B,D,D,A,C +S109,8Y,B,D,,D,D,A,B,D,D,A +S305,8Y,C,A,B,A,D,D,,A,D,B +S023,8X,B,C,C,C,D,A,D,D,D,A +S343,8X,A,B,B,,D,A,D,D,C, +S067,8X,A,D,B,B,D,B,D,B,A,A +S163,8X,A,,D,A,D,,D,D,B,D +S051,8Y,B,D,C,D,D,C,B,D,D,C +S328,8X,C,C,B,B,D,A,B,,A,B diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0029/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0029/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0029/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0029/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..86f8d459308e4c120cb28b3c5f9897fd3457c35e --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 29 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..b8fe4c32060c9cc9d2a045b11333339d6ef0d918 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/checks.json @@ -0,0 +1,183 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.579, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.184, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.3, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.237, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.974, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.316, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.447, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.342, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q3", + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.683, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0029/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0029/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..0b05acf31c0be0400dcf697704243065b834cd2b --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,C +Q2,C +Q3,A +Q4,C +Q5,C +Q6,C +Q7,A +Q8,B +Q9,D +Q10,D diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..7240b9625f703c12d74e18aae539f643a0c161e8 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/environment/workdir/responses.csv @@ -0,0 +1,38 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S076,8X,C,C,A,C,C,D,A,B,D,B +S260,8X,A,D,A,C,D,D,A,B,D,A +S233,8X,C,D,D,D,,A,A,A,A, +S078,8Y,C,C,A,C,C,C,A,B,D,C +S307,8X,C,C,A,C,A,C,A,B,A,D +S068,8Y,D,B,D,C,,A,A,D,D,A +S212,8X,C,B,D,C,B,D,A,D,D,D +S099,8X,D,A,A,D,B,D,A,B,D,C +S261,8Y,C,C,A,C,C,A,A,B,D,B +S024,8Y,D,D,B,C,C,A,A,B,D,D +S038,8X,C,C,A,C,D,D,A,B,D,C +S278,8X,C,C,A,C,D,A,A,B,D,D +S047,8X,B,D,A,C,B,,A,B,,C +S351,8X,A,C,A,B,D,D,A,A,B,B +S193,8Y,C,C,B,C,B,D,A,A,D,D +S231,8Y,C,C,A,C,C,D,A,A,D,D +S292,8X,C,C,D,C,C,D,A,C,B,A +S287,8X,C,D,B,C,D,C,A,B,B,C +S165,8Y,C,B,A,A,B,A,A,A,D,C +S384,8X,C,A,,C,D,C,A,B,D,D +S030,8X,C,C,A,C,D,D,A,D,B,C +S014,8Y,C,C,D,C,C,B,A,D,D,C +S311,8X,C,C,A,C,A,C,A,B,C,D +S120,8X,B,,A,C,D,A,A,A,C,D +S161,8Y,C,C,,C,A,D,A,C,D,A +S321,8Y,C,C,A,D,D,B,A,C,D,A +S143,8X,C,A,C,B,A,D,A,A,D,B +S027,8Y,B,C,A,D,D,C,A,B,D,B +S299,8X,A,B,D,B,D,B,B,,D,D +S166,8Y,C,B,D,A,D,,A,A,D,B +S008,8X,C,D,A,D,A,A,A,B,D,A +S286,8Y,A,C,A,C,A,B,A,D,D, +S180,8X,A,C,A,C,C,D,A,B,A,D +S044,8Y,C,C,,C,B,D,A,C,D,B +S368,8X,B,C,A,,B,D,A,,,A +S395,8Y,C,C,A,C,C,B,A,A,D,D +S353,8Y,C,C,A,B,D,B,A,B,D,A diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0041/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0041/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0041/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0041/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..b2fa7947681be144ea7f489e9c8cd8feb48e331b --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 41 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..e7c5390eedf8b7801fbfcf996c29cd77a0b6dcbd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/checks.json @@ -0,0 +1,184 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.676, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.595, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.622, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.676, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.243, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.162, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.3, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.973, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.459, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.703, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.324, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q7", + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q5", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.504, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0041/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0041/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..d7f0514ef1d699d1c09e2c726dfdb2bca7979958 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,C +Q3,D +Q4,A +Q5,D +Q6,B +Q7,B +Q8,D +Q9,D +Q10,B diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..88d306009b9a9e5cf6ba383d49b7f9179321f70f --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/environment/workdir/responses.csv @@ -0,0 +1,31 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S286,8X,C,C,B,A,D,A,A,D,A,B +S218,8Y,B,C,D,A,D,D,B,D,B,B +S236,8X,B,C,D,A,D,B,B,D,A,B +S318,8Y,B,C,D,A,D,C,A,D,D,C +S272,8Y,B,C,D,A,D,D,C,D,D,B +S302,8Y,B,C,D,A,D,A,B,D,D,B +S364,8Y,D,C,D,A,D,A,C,D,A,B +S103,8X,C,C,D,A,B,A,A,C,D,D +S008,8Y,D,C,B,A,A,C,A,A,D,D +S349,8X,B,C,D,A,D,A,B,D,D,B +S051,8Y,B,C,C,,C,D,A,A,D,B +S372,8Y,B,C,D,A,D,D,B,D,D,B +S013,8Y,D,C,C,A,C,D,D,D,D,C +S299,8X,B,C,D,C,A,A,,C,C,C +S378,8X,B,C,D,A,D,B,B,D,D,B +S131,8X,B,C,A,D,C,A,B,A,A,D +S281,8X,B,C,D,A,D,D,B,B,D,C +S251,8Y,B,C,B,,D,B,A,D,D,C +S303,8X,C,C,D,B,D,B,B,D,D,B +S397,8X,A,C,D,A,D,D,C,D,D,B +S167,8X,B,C,D,A,D,D,B,D,C,B +S102,8X,B,C,D,C,D,D,B,D,B,B +S380,8X,B,C,D,A,D,C,B,D,D,A +S363,8Y,B,C,D,A,D,A,B,D,D,B +S247,8Y,A,C,D,A,D,A,B,D,A,B +S386,8X,D,A,B,D,A,D,A,D,D,D +S040,8Y,B,C,C,A,B,C,B,C,D, +S028,8X,,C,D,A,D,C,B,B,D,A +S085,8X,B,C,D,A,D,C,D,B,D,C +S249,8X,D,C,,A,D,A,B,A,D,B diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0053/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0053/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0053/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0053/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..afbcd0af337b598dd95bfc52fa0e58afe0a5b3f5 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 53 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..1ccb590c588caf6f68faf12da3a710e74da67d0a --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/checks.json @@ -0,0 +1,183 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.633, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.967, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.125, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.767, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.733, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.133, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.567, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.567, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2", + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.653, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0053/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0053/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..830b4e5e2b34d57381b35f9029582348c8d0d592 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,A +Q3,C +Q4,A +Q5,D +Q6,B +Q7,B +Q8,C +Q9,C +Q10,D diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..a048b7e20364e6be469cb5c5c123173b0c1213c2 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/environment/workdir/responses.csv @@ -0,0 +1,31 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S010,8Y,B,A,D,B,B,B,B,A,C,B +S262,8Y,B,A,A,A,D,C,B,A,D,C +S269,8X,A,A,B,C,C,C,B,A,A,A +S080,8X,B,,B,D,C,A,B,D,A, +S224,8X,B,,D,A,D,B,B,B,B,B +S245,8Y,B,D,A,A,D,B,B,B,C,A +S031,8Y,D,A,A,B,D,A,A,D,A,A +S315,8Y,B,B,D,A,D,B,B,A,A,B +S030,8X,B,A,B,D,A,C,B,D,D,C +S065,8Y,B,D,,A,C,A,B,B,B,D +S234,8Y,B,D,A,B,B,A,B,A,B,C +S201,8X,B,C,A,A,D,B,B,B,A,C +S199,8Y,A,A,A,B,D,A,B,D,A,C +S388,8X,B,B,A,A,D,A,B,D,D,B +S211,8X,D,C,,D,C,D,C,D,A, +S354,8Y,B,C,B,A,A,B,B,C,C,B +S362,8X,B,A,D,C,A,B,B,B,,C +S376,8X,B,D,A,A,D,,B,D,D,D +S305,8X,B,D,A,A,D,B,B,B,D,D +S371,8X,B,C,C,A,D,B,B,B,C,D +S221,8Y,C,A,A,A,D,D,B,A,A,A +S239,8Y,C,C,B,D,A,D,B,C,A,C +S159,8Y,B,A,D,A,D,B,B,C,C,A +S379,8Y,B,A,C,A,D,B,B,C,C,D +S252,8Y,B,B,B,A,D,B,B,C,C,D +S278,8X,B,A,D,A,D,B,B,B,C,B +S186,8Y,B,A,D,A,D,B,B,C,C,A +S003,8X,B,A,B,A,D,B,B,B,C,D +S040,8X,B,D,A,A,D,B,B,A,C,D +S343,8X,B,A,B,A,,B,B,D,A,A diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0065/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0065/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0065/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0065/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7cdc301aac8bd70edf510f4840dc299355a021d4 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 65 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..27f5b357e4fd2906a84fd8c892f2163a1cfab78d --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/checks.json @@ -0,0 +1,184 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.467, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.125, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.067, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.633, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.567, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.933, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.2, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.375, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.367, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.267, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q10", + "Q3", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.739, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0065/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0065/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..c672d9caeaa14bc320aa13077869cd56afd885fc --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/item_analysis_notes.md @@ -0,0 +1,28 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..a20f2483085d26e22ac09704599684a77fdedfc7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,A +Q2,B +Q3,A +Q4,B +Q5,B +Q6,A +Q7,C +Q8,B +Q9,C +Q10,B diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..9393242ecc7a857da1c51e4920d11d49ea098421 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/environment/workdir/responses.csv @@ -0,0 +1,39 @@ +student_id,class,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S222,8Y,A,D,A,C,B,C,B,A,,A +S211,8Y,A,B,A,D,,C,B,B,A,D +S286,8X,A,B,A,C,C,C,A,B,C,C +S239,8Y,B,C,A,D,B,,C,B,B,C +S100,8Y,A,B,A,A,B,D,,B,C,A +S022,8X,,D,A,D,B,B,B,B,A,C +S023,8Y,B,D,B,A,B,D,B,D,C,A +S149,8Y,D,C,A,,B,C,A,A,B, +S326,8Y,D,C,D,D,,D,B,C,D,C +S204,8X,A,B,A,D,B,C,A,B,C,A +S090,8X,A,B,A,C,B,A,D,D,C,C +S146,8X,A,B,A,C,B,C,A,B,C,A +S067,8Y,B,A,C,C,B,D,B,A,C,A +S112,8X,A,B,A,A,B,B,C,B,,C +S028,8Y,A,B,A,B,A,A,C,B,C,B +S370,8Y,A,B,A,D,C,A,A,B,C,B +S084,8X,A,C,A,D,B,B,,A,C,D +S369,8X,A,B,A,D,D,C,B,B,C,A +S014,8Y,A,D,A,D,C,C,A,C,A,B +S088,8Y,A,B,A,B,D,A,C,B,B,B +S066,8X,D,B,A,A,B,B,C,C,D, +S317,8X,A,B,A,C,B,B,C,B,C,B +S179,8Y,A,B,A,A,B,B,B,B,C,B +S386,8Y,A,C,A,A,B,D,C,B,C,A +S384,8Y,D,B,A,D,B,A,A,B,B,C +S062,8X,A,B,A,A,D,A,C,B,C,A +S287,8X,A,B,A,C,C,D,B,B,D,D +S280,8Y,A,A,A,A,B,C,C,B,C,A +S051,8Y,A,D,A,A,B,C,C,B,B,D +S267,8Y,A,B,A,,,B,A,B,C,A +S102,8Y,A,B,A,C,,D,B,B,C,D +S390,8Y,A,B,A,D,C,D,C,B,,A +S041,8Y,A,,A,B,B,A,B,B,B,B +S352,8X,D,A,D,D,B,D,C,B,B, +S394,8X,A,B,A,A,A,B,A,,C,A +S302,8X,C,B,A,C,D,B,C,B,,A +S196,8Y,A,B,A,D,B,A,A,B,D,D +S141,8Y,A,B,A,B,C,A,C,B,C,D diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/instruction.md b/tasks/class-test/class-test-item-analysis-d2-t0077/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..9036d1443aad56ef6c70bdc70583768735ba40b7 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/instruction.md @@ -0,0 +1,16 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d2-t0077/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d2-t0077/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/task.toml b/tasks/class-test/class-test-item-analysis-d2-t0077/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..bc333f4176d2f7edf705b95348867045f8a3e2eb --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 77 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/tests/checks.json b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..451610703534cafbc007b587a6334c0411a36870 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/checks.json @@ -0,0 +1,184 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.737, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.632, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.895, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.105, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.579, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": -0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.237, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.368, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.3, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.737, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.526, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.184, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q10", + "Q4", + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/tests/test.sh b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d2-t0077/tests/verify.py b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d2-t0077/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..3eb6971a466f16059a7c81126d45444251936e3f --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,B +Q3,A +Q4,C +Q5,D +Q6,C +Q7,D +Q8,D +Q9,C +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..49bff9bf09a7164b76e6de8ef195d97bd6867371 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/environment/workdir/responses.csv @@ -0,0 +1,37 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S098,8X,present,C,B,A,C,A,A,D,D,A,C +S132,8X,present,A,B,A,C,C,C,A,D,D,C +S214,8Y,present,C,B,A,C,,,B,B,A,C +S042,8Y,present,C,B,D,C,C,B,D,B,A,C +S056,8Y,present,,B,A,C,B,B,A,A,D,A +S449,8Y,absent,,,,,,,,,, +S279,8X,present,D,A,A,C,A,B,B,C,B,B +S404,8X,absent,,,,,,,,,, +S357,8Y,present,B,B,A,C,A,C,D,D,B,C +S341,8X,present,C,B,D,B,A,B,D,D,D,A +S094,8X,present,B,B,A,C,C,A,C,D,B,C +S447,8X,absent,,,,,,,,,, +S118,8Y,present,B,B,A,C,A,C,A,D,A,D +S148,8X,present,D,B,A,D,,D,D,D,B,C +S487,8X,absent,,,,,,,,,, +S091,8X,present,D,B,A,C,B,A,D,C,C,C +S361,8Y,present,B,B,A,C,A,C,D,D,C,C +S216,8X,present,A,D,A,C,B,A,D,D,B,B +S108,8Y,present,D,B,A,C,C,A,D,D,D,C +S070,8Y,present,C,C,A,C,A,B,,B,D,D +S087,8Y,present,C,B,A,C,A,D,B,D,C,D +S138,8Y,present,C,B,A,C,A,A,B,D,B,B +S283,8X,present,B,A,A,C,A,D,B,D,C,D +S116,8X,present,B,B,A,C,,C,D,D,B,A +S100,8Y,present,A,D,A,A,A,C,,D,A,C +S245,8Y,present,C,C,A,A,A,B,,B,B,A +S391,8Y,present,B,B,A,C,D,C,D,D,C,C +S146,8X,present,C,B,A,A,B,D,A,A,D,D +S017,8Y,present,D,B,A,C,B,C,D,D,A,C +S076,8Y,present,D,B,B,C,,C,A,B,B,C +S218,8Y,present,B,B,C,B,C,B,C,D,D,C +S368,8Y,present,B,B,A,C,A,B,C,D,A,C +S032,8X,present,C,B,A,C,,C,A,D,C,D +S061,8Y,present,B,B,A,C,A,C,A,D,D,A +S265,8X,present,C,B,A,C,B,A,D,D,D,C +S210,8X,present,B,A,A,B,B,B,B,A,A,B diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/instruction.md b/tasks/class-test/class-test-item-analysis-d3-e0009/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-e0009/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-e0009/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/task.toml b/tasks/class-test/class-test-item-analysis-d3-e0009/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..04adb01b9c90a5fdd715def37066a3f003ea3ab9 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10009 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f40641bf8c33932f01746a48de1f78b7d83d06e2 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/checks.json @@ -0,0 +1,266 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 9, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.344, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.46, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.781, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.544, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.222, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.224, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.781, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.507, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.031, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.111, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.44, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.344, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.778, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.588, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.406, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.508, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.688, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.646, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.188, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.436, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.531, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.504, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q5", + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.64, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 32, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-e0009/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0009/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..accf89b38a787d231caddac9dd38de4dec53baed --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,D +Q2,D +Q3,D +Q4,B +Q5,C +Q6,A +Q7,D +Q8,C +Q9,A +Q10,A diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..53c7603c3929d9b7b377549812bb353a9e947688 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/environment/workdir/responses.csv @@ -0,0 +1,43 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S268,8Y,present,,A,D,C,D,A,B,C,C,A +S127,8X,present,A,A,B,B,B,D,D,C,D,A +S157,8Y,present,A,D,B,B,B,A,A,C,B,B +S122,8Y,present,B,D,A,A,B,A,D,C,A,B +S084,8X,present,B,D,C,A,C,A,D,C,B,A +S454,8X,absent,,,,,,,,,, +S205,8Y,present,C,B,B,D,C,A,B,C,C,C +S050,8X,present,,A,A,C,B,A,,C,C,A +S209,8Y,present,D,A,D,D,C,D,A,C,A,A +S348,8X,present,,A,D,C,A,A,B,C,A,A +S094,8X,present,B,D,B,D,C,A,C,C,D,D +S401,8X,absent,,,,,,,,,, +S051,8Y,present,B,C,D,C,A,A,A,B,A,A +S044,8X,present,A,C,C,D,D,D,A,C,A,B +S238,8Y,present,A,A,C,A,D,C,,C,C,B +S360,8Y,present,B,D,D,D,C,A,D,C,B,A +S332,8X,present,C,C,B,C,B,B,C,C,C,B +S128,8X,present,A,A,D,B,C,A,A,C,A,D +S300,8Y,present,B,A,C,C,B,A,C,C,D,A +S002,8X,present,B,A,B,C,D,A,A,C,A,B +S367,8Y,present,D,D,D,B,C,A,A,C,B,A +S140,8X,present,,A,C,B,C,C,A,C,D,A +S387,8Y,present,A,B,D,D,B,C,C,C,B,A +S017,8Y,present,B,C,D,B,C,A,B,C,D,A +S230,8Y,present,D,B,D,C,A,A,C,C,C,D +S321,8Y,present,B,A,B,C,A,A,C,C,,A +S247,8Y,present,C,C,D,C,B,B,C,C,C,C +S263,8Y,present,C,B,D,B,C,A,D,C,B,B +S138,8Y,present,C,B,C,B,A,A,B,C,A,D +S040,8X,present,A,D,A,D,D,A,D,C,,A +S004,8Y,present,A,B,C,B,C,A,A,C,A,A +S340,8Y,present,D,D,A,B,B,A,D,C,C,A +S007,8X,present,A,B,C,B,C,A,A,C,A,D +S282,8Y,present,C,D,C,B,B,C,D,C,C,D +S385,8Y,present,A,D,D,B,C,A,D,C,B,A +S266,8X,present,C,C,B,D,D,A,C,C,A,D +S129,8X,present,C,A,D,,A,A,A,C,B,B +S240,8X,present,A,C,A,D,D,A,,C,B,C +S147,8X,present,,D,C,A,C,A,C,C,A,A +S121,8X,present,A,D,D,,B,A,C,C,C,A +S041,8Y,present,A,A,C,C,B,B,A,C,A,B +S125,8Y,present,A,D,A,A,D,A,B,C,B,A diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/instruction.md b/tasks/class-test/class-test-item-analysis-d3-e0021/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-e0021/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-e0021/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/task.toml b/tasks/class-test/class-test-item-analysis-d3-e0021/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..f80cc1f29cc9a886d3984f39886c09eb33d47773 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "eval"] +isced_f = "0111" +family = "item-analysis" +split = "eval" +seed = 10021 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..a61778ed457f3e17e91434fa0883b1248cc1773c --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/checks.json @@ -0,0 +1,270 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.377, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.325, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.545, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.533, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.375, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.381, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.325, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.502, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.35, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.818, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.635, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.225, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.464, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.975, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.025, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.325, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.091, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.044, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.525, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.545, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.494, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q8", + "Q9" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q1", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.439, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 40, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q8", + "Q9" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-e0021/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-e0021/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..1bcd6e04f9f73f4900c124a1483a4557d76cbaa9 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,D +Q3,C +Q4,C +Q5,B +Q6,C +Q7,D +Q8,C +Q9,B +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..fde9e0ea3f91ad7e9c8cb47c2501132eb842db91 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/environment/workdir/responses.csv @@ -0,0 +1,40 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S389,8X,present,D,B,C,A,B,C,D,A,B,C +S394,8X,present,D,C,C,D,B,D,A,A,A,B +S078,8Y,present,B,C,C,C,C,B,D,,B,C +S126,8X,present,B,B,C,D,D,A,B,D,A,A +S165,8Y,present,A,A,C,A,D,B,D,D,D,D +S232,8Y,present,D,A,C,A,B,,A,B,C,A +S328,8Y,present,D,D,C,,A,C,A,A,B,A +S286,8Y,present,B,A,C,C,B,B,D,B,D,C +S247,8X,present,B,,C,A,A,C,C,C,B,C +S257,8X,present,C,C,C,A,B,B,B,C,B,B +S385,8Y,present,B,C,C,C,C,C,A,A,B,C +S218,8X,present,A,A,C,B,B,A,D,B,D,C +S050,8Y,present,D,A,C,,B,C,D,C,B,C +S113,8Y,present,B,A,C,C,A,C,D,C,B,C +S482,8Y,absent,,,,,,,,,, +S261,8X,present,B,B,C,B,B,D,C,C,B,C +S188,8Y,present,B,,C,A,C,,D,C,B,C +S030,8X,present,B,A,C,C,B,B,C,C,,C +S264,8Y,present,B,B,C,B,B,B,A,D,A,D +S063,8X,present,,C,C,A,B,B,D,A,A,D +S249,8Y,present,D,A,C,B,B,B,C,C,,B +S003,8X,present,A,A,B,B,B,B,A,,C,D +S298,8X,present,,A,C,B,A,A,D,,B,D +S332,8X,present,,C,C,A,B,B,C,D,D,B +S266,8X,present,B,B,C,B,C,,C,D,D,C +S397,8Y,present,,B,C,A,B,A,D,A,B,C +S256,8Y,present,A,D,C,D,B,C,A,A,B,C +S184,8Y,present,B,C,C,A,,B,C,A,B,C +S159,8X,present,B,D,D,B,B,C,D,B,A,B +S409,8Y,absent,,,,,,,,,, +S164,8Y,present,,B,C,,B,A,D,D,B,A +S342,8Y,present,,A,C,B,B,B,A,A,D,D +S209,8Y,present,D,B,C,B,A,D,D,A,B,C +S084,8X,present,B,C,C,C,B,A,D,,B,C +S067,8X,present,A,C,C,C,B,D,A,D,D,C +S036,8Y,present,B,A,C,C,D,B,D,C,B,C +S170,8Y,present,B,C,C,B,B,D,D,B,B,C +S227,8X,present,B,B,C,B,,A,C,D,B,A +S024,8Y,present,D,A,C,A,B,B,A,D,A,A diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0009/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0009/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0009/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0009/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c9265729b1bbf109a205779ffaaf4798ca7962d3 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 9 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..6e940341b71a7b3cf8a6c41c81c6907c34e0294a --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/checks.json @@ -0,0 +1,274 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.459, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.488, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.081, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.11, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.946, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.173, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.216, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.552, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.622, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": -0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": -0.105, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.216, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.481, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.459, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.459, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.243, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.484, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.541, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.637, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.541, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.785, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q2", + "Q4", + "Q6", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.489, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 37, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q2", + "Q3", + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0009/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0009/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..05a8cfa588ce63719da0c9423c6aeffafe88a3ed --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,D +Q3,A +Q4,A +Q5,C +Q6,C +Q7,B +Q8,C +Q9,A +Q10,B diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..3faa810fd81364ce1ee242df68e06e4b3a72fc43 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/environment/workdir/responses.csv @@ -0,0 +1,47 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S357,8Y,present,B,D,A,C,C,B,A,D,B,D +S323,8Y,present,B,D,A,A,B,D,A,A,C,A +S017,8X,present,B,D,A,A,C,C,B,B,D,B +S011,8X,present,B,A,A,A,C,C,A,C,A,B +S460,8X,absent,,,,,,,,,, +S382,8Y,present,B,D,A,A,C,B,B,C,A,B +S294,8X,present,B,D,A,A,C,C,C,,A,B +S292,8X,present,D,D,A,A,D,B,A,C,C,C +S155,8Y,present,C,D,A,A,C,D,D,A,B, +S165,8Y,present,B,B,A,A,D,B,D,C,A,A +S235,8Y,present,A,D,A,C,C,A,A,A,A,B +S360,8Y,present,A,B,C,B,D,A,C,C,C,D +S075,8X,present,D,D,A,A,A,D,D,C,C,D +S254,8X,present,B,D,A,A,C,C,C,A,A,B +S424,8Y,absent,,,,,,,,,, +S082,8Y,present,C,D,A,A,C,B,D,C,D,D +S330,8Y,present,C,D,A,C,A,C,D,C,C,C +S346,8X,present,B,D,A,A,C,D,A,D,C,B +S364,8X,present,A,A,A,D,A,A,D,A,C,C +S208,8X,present,C,,A,B,B,B,A,C,D,C +S281,8Y,present,D,D,A,A,D,C,C,C,B,D +S356,8Y,present,A,C,D,D,C,D,C,D,D,D +S105,8Y,present,A,B,A,D,D,,A,C,D,A +S138,8X,present,C,A,A,A,A,A,,C,D,B +S038,8X,present,A,D,A,D,C,D,A,A,D,B +S140,8X,present,B,,A,B,B,,,C,B,D +S347,8X,present,B,D,A,A,C,D,A,A,B,C +S046,8Y,present,A,D,A,C,D,D,,C,C,B +S359,8X,present,A,A,A,A,D,B,C,A,A,D +S115,8Y,present,B,D,A,A,C,C,A,B,A,B +S045,8X,present,D,D,A,A,C,B,D,D,A,B +S282,8Y,present,A,D,A,A,D,,C,A,,C +S100,8X,present,A,B,A,C,C,D,D,C,D,B +S257,8Y,present,C,D,A,A,C,,A,A,D,B +S185,8X,present,C,C,A,C,A,B,A,,C,D +S212,8X,present,A,D,A,A,C,C,B,D,A,B +S197,8X,present,B,D,A,A,C,C,D,B,A,B +S437,8Y,absent,,,,,,,,,, +S053,8Y,present,B,D,A,A,C,C,D,B,A,B +S050,8X,present,C,C,B,C,A,A,A,C,B,C +S261,8X,present,C,D,A,A,B,B,D,,B,D +S348,8Y,present,,,A,B,D,A,A,A,C,C +S436,8X,absent,,,,,,,,,, +S144,8X,present,C,D,A,D,C,D,D,C,A,A +S019,8Y,present,A,D,A,A,C,B,C,B,A,B +S318,8X,present,C,D,A,A,C,B,C,A,A,B diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0021/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0021/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0021/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0021/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..e91e6185002da5b614b955cb5a22e2770cf0683c --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 21 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..0e0b1d085c5f5fa27dcd13e28366d379919e1d3e --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/checks.json @@ -0,0 +1,268 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.595, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.603, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.929, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.433, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.619, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.653, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.548, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.909, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.676, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.238, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.646, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.071, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.405, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": -0.182, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": -0.178, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.357, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.818, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.679, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.452, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q6", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.706, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 42, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0021/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0021/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..2b0a1c6d37d7a791629e2232dfa748499ba82f21 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,D +Q2,B +Q3,C +Q4,B +Q5,C +Q6,D +Q7,C +Q8,C +Q9,D +Q10,D diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..dbcd06b8058216d715bdb8ff7c72ff700db1c6f0 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/environment/workdir/responses.csv @@ -0,0 +1,39 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S187,8Y,present,,D,B,B,C,B,C,,D,C +S082,8X,present,D,D,C,B,C,D,C,A,D,C +S319,8X,present,D,A,A,B,D,D,B,C,C,D +S246,8X,present,B,C,D,B,A,D,C,C,C,B +S143,8X,present,B,B,D,B,C,D,C,C,D,D +S250,8Y,present,A,,B,B,B,A,C,D,A,B +S356,8Y,present,D,B,C,B,C,A,C,C,D,D +S084,8Y,present,C,A,C,B,C,D,A,C,D,B +S323,8Y,present,C,C,B,B,D,C,C,D,C,A +S322,8X,present,A,C,B,B,C,A,C,C,D,D +S113,8X,present,B,C,A,B,D,D,A,A,C,D +S039,8Y,present,D,B,C,B,C,C,C,C,D,D +S234,8X,present,C,A,C,B,B,C,C,D,B,B +S301,8X,present,B,D,C,B,B,C,B,D,B,B +S091,8Y,present,A,D,C,B,C,B,C,C,D,D +S255,8Y,present,A,D,A,B,D,B,,B,A,B +S095,8X,present,A,C,D,B,B,B,B,A,A, +S067,8Y,present,D,A,C,B,C,D,C,B,D,D +S331,8X,present,C,C,C,B,C,C,C,A,D,B +S062,8X,present,B,C,C,D,C,D,C,A,D,B +S423,8Y,absent,,,,,,,,,, +S342,8X,present,B,C,A,B,D,B,C,C,B,B +S394,8Y,present,A,D,C,B,,D,C,C,D,D +S041,8Y,present,D,B,C,B,C,B,C,C,D,D +S312,8X,present,C,,D,B,C,A,C,C,D,D +S355,8Y,present,B,C,B,C,B,A,C,C,D,C +S070,8X,present,A,D,C,B,D,D,D,D,D,C +S166,8Y,present,B,D,D,B,C,D,D,C,D,D +S381,8Y,present,C,A,C,B,D,,B,B,D,D +S494,8Y,absent,,,,,,,,,, +S374,8X,present,D,A,C,B,C,D,C,A,D,D +S341,8Y,present,A,A,D,B,D,D,C,C,B,C +S353,8X,present,B,C,C,B,D,B,C,B,D,D +S235,8X,present,D,C,C,B,C,D,C,C,D,D +S218,8X,present,D,,B,B,C,D,C,A,D,D +S256,8X,present,A,C,C,A,B,D,C,C,,D +S385,8X,present,C,B,C,B,C,A,C,C,D,D +S068,8X,present,B,A,B,B,D,C,B,B,B,A diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0033/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0033/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0033/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0033/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7d11b11ae26713d4d84bb8513eb47cdd25c9f6d6 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 33 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..88b1999c9908664034b36b22ee6a96dee01f3713 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/checks.json @@ -0,0 +1,268 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.643, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.139, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.564, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.528, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.497, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.917, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.103, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.773, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.305, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.722, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.474, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.477, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.639, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.738, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.528, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.702, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q1", + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.728, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 36, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0033/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0033/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..bcccfb90c7abf56c02f6aad7135db248c8a744f3 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,B +Q2,B +Q3,B +Q4,D +Q5,D +Q6,D +Q7,B +Q8,C +Q9,D +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..cdff8d100596a1587ab3e89ef63031dd2d9f22a2 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/environment/workdir/responses.csv @@ -0,0 +1,37 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S484,8Y,absent,,,,,,,,,, +S025,8Y,present,B,C,B,D,D,C,D,C,D,C +S213,8X,present,C,D,B,D,B,B,B,B,D,D +S010,8Y,present,B,D,B,D,A,,B,B,A,B +S086,8X,present,A,D,B,D,D,D,B,C,D,C +S112,8Y,present,B,A,B,D,D,B,D,D,D,C +S380,8Y,present,B,C,A,D,D,D,B,,D,B +S250,8Y,present,B,A,B,D,D,C,D,A,D,A +S022,8X,present,B,B,B,D,D,D,B,C,D,C +S029,8X,present,C,B,B,D,A,C,B,A,D,C +S152,8X,present,A,C,B,D,D,D,B,,D,A +S032,8Y,present,C,D,B,D,D,D,A,C,,C +S081,8X,present,D,D,C,D,,C,D,,A,A +S138,8X,present,B,D,A,D,C,C,B,A,A,A +S296,8Y,present,,C,B,D,D,D,B,A,B,C +S386,8X,present,B,D,B,D,D,A,B,A,D,C +S143,8Y,present,B,B,B,D,D,D,B,C,D,C +S340,8X,present,B,C,B,D,B,B,B,C,B,C +S437,8X,absent,,,,,,,,,, +S047,8Y,present,A,C,A,D,C,D,C,D,D,A +S222,8X,present,B,D,D,C,C,D,D,D,,A +S202,8Y,present,B,D,B,D,C,D,B,C,D,A +S225,8Y,present,B,A,D,D,B,D,C,B,B,A +S079,8Y,present,A,D,D,D,C,A,A,C,B,C +S216,8X,present,C,,C,D,B,D,B,A,B,D +S375,8Y,present,B,D,B,D,B,B,B,B,D,A +S276,8X,present,B,D,B,D,D,D,B,C,D,C +S235,8X,present,B,A,B,D,D,A,B,C,D,C +S352,8Y,present,B,A,C,D,D,D,A,C,C,B +S232,8Y,present,B,D,D,D,A,B,B,A,D,B +S062,8X,present,B,A,B,D,D,D,B,D,D,D +S275,8X,present,B,A,B,C,C,D,A,C,,B +S289,8X,present,B,C,B,D,D,D,B,C,D,C +S287,8X,present,B,A,B,D,D,A,B,C,D,C +S240,8Y,present,D,A,B,D,A,C,,B,C,B +S058,8X,present,C,C,B,D,D,A,B,A,D,A diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0045/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0045/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0045/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0045/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..6f7771c2b9ab061f88ea73eadb977192537d15a8 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 45 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..45aa1acec8601c87b9bc5eca210c3ec154627722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/checks.json @@ -0,0 +1,265 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 9, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.647, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.444, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.357, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.088, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.222, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.424, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.706, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.778, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.941, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.222, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.274, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.529, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.728, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.111, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.255, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.647, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.517, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.412, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.586, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.618, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.889, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.627, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.441, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.778, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.67, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q6" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q4" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.687, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 34, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0045/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0045/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..ca98549c959af791da30eca57bdc028a045c47c6 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,A +Q2,D +Q3,A +Q4,D +Q5,B +Q6,C +Q7,A +Q8,A +Q9,A +Q10,A diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..ae02db5573ff0606d00bf699e88e23bcb3fda139 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/environment/workdir/responses.csv @@ -0,0 +1,46 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S197,8Y,present,A,D,A,C,A,A,A,C,D,A +S093,8X,present,A,C,C,A,A,D,D,B,B,A +S347,8X,present,A,C,A,D,A,C,A,B,A,A +S187,8X,present,A,A,D,D,A,B,B,D,A,B +S111,8X,present,A,D,D,C,D,A,C,C,C,A +S417,8X,absent,,,,,,,,,, +S193,8Y,present,A,D,A,C,B,C,A,C,C,B +S262,8X,present,A,B,B,C,B,A,B,C,D,B +S359,8Y,present,A,B,A,D,D,C,A,A,A,C +S057,8X,present,A,D,A,D,A,C,,C,A, +S397,8X,present,A,D,A,B,D,C,A,B,A,A +S113,8Y,present,A,D,D,C,A,A,D,C,D,A +S358,8Y,present,A,D,A,D,B,C,A,B,A,A +S349,8X,present,A,D,A,D,B,C,B,D,A,A +S136,8Y,present,A,D,A,D,B,C,A,C,A,A +S172,8Y,present,A,D,A,D,,D,,C,A,A +S237,8X,present,A,D,A,B,C,C,A,C,A,D +S094,8X,present,A,A,C,D,,B,B,B,C,A +S263,8X,present,A,D,A,A,D,B,D,D,A,D +S003,8Y,present,A,D,A,C,D,D,B,B,A,D +S177,8X,present,A,D,A,B,B,C,A,D,A,D +S122,8Y,present,A,A,A,D,,B,C,C,A,D +S246,8Y,present,A,D,A,D,B,C,C,B,A,A +S374,8X,present,A,D,A,D,C,C,,B,A,A +S286,8X,present,A,D,A,D,B,C,D,A,A,B +S309,8Y,present,A,D,A,D,B,C,C,D,B,C +S043,8Y,present,A,B,C,D,C,C,C,B,A,A +S169,8Y,present,A,D,A,D,C,,A,C,A,C +S135,8Y,present,A,D,A,D,B,C,A,A,A,A +S412,8X,absent,,,,,,,,,, +S270,8X,present,A,D,A,D,C,C,B,C,A,B +S165,8Y,present,A,D,A,D,B,C,C,D,A,A +S161,8X,present,C,B,,D,D,D,B,C,C,B +S340,8X,present,A,A,A,D,A,B,D,C,C,D +S183,8Y,present,A,B,B,A,A,A,B,,A,A +S324,8X,present,A,D,A,D,C,D,C,B,A,A +S302,8Y,present,A,A,C,D,C,A,D,,A,B +S018,8X,present,A,A,B,D,,B,D,A,A,A +S004,8X,present,A,A,B,C,C,D,C,B,A,A +S095,8X,present,A,D,A,D,C,C,A,C,A,A +S118,8X,present,A,D,A,D,A,D,D,C,A,A +S471,8Y,absent,,,,,,,,,, +S402,8X,absent,,,,,,,,,, +S196,8Y,present,A,,D,,C,B,C,C,C,D +S226,8Y,present,A,D,A,B,A,B,A,B,A,A diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0057/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0057/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0057/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0057/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..f635657af2f29a9f3ef4545fc2c5007df33db512 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 57 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..631d0ce7b59ad89c8077fd9f9663e25aee7faef5 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/checks.json @@ -0,0 +1,266 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.976, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.091, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.304, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.634, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.635, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.683, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.909, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.756, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.634, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.455, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.412, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.268, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.533, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.463, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.776, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.317, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.636, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.536, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.098, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.309, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.732, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.727, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.582, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.561, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.273, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.294, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q5", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.703, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 41, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0057/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0057/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..ae1511964358888e6335ccf83886ac94fc371b83 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,A +Q2,C +Q3,B +Q4,A +Q5,A +Q6,C +Q7,C +Q8,B +Q9,C +Q10,D diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..91555a5be903db078e0b819f2319b75bc81a7554 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/environment/workdir/responses.csv @@ -0,0 +1,36 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S398,8X,present,D,D,C,D,A,B,D,,B,B +S283,8Y,present,C,A,A,,A,A,B,B,,A +S056,8Y,present,C,B,D,A,A,C,C,B,A,A +S197,8X,present,D,,B,A,A,C,C,C,B,D +S316,8Y,present,B,D,D,C,A,A,D,B,B,B +S155,8X,present,D,B,,D,A,,C,B,C,B +S084,8Y,present,A,A,C,D,A,C,C,B,C,A +S279,8X,present,D,A,A,A,A,C,C,C,C,D +S160,8X,present,A,D,B,A,A,C,C,B,,D +S057,8X,present,B,A,C,A,A,C,C,C,C,B +S261,8Y,present,A,D,B,A,A,C,C,D,C,D +S423,8Y,absent,,,,,,,,,, +S486,8X,absent,,,,,,,,,, +S065,8X,present,D,B,B,A,A,D,C,B,C,D +S099,8Y,present,A,D,B,A,A,C,C,A,C,D +S182,8X,present,B,B,B,A,A,C,C,B,C,C +S128,8Y,present,D,D,C,B,A,C,C,B,A,D +S427,8X,absent,,,,,,,,,, +S030,8Y,present,A,B,C,D,A,A,C,C,D,B +S231,8X,present,B,,,C,A,A,C,B,C,A +S293,8Y,present,A,D,C,C,A,C,C,B,A,D +S359,8X,present,B,D,D,C,A,C,C,B,A,A +S216,8X,present,A,B,C,D,A,C,C,B,C,A +S326,8Y,present,D,A,B,C,A,D,C,B,C,D +S040,8X,present,C,A,C,B,A,B,C,B,A,A +S352,8Y,present,D,B,A,D,C,D,B,B,D,D +S386,8Y,present,A,D,B,C,A,C,C,B,C,D +S413,8X,absent,,,,,,,,,, +S294,8Y,present,A,A,B,B,A,,C,C,A,A +S329,8Y,present,A,D,A,D,A,A,C,D,D,D +S033,8X,present,A,D,B,B,A,,C,B,C,D +S185,8Y,present,C,D,D,C,A,B,C,B,B,C +S167,8X,present,A,C,D,,A,D,C,C,C,D +S272,8X,present,C,,D,B,A,B,C,A,A,D +S113,8X,present,A,C,B,A,A,D,C,C,C,D diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0069/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0069/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0069/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0069/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8748066c93f7b185c5387c7dbdd6259939d6b953 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 69 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..be96b8da7513239b375e1e39561404087a336261 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/checks.json @@ -0,0 +1,270 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.419, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.52, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.065, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.125, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.25, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.355, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 1.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.707, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.323, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.75, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.59, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.968, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": 0.125, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": 0.275, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.452, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.546, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.871, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.5, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.628, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.613, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.0, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": -0.04, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.484, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.875, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.637, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.516, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.625, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.539, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q2", + "Q5", + "Q8" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q5", + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.605, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 31, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q8" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0069/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0069/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/environment/Dockerfile b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/item_analysis_notes.md b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/item_analysis_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..538c377143b012c36be179c55874ebf1226c2534 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/item_analysis_notes.md @@ -0,0 +1,36 @@ +# Item analysis: definitions + +A student's item score is 1 if the answer is correct and 0 otherwise (an empty cell is an unanswered item and counts as wrong). A student's total is the sum of the item scores. `N` is the number of students who sat the test. Rows with status `absent` are not part of the analysis. + +## Facility + +The facility of an item is the proportion of the `N` students who got it right. + +## Discrimination index + +1. Put the students in order of total, highest first. Students with the same total are ordered by student id, ascending (`S012` before `S105`). +2. The group size `n_group` is 27% of `N`, rounded to the nearest whole number (round half up). +3. The upper group is the first `n_group` students in that order and the lower group is the last `n_group` students. +4. Discrimination = (number in the upper group who got the item right - number in the lower group who got it right) / `n_group`. + +An item has poor discrimination if its discrimination index is below 0.20. + +## Easy and hard items + +An item is too easy if its facility is above 0.85 and too hard if its facility is below 0.30. + +## KR-20 reliability + +With `k` items, facility `p_j` for item `j`, and `var` the variance of the students' totals computed with `N` in the denominator (population variance): + + KR-20 = k / (k - 1) * (1 - sum over items of p_j * (1 - p_j) / var) + +(the sum is taken first, then divided by `var`). + +## Point-biserial correlation + +For an item with facility `p`, let `M1` be the mean total of the students who got it right, `M0` the mean total of the students who got it wrong, and `s` the standard deviation of all `N` totals (square root of the population variance above). The total includes the item itself. + + r_pb = (M1 - M0) / s * sqrt(p * (1 - p)) + +An item needs review if its point-biserial is below 0.20. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/key.csv b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5a9ac4afd323b7fb5adde0bcce5213c915756456 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/key.csv @@ -0,0 +1,11 @@ +item,key +Q1,D +Q2,B +Q3,B +Q4,A +Q5,A +Q6,D +Q7,C +Q8,D +Q9,A +Q10,C diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/responses.csv b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/responses.csv new file mode 100644 index 0000000000000000000000000000000000000000..19bf4272e2d03ae7a30a86f7826cb27829743675 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/environment/workdir/responses.csv @@ -0,0 +1,41 @@ +student_id,class,status,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10 +S258,8Y,present,B,B,D,D,A,D,C,D,C,A +S302,8X,present,A,C,D,A,A,C,C,B,B,D +S192,8X,present,C,B,D,A,A,B,C,D,B,A +S324,8X,present,C,B,C,B,A,C,C,B,C, +S476,8X,absent,,,,,,,,,, +S393,8X,present,B,C,D,B,A,D,,A,B,D +S317,8X,present,D,B,B,C,,D,C,C,A,C +S237,8X,present,A,B,D,D,C,D,C,D,A,A +S333,8Y,present,C,A,D,D,A,A,D,C,D,A +S284,8Y,present,D,B,B,A,A,D,C,D,A,C +S224,8X,present,A,B,B,C,C,D,C,D,A,B +S231,8Y,present,D,B,B,B,A,B,C,A,,D +S016,8Y,present,A,A,,D,B,C,C,A,D,B +S298,8Y,present,B,B,B,C,D,D,C,D,C,A +S460,8Y,absent,,,,,,,,,, +S131,8X,present,D,B,B,A,A,D,C,D,C,A +S287,8X,present,B,B,B,A,A,D,C,D,A,C +S045,8X,present,C,B,A,C,A,B,,A,C,B +S309,8Y,present,C,C,C,C,B,C,C,C,B,A +S162,8Y,present,A,B,D,B,A,A,C,A,A,B +S134,8X,present,A,C,A,D,A,A,C,A,B,D +S116,8X,present,,A,B,A,A,D,A,C,C,D +S319,8X,present,D,B,B,A,A,C,C,B,A,D +S019,8X,present,D,D,B,A,C,D,C,D,B,A +S037,8Y,present,D,,B,A,A,D,C,D,D,B +S332,8X,present,D,B,B,A,,D,C,D,,B +S152,8Y,present,A,B,B,A,C,D,C,A,D, +S365,8Y,present,D,B,B,A,B,D,C,C,A,C +S180,8Y,present,A,B,B,D,A,D,C,A,C,D +S018,8X,present,D,B,B,A,A,D,C,B,A,B +S011,8Y,present,B,D,A,B,A,A,C,C,B,B +S191,8X,present,,C,B,C,A,A,C,C,D,D +S273,8X,present,C,B,B,A,B,C,C,A,B,A +S296,8Y,present,D,B,B,C,D,D,C,B,D,C +S491,8X,absent,,,,,,,,,, +S049,8Y,present,D,A,B,B,A,,C,C,A, +S239,8Y,present,C,C,B,D,C,C,C,,A,D +S161,8X,present,D,B,B,A,B,D,C,D,A,D +S400,8Y,absent,,,,,,,,,, +S289,8X,present,D,B,B,D,A,D,C,B,C,B diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/instruction.md b/tasks/class-test/class-test-item-analysis-d3-t0081/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1940d9bef4cc4237be736ec8acbbc0c301378a15 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/instruction.md @@ -0,0 +1,19 @@ +# Class test: item analysis + +`responses.csv` holds the options chosen by the students of two classes on a 10-item multiple-choice test, and `key.csv` the correct options. The rows are in no particular order. `item_analysis_notes.md` defines every statistic: follow it exactly. Analyse the two classes together as one group. + +Write `answer.json` in this folder, a JSON object with: + +- `n_group`: the size of the upper group (and of the lower group), an integer. +- `items`: an object keyed by item (`Q1` to `Q10`). Each value has + - `facility`: 3 decimals. + - `discrimination`: the discrimination index, 3 decimals. + - `point_biserial`: 3 decimals. +- `poor_discrimination`: list of the items with poor discrimination (any order; an empty list if none). +- `too_easy`: list of the items that are too easy (any order; an empty list if none). +- `too_hard`: list of the items that are too hard (any order; an empty list if none). +- `kr20`: the KR-20 reliability of the test, 3 decimals. +- `n_students`: the number of students in the analysis, an integer. +- `needs_review`: list of the items whose point-biserial is below 0.20 (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/solution/oracle.py b/tasks/class-test/class-test-item-analysis-d3-t0081/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/solution/solve.sh b/tasks/class-test/class-test-item-analysis-d3-t0081/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/task.toml b/tasks/class-test/class-test-item-analysis-d3-t0081/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..6ed289865a8a667a270d725464f22f4729c78758 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "item-analysis", "isced-0111", "train"] +isced_f = "0111" +family = "item-analysis" +split = "train" +seed = 81 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/tests/checks.json b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..74d85fe64187919b80d681939b845c665eb69d8a --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/checks.json @@ -0,0 +1,267 @@ +[ + { + "path": "n_group", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "items.Q1.facility", + "type": "number", + "value": 0.389, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q1.point_biserial", + "type": "number", + "value": 0.696, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.facility", + "type": "number", + "value": 0.639, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.discrimination", + "type": "number", + "value": 0.7, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q2.point_biserial", + "type": "number", + "value": 0.596, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.facility", + "type": "number", + "value": 0.639, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.discrimination", + "type": "number", + "value": 0.9, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q3.point_biserial", + "type": "number", + "value": 0.694, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.facility", + "type": "number", + "value": 0.417, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q4.point_biserial", + "type": "number", + "value": 0.594, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.facility", + "type": "number", + "value": 0.611, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.discrimination", + "type": "number", + "value": -0.1, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q5.point_biserial", + "type": "number", + "value": -0.067, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.facility", + "type": "number", + "value": 0.556, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.discrimination", + "type": "number", + "value": 0.8, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q6.point_biserial", + "type": "number", + "value": 0.667, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.facility", + "type": "number", + "value": 0.889, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.discrimination", + "type": "number", + "value": 0.3, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q7.point_biserial", + "type": "number", + "value": 0.404, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.facility", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q8.point_biserial", + "type": "number", + "value": 0.542, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.facility", + "type": "number", + "value": 0.333, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.discrimination", + "type": "number", + "value": 0.6, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q9.point_biserial", + "type": "number", + "value": 0.517, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.facility", + "type": "number", + "value": 0.139, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.discrimination", + "type": "number", + "value": 0.4, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "items.Q10.point_biserial", + "type": "number", + "value": 0.521, + "abs_tol": 0.001, + "weight": 1.0 + }, + { + "path": "poor_discrimination", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_easy", + "type": "set", + "value": [ + "Q7" + ], + "item": "str", + "weight": 2 + }, + { + "path": "too_hard", + "type": "set", + "value": [ + "Q10" + ], + "item": "str", + "weight": 2 + }, + { + "path": "kr20", + "type": "number", + "value": 0.693, + "abs_tol": 0.001, + "weight": 2 + }, + { + "path": "n_students", + "type": "int", + "value": 36, + "weight": 1.0 + }, + { + "path": "needs_review", + "type": "set", + "value": [ + "Q5" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/tests/test.sh b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-item-analysis-d3-t0081/tests/verify.py b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-item-analysis-d3-t0081/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-e0002/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-e0002/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..9f5cad45b6b288888abac43d761543bacfb77d84 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,D,1 +Q2,C,1 +Q3,C,1 +Q4,B,1 +Q5,D,1 +Q6,A,1 +Q7,C,1 +Q8,A,1 +Q9,A,1 +Q10,C,1 +Q11,B,1 +Q12,A,1 +Q13,A,1 +Q14,C,1 +Q15,A,1 +Q16,C,1 +Q17,A,1 +Q18,B,1 +Q19,D,1 +Q20,C,1 diff --git a/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..3334fb8e3ed64caee681b72cbc9bd0687229c179 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S011,9A,C,D,A,D,D,C,C,,D,D,D,B,C,,A,C,B,A,B,D +S024,9A,C,C,C,D,,A,,A,A,C,B,A,D,A,C,C,A,B,B,A +S023,9B,D,C,C,A,D,A,C,A,A,B,B,A,A,C,A,C,A,B,D,C +S075,9B,A,A,C,D,A,A,,A,D,D,A,C,,A,D,C,A,,D,A +S146,9B,A,C,A,B,A,A,C,A,A,A,C,A,C,D,,C,A,,,B +S161,9A,D,C,C,B,D,A,C,A,A,A,B,A,A,C,A,C,A,B,D, diff --git a/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..775a952536578d42a3108e0b67890078e461537e --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 50 or more | +| D | 35 or more | +| U | under 35 | diff --git a/tasks/class-test/class-test-marking-d1-e0002/instruction.md b/tasks/class-test/class-test-marking-d1-e0002/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-e0002/solution/oracle.py b/tasks/class-test/class-test-marking-d1-e0002/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-e0002/solution/solve.sh b/tasks/class-test/class-test-marking-d1-e0002/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-e0002/task.toml b/tasks/class-test/class-test-marking-d1-e0002/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8360c04e39ebd5af150ffddfd5952d7b284f3baf --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10002 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-e0002/tests/checks.json b/tasks/class-test/class-test-marking-d1-e0002/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..8ba5cfa004d24154a70d2c4040aaf36e617cddad --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/tests/checks.json @@ -0,0 +1,132 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S011.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S011.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S011.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S023.marks", + "type": "int", + "value": 18, + "weight": 1.0 + }, + { + "path": "students.S023.percent", + "type": "number", + "value": 90.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S023.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S024.marks", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "students.S024.percent", + "type": "number", + "value": 55.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S024.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S075.marks", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "students.S075.percent", + "type": "number", + "value": 30.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S075.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S146.marks", + "type": "int", + "value": 9, + "weight": 1.0 + }, + { + "path": "students.S146.percent", + "type": "number", + "value": 45.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S146.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S161.marks", + "type": "int", + "value": 18, + "weight": 1.0 + }, + { + "path": "students.S161.percent", + "type": "number", + "value": 90.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S161.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S011", + "S075" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-e0002/tests/test.sh b/tasks/class-test/class-test-marking-d1-e0002/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-e0002/tests/verify.py b/tasks/class-test/class-test-marking-d1-e0002/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0002/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-e0014/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-e0014/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..a3dab665d3b7494fe7737c71ca95e504c489138a --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,C,1 +Q2,B,1 +Q3,C,1 +Q4,B,1 +Q5,D,1 +Q6,A,1 +Q7,B,1 +Q8,C,1 +Q9,B,1 +Q10,A,1 +Q11,B,1 +Q12,C,1 +Q13,D,1 +Q14,D,1 +Q15,B,1 +Q16,D,1 +Q17,A,1 +Q18,A,1 +Q19,C,1 +Q20,C,1 diff --git a/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..1ca929f34178dbeedf7b82ccf850976907a889f6 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S001,9A,,B,B,C,B,C,D,B,B,A,D,B,D,C,B,B,D,A,A,B +S172,9A,C,B,C,B,D,A,B,C,B,A,B,A,D,D,B,D,A,A,C,D +S008,9A,C,B,A,B,D,A,B,C,B,A,B,C,D,D,B,D,A,A,C,C +S010,9B,A,B,C,B,A,C,D,C,D,A,D,A,D,A,B,C,C,C,B,B +S370,9A,C,B,D,C,C,C,D,A,C,C,B,B,C,D,A,D,B,C,A,B +S336,9A,C,B,C,B,B,A,B,C,A,C,B,C,D,D,B,B,A,B,D,C diff --git a/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..1c394ee98aaf64b9d11c5e0a691170bd8b566523 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 55 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d1-e0014/instruction.md b/tasks/class-test/class-test-marking-d1-e0014/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-e0014/solution/oracle.py b/tasks/class-test/class-test-marking-d1-e0014/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-e0014/solution/solve.sh b/tasks/class-test/class-test-marking-d1-e0014/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-e0014/task.toml b/tasks/class-test/class-test-marking-d1-e0014/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7083fa9f1ae3652779bc3ef5ede320d061d08b3b --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10014 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-e0014/tests/checks.json b/tasks/class-test/class-test-marking-d1-e0014/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..537ac0821d2df067e9a062178e50a763f6cc0f92 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/tests/checks.json @@ -0,0 +1,133 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S001.marks", + "type": "int", + "value": 6, + "weight": 1.0 + }, + { + "path": "students.S001.percent", + "type": "number", + "value": 30.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S001.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S008.marks", + "type": "int", + "value": 19, + "weight": 1.0 + }, + { + "path": "students.S008.percent", + "type": "number", + "value": 95.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S008.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S010.marks", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "students.S010.percent", + "type": "number", + "value": 35.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S010.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S172.marks", + "type": "int", + "value": 18, + "weight": 1.0 + }, + { + "path": "students.S172.percent", + "type": "number", + "value": 90.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S172.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S336.marks", + "type": "int", + "value": 14, + "weight": 1.0 + }, + { + "path": "students.S336.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S336.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S370.marks", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "students.S370.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S370.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S001", + "S010", + "S370" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-e0014/tests/test.sh b/tasks/class-test/class-test-marking-d1-e0014/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-e0014/tests/verify.py b/tasks/class-test/class-test-marking-d1-e0014/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-e0014/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0002/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0002/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..f3ea12f96dca3dc9c8e06046e412cf8f29673bb4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,A,1 +Q2,D,1 +Q3,A,1 +Q4,A,1 +Q5,B,1 +Q6,A,1 +Q7,A,1 +Q8,B,1 +Q9,B,1 +Q10,A,1 +Q11,B,1 +Q12,C,1 +Q13,A,1 +Q14,C,1 +Q15,D,1 +Q16,A,1 +Q17,D,1 +Q18,D,1 +Q19,A,1 +Q20,A,1 diff --git a/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..1574319071c2041d7bd3aa91c032960e4e69a259 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S260,9B,A,D,A,A,B,A,A,B,B,A,B,C,A,C,D,A,D,D,A,A +S152,9B,D,D,C,,,B,D,C,A,A,B,C,A,,D,D,D,C,,A +S149,9B,A,D,A,A,B,A,A,B,B,A,B,B,A,C,D,A,D,D,A,A +S154,9A,A,,D,B,A,A,C,D,D,C,D,A,A,C,B,C,D,A,B,D +S053,9A,C,D,D,C,A,D,D,,B,D,C,A,A,B,D,D,A,,C,D +S188,9A,A,D,A,A,B,A,A,B,B,A,B,C,A,A,D,A,D,D,A,A diff --git a/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..1c394ee98aaf64b9d11c5e0a691170bd8b566523 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 55 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d1-t0002/instruction.md b/tasks/class-test/class-test-marking-d1-t0002/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0002/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0002/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0002/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0002/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0002/task.toml b/tasks/class-test/class-test-marking-d1-t0002/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d0b13ce42a03ea281cd839e58d84d7c648bfd9a7 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 2 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0002/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0002/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..bea0f5e566e393628b0caf6a0f80e605d78b02e1 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/tests/checks.json @@ -0,0 +1,133 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S053.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S053.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S053.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S149.marks", + "type": "int", + "value": 19, + "weight": 1.0 + }, + { + "path": "students.S149.percent", + "type": "number", + "value": 95.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S149.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S152.marks", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "students.S152.percent", + "type": "number", + "value": 40.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S152.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S154.marks", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "students.S154.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S154.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S188.marks", + "type": "int", + "value": 19, + "weight": 1.0 + }, + { + "path": "students.S188.percent", + "type": "number", + "value": 95.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S188.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S260.marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S260.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S260.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S053", + "S152", + "S154" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0002/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0002/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0002/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0002/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0002/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0014/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0014/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..b85cb516575b369882f876aa6c9abbd1dba79f6e --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,D,1 +Q2,D,1 +Q3,B,1 +Q4,C,1 +Q5,B,1 +Q6,D,1 +Q7,D,1 +Q8,A,1 +Q9,B,1 +Q10,A,1 +Q11,A,1 +Q12,D,1 +Q13,B,1 +Q14,D,1 +Q15,B,1 +Q16,D,1 +Q17,A,1 +Q18,A,1 +Q19,C,1 +Q20,D,1 diff --git a/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..9312f82c3ce3870d33c285be789451c21f6a1049 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S075,9A,A,B,,C,B,A,C,A,D,,,B,D,C,A,C,B,A,B,C +S018,9A,D,A,B,B,B,D,A,A,B,A,A,,B,C,D,D,C,A,C,D +S135,9A,D,D,A,C,B,D,D,A,B,A,A,D,,D,C,D,A,A,C,D +S175,9B,C,C,C,,,C,A,B,B,D,D,A,C,C,B,A,D,D,D,A +S011,9A,D,C,D,,B,,B,,D,B,B,A,C,C,B,B,B,D,A,B +S163,9A,D,D,B,C,B,D,D,A,B,A,A,D,B,D,B,D,A,A,C,D diff --git a/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..a62383ad725cb33664753d038ed84853f9b97155 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 50 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d1-t0014/instruction.md b/tasks/class-test/class-test-marking-d1-t0014/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0014/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0014/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0014/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0014/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0014/task.toml b/tasks/class-test/class-test-marking-d1-t0014/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..539eeea4f7ed49cbf145e9fe27eedb60daa810d6 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 14 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0014/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0014/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..030261cf84be436ab7f62ad67efa07ee639a1e16 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/tests/checks.json @@ -0,0 +1,133 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S011.marks", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "students.S011.percent", + "type": "number", + "value": 15.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S011.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S018.marks", + "type": "int", + "value": 13, + "weight": 1.0 + }, + { + "path": "students.S018.percent", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S018.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S075.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S075.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S075.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S135.marks", + "type": "int", + "value": 17, + "weight": 1.0 + }, + { + "path": "students.S135.percent", + "type": "number", + "value": 85.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S135.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S163.marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S163.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S163.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S175.marks", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "students.S175.percent", + "type": "number", + "value": 10.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S175.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S011", + "S075", + "S175" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0014/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0014/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0014/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0014/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0014/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0026/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0026/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..642a2b99ff9d9b7a208dc97fbdbd71dc92b9c125 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,D,1 +Q2,D,1 +Q3,D,1 +Q4,C,1 +Q5,D,1 +Q6,C,1 +Q7,D,1 +Q8,D,1 +Q9,A,1 +Q10,B,1 +Q11,C,1 +Q12,D,1 +Q13,B,1 +Q14,A,1 +Q15,C,1 +Q16,C,1 +Q17,B,1 +Q18,B,1 +Q19,A,1 +Q20,A,1 diff --git a/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..9fa789d5865d8fa48b3338c448934144dd0e78a1 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S156,9B,,B,B,A,A,B,B,B,B,A,C,A,B,A,D,B,D,C,A,B +S114,9A,A,D,D,C,D,C,C,D,C,B,C,D,C,A,C,C,A,B,A,D +S351,9B,C,B,,B,D,C,D,C,D,B,A,D,B,A,C,C,A,D,D,A +S196,9B,C,,D,B,D,C,C,D,A,D,C,D,D,D,,B,B,A,B,A +S272,9A,D,D,D,C,D,C,A,A,A,B,C,D,B,A,C,C,B,B,A,A +S111,9B,B,A,C,D,B,D,B,,C,C,C,B,D,,D,A,C,D,C,A diff --git a/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..9bb6905915713cd136d7c40c5c74e2456917e685 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d1-t0026/instruction.md b/tasks/class-test/class-test-marking-d1-t0026/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0026/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0026/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0026/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0026/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0026/task.toml b/tasks/class-test/class-test-marking-d1-t0026/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..0e38712d38b8e1c8e1b437298f32eb05fee332bb --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 26 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0026/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0026/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..0b573ab91c043ff74eff2226528d3305c5cf67b4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/tests/checks.json @@ -0,0 +1,132 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S111.marks", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "students.S111.percent", + "type": "number", + "value": 10.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S111.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S114.marks", + "type": "int", + "value": 14, + "weight": 1.0 + }, + { + "path": "students.S114.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S114.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S156.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S156.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S156.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S196.marks", + "type": "int", + "value": 9, + "weight": 1.0 + }, + { + "path": "students.S196.percent", + "type": "number", + "value": 45.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S196.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S272.marks", + "type": "int", + "value": 18, + "weight": 1.0 + }, + { + "path": "students.S272.percent", + "type": "number", + "value": 90.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S272.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S351.marks", + "type": "int", + "value": 10, + "weight": 1.0 + }, + { + "path": "students.S351.percent", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S351.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S111", + "S156" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0026/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0026/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0026/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0026/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0026/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0038/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0038/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..e0391e4fe8b4d910affc9577d8c8acc79ba950ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,C,1 +Q2,D,1 +Q3,A,1 +Q4,A,1 +Q5,D,1 +Q6,B,1 +Q7,B,1 +Q8,D,1 +Q9,B,1 +Q10,B,1 +Q11,C,1 +Q12,D,1 +Q13,D,1 +Q14,A,1 +Q15,C,1 +Q16,B,1 +Q17,A,1 +Q18,C,1 +Q19,D,1 +Q20,B,1 diff --git a/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..f159688125ce995bcb65a714ac376e3328b9e3f3 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S044,9B,C,C,C,B,D,B,C,D,B,B,C,D,C,A,C,B,A,C,D,C +S013,9B,A,A,D,C,A,D,A,,B,B,B,A,C,A,A,,D,B,C,A +S319,9B,C,D,A,B,D,,A,D,B,,D,D,D,D,B,B,B,D,B,D +S068,9B,D,D,,A,D,B,B,A,B,B,D,D,D,A,C,D,D,C,D,B +S069,9A,B,B,,A,A,,B,A,D,,A,C,B,B,A,B,C,C,A, +S036,9B,C,D,C,A,D,B,B,D,B,B,C,D,D,A,C,B,A,D,D, diff --git a/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..9bb6905915713cd136d7c40c5c74e2456917e685 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d1-t0038/instruction.md b/tasks/class-test/class-test-marking-d1-t0038/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0038/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0038/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0038/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0038/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0038/task.toml b/tasks/class-test/class-test-marking-d1-t0038/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..c1177b8183772295986c568f8fefedf82ed59f89 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 38 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0038/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0038/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..6256db3563917cb3f2bf59b9717cf8eacc61c367 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/tests/checks.json @@ -0,0 +1,132 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S013.marks", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "students.S013.percent", + "type": "number", + "value": 15.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S013.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S036.marks", + "type": "int", + "value": 17, + "weight": 1.0 + }, + { + "path": "students.S036.percent", + "type": "number", + "value": 85.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S036.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S044.marks", + "type": "int", + "value": 14, + "weight": 1.0 + }, + { + "path": "students.S044.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S044.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S068.marks", + "type": "int", + "value": 14, + "weight": 1.0 + }, + { + "path": "students.S068.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S068.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S069.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S069.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S069.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S319.marks", + "type": "int", + "value": 9, + "weight": 1.0 + }, + { + "path": "students.S319.percent", + "type": "number", + "value": 45.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S319.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S013", + "S069" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0038/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0038/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0038/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0038/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0038/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0050/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0050/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..a69a5ad60ac4ddfa9ca820f763eed7029ab5b011 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,C,1 +Q2,D,1 +Q3,B,1 +Q4,D,1 +Q5,D,1 +Q6,D,1 +Q7,B,1 +Q8,D,1 +Q9,A,1 +Q10,C,1 +Q11,A,1 +Q12,B,1 +Q13,B,1 +Q14,C,1 +Q15,B,1 +Q16,A,1 +Q17,D,1 +Q18,C,1 +Q19,D,1 +Q20,D,1 diff --git a/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..d11c8c09c95addf969a0568e2586935aecd32b20 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S135,9A,B,B,A,B,D,C,A,D,B,B,A,B,B,,A,D,C,D,C,A +S054,9A,A,A,D,,C,B,A,D,C,D,B,B,D,D,,B,C,A,B,A +S056,9B,B,D,A,D,D,D,B,D,A,D,A,B,B,C,C,,D,C,D,D +S394,9B,C,B,B,D,D,D,B,D,A,D,B,B,B,C,B,A,D,C,D,D +S255,9B,B,B,D,D,D,D,C,D,C,D,A,,B,C,C,A,D,C,B,D +S123,9B,B,D,B,D,,D,B,D,B,B,A,B,B,C,B,A,B,B,D,D diff --git a/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..a62383ad725cb33664753d038ed84853f9b97155 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 50 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d1-t0050/instruction.md b/tasks/class-test/class-test-marking-d1-t0050/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0050/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0050/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0050/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0050/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0050/task.toml b/tasks/class-test/class-test-marking-d1-t0050/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8994ba1f8f07c54ff95ba814ea577288265ebca7 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 50 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0050/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0050/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..9f15ff27b375d0ac50417e76749af8240aed3290 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/tests/checks.json @@ -0,0 +1,132 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S054.marks", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "students.S054.percent", + "type": "number", + "value": 10.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S054.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S056.marks", + "type": "int", + "value": 15, + "weight": 1.0 + }, + { + "path": "students.S056.percent", + "type": "number", + "value": 75.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S056.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S123.marks", + "type": "int", + "value": 14, + "weight": 1.0 + }, + { + "path": "students.S123.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S123.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S135.marks", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "students.S135.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S135.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S255.marks", + "type": "int", + "value": 11, + "weight": 1.0 + }, + { + "path": "students.S255.percent", + "type": "number", + "value": 55.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S255.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S394.marks", + "type": "int", + "value": 17, + "weight": 1.0 + }, + { + "path": "students.S394.percent", + "type": "number", + "value": 85.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S394.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S054", + "S135" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0050/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0050/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0050/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0050/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0050/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0062/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0062/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..fc81ec145455046888acd8fa06f4b51b6c5836d5 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,D,1 +Q2,B,1 +Q3,D,1 +Q4,C,1 +Q5,B,1 +Q6,B,1 +Q7,B,1 +Q8,D,1 +Q9,B,1 +Q10,C,1 +Q11,D,1 +Q12,C,1 +Q13,C,1 +Q14,B,1 +Q15,B,1 +Q16,A,1 +Q17,B,1 +Q18,D,1 +Q19,D,1 +Q20,B,1 diff --git a/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..4437ef5915df38f6529f5ee94c0de1e3db9a3b41 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S043,9A,B,C,D,C,B,B,C,D,B,A,,,D,D,D,D,C,D,D,C +S059,9A,B,D,,D,C,D,B,C,D,B,B,B,B,C,C,,B,D,,B +S113,9B,D,B,B,A,C,A,D,D,B,,B,B,D,D,,D,D,,D,C +S349,9A,D,D,D,,D,D,A,,A,A,B,C,,D,D,D,A,C,A,A +S323,9A,A,D,D,B,B,B,B,D,B,C,D,C,C,C,B,A,B,D,D,B +S014,9B,C,B,B,C,A,A,C,A,C,,B,A,B,A,B,B,D,C,C,A diff --git a/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..331354459803f1875c1470d726b889807b8612db --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 60 or more | +| C | 55 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d1-t0062/instruction.md b/tasks/class-test/class-test-marking-d1-t0062/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0062/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0062/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0062/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0062/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0062/task.toml b/tasks/class-test/class-test-marking-d1-t0062/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7d92a625df46bffdea84492c24cca35c12d369c8 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 62 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0062/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0062/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..107733c5f455bff1ed7998397579a85ddf1db28b --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/tests/checks.json @@ -0,0 +1,135 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S014.marks", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "students.S014.percent", + "type": "number", + "value": 15.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S014.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S043.marks", + "type": "int", + "value": 8, + "weight": 1.0 + }, + { + "path": "students.S043.percent", + "type": "number", + "value": 40.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S043.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S059.marks", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "students.S059.percent", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S059.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S113.marks", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "students.S113.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S113.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S323.marks", + "type": "int", + "value": 16, + "weight": 1.0 + }, + { + "path": "students.S323.percent", + "type": "number", + "value": 80.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S323.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S349.marks", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "students.S349.percent", + "type": "number", + "value": 15.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S349.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S014", + "S043", + "S059", + "S113", + "S349" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0062/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0062/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0062/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0062/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0062/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d1-t0074/environment/Dockerfile b/tasks/class-test/class-test-marking-d1-t0074/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..a03cdb4573b8b34bf9dcb6f84df9ee7d1cd38de3 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_key.csv @@ -0,0 +1,21 @@ +item,key,marks +Q1,B,1 +Q2,C,1 +Q3,B,1 +Q4,C,1 +Q5,B,1 +Q6,A,1 +Q7,D,1 +Q8,B,1 +Q9,A,1 +Q10,C,1 +Q11,D,1 +Q12,C,1 +Q13,A,1 +Q14,A,1 +Q15,B,1 +Q16,A,1 +Q17,A,1 +Q18,D,1 +Q19,B,1 +Q20,A,1 diff --git a/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..290306c7bf60932b0ffa5e97bbf9b486419cc685 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/answer_sheets.csv @@ -0,0 +1,7 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20 +S185,9A,B,C,B,C,B,A,D,B,A,C,D,C,A,A,B,A,A,D,B,A +S261,9B,,C,B,C,B,A,D,B,A,C,D,C,A,A,B,A,A,D,B,A +S010,9B,A,B,B,A,D,C,A,A,C,,C,B,,B,,C,D,B,, +S342,9B,B,C,B,C,B,A,D,B,A,C,D,C,A,A,B,A,A,D,B,A +S290,9A,B,C,B,C,B,A,D,B,A,D,D,C,A,A,B,A,A,D,C,A +S195,9B,B,A,B,C,B,C,A,,D,C,D,C,A,A,A,A,A,D,B, diff --git a/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..fbe9d74742fd612306af6cec40438e6f9f3e7f03 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/environment/workdir/marking_scheme.md @@ -0,0 +1,20 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer or an unanswered item (empty cell) earns 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 50 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d1-t0074/instruction.md b/tasks/class-test/class-test-marking-d1-t0074/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..d77b54f0294dceed347e6a6a464958fbe1ab64d2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 6 students for a 20-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, an integer. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d1-t0074/solution/oracle.py b/tasks/class-test/class-test-marking-d1-t0074/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d1-t0074/solution/solve.sh b/tasks/class-test/class-test-marking-d1-t0074/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d1-t0074/task.toml b/tasks/class-test/class-test-marking-d1-t0074/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..77456286c98a18384ec76a0153a6c4601ac3430c --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 74 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d1-t0074/tests/checks.json b/tasks/class-test/class-test-marking-d1-t0074/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..a6b5a34caf912fc5b282f5692680fa6b72ad50d0 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/tests/checks.json @@ -0,0 +1,131 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S010.marks", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "students.S010.percent", + "type": "number", + "value": 5.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S010.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S185.marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S185.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S185.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S195.marks", + "type": "int", + "value": 13, + "weight": 1.0 + }, + { + "path": "students.S195.percent", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S195.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S261.marks", + "type": "int", + "value": 19, + "weight": 1.0 + }, + { + "path": "students.S261.percent", + "type": "number", + "value": 95.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S261.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S290.marks", + "type": "int", + "value": 18, + "weight": 1.0 + }, + { + "path": "students.S290.percent", + "type": "number", + "value": 90.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S290.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S342.marks", + "type": "int", + "value": 20, + "weight": 1.0 + }, + { + "path": "students.S342.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S342.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S010" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d1-t0074/tests/test.sh b/tasks/class-test/class-test-marking-d1-t0074/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d1-t0074/tests/verify.py b/tasks/class-test/class-test-marking-d1-t0074/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d1-t0074/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-e0006/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-e0006/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..66fceef6894ee3b5f1c4dd0ab5ef64468c42a297 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,D,1 +Q2,C,1 +Q3,C,1 +Q4,B,1 +Q5,C,3 +Q6,A,3 +Q7,B,2 +Q8,A,1 +Q9,D,2 +Q10,C,1 +Q11,A,1 +Q12,B,2 +Q13,C,1 +Q14,B,1 +Q15,D,2 +Q16,D,1 +Q17,C,2 +Q18,C,2 +Q19,A,1 +Q20,B,1 +Q21,D,1 +Q22,D,1 +Q23,B,1 +Q24,D,3 diff --git a/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..0455c61337f11e4f568aac420b8f9b1fe549050e --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S093,9A,D,,C,D,C,A,B,B,B,C,D,C,C,B,D,D,C,C,B,B,D,C,B,B +S342,9B,D,,B,B,C,C,B,A,B,C,B,,D,B,A,B,,C,,C,D,D,,A +S139,9A,D,C,C,D,D,A,B,A,B,D,D,A,A,,,D,,C,D,B,D,D,A,D +S302,9B,D,C,C,B,C,A,B,A,D,D,A,C,D,B,D,D,C,C,A,B,D,,B,A +S257,9B,,D,B,B,C,,B,A,,D,,D,,,A,D,C,,A,A,D,B,D,A +S210,9B,D,B,D,B,C,A,B,A,,C,A,B,C,B,C,D,A,D,A,B,D,D,B,D +S332,9A,,C,,B,C,A,A,A,B,A,A,B,C,B,A,C,D,C,A,B,D,D,A,C +S002,9A,C,D,C,A,C,,A,,D,B,D,A,,D,A,A,B,D,B,A,D,D,,C +S289,9A,D,C,C,B,,C,B,A,A,C,,B,C,B,D,D,A,C,C,B,D,D,D, diff --git a/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..d3256005e275d6c676d45c8b94f199dab3ffaeee --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-e0006/instruction.md b/tasks/class-test/class-test-marking-d2-e0006/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-e0006/solution/oracle.py b/tasks/class-test/class-test-marking-d2-e0006/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-e0006/solution/solve.sh b/tasks/class-test/class-test-marking-d2-e0006/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-e0006/task.toml b/tasks/class-test/class-test-marking-d2-e0006/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..6feda1d9dd7cbc9f019071177458fecf66d82885 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10006 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-e0006/tests/checks.json b/tasks/class-test/class-test-marking-d2-e0006/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..0cdbbc72ad9f40a8cc51953b30e571253ce5f193 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/tests/checks.json @@ -0,0 +1,199 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 36, + "weight": 1.0 + }, + { + "path": "students.S002.marks", + "type": "number", + "value": 2.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S002.percent", + "type": "number", + "value": 6.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S002.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S093.marks", + "type": "number", + "value": 20.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S093.percent", + "type": "number", + "value": 55.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S093.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S139.marks", + "type": "number", + "value": 14.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S139.percent", + "type": "number", + "value": 41.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S139.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S210.marks", + "type": "number", + "value": 24.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S210.percent", + "type": "number", + "value": 66.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S210.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S257.marks", + "type": "number", + "value": 8.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S257.percent", + "type": "number", + "value": 24.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S257.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S289.marks", + "type": "number", + "value": 17.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S289.percent", + "type": "number", + "value": 49.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S289.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S302.marks", + "type": "number", + "value": 26.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S302.percent", + "type": "number", + "value": 72.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S302.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S332.marks", + "type": "number", + "value": 16.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S332.percent", + "type": "number", + "value": 45.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S332.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S342.marks", + "type": "number", + "value": 10.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S342.percent", + "type": "number", + "value": 28.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S342.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S002", + "S257", + "S342" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-e0006/tests/test.sh b/tasks/class-test/class-test-marking-d2-e0006/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-e0006/tests/verify.py b/tasks/class-test/class-test-marking-d2-e0006/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0006/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-e0018/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-e0018/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..3c1f88a930fdc7174dd35eff2507960aa854fbed --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,D,2 +Q2,D,2 +Q3,D,1 +Q4,B,1 +Q5,A,3 +Q6,C,1 +Q7,A,1 +Q8,D,2 +Q9,B,3 +Q10,A,3 +Q11,C,2 +Q12,A,3 +Q13,B,1 +Q14,A,1 +Q15,C,1 +Q16,C,1 +Q17,B,3 +Q18,A,1 +Q19,D,3 +Q20,B,2 +Q21,C,1 +Q22,D,2 +Q23,C,1 +Q24,C,2 diff --git a/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..2f61f20cbedf41d8767ed57fda175c936f35f423 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S133,9B,D,D,D,D,A,C,A,D,B,A,C,A,B,B,C,,B,A,D,B,D,A,C,C +S141,9A,D,D,D,B,A,C,B,A,B,A,C,A,B,A,C,C,B,A,D,B,C,D,C,D +S356,9A,B,D,D,B,A,C,A,D,B,A,C,A,C,A,C,C,B,A,D,B,C,D,C,C +S091,9B,B,,,B,B,C,A,,B,,C,B,D,A,C,D,B,D,,A,C,D,C,B +S074,9B,B,,,,C,D,,C,,,,,,C,,B,B,C,,,A,D,,D +S082,9B,D,A,B,,A,C,A,A,B,,B,B,C,A,C,A,B,A,,B,C,D,C,C +S322,9A,B,D,A,B,,C,A,D,A,A,C,A,B,B,C,C,,A,D,B,C,D,C,C +S243,9A,D,D,B,,A,C,B,A,D,A,B,B,D,A,C,A,C,,D,C,D,C,A, +S296,9A,,D,A,D,A,C,A,D,B,C,D,D,B,A,C,B,D,A,,B,C,C,C,B diff --git a/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..199765312167433bd7cbaa96fbc6910897676da7 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-e0018/instruction.md b/tasks/class-test/class-test-marking-d2-e0018/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-e0018/solution/oracle.py b/tasks/class-test/class-test-marking-d2-e0018/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-e0018/solution/solve.sh b/tasks/class-test/class-test-marking-d2-e0018/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-e0018/task.toml b/tasks/class-test/class-test-marking-d2-e0018/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..3d39492912cdd85f4002208e793fa1dd8b58ba39 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10018 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-e0018/tests/checks.json b/tasks/class-test/class-test-marking-d2-e0018/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..e0eae565c4a08dc9a467eb8652f5a208d11ab7dc --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/tests/checks.json @@ -0,0 +1,200 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 43, + "weight": 1.0 + }, + { + "path": "students.S074.marks", + "type": "number", + "value": 1.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S074.percent", + "type": "number", + "value": 3.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S074.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S082.marks", + "type": "number", + "value": 21.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S082.percent", + "type": "number", + "value": 48.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S082.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S091.marks", + "type": "number", + "value": 13.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S091.percent", + "type": "number", + "value": 30.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S091.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S133.marks", + "type": "number", + "value": 35.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S133.percent", + "type": "number", + "value": 83.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S133.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S141.marks", + "type": "number", + "value": 36.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S141.percent", + "type": "number", + "value": 85.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S141.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S243.marks", + "type": "number", + "value": 10.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S243.percent", + "type": "number", + "value": 23.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S243.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S296.marks", + "type": "number", + "value": 15.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S296.percent", + "type": "number", + "value": 36.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S296.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S322.marks", + "type": "number", + "value": 28.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S322.percent", + "type": "number", + "value": 65.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S322.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S356.marks", + "type": "number", + "value": 39.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S356.percent", + "type": "number", + "value": 91.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S356.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S074", + "S091", + "S243", + "S296" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-e0018/tests/test.sh b/tasks/class-test/class-test-marking-d2-e0018/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-e0018/tests/verify.py b/tasks/class-test/class-test-marking-d2-e0018/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-e0018/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0006/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0006/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..4d6a28ec0514751bf8ee1051c4dd13f4f4d7707d --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,B,2 +Q2,B,3 +Q3,A,2 +Q4,C,2 +Q5,A,2 +Q6,A,2 +Q7,A,1 +Q8,D,1 +Q9,C,3 +Q10,C,2 +Q11,D,1 +Q12,C,2 +Q13,B,1 +Q14,A,1 +Q15,A,2 +Q16,D,1 +Q17,A,1 +Q18,D,3 +Q19,A,3 +Q20,B,2 +Q21,C,2 +Q22,A,2 +Q23,C,2 +Q24,C,1 diff --git a/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..3f5db60c9a67b7968b9890415379bf68109b3974 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S228,9B,D,,A,C,A,B,B,,C,C,D,C,B,A,A,C,A,D,A,B,C,A,C,C +S071,9B,,B,,D,C,B,C,A,A,,B,B,A,B,C,D,D,A,D,C,D,C,B,A +S205,9A,B,B,C,D,A,A,A,B,D,A,D,A,A,D,A,D,,C,,,,A,D,D +S327,9A,,B,,A,A,,A,D,B,B,B,C,,,D,A,D,B,A,C,D,D,C,A +S354,9B,B,B,,C,B,A,A,D,C,C,B,C,B,A,A,D,A,D,A,B,,,C,C +S323,9B,D,A,A,C,,A,B,D,D,A,D,D,C,,D,D,A,D,,B,B,A,C,C +S103,9B,B,B,A,C,A,A,,D,C,C,D,C,B,A,A,D,A,D,A,B,C,A,C,C +S217,9B,B,D,C,A,,C,D,B,,A,A,,A,,C,A,A,D,A,C,C,A,C,C +S305,9B,B,B,A,C,A,A,A,D,C,C,D,C,B,A,A,B,A,D,A,B,C,A,C,C diff --git a/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..c0f38e6173e18ed99bf5595a1698ec7e187377b3 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 60 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-t0006/instruction.md b/tasks/class-test/class-test-marking-d2-t0006/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0006/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0006/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0006/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0006/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0006/task.toml b/tasks/class-test/class-test-marking-d2-t0006/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..a65bc55cccc5fdfaff2ad24894b118189bdca7a6 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 6 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0006/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0006/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..7b4ab5d846310a93496dd4f0de486dece07660f7 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/tests/checks.json @@ -0,0 +1,201 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 44, + "weight": 1.0 + }, + { + "path": "students.S071.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S071.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S071.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S103.marks", + "type": "number", + "value": 43.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S103.percent", + "type": "number", + "value": 97.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S103.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S205.marks", + "type": "number", + "value": 11.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S205.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S205.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S217.marks", + "type": "number", + "value": 11.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S217.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S217.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S228.marks", + "type": "number", + "value": 32.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S228.percent", + "type": "number", + "value": 73.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S228.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S305.marks", + "type": "number", + "value": 42.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S305.percent", + "type": "number", + "value": 97.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S305.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S323.marks", + "type": "number", + "value": 15.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S323.percent", + "type": "number", + "value": 35.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S323.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S327.marks", + "type": "number", + "value": 8.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S327.percent", + "type": "number", + "value": 19.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S327.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S354.marks", + "type": "number", + "value": 34.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S354.percent", + "type": "number", + "value": 77.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S354.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S071", + "S205", + "S217", + "S323", + "S327" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0006/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0006/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0006/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0006/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0006/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0018/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0018/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..3c16fca34829f700defffbc68670f4fc78e187d8 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,A,2 +Q2,A,1 +Q3,C,2 +Q4,D,2 +Q5,D,2 +Q6,A,2 +Q7,B,3 +Q8,C,1 +Q9,A,1 +Q10,C,2 +Q11,D,3 +Q12,C,1 +Q13,B,1 +Q14,A,3 +Q15,C,1 +Q16,D,1 +Q17,A,1 +Q18,B,1 +Q19,D,2 +Q20,C,3 +Q21,D,3 +Q22,A,1 +Q23,A,1 +Q24,B,1 diff --git a/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..8c6e3d219acbb158cd88c9adcd32f29093dc7e26 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S064,9A,A,,B,D,D,D,B,C,C,D,B,C,B,,C,D,A,D,D,C,D,A,A,B +S147,9B,A,A,,D,D,A,B,A,C,C,,,,A,A,D,,A,C,C,D,C,D,B +S354,9A,A,B,C,D,D,A,D,A,B,B,D,C,B,,C,D,A,D,,C,B,A,,C +S383,9A,A,A,C,,A,C,C,C,B,,D,C,B,A,C,D,A,B,D,A,C,A,,B +S227,9B,,B,C,D,D,A,C,D,C,C,D,C,B,D,C,D,B,B,D,C,D,A,A,C +S122,9A,,,B,D,,D,D,,B,B,A,C,B,A,C,,,,A,C,B,C,A, +S089,9A,A,C,C,D,D,A,B,C,A,C,D,C,B,A,C,D,A,B,D,C,,,A,A +S151,9A,A,D,,D,C,A,D,C,A,B,B,C,C,A,C,D,,B,D,C,D,A,A,B +S021,9B,,A,,,A,C,D,,C,,B,C,,A,B,C,B,,A,D,A,D,B, diff --git a/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..5bd909b4730eaeba2443fcbbdd2caeb0734e815b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-t0018/instruction.md b/tasks/class-test/class-test-marking-d2-t0018/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0018/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0018/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0018/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0018/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0018/task.toml b/tasks/class-test/class-test-marking-d2-t0018/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..4feee242eb7fa8278c48c933c60c2b7703552a9d --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 18 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0018/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0018/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..aba18bf23369697da1dc0b61f88807a422ff4d43 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/tests/checks.json @@ -0,0 +1,198 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 41, + "weight": 1.0 + }, + { + "path": "students.S021.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S021.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S021.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S064.marks", + "type": "number", + "value": 23.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S064.percent", + "type": "number", + "value": 56.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S064.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S089.marks", + "type": "number", + "value": 34.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S089.percent", + "type": "number", + "value": 84.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S089.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S122.marks", + "type": "number", + "value": 7.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S122.percent", + "type": "number", + "value": 17.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S122.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S147.marks", + "type": "number", + "value": 23.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S147.percent", + "type": "number", + "value": 56.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S147.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S151.marks", + "type": "number", + "value": 23.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S151.percent", + "type": "number", + "value": 56.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S151.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S227.marks", + "type": "number", + "value": 25.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S227.percent", + "type": "number", + "value": 61.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S227.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S354.marks", + "type": "number", + "value": 18.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S354.percent", + "type": "number", + "value": 45.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S354.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S383.marks", + "type": "number", + "value": 18.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S383.percent", + "type": "number", + "value": 45.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S383.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S021", + "S122" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0018/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0018/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0018/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0018/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0018/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0030/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0030/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5e3520063d9d84fff31aff195310debb1a107807 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,A,2 +Q2,D,3 +Q3,B,1 +Q4,B,1 +Q5,D,1 +Q6,D,2 +Q7,C,3 +Q8,C,1 +Q9,B,2 +Q10,C,1 +Q11,D,3 +Q12,A,1 +Q13,C,2 +Q14,B,1 +Q15,C,1 +Q16,D,2 +Q17,B,3 +Q18,B,3 +Q19,B,2 +Q20,A,1 +Q21,B,3 +Q22,B,3 +Q23,D,3 +Q24,D,2 diff --git a/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..e640b7908f2b34bf1049bcef2b41ef83c816fc35 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S225,9B,D,A,D,B,D,D,C,,D,C,D,D,C,B,D,B,A,D,B,A,B,B,A,D +S392,9B,A,D,B,B,C,A,C,C,,A,,A,C,B,C,D,D,,A,C,B,B,D,D +S372,9B,,C,B,B,B,B,C,,D,C,,A,B,D,D,D,B,C,,D,C,D,A,D +S370,9A,B,,B,,B,C,,C,A,A,,A,C,C,,,B,A,,B,B,D,D,A +S192,9B,A,D,B,B,D,D,C,C,B,C,D,A,C,B,C,D,B,B,B,A,B,B,D,D +S351,9B,D,D,D,,C,D,A,A,A,,B,D,C,B,A,C,B,C,A,,B,,C,D +S292,9B,A,D,B,B,C,A,C,C,B,C,D,B,C,B,C,A,B,B,,A,,B,B, +S344,9A,A,D,B,D,D,D,C,C,B,C,D,A,C,B,C,D,B,D,B,A,B,B,D,D +S158,9A,A,C,,C,C,A,A,,D,C,,C,,A,C,,A,A,D,C,B,,A,D diff --git a/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..9fab5f6afc8018ff12178ceafbcf2f5fc907396e --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 60 or more | +| C | 50 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-t0030/instruction.md b/tasks/class-test/class-test-marking-d2-t0030/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0030/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0030/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0030/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0030/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0030/task.toml b/tasks/class-test/class-test-marking-d2-t0030/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..943a12118f7b05051ad4427723a6ff52b2271740 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 30 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0030/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0030/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ef6fc91c2985d935963d296d2876e0179de00265 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/tests/checks.json @@ -0,0 +1,200 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 47, + "weight": 1.0 + }, + { + "path": "students.S158.marks", + "type": "number", + "value": 2.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S158.percent", + "type": "number", + "value": 5.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S158.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S192.marks", + "type": "number", + "value": 47.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S192.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S225.marks", + "type": "number", + "value": 19.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S225.percent", + "type": "number", + "value": 42.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S225.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S292.marks", + "type": "number", + "value": 28.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S292.percent", + "type": "number", + "value": 61.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S292.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S344.marks", + "type": "number", + "value": 42.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S344.percent", + "type": "number", + "value": 89.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S344.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S351.marks", + "type": "number", + "value": 9.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S351.percent", + "type": "number", + "value": 20.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S351.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S370.marks", + "type": "number", + "value": 9.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S370.percent", + "type": "number", + "value": 20.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S370.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S372.marks", + "type": "number", + "value": 7.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S372.percent", + "type": "number", + "value": 16.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S372.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S392.marks", + "type": "number", + "value": 26.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S392.percent", + "type": "number", + "value": 56.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S392.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S158", + "S351", + "S370", + "S372" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0030/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0030/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0030/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0030/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0030/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0042/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0042/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..6d1f1592366af75147051ca4b71996d4fcb74395 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,B,1 +Q2,D,2 +Q3,C,3 +Q4,D,2 +Q5,B,2 +Q6,D,1 +Q7,A,3 +Q8,A,2 +Q9,C,2 +Q10,C,1 +Q11,C,1 +Q12,D,2 +Q13,B,2 +Q14,C,3 +Q15,A,2 +Q16,C,1 +Q17,C,2 +Q18,A,1 +Q19,B,1 +Q20,D,2 +Q21,C,1 +Q22,D,2 +Q23,A,2 +Q24,C,3 diff --git a/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..c0c968d981e90221e8752ea5a40777a6ce2c1d3e --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S179,9B,B,D,C,D,B,A,C,A,C,,B,C,B,C,D,C,,C,B,B,C,A,B,C +S192,9B,D,C,C,D,D,C,A,A,B,C,,,B,C,B,C,C,B,A,A,,D,C,C +S381,9A,B,D,,D,B,D,A,A,C,C,C,B,B,C,,C,C,A,B,D,C,D,A,C +S216,9A,B,C,C,D,B,D,A,,D,C,C,A,,B,B,,,B,B,D,C,D,,C +S104,9B,D,B,,,B,D,A,C,C,C,B,D,,C,C,C,C,A,C,D,C,D,B,C +S167,9B,C,D,C,C,B,,,D,A,C,C,B,A,A,D,,D,B,B,D,C,D,B,C +S110,9B,C,D,C,A,B,D,A,A,C,C,C,A,B,D,A,C,C,A,B,D,C,D,A,C +S021,9A,B,C,B,A,,A,C,B,A,C,,B,B,A,,B,C,A,C,D,D,,A, +S273,9A,,D,C,A,B,D,A,A,B,C,C,D,B,C,A,A,C,C,B,D,C,D,A,C diff --git a/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..96f57da22f3b6239ef25fe3abbde38a943903b6f --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 55 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d2-t0042/instruction.md b/tasks/class-test/class-test-marking-d2-t0042/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0042/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0042/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0042/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0042/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0042/task.toml b/tasks/class-test/class-test-marking-d2-t0042/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..950560930c0e9bff9b1cde523cd6cec003cae5e6 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 42 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0042/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0042/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..e0e7ffa48f921ca69294c349919f91a91b38a69a --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/tests/checks.json @@ -0,0 +1,201 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 44, + "weight": 1.0 + }, + { + "path": "students.S021.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S021.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S021.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S104.marks", + "type": "number", + "value": 20.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S104.percent", + "type": "number", + "value": 46.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S104.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S110.marks", + "type": "number", + "value": 32.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S110.percent", + "type": "number", + "value": 72.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S110.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S167.marks", + "type": "number", + "value": 7.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S167.percent", + "type": "number", + "value": 17.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S167.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S179.marks", + "type": "number", + "value": 17.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S179.percent", + "type": "number", + "value": 38.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S179.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S192.marks", + "type": "number", + "value": 16.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S192.percent", + "type": "number", + "value": 36.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S216.marks", + "type": "number", + "value": 17.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S216.percent", + "type": "number", + "value": 38.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S216.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S273.marks", + "type": "number", + "value": 34.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S273.percent", + "type": "number", + "value": 77.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S273.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S381.marks", + "type": "number", + "value": 36.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S381.percent", + "type": "number", + "value": 81.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S381.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S021", + "S167", + "S179", + "S192", + "S216" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0042/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0042/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0042/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0042/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0042/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0054/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0054/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5a73fc659ac4bd504b0895d9edf0389a4aed0721 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,A,2 +Q2,C,2 +Q3,D,3 +Q4,C,2 +Q5,C,1 +Q6,A,2 +Q7,A,1 +Q8,D,1 +Q9,A,2 +Q10,D,1 +Q11,B,2 +Q12,D,1 +Q13,A,2 +Q14,D,2 +Q15,B,2 +Q16,C,2 +Q17,C,1 +Q18,B,1 +Q19,D,2 +Q20,A,1 +Q21,B,2 +Q22,B,2 +Q23,C,2 +Q24,C,2 diff --git a/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..d32d08a977667fc1c971f1239b3884f425fd7ed2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S176,9A,A,C,D,C,C,A,A,D,B,D,B,D,A,D,B,C,C,B,D,A,B,B,C,C +S215,9A,C,C,A,C,D,C,C,,D,,B,A,A,A,B,D,D,B,C,D,,C,A,A +S121,9B,A,A,D,C,C,A,D,D,D,D,B,D,B,D,B,C,C,B,D,A,B,B,A,C +S066,9A,B,C,D,A,C,A,A,B,A,A,B,C,A,,B,C,C,C,D,B,B,B,C, +S347,9A,D,,,C,C,B,C,D,A,D,D,D,A,D,D,C,C,C,D,A,B,C,B,C +S035,9B,D,A,,D,A,B,C,,C,B,D,,C,D,D,B,B,A,,,,B,,D +S096,9A,A,C,B,C,C,D,D,C,A,D,B,D,,D,B,C,C,B,D,A,B,B,C,C +S130,9A,B,C,D,C,C,A,A,D,A,C,B,A,,D,B,,C,D,A,A,D,A,,C +S202,9B,A,C,D,C,C,,C,D,A,D,B,A,C,C,B,,C,B,D,A,B,A,C,C diff --git a/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..ded99af36338c83a6e4ed499a7777bdd1196353d --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 50 or more | +| D | 35 or more | +| U | under 35 | diff --git a/tasks/class-test/class-test-marking-d2-t0054/instruction.md b/tasks/class-test/class-test-marking-d2-t0054/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0054/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0054/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0054/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0054/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0054/task.toml b/tasks/class-test/class-test-marking-d2-t0054/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..a57a5649e676ba8326d6f9b58b24acccf6c8416f --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 54 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0054/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0054/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..652c75c2eeff83ea0ac946b9b45f6afa2af27924 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/tests/checks.json @@ -0,0 +1,198 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 41, + "weight": 1.0 + }, + { + "path": "students.S035.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S035.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S035.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S066.marks", + "type": "number", + "value": 23.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S066.percent", + "type": "number", + "value": 57.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S066.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S096.marks", + "type": "number", + "value": 28.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S096.percent", + "type": "number", + "value": 69.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S096.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S121.marks", + "type": "number", + "value": 27.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S121.percent", + "type": "number", + "value": 67.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S121.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S130.marks", + "type": "number", + "value": 18.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S130.percent", + "type": "number", + "value": 45.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S130.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S176.marks", + "type": "number", + "value": 38.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S176.percent", + "type": "number", + "value": 92.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S176.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S202.marks", + "type": "number", + "value": 25.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S202.percent", + "type": "number", + "value": 61.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S202.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S215.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S215.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S215.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S347.marks", + "type": "number", + "value": 15.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S347.percent", + "type": "number", + "value": 36.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S347.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S035", + "S215" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0054/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0054/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0054/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0054/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0054/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0066/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0066/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..d12bf3a45647ee78c61d1bfb76ddb367676de96c --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,C,1 +Q2,A,2 +Q3,A,3 +Q4,B,2 +Q5,A,2 +Q6,C,2 +Q7,D,1 +Q8,B,1 +Q9,A,1 +Q10,A,2 +Q11,B,2 +Q12,C,1 +Q13,A,1 +Q14,D,2 +Q15,B,1 +Q16,B,2 +Q17,D,2 +Q18,B,2 +Q19,B,1 +Q20,C,1 +Q21,C,1 +Q22,D,2 +Q23,A,1 +Q24,D,1 diff --git a/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..67c14a7ac6e780e030aa52fdb4edb4ce323d9cd4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S327,9B,C,C,,B,,B,D,B,D,A,B,C,A,D,D,D,D,B,B,A,C,A,C,A +S345,9A,C,A,A,B,A,C,D,B,A,A,B,C,A,D,,B,D,B,B,C,,,A,D +S350,9B,,A,A,C,A,D,D,B,,A,B,C,,D,B,B,D,B,B,C,C,D,D,D +S226,9A,C,A,A,C,A,B,D,B,A,A,B,C,A,A,B,B,D,B,,,C,D,A,D +S234,9A,D,B,D,B,D,,D,D,,,,,,D,A,B,C,C,C,B,D,D,C,B +S122,9B,C,C,A,D,,D,D,D,A,A,A,C,,C,B,,A,B,,C,C,C,A,C +S357,9B,,A,A,B,B,C,A,B,A,A,B,B,A,D,B,B,D,B,B,C,,D,C,D +S079,9B,A,C,B,D,,A,C,,D,,D,B,A,,C,,C,D,B,C,D,C,,A +S301,9A,C,A,A,B,A,C,D,B,A,A,B,B,A,D,B,B,D,D,B,C,C,D,A,D diff --git a/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..b7290c9de5400d9402ca7e1c6269fb3bcd352bfa --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 60 or more | +| C | 50 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d2-t0066/instruction.md b/tasks/class-test/class-test-marking-d2-t0066/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0066/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0066/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0066/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0066/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0066/task.toml b/tasks/class-test/class-test-marking-d2-t0066/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..66b63c9d8350c40c3b576dd0442e85c883fb3686 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 66 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0066/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0066/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..4389ba06ad6719ddde1d08d957595037ea2f4997 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/tests/checks.json @@ -0,0 +1,200 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 37, + "weight": 1.0 + }, + { + "path": "students.S079.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S079.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S079.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S122.marks", + "type": "number", + "value": 7.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S122.percent", + "type": "number", + "value": 18.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S122.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S226.marks", + "type": "number", + "value": 26.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S226.percent", + "type": "number", + "value": 70.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S226.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S234.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S234.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S234.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S301.marks", + "type": "number", + "value": 32.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S301.percent", + "type": "number", + "value": 87.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S301.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S327.marks", + "type": "number", + "value": 12.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S327.percent", + "type": "number", + "value": 33.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S327.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S345.marks", + "type": "number", + "value": 33.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S345.percent", + "type": "number", + "value": 89.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S345.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S350.marks", + "type": "number", + "value": 26.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S350.percent", + "type": "number", + "value": 71.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S350.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S357.marks", + "type": "number", + "value": 27.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S357.percent", + "type": "number", + "value": 74.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S357.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S079", + "S122", + "S234", + "S327" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0066/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0066/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0066/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0066/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0066/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d2-t0078/environment/Dockerfile b/tasks/class-test/class-test-marking-d2-t0078/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..219ec15128054ba09c48fcef8bd761293ff2c1c8 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_key.csv @@ -0,0 +1,25 @@ +item,key,marks +Q1,D,1 +Q2,D,3 +Q3,D,1 +Q4,B,2 +Q5,C,2 +Q6,B,1 +Q7,D,2 +Q8,C,1 +Q9,C,2 +Q10,D,2 +Q11,B,1 +Q12,D,2 +Q13,A,1 +Q14,C,3 +Q15,B,1 +Q16,C,2 +Q17,D,1 +Q18,A,3 +Q19,A,1 +Q20,A,1 +Q21,A,2 +Q22,B,1 +Q23,A,2 +Q24,C,2 diff --git a/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..9752fd9b1ee2cf7835aa7f18e1e06f454db42660 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/answer_sheets.csv @@ -0,0 +1,10 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24 +S001,9B,,C,,C,C,A,A,D,,C,C,,D,B,D,,B,C,B,C,B,,, +S112,9B,B,D,D,C,D,B,A,A,A,D,D,A,B,B,C,A,D,D,D,B,C,A,C, +S135,9B,C,D,D,C,C,B,D,D,C,D,B,D,A,B,B,C,D,A,B,A,C,,A, +S036,9A,D,D,D,B,D,C,D,C,C,D,B,B,A,C,B,C,D,D,A,A,A,A,A,C +S132,9B,D,D,A,B,C,B,,C,C,D,B,,A,B,B,,D,D,A,A,A,,A, +S362,9A,D,D,D,B,C,B,D,C,C,D,B,D,A,C,B,C,D,A,A,A,A,B,A,C +S328,9A,D,C,B,D,,B,B,B,A,D,C,B,B,C,,C,,A,B,C,A,B,A,C +S082,9B,D,B,D,B,A,B,D,C,,D,B,A,A,C,B,D,D,A,A,C,A,B,A,C +S355,9A,D,D,D,A,C,B,,C,B,D,B,D,A,C,B,C,D,A,A,A,A,B,A,C diff --git a/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..b40f299b2c4816d3fa4776119ceee0ee52518a3a --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/environment/workdir/marking_scheme.md @@ -0,0 +1,22 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. + +The maximum is the sum of the marks of all items. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 50 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d2-t0078/instruction.md b/tasks/class-test/class-test-marking-d2-t0078/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..6bcaf2f39793b2c6bf4c19d0c28f523566e7347b --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/instruction.md @@ -0,0 +1,14 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 9 students for a 24-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d2-t0078/solution/oracle.py b/tasks/class-test/class-test-marking-d2-t0078/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d2-t0078/solution/solve.sh b/tasks/class-test/class-test-marking-d2-t0078/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d2-t0078/task.toml b/tasks/class-test/class-test-marking-d2-t0078/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..571245ced5fb7b26e35edd1be8fb20d58dd5c96d --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 78 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d2-t0078/tests/checks.json b/tasks/class-test/class-test-marking-d2-t0078/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..b58b9506ef1e2368ecf8a42370beca527592ff39 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/tests/checks.json @@ -0,0 +1,199 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 40, + "weight": 1.0 + }, + { + "path": "students.S001.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S001.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S001.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S036.marks", + "type": "number", + "value": 28.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S036.percent", + "type": "number", + "value": 71.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S036.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S082.marks", + "type": "number", + "value": 25.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S082.percent", + "type": "number", + "value": 63.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S082.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S112.marks", + "type": "number", + "value": 0.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S112.percent", + "type": "number", + "value": 1.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S112.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S132.marks", + "type": "number", + "value": 22.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S132.percent", + "type": "number", + "value": 55.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S132.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S135.marks", + "type": "number", + "value": 24.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S135.percent", + "type": "number", + "value": 61.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S135.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S328.marks", + "type": "number", + "value": 14.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S328.percent", + "type": "number", + "value": 36.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S328.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S355.marks", + "type": "number", + "value": 33.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S355.percent", + "type": "number", + "value": 82.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S355.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S362.marks", + "type": "number", + "value": 40.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S362.percent", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S362.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S001", + "S112", + "S328" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-marking-d2-t0078/tests/test.sh b/tasks/class-test/class-test-marking-d2-t0078/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d2-t0078/tests/verify.py b/tasks/class-test/class-test-marking-d2-t0078/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d2-t0078/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-e0010/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-e0010/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..79d2ee6af974aa37c1f14b35aec3189dd01ac2f9 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,A,3 +Q2,A,1 +Q3,C,1 +Q4,VOID,1 +Q5,D,2 +Q6,D,2 +Q7,B,2 +Q8,A,1 +Q9,B,2 +Q10,B,2 +Q11,B,3 +Q12,B,2 +Q13,A,2 +Q14,B,2 +Q15,C,2 +Q16,D,2 +Q17,B,1 +Q18,A,3 +Q19,C,3 +Q20,C,3 +Q21,D,2 +Q22,A,2 +Q23,A,1 +Q24,D,1 +Q25,C,1 +Q26,B,2 +Q27,C,2 +Q28,A,3 +Q29,A,2 +Q30,D,1 diff --git a/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..bb1e803204320dcaa412054d5046943f7d5aa562 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S177,9B,A,B,C,D,,D,C,A,BCD,B,B,A,A,C,C,D,D,,C,C,,A,D,D,C,C,C,A,D, +S319,9A,A,A,,D,D,D,B,D,BD,B,A,,A,B,A,B,,D,C,C,D,A,A,D,C,AD,C,B,A,D +S286,9A,A,A,C,A,D,D,B,A,B,B,B,B,A,B,C,D,B,A,C,C,D,A,A,D,C,B,C,AB,B,A +S057,9A,C,A,D,A,D,D,B,B,B,B,B,B,A,B,C,D,,A,,C,D,A,B,D,C,B,C,,C,D +S295,9B,A,A,C,BC,D,D,B,A,B,B,A,B,A,B,C,D,BD,B,C,C,D,A,C,D,C,B,C,D,A,B +S375,9B,A,A,C,D,D,D,B,A,B,B,B,B,D,B,C,D,B,A,C,C,D,A,A,D,C,B,C,A,A,D +S225,9B,,B,D,B,A,D,,A,B,B,C,D,A,A,C,C,,,C,C,B,,A,C,A,A,B,A,A,D +S055,9A,B,,D,B,,D,B,B,,A,A,C,B,A,B,,C,D,D,,,C,,,A,D,D,A,A, +S320,9A,C,A,D,B,D,D,A,B,B,B,,B,A,D,B,C,B,C,C,,,D,B,B,C,D,A,A,A,D +S180,9B,A,A,C,A,D,D,B,A,B,B,B,,A,B,,D,B,A,C,,D,,A,,C,B,C,A,A,D diff --git a/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..fe3cfb0f498e3318b30e3799b10f401b441087dc --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 55 or more | +| D | 35 or more | +| U | under 35 | diff --git a/tasks/class-test/class-test-marking-d3-e0010/instruction.md b/tasks/class-test/class-test-marking-d3-e0010/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-e0010/solution/oracle.py b/tasks/class-test/class-test-marking-d3-e0010/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-e0010/solution/solve.sh b/tasks/class-test/class-test-marking-d3-e0010/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-e0010/task.toml b/tasks/class-test/class-test-marking-d3-e0010/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..a395a575ab3562c5e4fdb12226db95e8ff260a95 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10010 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-e0010/tests/checks.json b/tasks/class-test/class-test-marking-d3-e0010/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..45426a457fb833b1e2e3f0a8881fa01fb6915866 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/tests/checks.json @@ -0,0 +1,256 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 56, + "weight": 1.0 + }, + { + "path": "students.S055.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S055.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S055.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S057.marks", + "type": "number", + "value": 37.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S057.percent", + "type": "number", + "value": 66.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S057.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S177.marks", + "type": "number", + "value": 25.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S177.percent", + "type": "number", + "value": 45.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S177.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S180.marks", + "type": "number", + "value": 46.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S180.percent", + "type": "number", + "value": 82.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S180.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S225.marks", + "type": "number", + "value": 13.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S225.percent", + "type": "number", + "value": 24.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S225.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S286.marks", + "type": "number", + "value": 47.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S286.percent", + "type": "number", + "value": 83.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S286.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S295.marks", + "type": "number", + "value": 38.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S295.percent", + "type": "number", + "value": 67.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S295.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S319.marks", + "type": "number", + "value": 25.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S319.percent", + "type": "number", + "value": 44.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S319.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S320.marks", + "type": "number", + "value": 12.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S320.percent", + "type": "number", + "value": 21.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S320.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S375.marks", + "type": "number", + "value": 53.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S375.percent", + "type": "number", + "value": 94.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S375.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S055", + "S225", + "S320" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 53.0, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-e0010/tests/test.sh b/tasks/class-test/class-test-marking-d3-e0010/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-e0010/tests/verify.py b/tasks/class-test/class-test-marking-d3-e0010/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0010/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-e0022/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-e0022/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..a290c4eb406b794359b4e519658a4484e5d80152 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,C,2 +Q2,C,3 +Q3,C,2 +Q4,C,2 +Q5,B,1 +Q6,B,1 +Q7,B,1 +Q8,D,3 +Q9,C,2 +Q10,VOID,2 +Q11,A,2 +Q12,B,1 +Q13,A,2 +Q14,C,2 +Q15,D,2 +Q16,VOID,2 +Q17,B,3 +Q18,B,3 +Q19,C,1 +Q20,A,3 +Q21,A,1 +Q22,C,3 +Q23,D,3 +Q24,C,3 +Q25,B,2 +Q26,C,1 +Q27,B,2 +Q28,B,2 +Q29,B,2 +Q30,D,2 diff --git a/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..832b6f915c8e7a36161a6a3eb5f57e8398b4b310 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S107,9A,A,,B,B,A,B,D,A,C,B,A,B,,C,A,D,C,B,D,,C,C,B,C,B,C,D,B,D, +S314,9B,D,D,C,C,B,B,B,A,D,D,B,B,A,A,A,,,D,C,AB,B,C,D,D,B,B,B,D,B, +S253,9B,B,C,C,C,A,D,C,,A,C,A,,B,,D,D,A,A,C,,D,,,,D,,B,A,B, +S328,9A,C,A,C,C,D,A,B,D,D,ACD,A,B,A,C,A,A,,A,C,A,C,B,D,,A,,B,D,,C +S365,9A,C,C,C,A,B,B,B,D,C,A,A,B,A,C,D,B,B,B,C,C,A,C,D,C,B,C,B,B,B,D +S028,9B,D,B,D,C,,A,D,,B,B,C,,D,B,B,D,B,D,B,C,B,D,B,A,B,,,C,C,B +S042,9B,C,C,C,C,C,B,C,D,D,D,A,D,B,,D,D,D,C,,A,A,ABC,D,C,B,C,B,B,B,D +S192,9A,A,,C,B,A,B,B,,C,A,B,B,C,A,B,D,B,D,C,A,CD,B,,,B,A,A,C,B,B +S092,9B,C,,C,C,D,B,B,,A,A,A,D,D,C,D,B,B,B,B,,A,,D,AC,B,,B,D,A, +S271,9A,D,B,C,C,B,B,C,,C,,A,B,A,C,D,BCD,ABD,B,A,A,,C,D,C,B,C,B,B,D,D diff --git a/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..f313c0049ec4ee2330bba227279129292945671c --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 50 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d3-e0022/instruction.md b/tasks/class-test/class-test-marking-d3-e0022/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-e0022/solution/oracle.py b/tasks/class-test/class-test-marking-d3-e0022/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-e0022/solution/solve.sh b/tasks/class-test/class-test-marking-d3-e0022/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-e0022/task.toml b/tasks/class-test/class-test-marking-d3-e0022/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..62c5c697b8a4e6f30d58ac69a44e52cde515a6c8 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "eval"] +isced_f = "0111" +family = "marking" +split = "eval" +seed = 10022 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-e0022/tests/checks.json b/tasks/class-test/class-test-marking-d3-e0022/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d9688f9b803c56f6c9ee33241bd919b01f78f54a --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/tests/checks.json @@ -0,0 +1,260 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 57, + "weight": 1.0 + }, + { + "path": "students.S028.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S028.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S028.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S042.marks", + "type": "number", + "value": 30.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S042.percent", + "type": "number", + "value": 52.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S042.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S092.marks", + "type": "number", + "value": 21.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S092.percent", + "type": "number", + "value": 36.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S092.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S107.marks", + "type": "number", + "value": 9.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S107.percent", + "type": "number", + "value": 16.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S107.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S192.marks", + "type": "number", + "value": 4.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S192.percent", + "type": "number", + "value": 7.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S253.marks", + "type": "number", + "value": 6.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S253.percent", + "type": "number", + "value": 10.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S253.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S271.marks", + "type": "number", + "value": 35.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S271.percent", + "type": "number", + "value": 61.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S271.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S314.marks", + "type": "number", + "value": 8.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S314.percent", + "type": "number", + "value": 14.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S314.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S328.marks", + "type": "number", + "value": 15.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S328.percent", + "type": "number", + "value": 26.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S328.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S365.marks", + "type": "number", + "value": 49.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S365.percent", + "type": "number", + "value": 86.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S365.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S028", + "S092", + "S107", + "S192", + "S253", + "S314", + "S328" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 7, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 31.4, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-e0022/tests/test.sh b/tasks/class-test/class-test-marking-d3-e0022/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-e0022/tests/verify.py b/tasks/class-test/class-test-marking-d3-e0022/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-e0022/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0010/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0010/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..932edce076373a8321b3b7b55bf958a99fe2ac37 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,C,3 +Q2,A,1 +Q3,A,2 +Q4,A,1 +Q5,A,3 +Q6,C,2 +Q7,A,1 +Q8,C,1 +Q9,A,3 +Q10,D,1 +Q11,B,2 +Q12,A,2 +Q13,VOID,1 +Q14,C,2 +Q15,B,2 +Q16,D,1 +Q17,C,2 +Q18,C,1 +Q19,B,1 +Q20,D,2 +Q21,C,2 +Q22,D,2 +Q23,C,1 +Q24,A,2 +Q25,D,2 +Q26,B,3 +Q27,A,1 +Q28,C,3 +Q29,D,3 +Q30,D,2 diff --git a/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..5a5e2f92a3d16a23d9d61da8cec5dc517e31943b --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S113,9B,C,C,,A,A,C,A,C,A,D,B,A,C,C,B,D,C,C,B,D,C,D,C,A,D,B,A,C,D,D +S341,9B,C,AD,A,A,A,C,A,D,A,C,B,A,B,C,B,D,C,C,B,D,C,D,C,A,D,B,A,C,D,BD +S396,9A,C,A,A,A,AD,C,A,C,A,D,B,A,D,B,B,D,C,C,B,D,C,D,C,A,C,B,A,C,D,D +S071,9A,D,A,C,A,D,C,A,C,A,A,B,D,A,D,B,D,D,C,C,D,A,,CD,A,A,B,A,D,A,D +S040,9A,D,AC,D,,,B,,A,D,A,D,A,D,A,C,C,C,,C,,,,C,D,,A,D,D,C,C +S289,9B,C,D,A,A,B,C,A,C,D,,B,A,,C,B,D,C,C,B,D,C,D,C,A,D,B,A,A,A,D +S072,9B,,A,D,D,D,C,A,C,,A,B,A,C,B,B,D,C,C,B,D,C,D,C,B,B,,A,C,D,D +S296,9B,C,A,AD,A,A,D,B,,C,B,B,C,C,C,B,C,D,C,B,D,C,D,B,A,D,B,C,C,D,A +S147,9B,C,A,A,A,A,C,A,C,A,A,B,A,A,C,B,D,ACD,C,B,D,C,D,C,A,D,C,D,C,D,D +S390,9B,C,D,D,A,,C,D,C,A,D,B,ABC,D,C,B,A,B,C,B,D,C,D,C,A,D,B,A,C,B,D diff --git a/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..5b6be683921003c8b710c7208521107a238bc4f5 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d3-t0010/instruction.md b/tasks/class-test/class-test-marking-d3-t0010/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0010/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0010/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0010/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0010/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0010/task.toml b/tasks/class-test/class-test-marking-d3-t0010/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..52e9ca89fd2528f3468287b19422d1aec5eeb4ce --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 10 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0010/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0010/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..a2badec981ede002c7743df49b0b96fe27a5f48e --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/tests/checks.json @@ -0,0 +1,255 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 54, + "weight": 1.0 + }, + { + "path": "students.S040.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S040.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S040.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S071.marks", + "type": "number", + "value": 18.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S071.percent", + "type": "number", + "value": 33.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S071.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S072.marks", + "type": "number", + "value": 28.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S072.percent", + "type": "number", + "value": 53.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S072.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S113.marks", + "type": "number", + "value": 50.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S113.percent", + "type": "number", + "value": 94.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S113.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S147.marks", + "type": "number", + "value": 45.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S147.percent", + "type": "number", + "value": 83.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S147.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S289.marks", + "type": "number", + "value": 36.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S289.percent", + "type": "number", + "value": 68.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S289.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S296.marks", + "type": "number", + "value": 30.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S296.percent", + "type": "number", + "value": 56.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S296.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S341.marks", + "type": "number", + "value": 47.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S341.percent", + "type": "number", + "value": 88.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S341.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S390.marks", + "type": "number", + "value": 36.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S390.percent", + "type": "number", + "value": 66.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S390.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S396.marks", + "type": "number", + "value": 45.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S396.percent", + "type": "number", + "value": 83.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S396.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S040", + "S071" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 62.8, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0010/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0010/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0010/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0010/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0010/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0022/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0022/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..246527bd42b6953775fac17626e32983b8482d39 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,C,2 +Q2,C,1 +Q3,VOID,3 +Q4,D,3 +Q5,A,3 +Q6,D,2 +Q7,D,2 +Q8,A,3 +Q9,C,3 +Q10,D,3 +Q11,D,1 +Q12,C,1 +Q13,A,2 +Q14,C,1 +Q15,C,2 +Q16,C,3 +Q17,D,2 +Q18,B,2 +Q19,D,1 +Q20,D,3 +Q21,C,2 +Q22,B,3 +Q23,B,2 +Q24,D,1 +Q25,B,1 +Q26,D,3 +Q27,C,2 +Q28,VOID,3 +Q29,A,2 +Q30,C,2 diff --git a/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..3e9318887794db3cf21fa00a5bac4ce88e749d5f --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S352,9B,D,C,B,,A,D,B,A,,,D,C,D,A,C,,,B,,C,C,C,A,A,B,C,B,A,C,B +S011,9A,C,C,A,D,A,D,D,A,C,D,D,C,A,C,C,C,,B,D,D,C,A,B,D,ABC,B,C,BC,A, +S259,9A,AC,C,B,A,A,A,D,D,C,D,B,C,B,C,C,C,D,B,D,D,C,B,B,,B,C,D,AB,A,C +S010,9A,,D,A,A,C,B,B,D,,C,C,C,B,D,A,D,A,,B,,D,C,D,A,A,,B,D,,A +S169,9B,C,C,D,,A,D,D,B,C,D,D,C,,C,C,C,,B,D,A,C,CD,B,D,C,D,C,D,D,C +S313,9A,A,C,B,D,A,D,D,A,D,D,D,B,A,C,C,C,D,B,D,D,C,B,B,D,B,D,,C,D,C +S318,9B,A,B,,D,C,C,D,A,C,B,,C,A,A,B,C,C,B,D,B,C,C,B,D,AD,D,C,B,AB,D +S216,9A,C,B,A,D,A,D,D,A,C,D,D,D,A,C,C,C,D,B,D,D,C,B,B,D,ABC,D,C,A,C,C +S106,9B,B,D,C,A,B,A,CD,B,C,A,D,D,,C,C,,D,A,D,C,BC,B,D,D,C,,D,A,A,C +S120,9B,C,C,C,D,,D,,,,AD,D,C,B,C,C,A,B,ABD,C,D,C,C,C,D,A,B,B,A,A,C diff --git a/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..47a1170bdc12333630c6e803a4871c5d81115be2 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d3-t0022/instruction.md b/tasks/class-test/class-test-marking-d3-t0022/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0022/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0022/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0022/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0022/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0022/task.toml b/tasks/class-test/class-test-marking-d3-t0022/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..0c00f7f0ee214b52f29af61ad346754a35f69def --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 22 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0022/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0022/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..974e622447c579fb9fc55b87eaa24fbeda017340 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/tests/checks.json @@ -0,0 +1,258 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 58, + "weight": 1.0 + }, + { + "path": "students.S010.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S010.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S010.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S011.marks", + "type": "number", + "value": 43.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S011.percent", + "type": "number", + "value": 75.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S011.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S106.marks", + "type": "number", + "value": 2.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S106.percent", + "type": "number", + "value": 3.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S106.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S120.marks", + "type": "number", + "value": 11.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S120.percent", + "type": "number", + "value": 19.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S120.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S169.marks", + "type": "number", + "value": 33.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S169.percent", + "type": "number", + "value": 56.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S169.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S216.marks", + "type": "number", + "value": 50.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S216.percent", + "type": "number", + "value": 87.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S216.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S259.marks", + "type": "number", + "value": 30.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S259.percent", + "type": "number", + "value": 51.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S259.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S313.marks", + "type": "number", + "value": 44.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S313.percent", + "type": "number", + "value": 75.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S313.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S318.marks", + "type": "number", + "value": 16.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S318.percent", + "type": "number", + "value": 28.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S318.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S352.marks", + "type": "number", + "value": 5.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S352.percent", + "type": "number", + "value": 9.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S352.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S010", + "S106", + "S120", + "S318", + "S352" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 40.7, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0022/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0022/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0022/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0022/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0022/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0034/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0034/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..72c18b7855c11381519183bab3066d7530bbdca9 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,B,1 +Q2,B,1 +Q3,D,3 +Q4,A,2 +Q5,D,3 +Q6,A,1 +Q7,B,1 +Q8,A,1 +Q9,A,2 +Q10,A,2 +Q11,D,1 +Q12,A,1 +Q13,B,2 +Q14,VOID,1 +Q15,C,1 +Q16,C,3 +Q17,C,1 +Q18,VOID,2 +Q19,A,1 +Q20,A,1 +Q21,D,1 +Q22,C,2 +Q23,A,3 +Q24,B,1 +Q25,C,3 +Q26,C,3 +Q27,D,1 +Q28,D,2 +Q29,D,3 +Q30,B,1 diff --git a/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..5e2f2f6cd58a55914e21c8d383806c4e3f5ebbdc --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S334,9B,B,A,D,B,C,,,C,A,B,B,A,C,,,B,C,C,A,D,D,,,B,,C,A,A,C, +S234,9A,B,B,D,A,B,A,ABC,A,A,A,D,A,B,C,C,C,C,C,A,AD,D,C,A,B,B,,D,D,D,B +S012,9B,D,D,A,A,,C,D,D,A,C,C,D,D,B,D,A,A,B,B,C,C,AB,B,B,C,B,,A,C, +S208,9B,B,B,D,D,D,A,B,A,A,,D,B,,B,C,C,C,D,D,ACD,D,C,A,C,C,C,B,D,D,B +S018,9B,,B,D,A,A,A,B,B,A,A,A,A,B,D,C,A,C,A,A,C,,D,A,B,C,C,D,B,D,D +S206,9A,B,A,D,A,B,A,,AB,C,,C,A,,A,C,ACD,A,B,A,,C,B,A,A,,A,C,D,D,B +S041,9A,B,D,D,A,B,B,B,A,A,A,D,A,B,,B,A,C,A,A,,D,,B,,C,C,D,D,D,B +S287,9B,B,,D,,C,B,C,,A,D,,A,B,A,,C,C,C,A,A,D,,A,B,C,,B,C,C,D +S039,9A,B,B,D,A,D,A,B,A,A,A,B,A,B,BCD,,C,C,A,A,A,D,C,ABC,B,,C,D,D,D,B +S355,9A,BC,A,D,A,C,D,A,A,A,,D,D,B,B,C,D,,C,A,C,D,C,A,A,C,A,,D,C,B diff --git a/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..867f7f79e6c3a4011f5af0cdd4c03ed8b5396c6c --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 60 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d3-t0034/instruction.md b/tasks/class-test/class-test-marking-d3-t0034/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0034/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0034/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0034/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0034/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0034/task.toml b/tasks/class-test/class-test-marking-d3-t0034/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..86fdaf751928b60b00d16582568852dfa0b3797e --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 34 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0034/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0034/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..466299f9f49cedb3b20e07b3f6cbd876d800b24d --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/tests/checks.json @@ -0,0 +1,258 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 48, + "weight": 1.0 + }, + { + "path": "students.S012.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S012.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S012.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S018.marks", + "type": "number", + "value": 25.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S018.percent", + "type": "number", + "value": 52.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S018.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S039.marks", + "type": "number", + "value": 38.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S039.percent", + "type": "number", + "value": 79.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S039.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S041.marks", + "type": "number", + "value": 26.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S041.percent", + "type": "number", + "value": 54.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S041.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S206.marks", + "type": "number", + "value": 9.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S206.percent", + "type": "number", + "value": 18.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S206.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S208.marks", + "type": "number", + "value": 33.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S208.percent", + "type": "number", + "value": 69.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S208.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S234.marks", + "type": "number", + "value": 33.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S234.percent", + "type": "number", + "value": 68.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S234.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S287.marks", + "type": "number", + "value": 16.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S287.percent", + "type": "number", + "value": 33.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S287.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S334.marks", + "type": "number", + "value": 3.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S334.percent", + "type": "number", + "value": 6.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S334.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S355.marks", + "type": "number", + "value": 15.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S355.percent", + "type": "number", + "value": 32.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S355.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S012", + "S206", + "S287", + "S334", + "S355" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 41.5, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0034/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0034/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0034/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0034/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0034/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0046/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0046/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..dc69da929dde8e32f7f4a9dec80fdee958197034 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,B,1 +Q2,D,1 +Q3,C,3 +Q4,A,3 +Q5,A,3 +Q6,D,2 +Q7,D,3 +Q8,C,1 +Q9,A,3 +Q10,C,3 +Q11,C,1 +Q12,VOID,2 +Q13,C,2 +Q14,C,1 +Q15,A,3 +Q16,C,3 +Q17,B,1 +Q18,A,2 +Q19,A,1 +Q20,D,3 +Q21,D,1 +Q22,C,2 +Q23,B,2 +Q24,B,1 +Q25,C,3 +Q26,D,3 +Q27,VOID,2 +Q28,C,1 +Q29,A,1 +Q30,D,1 diff --git a/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..a02d296b2695cdb5dade6cf53736fca9076ead65 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S055,9B,B,D,B,C,,,C,C,,,D,B,B,,D,B,D,B,A,CD,D,,A,C,,C,C,A,D,A +S260,9A,B,D,C,A,A,D,D,C,A,C,C,A,C,C,A,C,B,A,A,D,D,C,B,B,ABC,ABD,C,C,A,D +S310,9B,B,D,C,A,A,D,B,AC,A,C,C,,C,C,A,C,B,A,B,D,D,C,B,B,C,D,B,C,A,D +S278,9B,B,A,C,A,A,C,AD,C,,,B,B,C,C,D,C,B,A,B,,A,A,B,B,C,A,D,C,C,B +S259,9B,A,D,C,D,,C,D,,C,C,C,D,C,D,C,B,B,,B,D,D,B,D,D,A,D,C,C,AC,D +S136,9A,A,A,C,A,A,,D,D,A,C,,A,C,B,A,C,B,A,,C,D,C,B,B,C,B,B,C,A,D +S178,9A,D,,B,C,,D,D,C,B,,,C,,,A,C,D,B,,,D,C,A,A,C,B,A,C,C,C +S287,9A,B,D,C,A,A,D,D,C,A,C,C,B,C,C,B,C,A,A,C,D,D,C,B,B,C,D,A,C,A,D +S113,9B,B,D,C,B,D,D,,,A,C,B,B,C,,,D,B,A,A,D,B,C,B,B,C,D,D,C,A,D +S314,9A,B,D,C,A,A,BD,D,C,A,C,BC,D,C,C,A,C,B,A,A,ACD,B,C,A,D,C,D,C,C,A,D diff --git a/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..2841f24bcd4e348024d6173db33772a087cf619d --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 50 or more | +| D | 35 or more | +| U | under 35 | diff --git a/tasks/class-test/class-test-marking-d3-t0046/instruction.md b/tasks/class-test/class-test-marking-d3-t0046/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0046/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0046/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0046/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0046/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0046/task.toml b/tasks/class-test/class-test-marking-d3-t0046/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9e9227980f996e2ef2fc295504ec55ad6dd1a06e --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 46 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0046/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0046/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..1a76eb895c8c7a55a3237e56f748ca825b5c0406 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/tests/checks.json @@ -0,0 +1,256 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 55, + "weight": 1.0 + }, + { + "path": "students.S055.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S055.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S055.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S113.marks", + "type": "number", + "value": 33.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S113.percent", + "type": "number", + "value": 60.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S113.grade", + "type": "str", + "value": "C", + "weight": 1.0 + }, + { + "path": "students.S136.marks", + "type": "number", + "value": 38.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S136.percent", + "type": "number", + "value": 70.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S136.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S178.marks", + "type": "number", + "value": 13.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S178.percent", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S178.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S259.marks", + "type": "number", + "value": 16.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S259.percent", + "type": "number", + "value": 30.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S259.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S260.marks", + "type": "number", + "value": 47.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S260.percent", + "type": "number", + "value": 86.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S260.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S278.marks", + "type": "number", + "value": 22.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S278.percent", + "type": "number", + "value": 40.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S278.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S287.marks", + "type": "number", + "value": 48.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S287.percent", + "type": "number", + "value": 88.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S287.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S310.marks", + "type": "number", + "value": 48.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S310.percent", + "type": "number", + "value": 88.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S310.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S314.marks", + "type": "number", + "value": 42.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S314.percent", + "type": "number", + "value": 77.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S314.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S055", + "S178", + "S259" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 56.7, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0046/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0046/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0046/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0046/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0046/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0058/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0058/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..db56f14158a78e35afa5ef6ce9952fc58c23ce6b --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,A,2 +Q2,D,2 +Q3,VOID,1 +Q4,B,2 +Q5,D,1 +Q6,D,1 +Q7,A,1 +Q8,B,2 +Q9,D,1 +Q10,C,1 +Q11,B,3 +Q12,B,2 +Q13,D,2 +Q14,B,2 +Q15,A,1 +Q16,D,1 +Q17,VOID,2 +Q18,D,2 +Q19,B,3 +Q20,B,2 +Q21,B,2 +Q22,A,2 +Q23,D,1 +Q24,D,2 +Q25,C,1 +Q26,C,1 +Q27,A,1 +Q28,C,1 +Q29,C,1 +Q30,B,2 diff --git a/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..0a8d0594d81749d398473b7f03a48d348f0a9253 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S343,9B,B,D,C,B,D,D,A,B,,ABC,,B,D,B,A,D,,D,,B,A,A,D,D,C,C,A,C,C,B +S136,9B,C,,B,A,D,D,A,C,A,C,C,C,B,B,,D,B,C,B,B,,A,D,D,A,C,B,C,B, +S098,9B,D,AC,,D,,D,A,C,A,,C,B,C,B,C,,B,D,A,A,ACD,A,B,C,,C,C,,C,A +S026,9B,C,D,,A,B,D,,C,D,D,D,B,C,,,,B,,AB,,A,C,C,,D,C,D,B,C,A +S312,9A,,D,A,B,D,D,A,B,D,C,B,B,D,B,A,D,C,D,B,B,B,A,D,D,C,C,A,C,C,B +S170,9B,,D,D,B,D,D,A,B,D,C,B,B,D,,D,D,B,D,B,B,B,A,D,D,C,,A,C,C,B +S271,9A,A,B,D,B,D,D,A,B,D,C,B,B,B,B,A,D,D,,B,ABD,B,A,D,D,C,C,A,C,C,B +S159,9B,A,D,A,C,B,D,,B,A,C,B,B,D,D,,D,B,D,AB,B,B,A,D,D,C,C,A,C,C,B +S233,9B,A,A,C,B,D,D,A,,C,B,D,A,B,B,,A,,D,B,A,A,B,D,D,D,A,A,D,C,B +S140,9A,A,A,A,B,A,C,A,C,D,C,,B,,A,A,,C,D,A,AB,,A,D,D,B,C,A,B,A, diff --git a/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..b6dfd590446b256a000bbe0c38a7c60843bdd5c6 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one half of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 80 or more | +| B | 65 or more | +| C | 50 or more | +| D | 45 or more | +| U | under 45 | diff --git a/tasks/class-test/class-test-marking-d3-t0058/instruction.md b/tasks/class-test/class-test-marking-d3-t0058/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0058/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0058/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0058/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0058/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0058/task.toml b/tasks/class-test/class-test-marking-d3-t0058/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..cb096314c2c74762f5651a2c1ef39ef6c8c0d82f --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 58 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0058/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0058/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..07857192b244da5fe0cff545191f03f0f9daa05b --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/tests/checks.json @@ -0,0 +1,258 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 45, + "weight": 1.0 + }, + { + "path": "students.S026.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S026.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S026.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S098.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S098.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S098.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S136.marks", + "type": "number", + "value": 9.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S136.percent", + "type": "number", + "value": 21.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S136.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S140.marks", + "type": "number", + "value": 11.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S140.percent", + "type": "number", + "value": 24.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S140.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S159.marks", + "type": "number", + "value": 29.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S159.percent", + "type": "number", + "value": 65.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S159.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S170.marks", + "type": "number", + "value": 38.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S170.percent", + "type": "number", + "value": 85.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S170.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S233.marks", + "type": "number", + "value": 10.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S233.percent", + "type": "number", + "value": 23.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S233.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S271.marks", + "type": "number", + "value": 34.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S271.percent", + "type": "number", + "value": 75.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S271.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S312.marks", + "type": "number", + "value": 43.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S312.percent", + "type": "number", + "value": 95.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S312.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S343.marks", + "type": "number", + "value": 30.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S343.percent", + "type": "number", + "value": 67.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S343.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S026", + "S098", + "S136", + "S140", + "S233" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 45.9, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0058/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0058/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0058/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0058/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0058/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0070/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0070/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..0198c239647956c560c6f44067af93429d28243c --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,D,1 +Q2,A,1 +Q3,B,1 +Q4,B,3 +Q5,C,2 +Q6,C,2 +Q7,D,1 +Q8,D,1 +Q9,A,1 +Q10,C,3 +Q11,C,3 +Q12,C,2 +Q13,A,1 +Q14,B,1 +Q15,B,2 +Q16,A,1 +Q17,B,2 +Q18,B,2 +Q19,D,2 +Q20,A,1 +Q21,C,1 +Q22,B,3 +Q23,A,1 +Q24,B,2 +Q25,D,2 +Q26,VOID,3 +Q27,B,2 +Q28,C,3 +Q29,A,3 +Q30,D,3 diff --git a/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..d582d1144d451a17bc1abed0e5350f1d9681ea7a --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S030,9B,B,,,D,D,C,ABD,C,B,B,D,B,,B,D,D,D,A,,D,D,A,B,A,C,D,B,,C,D +S146,9A,B,A,B,B,C,C,D,D,A,C,C,C,A,B,B,C,,B,D,,C,B,A,D,D,C,,C,A,D +S229,9A,C,B,B,C,C,C,D,D,A,,C,A,,,,A,A,C,A,A,C,A,C,B,D,C,B,,, +S159,9B,D,A,BC,B,D,C,D,D,A,C,ACD,C,A,B,B,A,B,B,D,A,C,B,A,B,D,C,B,C,A,D +S296,9A,D,A,,B,C,C,,D,A,C,C,AC,A,B,B,AC,B,B,D,A,C,,A,B,,B,B,C,A,D +S343,9A,D,A,B,B,C,C,D,D,A,C,C,C,A,B,B,D,B,B,D,A,C,B,A,AB,D,B,B,C,A,D +S118,9A,D,,C,B,,C,ABD,,,A,A,C,A,B,B,A,D,B,C,,C,,B,B,C,C,C,C,,D +S186,9A,C,A,B,B,C,C,D,D,A,C,C,C,,B,B,A,A,C,B,A,D,B,B,ABC,D,D,B,C,A,D +S127,9B,,,D,B,C,D,B,D,B,C,A,B,A,ABD,C,B,,B,C,A,D,C,,B,ACD,,C,B,AC,B +S105,9B,D,A,B,B,C,C,BCD,B,A,,C,C,A,B,B,A,B,B,D,A,C,B,A,B,AD,B,B,C,A,D diff --git a/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..2841f24bcd4e348024d6173db33772a087cf619d --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 65 or more | +| C | 50 or more | +| D | 35 or more | +| U | under 35 | diff --git a/tasks/class-test/class-test-marking-d3-t0070/instruction.md b/tasks/class-test/class-test-marking-d3-t0070/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0070/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0070/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0070/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0070/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0070/task.toml b/tasks/class-test/class-test-marking-d3-t0070/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..cc0d1d4f5cb0ed14ff3fb4d61e32c94926bce678 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 70 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0070/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0070/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..84225e8d209d353a793b4d5bc39134b39741fb9a --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/tests/checks.json @@ -0,0 +1,256 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 53, + "weight": 1.0 + }, + { + "path": "students.S030.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S030.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S030.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S105.marks", + "type": "number", + "value": 45.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S105.percent", + "type": "number", + "value": 84.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S105.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S118.marks", + "type": "number", + "value": 19.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S118.percent", + "type": "number", + "value": 37.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S118.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S127.marks", + "type": "number", + "value": 6.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S127.percent", + "type": "number", + "value": 12.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S127.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S146.marks", + "type": "number", + "value": 43.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S146.percent", + "type": "number", + "value": 81.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S146.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S159.marks", + "type": "number", + "value": 45.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S159.percent", + "type": "number", + "value": 85.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S159.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S186.marks", + "type": "number", + "value": 38.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S186.percent", + "type": "number", + "value": 72.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S186.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S229.marks", + "type": "number", + "value": 15.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S229.percent", + "type": "number", + "value": 29.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S229.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S296.marks", + "type": "number", + "value": 42.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S296.percent", + "type": "number", + "value": 79.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S296.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S343.marks", + "type": "number", + "value": 49.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S343.percent", + "type": "number", + "value": 92.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S343.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S030", + "S127", + "S229" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 5, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 57.6, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0070/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0070/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0070/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0070/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0070/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-marking-d3-t0082/environment/Dockerfile b/tasks/class-test/class-test-marking-d3-t0082/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_key.csv b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_key.csv new file mode 100644 index 0000000000000000000000000000000000000000..5eb55f2b6b2a4f2736470d720604b5ddd2681054 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_key.csv @@ -0,0 +1,31 @@ +item,key,marks +Q1,A,1 +Q2,C,3 +Q3,B,3 +Q4,B,3 +Q5,C,2 +Q6,D,3 +Q7,D,3 +Q8,D,2 +Q9,B,1 +Q10,VOID,2 +Q11,A,2 +Q12,B,2 +Q13,D,1 +Q14,B,1 +Q15,B,1 +Q16,C,3 +Q17,C,3 +Q18,B,3 +Q19,D,1 +Q20,C,1 +Q21,B,3 +Q22,B,2 +Q23,D,2 +Q24,VOID,2 +Q25,B,3 +Q26,C,2 +Q27,A,1 +Q28,B,2 +Q29,D,2 +Q30,D,2 diff --git a/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_sheets.csv b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_sheets.csv new file mode 100644 index 0000000000000000000000000000000000000000..99c2aff247d07c79aada38ccfe12bf4bafb208c0 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/answer_sheets.csv @@ -0,0 +1,11 @@ +student_id,form,Q1,Q2,Q3,Q4,Q5,Q6,Q7,Q8,Q9,Q10,Q11,Q12,Q13,Q14,Q15,Q16,Q17,Q18,Q19,Q20,Q21,Q22,Q23,Q24,Q25,Q26,Q27,Q28,Q29,Q30 +S132,9A,A,,B,B,C,D,D,D,B,D,A,B,D,B,D,C,C,B,D,,B,BC,C,D,B,A,A,B,D,D +S151,9B,,C,,C,C,D,D,B,B,B,AB,A,D,B,BCD,C,C,B,D,C,AB,B,A,,B,D,B,B,D,D +S107,9B,A,C,B,B,C,D,D,D,B,C,A,B,D,B,B,C,C,B,D,C,B,BCD,D,C,B,C,A,B,D,D +S195,9B,,A,B,B,B,,A,B,C,C,B,B,B,A,B,C,,,B,D,,B,C,B,A,,C,C,A,D +S146,9B,A,C,C,D,C,D,D,D,D,C,A,B,D,A,B,C,C,B,D,B,B,B,B,A,B,D,A,B,D,D +S235,9B,B,C,B,B,C,D,,D,B,D,A,A,D,C,B,C,D,B,D,C,B,B,D,C,B,C,A,B,D,D +S159,9A,A,C,C,D,C,D,D,D,B,B,A,B,CD,B,C,C,C,B,D,A,B,B,D,A,B,D,C,B,D,D +S032,9B,,,A,C,B,A,A,,D,D,D,C,A,B,B,A,,BD,A,A,A,,B,A,D,C,B,C,D, +S327,9A,,B,,,C,D,C,C,A,A,A,C,D,,A,BCD,C,A,C,A,BD,A,D,A,C,B,C,AB,B,A +S174,9A,A,C,B,B,C,D,ACD,,B,D,A,C,D,,B,C,C,B,B,C,B,A,D,D,B,C,A,B,D,D diff --git a/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/marking_scheme.md b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/marking_scheme.md new file mode 100644 index 0000000000000000000000000000000000000000..a7df01490762035d2f2d830d5002bb06ec8b27fc --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/environment/workdir/marking_scheme.md @@ -0,0 +1,24 @@ +# Marking scheme + +Each item has one correct option, given in `answer_key.csv` with the marks it carries. + +- A correct answer earns the item's marks. +- A wrong answer loses one quarter of the item's marks (negative marking). +- An unanswered item (empty cell) earns 0 and loses nothing. +- If a student's total comes out below 0, it is recorded as 0. +- A cell with more than one letter (for example `BD`) means the student marked more than one option: treat it as a wrong answer. +- An item whose key is `VOID` has been withdrawn. It earns and loses nothing for anyone, and its marks are not part of the maximum. + +The maximum is the sum of the marks of all items that have not been withdrawn. The percentage is the student's total divided by the maximum, times 100. + +## Grade boundaries + +The grade is decided on the exact percentage, before any rounding. + +| Grade | Percentage | +|---|---| +| A | 75 or more | +| B | 60 or more | +| C | 55 or more | +| D | 40 or more | +| U | under 40 | diff --git a/tasks/class-test/class-test-marking-d3-t0082/instruction.md b/tasks/class-test/class-test-marking-d3-t0082/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..5273c9ac123f39edcee835fa8968aea96d8bd6e4 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/instruction.md @@ -0,0 +1,16 @@ +# Class test: marking + +`answer_sheets.csv` holds the answer sheets of 10 students for a 30-item multiple-choice test. `answer_key.csv` gives the key and the marks for each item. `marking_scheme.md` gives the marking rules and the grade boundaries: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `max_marks`: the maximum total, an integer. +- `students`: an object keyed by student id. Each value has + - `marks`: the student's total, 2 decimals. + - `percent`: the percentage, 1 decimal. + - `grade`: one of `A`, `B`, `C`, `D`, `U`. +- `grade_u`: list of the student ids with grade `U` (any order; an empty list if none). +- `grade_counts`: an object with the keys `A`, `B`, `C`, `D`, `U`, each the number of students with that grade (0 if none). +- `mean_percent`: the mean of the students' unrounded percentages, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-marking-d3-t0082/solution/oracle.py b/tasks/class-test/class-test-marking-d3-t0082/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-marking-d3-t0082/solution/solve.sh b/tasks/class-test/class-test-marking-d3-t0082/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-marking-d3-t0082/task.toml b/tasks/class-test/class-test-marking-d3-t0082/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..950e58a880660bb7f15da6836ee18038845c29d0 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "marking", "isced-0111", "train"] +isced_f = "0111" +family = "marking" +split = "train" +seed = 82 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-marking-d3-t0082/tests/checks.json b/tasks/class-test/class-test-marking-d3-t0082/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..cf8f3b98a28ffd3ae711f697749c72377cf17ff9 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/tests/checks.json @@ -0,0 +1,256 @@ +[ + { + "path": "max_marks", + "type": "int", + "value": 58, + "weight": 1.0 + }, + { + "path": "students.S032.marks", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S032.percent", + "type": "number", + "value": 0.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S032.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S107.marks", + "type": "number", + "value": 55.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S107.percent", + "type": "number", + "value": 95.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S107.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S132.marks", + "type": "number", + "value": 45.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S132.percent", + "type": "number", + "value": 78.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S132.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S146.marks", + "type": "number", + "value": 41.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S146.percent", + "type": "number", + "value": 72.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S146.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S151.marks", + "type": "number", + "value": 31.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S151.percent", + "type": "number", + "value": 54.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S151.grade", + "type": "str", + "value": "D", + "weight": 1.0 + }, + { + "path": "students.S159.marks", + "type": "number", + "value": 43.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S159.percent", + "type": "number", + "value": 74.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S159.grade", + "type": "str", + "value": "B", + "weight": 1.0 + }, + { + "path": "students.S174.marks", + "type": "number", + "value": 45.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S174.percent", + "type": "number", + "value": 77.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S174.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S195.marks", + "type": "number", + "value": 9.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S195.percent", + "type": "number", + "value": 15.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S195.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "students.S235.marks", + "type": "number", + "value": 46.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S235.percent", + "type": "number", + "value": 79.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S235.grade", + "type": "str", + "value": "A", + "weight": 1.0 + }, + { + "path": "students.S327.marks", + "type": "number", + "value": 3.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S327.percent", + "type": "number", + "value": 6.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S327.grade", + "type": "str", + "value": "U", + "weight": 1.0 + }, + { + "path": "grade_u", + "type": "set", + "value": [ + "S032", + "S195", + "S327" + ], + "item": "str", + "weight": 2 + }, + { + "path": "grade_counts.A", + "type": "int", + "value": 4, + "weight": 1.0 + }, + { + "path": "grade_counts.B", + "type": "int", + "value": 2, + "weight": 1.0 + }, + { + "path": "grade_counts.C", + "type": "int", + "value": 0, + "weight": 1.0 + }, + { + "path": "grade_counts.D", + "type": "int", + "value": 1, + "weight": 1.0 + }, + { + "path": "grade_counts.U", + "type": "int", + "value": 3, + "weight": 1.0 + }, + { + "path": "mean_percent", + "type": "number", + "value": 55.4, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-marking-d3-t0082/tests/test.sh b/tasks/class-test/class-test-marking-d3-t0082/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-marking-d3-t0082/tests/verify.py b/tasks/class-test/class-test-marking-d3-t0082/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-marking-d3-t0082/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-e0003/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-e0003/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..1400e71d19e337a27ffcf19cffa73d444c940c5a --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S370,9,18 +S189,12,12 +S215,7,15 +S270,25,13 +S194,20,18 +S196,5,14 +S326,28,18 +S345,29,10 +S283,18,11 +S037,2,16 diff --git a/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..4cadc6bd8187973694c4fc83de6b8aae9b4869e4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 50. + +## Rescaling to a target mean and standard deviation + +The target mean is 32 and the target standard deviation is 8. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 32 + (x - M) / SD * 8 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-e0003/instruction.md b/tasks/class-test/class-test-moderation-d1-e0003/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-e0003/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-e0003/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-e0003/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-e0003/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-e0003/task.toml b/tasks/class-test/class-test-moderation-d1-e0003/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..2ae9f4e41b6001ff974ac0841aa45e7b7f3c2d91 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10003 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-e0003/tests/checks.json b/tasks/class-test/class-test-moderation-d1-e0003/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..7c4ceb09f2d9ec422028e1b1888cb6644aa5ee86 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 14.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 2.84, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S037.scaled", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S189.scaled", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S194.scaled", + "type": "number", + "value": 41.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S196.scaled", + "type": "number", + "value": 30.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S215.scaled", + "type": "number", + "value": 33.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S270.scaled", + "type": "number", + "value": 27.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S283.scaled", + "type": "number", + "value": 22.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S326.scaled", + "type": "number", + "value": 41.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S345.scaled", + "type": "number", + "value": 19.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S370.scaled", + "type": "number", + "value": 41.9, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-e0003/tests/test.sh b/tasks/class-test/class-test-moderation-d1-e0003/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-e0003/tests/verify.py b/tasks/class-test/class-test-moderation-d1-e0003/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0003/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-e0015/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-e0015/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..4066dcc242230e1d5825b63ebcf5825cf5c6f08a --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S006,6,22 +S352,15,16 +S186,26,14 +S148,30,24 +S098,22,32 +S343,11,29 +S378,15,17 +S181,6,12 +S361,14,25 +S363,13,19 diff --git a/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..4ad55dc78958c7d641e382a03d69b1f586b08f62 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 39 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 39 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-e0015/instruction.md b/tasks/class-test/class-test-moderation-d1-e0015/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-e0015/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-e0015/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-e0015/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-e0015/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-e0015/task.toml b/tasks/class-test/class-test-moderation-d1-e0015/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..b7d9ef27a7816975550b737e93bdf7dd16b19fe1 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10015 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-e0015/tests/checks.json b/tasks/class-test/class-test-moderation-d1-e0015/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..e302058a84fbaebe5d92d4a37c47aa3d55225cf9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 21.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 6.21, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S006.scaled", + "type": "number", + "value": 40.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S098.scaled", + "type": "number", + "value": 56.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S148.scaled", + "type": "number", + "value": 43.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S181.scaled", + "type": "number", + "value": 24.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S186.scaled", + "type": "number", + "value": 27.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S343.scaled", + "type": "number", + "value": 51.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S352.scaled", + "type": "number", + "value": 31.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S361.scaled", + "type": "number", + "value": 45.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S363.scaled", + "type": "number", + "value": 35.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S378.scaled", + "type": "number", + "value": 32.6, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-e0015/tests/test.sh b/tasks/class-test/class-test-moderation-d1-e0015/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-e0015/tests/verify.py b/tasks/class-test/class-test-moderation-d1-e0015/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-e0015/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0003/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0003/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..2230a1531cb2c485b7504ce48be9d954180b9b8a --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S356,18,37 +S326,28,49 +S079,8,46 +S183,6,49 +S350,18,39 +S210,20,45 +S365,3,54 +S091,9,47 +S311,7,43 +S050,28,55 diff --git a/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..8bfb699e56c76b8f2f21ec860096e8e29a6b0744 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 19. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 19 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0003/instruction.md b/tasks/class-test/class-test-moderation-d1-t0003/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0003/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0003/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0003/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0003/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0003/task.toml b/tasks/class-test/class-test-moderation-d1-t0003/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..f37ae30ed5f97956e278f2e1ba9754a738e73577 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 3 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0003/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0003/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..3dc3fa71f98b75e608a3764c10692939cc692700 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 46.4, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 5.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S050.scaled", + "type": "number", + "value": 94.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S079.scaled", + "type": "number", + "value": 63.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S091.scaled", + "type": "number", + "value": 67.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S183.scaled", + "type": "number", + "value": 74.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S210.scaled", + "type": "number", + "value": 60.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S311.scaled", + "type": "number", + "value": 53.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S326.scaled", + "type": "number", + "value": 74.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S350.scaled", + "type": "number", + "value": 39.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S356.scaled", + "type": "number", + "value": 32.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S365.scaled", + "type": "number", + "value": 91.3, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0003/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0003/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0003/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0003/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0003/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0015/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0015/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..00502a289a645f2cc3f884dd6bfe03b99d3f5260 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S107,29,42 +S068,15,42 +S334,3,41 +S246,9,37 +S261,14,26 +S280,23,33 +S314,18,30 +S195,25,29 +S036,25,33 +S024,14,36 diff --git a/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..7b01c91329f7a788a7eeffd4ce0ec189b31bba6d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 80. + +## Rescaling to a target mean and standard deviation + +The target mean is 52 and the target standard deviation is 15. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 52 + (x - M) / SD * 15 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0015/instruction.md b/tasks/class-test/class-test-moderation-d1-t0015/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0015/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0015/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0015/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0015/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0015/task.toml b/tasks/class-test/class-test-moderation-d1-t0015/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..549037eb85db4be0e287834dca752e2716ff410d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 15 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0015/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0015/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..c12a783519a7257adb003a1ba33c399d271f51ac --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 34.9, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 5.37, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S024.scaled", + "type": "number", + "value": 55.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S036.scaled", + "type": "number", + "value": 46.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S068.scaled", + "type": "number", + "value": 71.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S107.scaled", + "type": "number", + "value": 71.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S195.scaled", + "type": "number", + "value": 35.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S246.scaled", + "type": "number", + "value": 57.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S261.scaled", + "type": "number", + "value": 27.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S280.scaled", + "type": "number", + "value": 46.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S314.scaled", + "type": "number", + "value": 38.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S334.scaled", + "type": "number", + "value": 69.0, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0015/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0015/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0015/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0015/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0015/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0027/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0027/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..c61d00516df31a68441c4df09223eac815b8acd0 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S341,4,25 +S161,29,18 +S045,24,18 +S115,24,34 +S337,14,21 +S195,8,31 +S286,19,21 +S308,15,27 +S313,26,17 +S357,28,27 diff --git a/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..da9f05348faa08371fc99b55522f2d81da906a9e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0027/instruction.md b/tasks/class-test/class-test-moderation-d1-t0027/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0027/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0027/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0027/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0027/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0027/task.toml b/tasks/class-test/class-test-moderation-d1-t0027/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..51794d940583453030a897da8c991e5d3dc5eeed --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 27 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0027/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0027/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..07fc6acb6c82a47bf2b4b261148d23b6c356fa3e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 23.9, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 5.54, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S045.scaled", + "type": "number", + "value": 25.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S115.scaled", + "type": "number", + "value": 57.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S161.scaled", + "type": "number", + "value": 25.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S195.scaled", + "type": "number", + "value": 51.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S286.scaled", + "type": "number", + "value": 31.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S308.scaled", + "type": "number", + "value": 43.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S313.scaled", + "type": "number", + "value": 23.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S337.scaled", + "type": "number", + "value": 31.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S341.scaled", + "type": "number", + "value": 39.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S357.scaled", + "type": "number", + "value": 43.2, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0027/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0027/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0027/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0027/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0027/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0039/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0039/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..f621421682e104fd99b271fa6c04dc2390d0145d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S139,17,36 +S102,2,22 +S153,30,23 +S227,28,21 +S305,12,29 +S353,17,29 +S334,7,26 +S128,29,21 +S231,1,35 +S342,9,21 diff --git a/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..f06f2e0da03e83b7331d70ccfe5655991bb2a214 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0039/instruction.md b/tasks/class-test/class-test-moderation-d1-t0039/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0039/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0039/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0039/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0039/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0039/task.toml b/tasks/class-test/class-test-moderation-d1-t0039/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..46768a8a5665d003a651dac00cb7b6f7819d1bd4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 39 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0039/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0039/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..74be28b514e6366ddb08e0fa224dd86f1a2863f1 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 26.3, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 5.46, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S102.scaled", + "type": "number", + "value": 29.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S128.scaled", + "type": "number", + "value": 27.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S139.scaled", + "type": "number", + "value": 54.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S153.scaled", + "type": "number", + "value": 31.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S227.scaled", + "type": "number", + "value": 27.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S231.scaled", + "type": "number", + "value": 52.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S305.scaled", + "type": "number", + "value": 41.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S334.scaled", + "type": "number", + "value": 36.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S342.scaled", + "type": "number", + "value": 27.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S353.scaled", + "type": "number", + "value": 41.9, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0039/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0039/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0039/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0039/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0039/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0051/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0051/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..aadf1c95b4bbfa8581a956f8874bd89eace6d100 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S210,15,30 +S148,4,37 +S266,2,31 +S279,22,23 +S322,8,31 +S147,9,34 +S174,23,25 +S307,5,35 +S318,5,34 +S062,6,35 diff --git a/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..da9f05348faa08371fc99b55522f2d81da906a9e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0051/instruction.md b/tasks/class-test/class-test-moderation-d1-t0051/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0051/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0051/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0051/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0051/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0051/task.toml b/tasks/class-test/class-test-moderation-d1-t0051/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..f5e309a9d28b398ccbd28b2c44626dc508e2d6ba --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 51 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0051/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0051/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f41e1a45063bcebc75e0f2f6b4aba0d984eed7a6 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 31.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 4.3, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S062.scaled", + "type": "number", + "value": 46.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S147.scaled", + "type": "number", + "value": 43.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S148.scaled", + "type": "number", + "value": 51.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S174.scaled", + "type": "number", + "value": 20.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S210.scaled", + "type": "number", + "value": 33.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S266.scaled", + "type": "number", + "value": 35.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S279.scaled", + "type": "number", + "value": 15.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S307.scaled", + "type": "number", + "value": 46.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S318.scaled", + "type": "number", + "value": 43.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S322.scaled", + "type": "number", + "value": 35.7, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0051/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0051/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0051/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0051/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0051/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0063/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0063/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..b3bf28f3f841bf34808598207fb0720e3b280104 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S275,14,16 +S296,15,39 +S006,17,32 +S025,7,28 +S071,20,32 +S129,29,15 +S162,4,25 +S098,19,19 +S151,28,20 +S270,22,27 diff --git a/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..b82d8f1022d8a2d79e932591aacc50b1405ca2c5 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 80. + +## Rescaling to a target mean and standard deviation + +The target mean is 52 and the target standard deviation is 14. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 52 + (x - M) / SD * 14 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0063/instruction.md b/tasks/class-test/class-test-moderation-d1-t0063/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0063/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0063/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0063/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0063/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0063/task.toml b/tasks/class-test/class-test-moderation-d1-t0063/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d5530231c5aec1b7a69bb07dbe04eac63db1eff0 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 63 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0063/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0063/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ef16501e95cf28506f71b626929421b51341cd1a --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 25.3, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 7.4, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S006.scaled", + "type": "number", + "value": 64.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S025.scaled", + "type": "number", + "value": 57.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S071.scaled", + "type": "number", + "value": 64.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S098.scaled", + "type": "number", + "value": 40.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S129.scaled", + "type": "number", + "value": 32.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S151.scaled", + "type": "number", + "value": 42.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S162.scaled", + "type": "number", + "value": 51.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S270.scaled", + "type": "number", + "value": 55.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S275.scaled", + "type": "number", + "value": 34.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S296.scaled", + "type": "number", + "value": 77.9, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0063/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0063/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0063/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0063/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0063/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d1-t0075/environment/Dockerfile b/tasks/class-test/class-test-moderation-d1-t0075/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..9d9c001e5197108dae2de2a0dfb895d45386f825 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/marks.csv @@ -0,0 +1,11 @@ +student_id,seat,mark +S280,16,11 +S153,6,31 +S208,4,20 +S277,9,14 +S151,23,29 +S063,24,30 +S192,2,16 +S091,6,17 +S183,8,19 +S308,16,29 diff --git a/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..da9f05348faa08371fc99b55522f2d81da906a9e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/environment/workdir/moderation_notes.md @@ -0,0 +1,11 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). diff --git a/tasks/class-test/class-test-moderation-d1-t0075/instruction.md b/tasks/class-test/class-test-moderation-d1-t0075/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..1bd780f5f4bdf35d52b89ac2d33200e108d603d8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/instruction.md @@ -0,0 +1,11 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of 10 students on a class test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `raw_mean`: the mean of the marks, 2 decimals. +- `raw_sd`: the population standard deviation of the marks, 2 decimals. +- `students`: an object keyed by student id, each with `scaled`: the rescaled mark, 1 decimal. + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d1-t0075/solution/oracle.py b/tasks/class-test/class-test-moderation-d1-t0075/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d1-t0075/solution/solve.sh b/tasks/class-test/class-test-moderation-d1-t0075/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d1-t0075/task.toml b/tasks/class-test/class-test-moderation-d1-t0075/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..ad07dd87ad24529f01d43e818b90fb62c344c902 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "easy" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 75 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d1-t0075/tests/checks.json b/tasks/class-test/class-test-moderation-d1-t0075/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..73f15b752d0e70b9ada7935e12443cfc8715a99e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/tests/checks.json @@ -0,0 +1,86 @@ +[ + { + "path": "raw_mean", + "type": "number", + "value": 21.6, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "raw_sd", + "type": "number", + "value": 7.07, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S063.scaled", + "type": "number", + "value": 50.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S091.scaled", + "type": "number", + "value": 29.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S151.scaled", + "type": "number", + "value": 48.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S153.scaled", + "type": "number", + "value": 51.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S183.scaled", + "type": "number", + "value": 33.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.scaled", + "type": "number", + "value": 28.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S208.scaled", + "type": "number", + "value": 34.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S277.scaled", + "type": "number", + "value": 25.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S280.scaled", + "type": "number", + "value": 20.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S308.scaled", + "type": "number", + "value": 48.5, + "abs_tol": 0.1, + "weight": 1.0 + } +] diff --git a/tasks/class-test/class-test-moderation-d1-t0075/tests/test.sh b/tasks/class-test/class-test-moderation-d1-t0075/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d1-t0075/tests/verify.py b/tasks/class-test/class-test-moderation-d1-t0075/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d1-t0075/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-e0007/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-e0007/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..7db4b334e511a2d9401c08e2b82453b1303217d9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S070,7C, +S080,7D,23 +S230,7C,47 +S135,7D,33 +S006,7D,35 +S053,7C,25 +S100,7C,44 +S249,7D, +S137,7D,26 +S048,7C,45 +S143,7D,16 +S398,7C,44 +S268,7C,33 +S040,7D,45 +S285,7C,36 +S241,7D,42 +S190,7D,20 +S338,7C,80 diff --git a/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..bcf705b8bbc71f72f75e12ed9e0b3c4cea7fba83 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 80. + +## Rescaling to a target mean and standard deviation + +The target mean is 52 and the target standard deviation is 15. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 52 + (x - M) / SD * 15 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 80 is set to 80, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-e0007/instruction.md b/tasks/class-test/class-test-moderation-d2-e0007/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-e0007/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-e0007/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-e0007/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-e0007/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-e0007/task.toml b/tasks/class-test/class-test-moderation-d2-e0007/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..9537f96f38d9a2c54ceff56fe6c4262795d56b6f --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10007 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-e0007/tests/checks.json b/tasks/class-test/class-test-moderation-d2-e0007/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..2525bb9b9d0b6a6e655f5f181a566546e695078c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7C.raw_mean", + "type": "number", + "value": 44.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_sd", + "type": "number", + "value": 15.21, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_mean", + "type": "number", + "value": 30.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_sd", + "type": "number", + "value": 9.77, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S006.scaled", + "type": "number", + "value": 59.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S040.scaled", + "type": "number", + "value": 75.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S048.scaled", + "type": "number", + "value": 52.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S053.scaled", + "type": "number", + "value": 33.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S080.scaled", + "type": "number", + "value": 41.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S100.scaled", + "type": "number", + "value": 51.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S135.scaled", + "type": "number", + "value": 56.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S137.scaled", + "type": "number", + "value": 45.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S143.scaled", + "type": "number", + "value": 30.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S190.scaled", + "type": "number", + "value": 36.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S230.scaled", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S241.scaled", + "type": "number", + "value": 70.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S268.scaled", + "type": "number", + "value": 40.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S285.scaled", + "type": "number", + "value": 43.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S338.scaled", + "type": "number", + "value": 80.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S398.scaled", + "type": "number", + "value": 51.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S338" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S070", + "S249" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-e0007/tests/test.sh b/tasks/class-test/class-test-moderation-d2-e0007/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-e0007/tests/verify.py b/tasks/class-test/class-test-moderation-d2-e0007/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0007/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-e0019/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-e0019/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..c1c875e1c7007435ea0dcd3a607cb65fa2232495 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S018,7A,19 +S190,7A,73 +S388,7B,30 +S264,7B, +S108,7A,23 +S116,7A,24 +S386,7A, +S332,7B,35 +S277,7A,16 +S216,7B,29 +S362,7B,36 +S321,7A,17 +S222,7B,31 +S317,7B,33 +S175,7A,34 +S294,7A,16 +S352,7B,25 +S094,7B,39 diff --git a/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..bcf705b8bbc71f72f75e12ed9e0b3c4cea7fba83 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 80. + +## Rescaling to a target mean and standard deviation + +The target mean is 52 and the target standard deviation is 15. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 52 + (x - M) / SD * 15 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 80 is set to 80, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-e0019/instruction.md b/tasks/class-test/class-test-moderation-d2-e0019/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-e0019/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-e0019/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-e0019/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-e0019/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-e0019/task.toml b/tasks/class-test/class-test-moderation-d2-e0019/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..a3167eced267238646aad643059b746ff1b886dd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10019 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-e0019/tests/checks.json b/tasks/class-test/class-test-moderation-d2-e0019/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..01f0526552305f3f5b41acc1f0eea3c5db4b9bf6 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7A.raw_mean", + "type": "number", + "value": 27.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7A.raw_sd", + "type": "number", + "value": 18.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_mean", + "type": "number", + "value": 32.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_sd", + "type": "number", + "value": 4.15, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S018.scaled", + "type": "number", + "value": 44.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S094.scaled", + "type": "number", + "value": 76.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S108.scaled", + "type": "number", + "value": 48.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S116.scaled", + "type": "number", + "value": 48.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S175.scaled", + "type": "number", + "value": 57.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S190.scaled", + "type": "number", + "value": 80.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S216.scaled", + "type": "number", + "value": 40.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S222.scaled", + "type": "number", + "value": 47.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S277.scaled", + "type": "number", + "value": 42.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S294.scaled", + "type": "number", + "value": 42.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S317.scaled", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S321.scaled", + "type": "number", + "value": 43.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S332.scaled", + "type": "number", + "value": 61.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S352.scaled", + "type": "number", + "value": 25.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S362.scaled", + "type": "number", + "value": 65.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S388.scaled", + "type": "number", + "value": 43.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S190" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S264", + "S386" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-e0019/tests/test.sh b/tasks/class-test/class-test-moderation-d2-e0019/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-e0019/tests/verify.py b/tasks/class-test/class-test-moderation-d2-e0019/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-e0019/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0007/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0007/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..7fa62316c4c3d24be5ca63228c1b256b46555762 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S224,7D, +S062,7D,12 +S090,7D, +S255,7D,18 +S273,7D,18 +S113,7C,6 +S053,7C,20 +S178,7C,15 +S376,7D,39 +S256,7D,20 +S330,7C,26 +S271,7C,16 +S264,7D,16 +S024,7C,20 +S339,7D,14 +S026,7C,18 +S018,7D,22 +S048,7C,21 diff --git a/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..565dd9b7ff30c0e3c34db9b188eb77e39dea656e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 50. + +## Rescaling to a target mean and standard deviation + +The target mean is 31 and the target standard deviation is 8. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 31 + (x - M) / SD * 8 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 50 is set to 50, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0007/instruction.md b/tasks/class-test/class-test-moderation-d2-t0007/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0007/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0007/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0007/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0007/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0007/task.toml b/tasks/class-test/class-test-moderation-d2-t0007/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d6ba85f2fe6d88166d008873c22fe1544b42f7b5 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 7 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0007/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0007/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..15cc6d60fcd877006f8952774628ef1b458d64da --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7C.raw_mean", + "type": "number", + "value": 17.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_sd", + "type": "number", + "value": 5.45, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_mean", + "type": "number", + "value": 19.88, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_sd", + "type": "number", + "value": 7.82, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S018.scaled", + "type": "number", + "value": 33.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S024.scaled", + "type": "number", + "value": 34.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S026.scaled", + "type": "number", + "value": 31.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S048.scaled", + "type": "number", + "value": 35.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S053.scaled", + "type": "number", + "value": 34.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S062.scaled", + "type": "number", + "value": 22.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S113.scaled", + "type": "number", + "value": 13.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S178.scaled", + "type": "number", + "value": 27.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S255.scaled", + "type": "number", + "value": 29.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S256.scaled", + "type": "number", + "value": 31.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S264.scaled", + "type": "number", + "value": 27.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S271.scaled", + "type": "number", + "value": 28.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S273.scaled", + "type": "number", + "value": 29.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S330.scaled", + "type": "number", + "value": 43.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S339.scaled", + "type": "number", + "value": 25.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S376.scaled", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S376" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S090", + "S224" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0007/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0007/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0007/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0007/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0007/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0019/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0019/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..17d2133311c49ab4d83b8c656bd78b8d010091b4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S177,7B,42 +S109,7D, +S390,7B, +S192,7D,51 +S312,7D,35 +S216,7B,35 +S214,7B,46 +S092,7B,38 +S028,7D,41 +S175,7D,40 +S038,7B,58 +S188,7D,81 +S393,7D,48 +S098,7B,92 +S052,7B,33 +S197,7D,46 +S384,7B,52 +S234,7D,42 diff --git a/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..e9c78e7b34be712ada23434ecad486d0a7065c6e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 17. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 17 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0019/instruction.md b/tasks/class-test/class-test-moderation-d2-t0019/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0019/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0019/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0019/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0019/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0019/task.toml b/tasks/class-test/class-test-moderation-d2-t0019/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..2ca7f0bec5049b999cecf51c0641357004d1f12d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 19 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0019/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0019/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..dfa1c0fc8c5f7996c73114638c31aa6fdf219833 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/tests/checks.json @@ -0,0 +1,162 @@ +[ + { + "path": "groups.7B.raw_mean", + "type": "number", + "value": 49.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_sd", + "type": "number", + "value": 17.92, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_mean", + "type": "number", + "value": 48.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_sd", + "type": "number", + "value": 13.32, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S028.scaled", + "type": "number", + "value": 56.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S038.scaled", + "type": "number", + "value": 73.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S052.scaled", + "type": "number", + "value": 49.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S092.scaled", + "type": "number", + "value": 54.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S098.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S175.scaled", + "type": "number", + "value": 54.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S177.scaled", + "type": "number", + "value": 57.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S188.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.scaled", + "type": "number", + "value": 68.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S197.scaled", + "type": "number", + "value": 62.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S214.scaled", + "type": "number", + "value": 61.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S216.scaled", + "type": "number", + "value": 51.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S234.scaled", + "type": "number", + "value": 57.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S312.scaled", + "type": "number", + "value": 48.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S384.scaled", + "type": "number", + "value": 67.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S393.scaled", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S098", + "S188" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S109", + "S390" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0019/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0019/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0019/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0019/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0019/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0031/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0031/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..06597b255254423e78063ad88408cf10eb22b6b3 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S182,7C,20 +S343,7C, +S096,7B,24 +S333,7B,19 +S165,7C,13 +S376,7C,21 +S313,7C,24 +S036,7C,16 +S002,7B,24 +S389,7C,41 +S142,7B,17 +S358,7C,16 +S245,7C, +S162,7B,23 +S377,7C,17 +S045,7B,30 +S110,7B,32 +S264,7B,21 diff --git a/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..b903035b93ce38a25b0f63a688162fe278da6d6e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0031/instruction.md b/tasks/class-test/class-test-moderation-d2-t0031/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0031/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0031/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0031/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0031/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0031/task.toml b/tasks/class-test/class-test-moderation-d2-t0031/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..49b3dcbda961faf02620d957010444dc069039af --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 31 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0031/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0031/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..d124c5bca6ef8d5fec46739e035730f95f40208d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7B.raw_mean", + "type": "number", + "value": 23.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_sd", + "type": "number", + "value": 4.79, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_mean", + "type": "number", + "value": 21.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_sd", + "type": "number", + "value": 8.22, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S002.scaled", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S036.scaled", + "type": "number", + "value": 30.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S045.scaled", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S096.scaled", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S110.scaled", + "type": "number", + "value": 54.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S142.scaled", + "type": "number", + "value": 22.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S162.scaled", + "type": "number", + "value": 35.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S165.scaled", + "type": "number", + "value": 27.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S182.scaled", + "type": "number", + "value": 35.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S264.scaled", + "type": "number", + "value": 31.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S313.scaled", + "type": "number", + "value": 40.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S333.scaled", + "type": "number", + "value": 27.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S358.scaled", + "type": "number", + "value": 30.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S376.scaled", + "type": "number", + "value": 37.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S377.scaled", + "type": "number", + "value": 32.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S389.scaled", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S389" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S245", + "S343" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0031/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0031/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0031/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0031/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0031/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0043/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0043/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..623200a5d9997a2de52613febf04b02850ffccfa --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S071,7B,32 +S247,7C, +S019,7B,32 +S304,7C,52 +S323,7B,26 +S367,7B,41 +S296,7C,33 +S014,7C,40 +S310,7B,89 +S094,7C,31 +S173,7C,35 +S096,7C,46 +S273,7B,34 +S041,7C,39 +S164,7C,49 +S257,7B,47 +S057,7C, +S312,7B,33 diff --git a/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..e9c78e7b34be712ada23434ecad486d0a7065c6e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 17. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 17 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0043/instruction.md b/tasks/class-test/class-test-moderation-d2-t0043/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0043/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0043/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0043/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0043/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0043/task.toml b/tasks/class-test/class-test-moderation-d2-t0043/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..6c0699628263d0f143905c035446bb8463825fce --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 43 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0043/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0043/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..fc58f990118b95b2061f03e9fab2435e092984c9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7B.raw_mean", + "type": "number", + "value": 41.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_sd", + "type": "number", + "value": 18.83, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_mean", + "type": "number", + "value": 40.62, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_sd", + "type": "number", + "value": 7.19, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S014.scaled", + "type": "number", + "value": 63.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S019.scaled", + "type": "number", + "value": 56.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S041.scaled", + "type": "number", + "value": 61.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S071.scaled", + "type": "number", + "value": 56.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S094.scaled", + "type": "number", + "value": 42.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S096.scaled", + "type": "number", + "value": 77.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S164.scaled", + "type": "number", + "value": 84.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S173.scaled", + "type": "number", + "value": 51.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S257.scaled", + "type": "number", + "value": 69.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S273.scaled", + "type": "number", + "value": 58.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S296.scaled", + "type": "number", + "value": 47.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S304.scaled", + "type": "number", + "value": 91.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S310.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S312.scaled", + "type": "number", + "value": 57.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S323.scaled", + "type": "number", + "value": 50.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S367.scaled", + "type": "number", + "value": 64.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S310" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S057", + "S247" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0043/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0043/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0043/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0043/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0043/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0055/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0055/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..04ed4c69a50cd25a268f03f7b8dd42b4e67c08f9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S139,7D, +S060,7D,79 +S286,7A,28 +S217,7D,32 +S072,7D,40 +S375,7A,43 +S271,7A,52 +S379,7A,18 +S150,7D,41 +S299,7D,41 +S122,7A, +S297,7D,39 +S248,7A,26 +S306,7A,84 +S083,7D,44 +S149,7A,25 +S298,7A,31 +S152,7D,51 diff --git a/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..ea45661e2cffc1a53db31f00e0abf5ed9fc06652 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 62 and the target standard deviation is 19. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 62 + (x - M) / SD * 19 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0055/instruction.md b/tasks/class-test/class-test-moderation-d2-t0055/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0055/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0055/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0055/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0055/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0055/task.toml b/tasks/class-test/class-test-moderation-d2-t0055/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..8adec3e70ee08c193a721f3b857275b2169902fd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 55 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0055/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0055/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..6ad78ce466519a670b43f73a38348deb66b56dc9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/tests/checks.json @@ -0,0 +1,162 @@ +[ + { + "path": "groups.7A.raw_mean", + "type": "number", + "value": 38.38, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7A.raw_sd", + "type": "number", + "value": 19.99, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_mean", + "type": "number", + "value": 45.88, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7D.raw_sd", + "type": "number", + "value": 13.46, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S060.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S072.scaled", + "type": "number", + "value": 53.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S083.scaled", + "type": "number", + "value": 59.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S149.scaled", + "type": "number", + "value": 49.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S150.scaled", + "type": "number", + "value": 55.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S152.scaled", + "type": "number", + "value": 69.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S217.scaled", + "type": "number", + "value": 42.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S248.scaled", + "type": "number", + "value": 50.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S271.scaled", + "type": "number", + "value": 74.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S286.scaled", + "type": "number", + "value": 52.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S297.scaled", + "type": "number", + "value": 52.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S298.scaled", + "type": "number", + "value": 55.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S299.scaled", + "type": "number", + "value": 55.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S306.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S375.scaled", + "type": "number", + "value": 66.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S379.scaled", + "type": "number", + "value": 42.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S060", + "S306" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S122", + "S139" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0055/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0055/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0055/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0055/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0055/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0067/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0067/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..cfd1e6d6f3a66ed2a1b979ec3fc2c462d2352968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S161,7A, +S066,7B,18 +S190,7A,9 +S395,7B,43 +S171,7A,18 +S236,7B,23 +S280,7A,20 +S381,7B,24 +S182,7B,27 +S255,7B,22 +S362,7A,22 +S276,7B,27 +S227,7B, +S320,7A,17 +S384,7A,13 +S299,7A,23 +S168,7B,32 +S301,7A,10 diff --git a/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..f3b275628671f66ad8c1f733b753932bd1c62d6c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 50. + +## Rescaling to a target mean and standard deviation + +The target mean is 31 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 31 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 50 is set to 50, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0067/instruction.md b/tasks/class-test/class-test-moderation-d2-t0067/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0067/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0067/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0067/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0067/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0067/task.toml b/tasks/class-test/class-test-moderation-d2-t0067/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..af65f7a25330c4ef55770c0965e47045b8995d0c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 67 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0067/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0067/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..f06c03fea4133cb025d2eee06ae687fba98e9c04 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7A.raw_mean", + "type": "number", + "value": 16.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7A.raw_sd", + "type": "number", + "value": 4.97, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_mean", + "type": "number", + "value": 27.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7B.raw_sd", + "type": "number", + "value": 7.18, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S066.scaled", + "type": "number", + "value": 18.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S168.scaled", + "type": "number", + "value": 38.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S171.scaled", + "type": "number", + "value": 34.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S182.scaled", + "type": "number", + "value": 31.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S190.scaled", + "type": "number", + "value": 15.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S236.scaled", + "type": "number", + "value": 25.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S255.scaled", + "type": "number", + "value": 24.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S276.scaled", + "type": "number", + "value": 31.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S280.scaled", + "type": "number", + "value": 38.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S299.scaled", + "type": "number", + "value": 44.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S301.scaled", + "type": "number", + "value": 17.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S320.scaled", + "type": "number", + "value": 32.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S362.scaled", + "type": "number", + "value": 42.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S381.scaled", + "type": "number", + "value": 26.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S384.scaled", + "type": "number", + "value": 24.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S395.scaled", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S395" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S161", + "S227" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0067/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0067/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0067/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0067/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0067/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d2-t0079/environment/Dockerfile b/tasks/class-test/class-test-moderation-d2-t0079/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/marks.csv b/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/marks.csv new file mode 100644 index 0000000000000000000000000000000000000000..619f7ae0bf7c10f43ba80ee88e63e15d894cb364 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/marks.csv @@ -0,0 +1,19 @@ +student_id,group,mark +S054,7A, +S124,7C,46 +S285,7C,28 +S066,7C,34 +S297,7A,62 +S210,7C, +S393,7A,36 +S325,7A,40 +S016,7C,79 +S313,7A,26 +S360,7C,35 +S058,7C,22 +S029,7A,46 +S386,7C,41 +S161,7A,51 +S279,7A,44 +S260,7A,50 +S135,7C,44 diff --git a/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..9cdffeed6fdbf48ef01c72c07ffbc7f8db114b77 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/environment/workdir/moderation_notes.md @@ -0,0 +1,15 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 19. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 19 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +Each teaching group is rescaled separately, with its own `M` and `SD`. A student with no mark was absent and is left out of everything. + +A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d2-t0079/instruction.md b/tasks/class-test/class-test-moderation-d2-t0079/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..b8e27f0f66ca38df2b1535ec2885d206e7c777c8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/instruction.md @@ -0,0 +1,12 @@ +# Class test: standardising marks + +`marks.csv` holds the marks of two teaching groups on the same test. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `groups`: an object keyed by teaching group, each with `raw_mean` and `raw_sd` (population standard deviation) of that group's marks, 2 decimals. +- `students`: an object keyed by student id, for every student with a mark, each with `scaled`: the rescaled mark after any limiting, 1 decimal. +- `clamped`: list of the student ids whose scaled mark had to be limited to the top or bottom of the range (any order; an empty list if none). +- `absent`: list of the student ids with no mark (any order). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d2-t0079/solution/oracle.py b/tasks/class-test/class-test-moderation-d2-t0079/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d2-t0079/solution/solve.sh b/tasks/class-test/class-test-moderation-d2-t0079/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d2-t0079/task.toml b/tasks/class-test/class-test-moderation-d2-t0079/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..e8b809eafdff9515e42fd31935468308a42e8585 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "medium" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 79 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d2-t0079/tests/checks.json b/tasks/class-test/class-test-moderation-d2-t0079/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..48a540e738378e054f1b23f7e7176465488dd5be --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/tests/checks.json @@ -0,0 +1,161 @@ +[ + { + "path": "groups.7A.raw_mean", + "type": "number", + "value": 44.38, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7A.raw_sd", + "type": "number", + "value": 10.1, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_mean", + "type": "number", + "value": 41.12, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "groups.7C.raw_sd", + "type": "number", + "value": 16.17, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S016.scaled", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S029.scaled", + "type": "number", + "value": 68.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S058.scaled", + "type": "number", + "value": 42.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S066.scaled", + "type": "number", + "value": 56.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S124.scaled", + "type": "number", + "value": 70.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S135.scaled", + "type": "number", + "value": 68.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S161.scaled", + "type": "number", + "value": 77.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S260.scaled", + "type": "number", + "value": 75.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S279.scaled", + "type": "number", + "value": 64.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S285.scaled", + "type": "number", + "value": 49.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S297.scaled", + "type": "number", + "value": 98.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S313.scaled", + "type": "number", + "value": 30.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S325.scaled", + "type": "number", + "value": 56.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S360.scaled", + "type": "number", + "value": 57.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S386.scaled", + "type": "number", + "value": 64.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S393.scaled", + "type": "number", + "value": 49.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S016" + ], + "item": "str", + "weight": 2 + }, + { + "path": "absent", + "type": "set", + "value": [ + "S054", + "S210" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d2-t0079/tests/test.sh b/tasks/class-test/class-test-moderation-d2-t0079/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d2-t0079/tests/verify.py b/tasks/class-test/class-test-moderation-d2-t0079/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d2-t0079/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-e0011/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-e0011/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..36f5755c4931710851cbd978566bc377f95c5338 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 50. + +## Rescaling to a target mean and standard deviation + +The target mean is 31 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 31 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 1.5 marks in either direction (above 1.5 or below -1.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 50 is set to 50, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..7ef35f6fb7ed00de30c28d681a0d9af084361a4e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/moderation_sample.csv @@ -0,0 +1,11 @@ +student_id,moderator_mark +S023,30 +S164,11 +S215,29 +S288,7 +S100,18 +S195,22 +S032,29 +S005,20 +S220,12 +S341,26 diff --git a/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..46e8529ea393681a31e0a6b3328b1d3d02985920 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S251,M3,17 +S220,M3,15 +S399,M3,18 +S317,M1,16 +S288,M3,10 +S023,M2,26 +S335,M2,27 +S164,M3,14 +S095,M2,23 +S215,M2,24 +S065,M2,25 +S341,M1,26 +S032,M2,25 +S195,M1,23 +S279,M1,25 +S355,M2,29 +S112,M1,14 +S246,M1,48 +S212,M3,18 +S005,M1,20 +S100,M3,20 diff --git a/tasks/class-test/class-test-moderation-d3-e0011/instruction.md b/tasks/class-test/class-test-moderation-d3-e0011/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-e0011/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-e0011/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-e0011/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-e0011/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-e0011/task.toml b/tasks/class-test/class-test-moderation-d3-e0011/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..d8b096fb236aa27a62eb79b303bba12da2f3e1e8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10011 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-e0011/tests/checks.json b/tasks/class-test/class-test-moderation-d3-e0011/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..09b2d0ad74163449c58a6f48c52ba312c6a65f7f --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/tests/checks.json @@ -0,0 +1,203 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": -0.33, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 4.33, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": -2.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M2", + "M3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 22.58, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 9.42, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S005.final", + "type": "number", + "value": 28.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S023.final", + "type": "number", + "value": 39.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S032.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S065.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S095.final", + "type": "number", + "value": 36.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S100.final", + "type": "number", + "value": 25.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S112.final", + "type": "number", + "value": 21.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S164.final", + "type": "number", + "value": 19.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S195.final", + "type": "number", + "value": 31.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S212.final", + "type": "number", + "value": 23.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S215.final", + "type": "number", + "value": 37.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S220.final", + "type": "number", + "value": 20.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S246.final", + "type": "number", + "value": 50.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S251.final", + "type": "number", + "value": 22.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S279.final", + "type": "number", + "value": 33.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S288.final", + "type": "number", + "value": 14.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S317.final", + "type": "number", + "value": 24.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S335.final", + "type": "number", + "value": 40.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S341.final", + "type": "number", + "value": 34.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S355.final", + "type": "number", + "value": 42.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S399.final", + "type": "number", + "value": 23.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S246" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-e0011/tests/test.sh b/tasks/class-test/class-test-moderation-d3-e0011/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-e0011/tests/verify.py b/tasks/class-test/class-test-moderation-d3-e0011/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0011/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-e0023/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-e0023/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..ff95a36e5a25b2d45eb73adf09edc9af245802bb --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 1.5 marks in either direction (above 1.5 or below -1.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..08c3033d76d0541d1d84eeecd1c486720ff79def --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/moderation_sample.csv @@ -0,0 +1,12 @@ +student_id,moderator_mark +S184,23 +S178,25 +S149,27 +S255,20 +S300,27 +S348,25 +S389,20 +S085,29 +S293,26 +S384,31 +S335,15 diff --git a/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..82368b89da4941a36b661441a1ede9b9382faab1 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S085,M2,25 +S145,M3,27 +S348,M3,25 +S389,M3,20 +S063,M2,26 +S255,M3,19 +S159,M2,25 +S300,M2,26 +S306,M1,33 +S178,M1,24 +S384,M2,30 +S355,M1,58 +S280,M3,22 +S037,M2,25 +S335,M3,16 +S149,M1,29 +S293,M1,26 +S368,M1,19 +S291,M3,16 +S184,M1,22 +S182,M2,21 diff --git a/tasks/class-test/class-test-moderation-d3-e0023/instruction.md b/tasks/class-test/class-test-moderation-d3-e0023/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-e0023/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-e0023/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-e0023/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-e0023/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-e0023/task.toml b/tasks/class-test/class-test-moderation-d3-e0023/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..61e4cab2518507b190f9c108bf12e5f04e79e0c2 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "eval"] +isced_f = "0111" +family = "moderation" +split = "eval" +seed = 10023 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-e0023/tests/checks.json b/tasks/class-test/class-test-moderation-d3-e0023/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..59240766210c9e5e9d2cdea50fdc8f5b942133e4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/tests/checks.json @@ -0,0 +1,202 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 2.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 26.1, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 8.48, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S037.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S063.final", + "type": "number", + "value": 39.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S085.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S145.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S149.final", + "type": "number", + "value": 40.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S159.final", + "type": "number", + "value": 38.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S178.final", + "type": "number", + "value": 34.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S182.final", + "type": "number", + "value": 33.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S184.final", + "type": "number", + "value": 31.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S255.final", + "type": "number", + "value": 27.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S280.final", + "type": "number", + "value": 31.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S291.final", + "type": "number", + "value": 23.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S293.final", + "type": "number", + "value": 36.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S300.final", + "type": "number", + "value": 39.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S306.final", + "type": "number", + "value": 46.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S335.final", + "type": "number", + "value": 23.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S348.final", + "type": "number", + "value": 35.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S355.final", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S368.final", + "type": "number", + "value": 27.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S384.final", + "type": "number", + "value": 44.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S389.final", + "type": "number", + "value": 29.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S355" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-e0023/tests/test.sh b/tasks/class-test/class-test-moderation-d3-e0023/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-e0023/tests/verify.py b/tasks/class-test/class-test-moderation-d3-e0023/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-e0023/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0011/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0011/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..055dd526ee5d9ec989d5be55fd7c079c8b142953 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 80. + +## Rescaling to a target mean and standard deviation + +The target mean is 50 and the target standard deviation is 15. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 50 + (x - M) / SD * 15 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 2.5 marks in either direction (above 2.5 or below -2.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 80 is set to 80, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..c6c571b9c08cd0705a659af8803e55af68a5c6a1 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/moderation_sample.csv @@ -0,0 +1,11 @@ +student_id,moderator_mark +S256,45 +S279,36 +S191,26 +S009,17 +S241,32 +S123,13 +S047,28 +S167,24 +S289,39 +S034,33 diff --git a/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..35745b065aad934f34ca515470d6883eb10adce3 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S256,M2,40 +S279,M2,31 +S330,M1,42 +S047,M3,30 +S034,M2,29 +S380,M1,35 +S191,M1,35 +S196,M2,35 +S289,M3,39 +S009,M3,18 +S189,M3,46 +S335,M1,74 +S123,M1,20 +S048,M2,26 +S056,M1,40 +S218,M3,47 +S241,M3,33 +S167,M1,33 +S057,M2,33 +S085,M2,23 +S329,M3,35 diff --git a/tasks/class-test/class-test-moderation-d3-t0011/instruction.md b/tasks/class-test/class-test-moderation-d3-t0011/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0011/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0011/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0011/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0011/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0011/task.toml b/tasks/class-test/class-test-moderation-d3-t0011/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..01e68af6fce6cb53aca75026d18104877f4febbc --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 11 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0011/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0011/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..9b0ee21058cb670ddfbcacf14b152e27e8a953b4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/tests/checks.json @@ -0,0 +1,203 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": -8.33, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 4.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": -1.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M1", + "M2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 34.21, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 10.97, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S009.final", + "type": "number", + "value": 27.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S034.final", + "type": "number", + "value": 49.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S047.final", + "type": "number", + "value": 44.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S048.final", + "type": "number", + "value": 45.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S056.final", + "type": "number", + "value": 46.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S057.final", + "type": "number", + "value": 54.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S085.final", + "type": "number", + "value": 41.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S123.final", + "type": "number", + "value": 19.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S167.final", + "type": "number", + "value": 37.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S189.final", + "type": "number", + "value": 66.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S191.final", + "type": "number", + "value": 39.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S196.final", + "type": "number", + "value": 57.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S218.final", + "type": "number", + "value": 67.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S241.final", + "type": "number", + "value": 48.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S256.final", + "type": "number", + "value": 64.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S279.final", + "type": "number", + "value": 52.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S289.final", + "type": "number", + "value": 56.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S329.final", + "type": "number", + "value": 51.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S330.final", + "type": "number", + "value": 49.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S335.final", + "type": "number", + "value": 80.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S380.final", + "type": "number", + "value": 39.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S335" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0011/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0011/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0011/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0011/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0011/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0023/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0023/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..24aad71a443b3e89015b074b5a85d44032d2e77c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 39 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 39 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 2.0 marks in either direction (above 2.0 or below -2.0), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..a8b09bccb675b6c0e47765d83f7bb7aa15dd9e76 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/moderation_sample.csv @@ -0,0 +1,12 @@ +student_id,moderator_mark +S315,28 +S073,24 +S204,60 +S137,14 +S192,26 +S191,25 +S298,32 +S009,17 +S098,24 +S124,17 +S263,26 diff --git a/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..a54b3ee657e94dd81d1b110a6f782f641a17e734 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S211,M2,23 +S073,M3,20 +S204,M1,60 +S124,M2,22 +S324,M2,16 +S007,M1,24 +S398,M1,38 +S344,M1,32 +S315,M1,28 +S263,M1,26 +S009,M2,22 +S191,M3,20 +S137,M2,21 +S393,M3,19 +S203,M2,15 +S192,M3,20 +S131,M3,21 +S047,M2,21 +S298,M1,32 +S098,M3,20 +S376,M3,14 diff --git a/tasks/class-test/class-test-moderation-d3-t0023/instruction.md b/tasks/class-test/class-test-moderation-d3-t0023/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0023/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0023/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0023/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0023/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0023/task.toml b/tasks/class-test/class-test-moderation-d3-t0023/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..fef4db09b9951e4ab393a5abbe6384824445fb7c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 23 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0023/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0023/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..ea224b7c0680191831989d176cb1474fb01433d0 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/tests/checks.json @@ -0,0 +1,203 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": -5.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": 4.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M2", + "M3" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 24.17, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 10.66, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S007.final", + "type": "number", + "value": 38.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S009.final", + "type": "number", + "value": 30.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S047.final", + "type": "number", + "value": 29.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S073.final", + "type": "number", + "value": 39.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S098.final", + "type": "number", + "value": 39.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S124.final", + "type": "number", + "value": 30.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S131.final", + "type": "number", + "value": 40.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S137.final", + "type": "number", + "value": 29.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S191.final", + "type": "number", + "value": 39.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S192.final", + "type": "number", + "value": 39.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S203.final", + "type": "number", + "value": 23.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S204.final", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S211.final", + "type": "number", + "value": 31.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S263.final", + "type": "number", + "value": 40.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S298.final", + "type": "number", + "value": 47.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S315.final", + "type": "number", + "value": 43.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S324.final", + "type": "number", + "value": 24.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S344.final", + "type": "number", + "value": 47.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S376.final", + "type": "number", + "value": 33.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S393.final", + "type": "number", + "value": 38.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S398.final", + "type": "number", + "value": 53.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S204" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0023/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0023/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0023/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0023/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0023/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0035/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0035/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..44ab93e44933d34e83252622b38be75da3bed936 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 17. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 17 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 1.5 marks in either direction (above 1.5 or below -1.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..0b13000888c94a80570f89024403087eb5dfcf6d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/moderation_sample.csv @@ -0,0 +1,12 @@ +student_id,moderator_mark +S157,43 +S283,26 +S019,35 +S307,41 +S130,52 +S164,42 +S308,31 +S382,82 +S152,61 +S337,37 +S089,40 diff --git a/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..e4803c4fe283e9af1da8d72d0a4cf9cf08573eff --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S305,M3,29 +S107,M2,62 +S019,M3,35 +S197,M1,40 +S152,M2,54 +S337,M3,38 +S089,M3,39 +S168,M1,58 +S164,M1,42 +S211,M2,55 +S157,M2,39 +S353,M1,45 +S108,M3,23 +S308,M3,29 +S063,M2,54 +S307,M1,40 +S382,M1,83 +S283,M1,26 +S132,M2,39 +S056,M3,28 +S130,M2,45 diff --git a/tasks/class-test/class-test-moderation-d3-t0035/instruction.md b/tasks/class-test/class-test-moderation-d3-t0035/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0035/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0035/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0035/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0035/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0035/task.toml b/tasks/class-test/class-test-moderation-d3-t0035/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..aea4d3939eb508b145ff8200f521d42c30a3cd2c --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 35 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0035/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0035/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..a9d481a19aed2ee6c615f3072ddff6b0da7967cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/tests/checks.json @@ -0,0 +1,202 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 6.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": 0.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 45.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 15.08, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S019.final", + "type": "number", + "value": 53.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S056.final", + "type": "number", + "value": 45.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S063.final", + "type": "number", + "value": 81.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S089.final", + "type": "number", + "value": 58.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S107.final", + "type": "number", + "value": 90.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S108.final", + "type": "number", + "value": 40.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S130.final", + "type": "number", + "value": 71.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S132.final", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S152.final", + "type": "number", + "value": 81.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S157.final", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S164.final", + "type": "number", + "value": 61.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S168.final", + "type": "number", + "value": 79.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S197.final", + "type": "number", + "value": 59.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S211.final", + "type": "number", + "value": 83.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S283.final", + "type": "number", + "value": 43.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S305.final", + "type": "number", + "value": 47.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S307.final", + "type": "number", + "value": 59.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S308.final", + "type": "number", + "value": 47.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S337.final", + "type": "number", + "value": 57.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S353.final", + "type": "number", + "value": 65.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S382.final", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S382" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0035/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0035/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0035/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0035/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0035/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0047/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0047/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..ec3aa1003e08fa5e173f56bea38975a11565aea1 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 2.0 marks in either direction (above 2.0 or below -2.0), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..2450c5397c01abd77d95bf3b5e4da4bda9c40105 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/moderation_sample.csv @@ -0,0 +1,11 @@ +student_id,moderator_mark +S111,27 +S376,24 +S048,20 +S093,14 +S089,21 +S154,18 +S272,25 +S331,16 +S389,21 +S245,22 diff --git a/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..354084472cf2660564bd20d3801666b8d6b542e4 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S048,M3,21 +S272,M1,31 +S376,M1,32 +S164,M1,51 +S377,M3,16 +S148,M3,17 +S389,M2,17 +S076,M2,22 +S245,M2,19 +S137,M3,17 +S025,M1,25 +S102,M3,20 +S381,M2,27 +S154,M3,19 +S111,M2,23 +S367,M1,19 +S158,M2,21 +S331,M1,21 +S187,M2,31 +S089,M1,26 +S093,M3,14 diff --git a/tasks/class-test/class-test-moderation-d3-t0047/instruction.md b/tasks/class-test/class-test-moderation-d3-t0047/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0047/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0047/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0047/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0047/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0047/task.toml b/tasks/class-test/class-test-moderation-d3-t0047/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..30aa63b817b0afaeae00945821dd7a6ed203aeda --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 47 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0047/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0047/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..99c0b61133ab59941de5876eba2586dcdd3ca6be --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/tests/checks.json @@ -0,0 +1,203 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": -6.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 3.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": -0.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M1", + "M2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 22.51, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 7.35, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S025.final", + "type": "number", + "value": 31.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S048.final", + "type": "number", + "value": 34.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S076.final", + "type": "number", + "value": 41.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S089.final", + "type": "number", + "value": 33.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S093.final", + "type": "number", + "value": 24.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S102.final", + "type": "number", + "value": 33.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S111.final", + "type": "number", + "value": 43.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S137.final", + "type": "number", + "value": 28.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S148.final", + "type": "number", + "value": 28.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S154.final", + "type": "number", + "value": 31.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S158.final", + "type": "number", + "value": 40.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S164.final", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S187.final", + "type": "number", + "value": 55.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S245.final", + "type": "number", + "value": 37.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S272.final", + "type": "number", + "value": 40.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S331.final", + "type": "number", + "value": 25.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S367.final", + "type": "number", + "value": 22.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S376.final", + "type": "number", + "value": 42.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S377.final", + "type": "number", + "value": 27.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S381.final", + "type": "number", + "value": 49.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S389.final", + "type": "number", + "value": 34.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S164" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0047/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0047/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0047/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0047/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0047/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0059/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0059/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..5e7b49b5d287b8364f8f7820c41cdab0c59c6450 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 11. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 11 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 2.5 marks in either direction (above 2.5 or below -2.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..875643546f8092df26476a201846e5f8a6d28172 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/moderation_sample.csv @@ -0,0 +1,13 @@ +student_id,moderator_mark +S318,20 +S327,32 +S210,57 +S116,27 +S075,32 +S164,28 +S169,31 +S285,31 +S331,26 +S362,36 +S302,31 +S346,29 diff --git a/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..48b362c39e6c784e94781d9601d862f1aec7843d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S019,M2,35 +S075,M2,32 +S169,M2,31 +S089,M3,30 +S362,M1,38 +S102,M3,29 +S112,M3,30 +S346,M1,32 +S291,M1,30 +S331,M2,24 +S032,M1,25 +S285,M3,30 +S027,M2,29 +S164,M3,29 +S318,M1,24 +S388,M2,33 +S302,M3,31 +S210,M1,60 +S158,M1,13 +S116,M3,27 +S327,M2,31 diff --git a/tasks/class-test/class-test-moderation-d3-t0059/instruction.md b/tasks/class-test/class-test-moderation-d3-t0059/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0059/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0059/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0059/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0059/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0059/task.toml b/tasks/class-test/class-test-moderation-d3-t0059/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..ea67a7e310995cd7b45da6a348b6f4fbacdb3fce --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 59 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0059/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0059/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..53b192dcf09dfd7118548b861a158d5382706cbd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/tests/checks.json @@ -0,0 +1,202 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": -3.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 0.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": 0.0, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 29.62, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 8.15, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S019.final", + "type": "number", + "value": 44.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S027.final", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S032.final", + "type": "number", + "value": 26.7, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S075.final", + "type": "number", + "value": 40.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S089.final", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S102.final", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S112.final", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S116.final", + "type": "number", + "value": 33.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S158.final", + "type": "number", + "value": 10.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S164.final", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S169.final", + "type": "number", + "value": 38.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S210.final", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S285.final", + "type": "number", + "value": 37.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S291.final", + "type": "number", + "value": 33.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S302.final", + "type": "number", + "value": 38.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S318.final", + "type": "number", + "value": 25.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S327.final", + "type": "number", + "value": 38.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S331.final", + "type": "number", + "value": 29.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S346.final", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S362.final", + "type": "number", + "value": 44.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S388.final", + "type": "number", + "value": 41.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S210" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0059/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0059/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0059/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0059/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0059/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0071/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0071/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..d86a9d85416b327c668f2591dc1636e12863b6e5 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 60. + +## Rescaling to a target mean and standard deviation + +The target mean is 37 and the target standard deviation is 10. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 37 + (x - M) / SD * 10 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 1.5 marks in either direction (above 1.5 or below -1.5), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 60 is set to 60, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..ae19f405d49e70fdd13ee679a973aacd8853ae9e --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/moderation_sample.csv @@ -0,0 +1,12 @@ +student_id,moderator_mark +S088,27 +S278,31 +S160,30 +S151,33 +S073,23 +S236,28 +S125,27 +S247,35 +S351,30 +S354,21 +S137,28 diff --git a/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..0cca701e186077f6ce1ed7a3c76eabf5ea02c21d --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S157,M3,25 +S351,M3,29 +S236,M2,29 +S331,M3,24 +S088,M3,27 +S252,M1,55 +S073,M1,19 +S137,M3,27 +S080,M2,35 +S278,M2,30 +S151,M1,29 +S174,M2,31 +S193,M3,31 +S186,M2,19 +S354,M2,19 +S200,M2,30 +S341,M1,31 +S187,M1,19 +S125,M3,27 +S247,M1,31 +S160,M1,28 diff --git a/tasks/class-test/class-test-moderation-d3-t0071/instruction.md b/tasks/class-test/class-test-moderation-d3-t0071/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0071/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0071/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0071/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0071/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0071/task.toml b/tasks/class-test/class-test-moderation-d3-t0071/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..da36839a422a3fa02b6efdc22b2e1c8dd4100c06 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 71 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0071/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0071/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..4c45999e4a28f3436b9a9e9eb738263cf88ca330 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/tests/checks.json @@ -0,0 +1,202 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": 3.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": 0.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": 0.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M1" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 29.5, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 7.95, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S073.final", + "type": "number", + "value": 28.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S080.final", + "type": "number", + "value": 43.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S088.final", + "type": "number", + "value": 33.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S125.final", + "type": "number", + "value": 33.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S137.final", + "type": "number", + "value": 33.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S151.final", + "type": "number", + "value": 40.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S157.final", + "type": "number", + "value": 31.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S160.final", + "type": "number", + "value": 39.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S174.final", + "type": "number", + "value": 38.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S186.final", + "type": "number", + "value": 23.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S187.final", + "type": "number", + "value": 28.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S193.final", + "type": "number", + "value": 38.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S200.final", + "type": "number", + "value": 37.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S236.final", + "type": "number", + "value": 36.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S247.final", + "type": "number", + "value": 43.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S252.final", + "type": "number", + "value": 60.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S278.final", + "type": "number", + "value": 37.6, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S331.final", + "type": "number", + "value": 30.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S341.final", + "type": "number", + "value": 43.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S351.final", + "type": "number", + "value": 36.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S354.final", + "type": "number", + "value": 23.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S252" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0071/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0071/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0071/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0071/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0071/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main() diff --git a/tasks/class-test/class-test-moderation-d3-t0083/environment/Dockerfile b/tasks/class-test/class-test-moderation-d3-t0083/environment/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..d2579fdacf261c4800d0280c0927cbaace75c6cd --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/environment/Dockerfile @@ -0,0 +1,3 @@ +FROM python:3.12-slim +WORKDIR /app +COPY workdir/ /app/ diff --git a/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_notes.md b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_notes.md new file mode 100644 index 0000000000000000000000000000000000000000..e9529e131cb65f48252a2ca66b99a92f4b4f9887 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_notes.md @@ -0,0 +1,25 @@ +# Standardising marks + +The test is marked out of 100. + +## Rescaling to a target mean and standard deviation + +The target mean is 65 and the target standard deviation is 19. For a set of marks with mean `M` and standard deviation `SD`, a mark `x` becomes + + scaled = 65 + (x - M) / SD * 19 + +`SD` is the population standard deviation: the square root of the mean squared deviation from `M`, with the number of marks in the denominator (not one less). + +## Step 1: moderation of markers + +A moderator has re-marked a sample of each marker's scripts (`moderation_sample.csv`). For each marker: + +1. For each sampled script of that marker, take the moderator's mark minus the marker's mark. +2. The marker's mean difference is the mean of these differences. +3. If the mean difference is more than 2.0 marks in either direction (above 2.0 or below -2.0), add the unrounded mean difference to **every** mark given by that marker, sampled or not. Otherwise leave that marker's marks as they are. + +Do not limit or round the marks at this step. The moderator's own marks are used only to work out the mean difference. + +## Step 2: rescaling + +Take the marks of all students after step 1 as one set (all markers together), and rescale them to the target mean and standard deviation with the formula above. A scaled mark above 100 is set to 100, and a scaled mark below 0 is set to 0. diff --git a/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_sample.csv b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_sample.csv new file mode 100644 index 0000000000000000000000000000000000000000..4db398de9c7ce8dd0116f5ef896c749d65e01450 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/moderation_sample.csv @@ -0,0 +1,12 @@ +student_id,moderator_mark +S183,40 +S392,31 +S019,52 +S092,43 +S222,31 +S131,23 +S380,71 +S342,29 +S117,32 +S356,45 +S055,36 diff --git a/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/scripts.csv b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/scripts.csv new file mode 100644 index 0000000000000000000000000000000000000000..b4081ebdbd6a59729e030fbfd16546af0d6708c9 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/environment/workdir/scripts.csv @@ -0,0 +1,22 @@ +student_id,marker,mark +S288,M2,52 +S058,M3,34 +S222,M1,30 +S392,M3,34 +S336,M1,47 +S356,M2,56 +S055,M2,45 +S275,M3,30 +S183,M1,40 +S085,M1,30 +S131,M1,22 +S238,M3,37 +S019,M2,60 +S390,M2,41 +S092,M3,43 +S342,M3,28 +S117,M2,41 +S348,M2,49 +S075,M1,25 +S380,M1,70 +S114,M3,41 diff --git a/tasks/class-test/class-test-moderation-d3-t0083/instruction.md b/tasks/class-test/class-test-moderation-d3-t0083/instruction.md new file mode 100644 index 0000000000000000000000000000000000000000..814ac57c474b9a085c354b5bfa389ffd7a5d8da8 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/instruction.md @@ -0,0 +1,13 @@ +# Class test: standardising marks + +`scripts.csv` holds the marks three markers gave to their scripts, and `moderation_sample.csv` the marks a moderator gave to a sample of those scripts. `moderation_notes.md` gives the procedure, the targets and the formula: follow it exactly. + +Write `answer.json` in this folder, a JSON object with: + +- `markers`: an object keyed by marker, each with `mean_difference`: the mean of moderator's mark minus marker's mark over that marker's sampled scripts, 2 decimals, with its sign. +- `adjusted_markers`: list of the markers whose marks were adjusted in step 1 (any order; an empty list if none). +- `cohort_mean` and `cohort_sd`: the mean and population standard deviation of all students' marks after step 1, 2 decimals. +- `students`: an object keyed by student id, each with `final`: the mark after step 2, including any limiting, 1 decimal. +- `clamped`: list of the student ids whose final mark had to be limited to the top or bottom of the range (any order; an empty list if none). + +Numbers are accepted within one unit of the last decimal place asked for. diff --git a/tasks/class-test/class-test-moderation-d3-t0083/solution/oracle.py b/tasks/class-test/class-test-moderation-d3-t0083/solution/oracle.py new file mode 100644 index 0000000000000000000000000000000000000000..e043ed0b60f023c40b06c9442e39c79083b55968 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/solution/oracle.py @@ -0,0 +1,162 @@ +#!/usr/bin/env python3 +"""Reference solution for Class Test. Works only from the files in the current directory.""" +import csv, json, math, os, re +from decimal import Decimal, ROUND_HALF_UP +from fractions import Fraction + +def rows(name): + with open(name, newline="") as f: return list(csv.DictReader(f)) + +def text(name): + with open(name) as f: return f.read() + +def item_cols(row): + return sorted([c for c in row if re.match(r"^Q\d+$", c)], key=lambda c: int(c[1:])) + +# ---------------------------------------------------------------- marking +def marking(): + scheme = text("marking_scheme.md") + penalty = Fraction(0) + if "one quarter" in scheme: penalty = Fraction(1, 4) + elif "one half" in scheme: penalty = Fraction(1, 2) + bounds = [(g, int(lo)) for g, lo in re.findall(r"^\| ([A-D]) \| (\d+) or more \|", scheme, re.M)] + bounds.sort(key=lambda b: -b[1]) + key = dict((r["item"], (r["key"].strip(), int(r["marks"]))) for r in rows("answer_key.csv")) + live = [q for q in key if key[q][0] != "VOID"] + top = sum(key[q][1] for q in live) + ans = {"max_marks": top, "students": {}, "grade_u": [], "grade_counts": dict((g, 0) for g in "ABCDU")} + pcts = [] + for r in rows("answer_sheets.csv"): + total = Fraction(0) + for q in live: + a = r[q].strip() + if a == "": continue + if a == key[q][0]: total += key[q][1] + else: total -= penalty * key[q][1] + if total < 0: total = Fraction(0) + pct = total * 100 / top + grade = "U" + for g, lo in bounds: + if pct >= lo: + grade = g; break + ans["students"][r["student_id"]] = {"marks": float(total), "percent": round(float(pct), 1), "grade": grade} + ans["grade_counts"][grade] += 1 + if grade == "U": ans["grade_u"].append(r["student_id"]) + pcts.append(pct) + ans["mean_percent"] = round(float(sum(pcts) / len(pcts)), 1) + return ans + +# ---------------------------------------------------------------- item analysis +def item_analysis(): + if os.path.exists("scores.csv"): + data = rows("scores.csv"); items = item_cols(data[0]) + score = dict((r["student_id"], [int(r[q]) for q in items]) for r in data) + else: + key = dict((r["item"], r["key"].strip()) for r in rows("key.csv")) + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + items = item_cols(data[0]) + score = dict((r["student_id"], [1 if r[q].strip() == key[q] else 0 for q in items]) for r in data) + n, k = len(score), len(items) + total = dict((s, sum(v)) for s, v in score.items()) + order = sorted(score, key=lambda s: s) + order.sort(key=lambda s: total[s], reverse=True) # stable: equal totals stay in id order + g = int((Decimal(27) * n / Decimal(100)).quantize(Decimal(1), rounding=ROUND_HALF_UP)) + upper, lower = order[:g], order[n - g:] + mean_t = sum(total.values()) / n + var_t = sum((t - mean_t) ** 2 for t in total.values()) / n + ans = {"n_group": g, "n_students": n, "items": {}, "poor_discrimination": [], "too_easy": [], "too_hard": [], "needs_review": []} + sum_pq = 0.0 + for j, q in enumerate(items): + right = [s for s in score if score[s][j] == 1]; wrong = [s for s in score if score[s][j] == 0] + p = len(right) / n + disc = (sum(score[s][j] for s in upper) - sum(score[s][j] for s in lower)) / g + m1 = sum(total[s] for s in right) / len(right); m0 = sum(total[s] for s in wrong) / len(wrong) + rpb = (m1 - m0) / math.sqrt(var_t) * math.sqrt(p * (1 - p)) + sum_pq += p * (1 - p) + ans["items"][q] = {"facility": round(p, 3), "discrimination": round(disc, 3), "point_biserial": round(rpb, 3)} + if disc < 0.20: ans["poor_discrimination"].append(q) + if p > 0.85: ans["too_easy"].append(q) + if p < 0.30: ans["too_hard"].append(q) + if rpb < 0.20: ans["needs_review"].append(q) + ans["kr20"] = round(k / (k - 1) * (1 - sum_pq / var_t), 3) + return ans + +# ---------------------------------------------------------------- distractors +def distractors(): + keys = rows("key.csv") + data = [r for r in rows("responses.csv") if r.get("status", "present") != "absent"] + n = len(data) + ans = {"items": {}, "has_non_functioning": [], "key_beaten": [], "n_students": n} + for kr in keys: + q, key = kr["item"], kr["key"].strip() + opts = "ABCDE"[:int(kr.get("n_options") or 4)] + tally = dict((o, 0) for o in opts); omitted = 0 + for r in data: + a = r[q].strip() + if a == "": omitted += 1 + else: tally[a] += 1 + dead = [o for o in opts if o != key and Fraction(tally[o], n) < Fraction(5, 100)] + others = sorted([o for o in opts if o != key], key=lambda o: -tally[o]) + ans["items"][q] = {"counts": [tally[o] for o in opts], "non_functioning": dead, "omitted": omitted, + "key_pct": round(100.0 * tally[key] / n, 1), "top_distractor": others[0]} + if dead: ans["has_non_functioning"].append(q) + if tally[others[0]] > tally[key]: ans["key_beaten"].append(q) + return ans + +# ---------------------------------------------------------------- moderation +def stats(xs): + m = sum(xs) / len(xs) + return m, math.sqrt(sum((x - m) ** 2 for x in xs) / len(xs)) + +def moderation(): + notes = text("moderation_notes.md") + top = float(re.search(r"marked out of (\d+)", notes).group(1)) + tm = float(re.search(r"target mean is (\d+)", notes).group(1)) + ts = float(re.search(r"target standard deviation is (\d+)", notes).group(1)) + def limit(v): return min(top, max(0.0, v)) + if os.path.exists("scripts.csv"): + tol = float(re.search(r"more than ([\d.]+) marks", notes).group(1)) + scripts = rows("scripts.csv") + mark = dict((r["student_id"], float(r["mark"])) for r in scripts) + marker = dict((r["student_id"], r["marker"]) for r in scripts) + diffs = {} + for r in rows("moderation_sample.csv"): + diffs.setdefault(marker[r["student_id"]], []).append(float(r["moderator_mark"]) - mark[r["student_id"]]) + ans = {"markers": {}, "adjusted_markers": [], "students": {}, "clamped": []} + shift = {} + for m in sorted(set(marker.values())): + md = sum(diffs[m]) / len(diffs[m]) + ans["markers"][m] = {"mean_difference": round(md, 2)} + shift[m] = md if (md > tol or md < -tol) else 0.0 + if shift[m]: ans["adjusted_markers"].append(m) + adj = dict((s, mark[s] + shift[marker[s]]) for s in mark) + M, SD = stats(list(adj.values())) + ans["cohort_mean"], ans["cohort_sd"] = round(M, 2), round(SD, 2) + for s, x in adj.items(): + v = tm + (x - M) / SD * ts + ans["students"][s] = {"final": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(s) + return ans + data = rows("marks.csv") + if "group" not in data[0]: + xs = [float(r["mark"]) for r in data] + M, SD = stats(xs) + return {"raw_mean": round(M, 2), "raw_sd": round(SD, 2), + "students": dict((r["student_id"], {"scaled": round(tm + (float(r["mark"]) - M) / SD * ts, 1)}) for r in data)} + ans = {"groups": {}, "students": {}, "clamped": [], "absent": [r["student_id"] for r in data if r["mark"].strip() == ""]} + present = [r for r in data if r["mark"].strip() != ""] + for g in sorted(set(r["group"] for r in present)): + mine = [r for r in present if r["group"] == g] + M, SD = stats([float(r["mark"]) for r in mine]) + ans["groups"][g] = {"raw_mean": round(M, 2), "raw_sd": round(SD, 2)} + for r in mine: + v = tm + (float(r["mark"]) - M) / SD * ts + ans["students"][r["student_id"]] = {"scaled": round(limit(v), 1)} + if v > top or v < 0: ans["clamped"].append(r["student_id"]) + return ans + +if os.path.exists("answer_sheets.csv"): answer = marking() +elif os.path.exists("item_analysis_notes.md"): answer = item_analysis() +elif os.path.exists("distractor_notes.md"): answer = distractors() +else: answer = moderation() +with open("answer.json", "w") as f: json.dump(answer, f, indent=1) diff --git a/tasks/class-test/class-test-moderation-d3-t0083/solution/solve.sh b/tasks/class-test/class-test-moderation-d3-t0083/solution/solve.sh new file mode 100644 index 0000000000000000000000000000000000000000..e181145ecdd916c6ebba65c9c854625c6e95d9ab --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/solution/solve.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +cd /app +python3 /solution/oracle.py diff --git a/tasks/class-test/class-test-moderation-d3-t0083/task.toml b/tasks/class-test/class-test-moderation-d3-t0083/task.toml new file mode 100644 index 0000000000000000000000000000000000000000..7a0bb6f7113c2990ace292646e8151085167c0ad --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/task.toml @@ -0,0 +1,25 @@ +version = "1.0" + +[metadata] +author_name = "Renaissance Labs" +author_email = "unknown" +difficulty = "hard" +category = "class-test" +tags = ["class-test", "moderation", "isced-0111", "train"] +isced_f = "0111" +family = "moderation" +split = "train" +seed = 83 + +[verifier] +timeout_sec = 120.0 + +[agent] +timeout_sec = 900.0 + +[environment] +build_timeout_sec = 600.0 +cpus = 1 +memory_mb = 1024 +storage_mb = 2048 +allow_internet = false diff --git a/tasks/class-test/class-test-moderation-d3-t0083/tests/checks.json b/tasks/class-test/class-test-moderation-d3-t0083/tests/checks.json new file mode 100644 index 0000000000000000000000000000000000000000..47dcb16b30d37af3131f77c89c7c307edb1a66d7 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/tests/checks.json @@ -0,0 +1,202 @@ +[ + { + "path": "markers.M1.mean_difference", + "type": "number", + "value": 0.75, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M2.mean_difference", + "type": "number", + "value": -9.25, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "markers.M3.mean_difference", + "type": "number", + "value": -0.67, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "adjusted_markers", + "type": "set", + "value": [ + "M2" + ], + "item": "str", + "weight": 2 + }, + { + "path": "cohort_mean", + "type": "number", + "value": 37.63, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "cohort_sd", + "type": "number", + "value": 10.32, + "abs_tol": 0.01, + "weight": 1.0 + }, + { + "path": "students.S019.final", + "type": "number", + "value": 89.1, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S055.final", + "type": "number", + "value": 61.5, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S058.final", + "type": "number", + "value": 58.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S075.final", + "type": "number", + "value": 41.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S085.final", + "type": "number", + "value": 51.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S092.final", + "type": "number", + "value": 74.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S114.final", + "type": "number", + "value": 71.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S117.final", + "type": "number", + "value": 54.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S131.final", + "type": "number", + "value": 36.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S183.final", + "type": "number", + "value": 69.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S222.final", + "type": "number", + "value": 51.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S238.final", + "type": "number", + "value": 63.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S275.final", + "type": "number", + "value": 51.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S288.final", + "type": "number", + "value": 74.4, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S336.final", + "type": "number", + "value": 82.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S342.final", + "type": "number", + "value": 47.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S348.final", + "type": "number", + "value": 68.9, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S356.final", + "type": "number", + "value": 81.8, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S380.final", + "type": "number", + "value": 100.0, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S390.final", + "type": "number", + "value": 54.2, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "students.S392.final", + "type": "number", + "value": 58.3, + "abs_tol": 0.1, + "weight": 1.0 + }, + { + "path": "clamped", + "type": "set", + "value": [ + "S380" + ], + "item": "str", + "weight": 2 + } +] diff --git a/tasks/class-test/class-test-moderation-d3-t0083/tests/test.sh b/tasks/class-test/class-test-moderation-d3-t0083/tests/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..3f1149df2619f3f11ecc1e06c9ee5b6599e8ab71 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/tests/test.sh @@ -0,0 +1,3 @@ +#!/bin/bash +mkdir -p /logs/verifier +python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt diff --git a/tasks/class-test/class-test-moderation-d3-t0083/tests/verify.py b/tasks/class-test/class-test-moderation-d3-t0083/tests/verify.py new file mode 100644 index 0000000000000000000000000000000000000000..e03124340b6015e692f6944976f4dedcadc0e722 --- /dev/null +++ b/tasks/class-test/class-test-moderation-d3-t0083/tests/verify.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python3 +"""Verifier for Renaissance Labs practice environments. Standard library only. + +Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1]. +The reward is the weighted share of checks that pass. A missing, unreadable or non-object +answer scores 0. Each check addresses one value in the answer by a dotted path. + +Check types + int exact integer (a float with integral value is accepted; booleans are not) + number within abs_tol of value (default 1e-9); booleans and strings are rejected + str equal after trimming and case-folding + bool exact boolean + list ordered list; items compared as int/number/str according to "item" (default str) + set unordered collection of distinct strings (or ints) + custom a function in validators.py: name(answer, args) -> bool or float in [0, 1] +""" +import argparse, json, math, os, sys + +MISSING = object() + +def lookup(obj, path): + cur = obj + for part in path.split("."): + if isinstance(cur, dict): + if part not in cur: return MISSING + cur = cur[part] + elif isinstance(cur, list): + try: cur = cur[int(part)] + except (ValueError, IndexError): return MISSING + else: + return MISSING + return cur + +def is_num(x): + return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x))) + +def eq_int(got, want): + return is_num(got) and float(got) == float(int(got)) and int(got) == want + +def eq_num(got, want, tol): + return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12 + +def eq_str(got, want): + return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold() + +def eq_item(got, want, kind, tol): + if kind == "int": return eq_int(got, want) + if kind == "number": return eq_num(got, want, tol) + return eq_str(got, want) + +def run_check(answer, chk, validators): + t = chk["type"] + if t == "custom": + fn = getattr(validators, chk["name"], None) if validators else None + if fn is None: return 0.0 + try: r = fn(answer, chk.get("args", {})) + except Exception: return 0.0 + if r is True: return 1.0 + if r is False or r is None: return 0.0 + return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0 + got = lookup(answer, chk["path"]) + if got is MISSING: return 0.0 + want, tol = chk["value"], chk.get("abs_tol", 1e-9) + if t == "int": ok = eq_int(got, want) + elif t == "number": ok = eq_num(got, want, tol) + elif t == "str": ok = eq_str(got, want) + elif t == "bool": ok = isinstance(got, bool) and got is want + elif t == "list": + ok = isinstance(got, list) and len(got) == len(want) and all(eq_item(g, w, chk.get("item", "str"), tol) for g, w in zip(got, want)) + elif t == "set": + kind = chk.get("item", "str") + if not isinstance(got, list): ok = False + else: + norm = (lambda x: x.strip().casefold() if isinstance(x, str) else None) if kind == "str" else (lambda x: int(x) if eq_int(x, int(x) if is_num(x) else 0) else None) + g = [norm(x) for x in got]; w = [norm(x) for x in want] + ok = None not in g and len(g) == len(set(g)) and set(g) == set(w) + else: ok = False + return 1.0 if ok else 0.0 + +def grade(answer, checks, validators=None): + """Return (reward, details). answer is the parsed JSON, or None if it could not be read.""" + if not isinstance(answer, dict): + return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks] + total = sum(c.get("weight", 1.0) for c in checks) + got, details = 0.0, [] + for c in checks: + s = run_check(answer, c, validators) + got += s * c.get("weight", 1.0) + details.append({"check": c.get("path", c.get("name")), "score": s}) + return (round(got / total, 6) if total else 0.0), details + +def main(): + ap = argparse.ArgumentParser() + ap.add_argument("--answer", default="/app/answer.json") + ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json")) + ap.add_argument("--out", default="/logs/verifier") + a = ap.parse_args() + checks = json.load(open(a.checks)) + try: + with open(a.answer) as f: answer = json.load(f) + except Exception: + answer = None + validators = None + here = os.path.dirname(os.path.abspath(__file__)) + if os.path.exists(os.path.join(here, "validators.py")): + sys.path.insert(0, here) + import validators # noqa + reward, details = grade(answer, checks, validators) + os.makedirs(a.out, exist_ok=True) + with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward)) + with open(os.path.join(a.out, "reward.json"), "w") as f: + json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f) + print("reward", reward) + +if __name__ == "__main__": + main()