Add 108 offline Harbor tasks for psychometrics
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- tasks/psychometrics/psychometrics-norms-d1-e0000/environment/Dockerfile +3 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/clients.csv +7 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/norms.csv +9 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/norms_guide.md +13 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/instruction.md +14 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/solution/oracle.py +170 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/solution/solve.sh +4 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/task.toml +25 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/tests/checks.json +128 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/tests/test.sh +3 -0
- tasks/psychometrics/psychometrics-norms-d1-e0000/tests/verify.py +116 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/environment/Dockerfile +3 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/clients.csv +7 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/norms.csv +9 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/norms_guide.md +13 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/instruction.md +14 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/solution/oracle.py +170 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/solution/solve.sh +4 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/task.toml +25 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/tests/checks.json +128 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/tests/test.sh +3 -0
- tasks/psychometrics/psychometrics-norms-d1-e0012/tests/verify.py +116 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/environment/Dockerfile +3 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/clients.csv +7 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/norms.csv +9 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/norms_guide.md +13 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/instruction.md +14 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/solution/oracle.py +170 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/solution/solve.sh +4 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/task.toml +25 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/tests/checks.json +128 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/tests/test.sh +3 -0
- tasks/psychometrics/psychometrics-norms-d1-t0000/tests/verify.py +116 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/environment/Dockerfile +3 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/clients.csv +7 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/norms.csv +9 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/norms_guide.md +13 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/instruction.md +14 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/solution/oracle.py +170 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/solution/solve.sh +4 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/task.toml +25 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/tests/checks.json +128 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/tests/test.sh +3 -0
- tasks/psychometrics/psychometrics-norms-d1-t0012/tests/verify.py +116 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/environment/Dockerfile +3 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/clients.csv +7 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/norms.csv +9 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/norms_guide.md +13 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/instruction.md +14 -0
- tasks/psychometrics/psychometrics-norms-d1-t0024/solution/oracle.py +170 -0
tasks/psychometrics/psychometrics-norms-d1-e0000/environment/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY workdir/ /app/
|
tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/clients.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
client_id,sex,age,raw_score,referral
|
| 2 |
+
C116,F,37,22,occupational health
|
| 3 |
+
C337,F,25,28,self
|
| 4 |
+
C286,M,33,37,GP
|
| 5 |
+
C228,F,72,25,self
|
| 6 |
+
C324,M,39,28,self
|
| 7 |
+
C319,F,40,27,self
|
tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/norms.csv
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scale,scale_name,age_band,sex,n,mean,sd
|
| 2 |
+
IRR,Irritability Scale,45-59,M,681,18.9,6.0
|
| 3 |
+
IRR,Irritability Scale,18-29,M,267,23.7,7.8
|
| 4 |
+
IRR,Irritability Scale,30-44,M,651,24.8,8.1
|
| 5 |
+
IRR,Irritability Scale,45-59,F,239,23.7,7.0
|
| 6 |
+
IRR,Irritability Scale,18-29,F,484,22.5,5.9
|
| 7 |
+
IRR,Irritability Scale,30-44,F,857,25.2,5.4
|
| 8 |
+
IRR,Irritability Scale,60-74,M,814,24.1,7.6
|
| 9 |
+
IRR,Irritability Scale,60-74,F,504,22.6,8.1
|
tasks/psychometrics/psychometrics-norms-d1-e0000/environment/workdir/norms_guide.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using the norm table
|
| 2 |
+
|
| 3 |
+
The scales and norms are invented for practice.
|
| 4 |
+
|
| 5 |
+
`norms.csv` gives the mean and standard deviation (SD) of the raw score in each norm group. A norm group is an age band and a sex. The age bands are inclusive: `30-44` covers ages 30 to 44.
|
| 6 |
+
|
| 7 |
+
For a client with raw score `x` in a norm group with mean `M` and standard deviation `SD`:
|
| 8 |
+
|
| 9 |
+
z = (x - M) / SD
|
| 10 |
+
T score = 50 + 10 * z
|
| 11 |
+
percentile = 100 * Phi(z), Phi(z) = 0.5 * (1 + erf(z / sqrt(2)))
|
| 12 |
+
|
| 13 |
+
`erf` is the error function (`math.erf` in Python). Use the unrounded z in the T score and the percentile.
|
tasks/psychometrics/psychometrics-norms-d1-e0000/instruction.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Norm-referenced scores
|
| 2 |
+
|
| 3 |
+
`clients.csv` holds raw scores on the Irritability Scale. `norms.csv` is the norm table and `norms_guide.md` says how to use it.
|
| 4 |
+
|
| 5 |
+
Convert every raw score using the norm group that matches the client's age and sex.
|
| 6 |
+
|
| 7 |
+
Write `answer.json` in this folder, a JSON object with:
|
| 8 |
+
|
| 9 |
+
- `clients`: an object keyed by client id. Each value has
|
| 10 |
+
- `z`: the z score, 2 decimals.
|
| 11 |
+
- `t_score`: the T score, 1 decimal.
|
| 12 |
+
- `percentile`: the percentile rank, 1 decimal.
|
| 13 |
+
|
| 14 |
+
Numbers are accepted within one unit of the last decimal place asked for.
|
tasks/psychometrics/psychometrics-norms-d1-e0000/solution/oracle.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reference solution for Psychometrics. Works only from the files in the current directory."""
|
| 3 |
+
import csv, json, math, os, re
|
| 4 |
+
from decimal import Decimal, ROUND_HALF_UP
|
| 5 |
+
from fractions import Fraction
|
| 6 |
+
|
| 7 |
+
def rows(name):
|
| 8 |
+
with open(name, newline="") as f: return list(csv.DictReader(f))
|
| 9 |
+
|
| 10 |
+
def text(name):
|
| 11 |
+
with open(name) as f: return f.read()
|
| 12 |
+
|
| 13 |
+
def nums(s): return [int(x) for x in re.findall(r"\d+", s)]
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------------------- scoring
|
| 16 |
+
def scoring():
|
| 17 |
+
man = text("scoring_manual.md")
|
| 18 |
+
top = int(re.search(r"scale from 0 to (\d+)", man).group(1))
|
| 19 |
+
reverse = set(nums(re.search(r"^Reverse-keyed items:(.*)$", man, re.M).group(1)))
|
| 20 |
+
subs, bands = [], []
|
| 21 |
+
for line in man.splitlines():
|
| 22 |
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
| 23 |
+
if len(cells) == 3 and re.match(r"^[A-Z]{3}$", cells[1]): subs.append((cells[1], nums(cells[2])))
|
| 24 |
+
if len(cells) == 2 and re.match(r"^\d+ to \d+$", cells[1]): bands.append((cells[0], nums(cells[1])))
|
| 25 |
+
missing_codes = {""}
|
| 26 |
+
if "code 9" in man: missing_codes.add("9")
|
| 27 |
+
m = re.search(r"Item (\d+) is the risk item", man)
|
| 28 |
+
risk = int(m.group(1)) if m else None
|
| 29 |
+
latest = {}
|
| 30 |
+
for r in rows("responses.csv"):
|
| 31 |
+
if r["client_id"] not in latest or r["administered"] > latest[r["client_id"]]["administered"]: latest[r["client_id"]] = r
|
| 32 |
+
ans = {"clients": {}, "moderate_or_severe": [], "unscorable": [], "risk_flagged": []}
|
| 33 |
+
for cid, r in latest.items():
|
| 34 |
+
out = {"subscales": {}}
|
| 35 |
+
ok = True
|
| 36 |
+
for code, items in subs:
|
| 37 |
+
vals = []
|
| 38 |
+
for i in items:
|
| 39 |
+
cell = r["item_%02d" % i].strip()
|
| 40 |
+
if cell in missing_codes: continue
|
| 41 |
+
v = int(cell)
|
| 42 |
+
vals.append(top - v if i in reverse else v)
|
| 43 |
+
n_miss = len(items) - len(vals)
|
| 44 |
+
if Fraction(n_miss, len(items)) > Fraction(20, 100):
|
| 45 |
+
ok = False; ans["unscorable"].append("%s:%s" % (cid, code)); continue
|
| 46 |
+
if n_miss == 0: out["subscales"][code] = sum(vals)
|
| 47 |
+
else:
|
| 48 |
+
pro = Decimal(sum(vals)) * Decimal(len(items)) / Decimal(len(vals))
|
| 49 |
+
out["subscales"][code] = int(pro.quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
| 50 |
+
if ok:
|
| 51 |
+
total = sum(out["subscales"].values())
|
| 52 |
+
out["total"] = total
|
| 53 |
+
for name, (lo, hi) in bands:
|
| 54 |
+
if lo <= total <= hi: out["band"] = name
|
| 55 |
+
if out["band"] in ("moderate", "severe"): ans["moderate_or_severe"].append(cid)
|
| 56 |
+
if risk is not None:
|
| 57 |
+
cell = r["item_%02d" % risk].strip()
|
| 58 |
+
if cell not in missing_codes and int(cell) >= 2: ans["risk_flagged"].append(cid)
|
| 59 |
+
ans["clients"][cid] = out
|
| 60 |
+
return ans
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------------------------------- reliable change
|
| 63 |
+
def reliable_change():
|
| 64 |
+
inst = dict((r["code"], r) for r in rows("instruments.csv"))
|
| 65 |
+
ans = {"instruments": {}, "results": {}, "recovered": [], "deteriorated": [], "no_post": []}
|
| 66 |
+
counts = {}
|
| 67 |
+
for r in rows("outcomes.csv"):
|
| 68 |
+
code, cid = r["instrument"], r["client_id"]
|
| 69 |
+
i = inst[code]
|
| 70 |
+
se = float(i["sd"]) * math.sqrt(1 - float(i["reliability"]))
|
| 71 |
+
sdiff = math.sqrt(2 * se * se)
|
| 72 |
+
ans["instruments"].setdefault(code, {})["s_diff"] = round(sdiff, 2)
|
| 73 |
+
if r["post_score"].strip() == "":
|
| 74 |
+
ans["no_post"].append("%s:%s" % (cid, code)); continue
|
| 75 |
+
pre, post, cut = float(r["pre_score"]), float(r["post_score"]), float(i["clinical_cutoff"])
|
| 76 |
+
x = (post - pre) / sdiff
|
| 77 |
+
up_is_bad = i.get("direction", "higher_is_worse") == "higher_is_worse"
|
| 78 |
+
if up_is_bad: pre_clin, post_clin, better = pre >= cut, post >= cut, post < pre
|
| 79 |
+
else: pre_clin, post_clin, better = pre <= cut, post <= cut, post > pre
|
| 80 |
+
if not (x > 1.96 or x < -1.96): status = "unchanged"
|
| 81 |
+
elif not better: status = "deteriorated"
|
| 82 |
+
elif pre_clin and not post_clin: status = "recovered"
|
| 83 |
+
else: status = "improved"
|
| 84 |
+
ans["results"].setdefault(cid, {})[code] = {"rci": round(x, 2), "status": status}
|
| 85 |
+
if status in ("recovered", "deteriorated"): ans[status].append("%s:%s" % (cid, code))
|
| 86 |
+
c = counts.setdefault(code, [0, 0]); c[0] += 1; c[1] += status == "recovered"
|
| 87 |
+
for code, (n, rec) in counts.items():
|
| 88 |
+
ans["instruments"][code]["recovered_pct"] = round(100.0 * rec / n, 1)
|
| 89 |
+
return ans
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------------------- norms
|
| 92 |
+
def norms():
|
| 93 |
+
table = {}
|
| 94 |
+
for r in rows("norms.csv"):
|
| 95 |
+
lo, hi = r["age_band"].split("-")
|
| 96 |
+
table.setdefault((r["scale"], r["sex"]), []).append((int(lo), int(hi), float(r["mean"]), float(r["sd"])))
|
| 97 |
+
only_scale = sorted(set(k[0] for k in table))[0]
|
| 98 |
+
guide = text("norms_guide.md")
|
| 99 |
+
with_ranges = "## Ranges" in guide
|
| 100 |
+
ans = {"clients": {}, "no_norm_group": [], "high_or_above": []}
|
| 101 |
+
for r in rows("clients.csv"):
|
| 102 |
+
cid = r["client_id"]
|
| 103 |
+
if "age" in r: age = int(r["age"])
|
| 104 |
+
else:
|
| 105 |
+
by, bm, bd = [int(x) for x in r["date_of_birth"].split("-")]
|
| 106 |
+
ty, tm, td = [int(x) for x in r["test_date"].split("-")]
|
| 107 |
+
age = ty - by
|
| 108 |
+
if tm < bm or (tm == bm and td < bd): age -= 1
|
| 109 |
+
scale = r.get("scale") or only_scale
|
| 110 |
+
group = [g for g in table[(scale, r["sex"])] if g[0] <= age <= g[1]]
|
| 111 |
+
if not group:
|
| 112 |
+
if cid not in ans["no_norm_group"]: ans["no_norm_group"].append(cid)
|
| 113 |
+
continue
|
| 114 |
+
z = (float(r["raw_score"]) - group[0][2]) / group[0][3]
|
| 115 |
+
t = 50 + 10 * z
|
| 116 |
+
res = {"z": round(z, 2), "t_score": round(t, 1), "percentile": round(50 * (1 + math.erf(z / math.sqrt(2))), 1)}
|
| 117 |
+
if with_ranges:
|
| 118 |
+
res["range"] = "very-high" if t >= 70 else "high" if t >= 60 else "average" if t >= 40 else "low"
|
| 119 |
+
if t >= 60: ans["high_or_above"].append("%s:%s" % (cid, scale))
|
| 120 |
+
c = ans["clients"].setdefault(cid, {"age": age, "scales": {}})
|
| 121 |
+
c["scales"][scale] = res
|
| 122 |
+
else:
|
| 123 |
+
res["age"] = age
|
| 124 |
+
ans["clients"][cid] = res
|
| 125 |
+
return ans
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------- reliability
|
| 128 |
+
def variance(xs):
|
| 129 |
+
m = sum(xs) / len(xs)
|
| 130 |
+
return sum((x - m) * (x - m) for x in xs) / (len(xs) - 1)
|
| 131 |
+
|
| 132 |
+
def corr(xs, ys):
|
| 133 |
+
n = len(xs); mx = sum(xs) / n; my = sum(ys) / n
|
| 134 |
+
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / (n - 1)
|
| 135 |
+
return cov / math.sqrt(variance(xs) * variance(ys))
|
| 136 |
+
|
| 137 |
+
def alpha_of(cols):
|
| 138 |
+
k = len(cols)
|
| 139 |
+
totals = [sum(c[i] for c in cols) for i in range(len(cols[0]))]
|
| 140 |
+
return k / (k - 1) * (1 - sum(variance(c) for c in cols) / variance(totals))
|
| 141 |
+
|
| 142 |
+
def reliability():
|
| 143 |
+
notes = text("reliability_notes.md")
|
| 144 |
+
m = re.search(r"^Reverse-keyed items:([^.]*)\.", notes, re.M)
|
| 145 |
+
reverse = set(re.findall(r"q\d+", m.group(1))) if m else set()
|
| 146 |
+
data = rows("item_responses.csv")
|
| 147 |
+
qs = sorted([c for c in data[0] if re.match(r"^q\d+$", c)], key=lambda c: int(c[1:]))
|
| 148 |
+
complete = [r for r in data if all(r[q].strip() != "" for q in qs)]
|
| 149 |
+
cols = dict((q, [(6 - int(r[q])) if q in reverse else int(r[q]) for r in complete]) for q in qs)
|
| 150 |
+
alpha = alpha_of([cols[q] for q in qs])
|
| 151 |
+
totals = [sum(cols[q][i] for q in qs) for i in range(len(complete))]
|
| 152 |
+
ans = {"items": {}, "total_variance": round(variance(totals), 3), "alpha": round(alpha, 3), "weak_items": [], "n_used": len(complete)}
|
| 153 |
+
best, best_alpha = None, None
|
| 154 |
+
for q in qs:
|
| 155 |
+
rest = [t - x for t, x in zip(totals, cols[q])]
|
| 156 |
+
rr = corr(cols[q], rest)
|
| 157 |
+
a_del = alpha_of([cols[p] for p in qs if p != q])
|
| 158 |
+
ans["items"][q] = {"mean": round(sum(cols[q]) / len(cols[q]), 2), "variance": round(variance(cols[q]), 3),
|
| 159 |
+
"item_rest_r": round(rr, 3), "alpha_if_deleted": round(a_del, 3)}
|
| 160 |
+
if rr < 0.30: ans["weak_items"].append(q)
|
| 161 |
+
if best is None or a_del > best_alpha: best, best_alpha = q, a_del
|
| 162 |
+
ans["best_item_to_drop"] = best
|
| 163 |
+
ans["alpha_gain"] = round(best_alpha - alpha, 3)
|
| 164 |
+
return ans
|
| 165 |
+
|
| 166 |
+
if os.path.exists("scoring_manual.md"): answer = scoring()
|
| 167 |
+
elif os.path.exists("rci_rules.md"): answer = reliable_change()
|
| 168 |
+
elif os.path.exists("norms.csv"): answer = norms()
|
| 169 |
+
else: answer = reliability()
|
| 170 |
+
with open("answer.json", "w") as f: json.dump(answer, f, indent=1)
|
tasks/psychometrics/psychometrics-norms-d1-e0000/solution/solve.sh
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
+
cd /app
|
| 4 |
+
python3 /solution/oracle.py
|
tasks/psychometrics/psychometrics-norms-d1-e0000/task.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version = "1.0"
|
| 2 |
+
|
| 3 |
+
[metadata]
|
| 4 |
+
author_name = "Renaissance Labs"
|
| 5 |
+
author_email = "unknown"
|
| 6 |
+
difficulty = "easy"
|
| 7 |
+
category = "psychometrics"
|
| 8 |
+
tags = ["psychometrics", "norms", "isced-0313", "eval"]
|
| 9 |
+
isced_f = "0313"
|
| 10 |
+
family = "norms"
|
| 11 |
+
split = "eval"
|
| 12 |
+
seed = 10000
|
| 13 |
+
|
| 14 |
+
[verifier]
|
| 15 |
+
timeout_sec = 120.0
|
| 16 |
+
|
| 17 |
+
[agent]
|
| 18 |
+
timeout_sec = 900.0
|
| 19 |
+
|
| 20 |
+
[environment]
|
| 21 |
+
build_timeout_sec = 600.0
|
| 22 |
+
cpus = 1
|
| 23 |
+
memory_mb = 1024
|
| 24 |
+
storage_mb = 2048
|
| 25 |
+
allow_internet = false
|
tasks/psychometrics/psychometrics-norms-d1-e0000/tests/checks.json
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"path": "clients.C228.z",
|
| 4 |
+
"type": "number",
|
| 5 |
+
"value": 0.3,
|
| 6 |
+
"abs_tol": 0.01,
|
| 7 |
+
"weight": 1.0
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"path": "clients.C228.t_score",
|
| 11 |
+
"type": "number",
|
| 12 |
+
"value": 53.0,
|
| 13 |
+
"abs_tol": 0.1,
|
| 14 |
+
"weight": 1.0
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"path": "clients.C228.percentile",
|
| 18 |
+
"type": "number",
|
| 19 |
+
"value": 61.6,
|
| 20 |
+
"abs_tol": 0.1,
|
| 21 |
+
"weight": 1.0
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"path": "clients.C337.z",
|
| 25 |
+
"type": "number",
|
| 26 |
+
"value": 0.93,
|
| 27 |
+
"abs_tol": 0.01,
|
| 28 |
+
"weight": 1.0
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "clients.C337.t_score",
|
| 32 |
+
"type": "number",
|
| 33 |
+
"value": 59.3,
|
| 34 |
+
"abs_tol": 0.1,
|
| 35 |
+
"weight": 1.0
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"path": "clients.C337.percentile",
|
| 39 |
+
"type": "number",
|
| 40 |
+
"value": 82.4,
|
| 41 |
+
"abs_tol": 0.1,
|
| 42 |
+
"weight": 1.0
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"path": "clients.C116.z",
|
| 46 |
+
"type": "number",
|
| 47 |
+
"value": -0.59,
|
| 48 |
+
"abs_tol": 0.01,
|
| 49 |
+
"weight": 1.0
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "clients.C116.t_score",
|
| 53 |
+
"type": "number",
|
| 54 |
+
"value": 44.1,
|
| 55 |
+
"abs_tol": 0.1,
|
| 56 |
+
"weight": 1.0
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "clients.C116.percentile",
|
| 60 |
+
"type": "number",
|
| 61 |
+
"value": 27.7,
|
| 62 |
+
"abs_tol": 0.1,
|
| 63 |
+
"weight": 1.0
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"path": "clients.C286.z",
|
| 67 |
+
"type": "number",
|
| 68 |
+
"value": 1.51,
|
| 69 |
+
"abs_tol": 0.01,
|
| 70 |
+
"weight": 1.0
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"path": "clients.C286.t_score",
|
| 74 |
+
"type": "number",
|
| 75 |
+
"value": 65.1,
|
| 76 |
+
"abs_tol": 0.1,
|
| 77 |
+
"weight": 1.0
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"path": "clients.C286.percentile",
|
| 81 |
+
"type": "number",
|
| 82 |
+
"value": 93.4,
|
| 83 |
+
"abs_tol": 0.1,
|
| 84 |
+
"weight": 1.0
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"path": "clients.C324.z",
|
| 88 |
+
"type": "number",
|
| 89 |
+
"value": 0.4,
|
| 90 |
+
"abs_tol": 0.01,
|
| 91 |
+
"weight": 1.0
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"path": "clients.C324.t_score",
|
| 95 |
+
"type": "number",
|
| 96 |
+
"value": 54.0,
|
| 97 |
+
"abs_tol": 0.1,
|
| 98 |
+
"weight": 1.0
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"path": "clients.C324.percentile",
|
| 102 |
+
"type": "number",
|
| 103 |
+
"value": 65.4,
|
| 104 |
+
"abs_tol": 0.1,
|
| 105 |
+
"weight": 1.0
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"path": "clients.C319.z",
|
| 109 |
+
"type": "number",
|
| 110 |
+
"value": 0.33,
|
| 111 |
+
"abs_tol": 0.01,
|
| 112 |
+
"weight": 1.0
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"path": "clients.C319.t_score",
|
| 116 |
+
"type": "number",
|
| 117 |
+
"value": 53.3,
|
| 118 |
+
"abs_tol": 0.1,
|
| 119 |
+
"weight": 1.0
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"path": "clients.C319.percentile",
|
| 123 |
+
"type": "number",
|
| 124 |
+
"value": 63.1,
|
| 125 |
+
"abs_tol": 0.1,
|
| 126 |
+
"weight": 1.0
|
| 127 |
+
}
|
| 128 |
+
]
|
tasks/psychometrics/psychometrics-norms-d1-e0000/tests/test.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
mkdir -p /logs/verifier
|
| 3 |
+
python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt
|
tasks/psychometrics/psychometrics-norms-d1-e0000/tests/verify.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Verifier for Renaissance Labs practice environments. Standard library only.
|
| 3 |
+
|
| 4 |
+
Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1].
|
| 5 |
+
The reward is the weighted share of checks that pass. A missing, unreadable or non-object
|
| 6 |
+
answer scores 0. Each check addresses one value in the answer by a dotted path.
|
| 7 |
+
|
| 8 |
+
Check types
|
| 9 |
+
int exact integer (a float with integral value is accepted; booleans are not)
|
| 10 |
+
number within abs_tol of value (default 1e-9); booleans and strings are rejected
|
| 11 |
+
str equal after trimming and case-folding
|
| 12 |
+
bool exact boolean
|
| 13 |
+
list ordered list; items compared as int/number/str according to "item" (default str)
|
| 14 |
+
set unordered collection of distinct strings (or ints)
|
| 15 |
+
custom a function in validators.py: name(answer, args) -> bool or float in [0, 1]
|
| 16 |
+
"""
|
| 17 |
+
import argparse, json, math, os, sys
|
| 18 |
+
|
| 19 |
+
MISSING = object()
|
| 20 |
+
|
| 21 |
+
def lookup(obj, path):
|
| 22 |
+
cur = obj
|
| 23 |
+
for part in path.split("."):
|
| 24 |
+
if isinstance(cur, dict):
|
| 25 |
+
if part not in cur: return MISSING
|
| 26 |
+
cur = cur[part]
|
| 27 |
+
elif isinstance(cur, list):
|
| 28 |
+
try: cur = cur[int(part)]
|
| 29 |
+
except (ValueError, IndexError): return MISSING
|
| 30 |
+
else:
|
| 31 |
+
return MISSING
|
| 32 |
+
return cur
|
| 33 |
+
|
| 34 |
+
def is_num(x):
|
| 35 |
+
return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x)))
|
| 36 |
+
|
| 37 |
+
def eq_int(got, want):
|
| 38 |
+
return is_num(got) and float(got) == float(int(got)) and int(got) == want
|
| 39 |
+
|
| 40 |
+
def eq_num(got, want, tol):
|
| 41 |
+
return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12
|
| 42 |
+
|
| 43 |
+
def eq_str(got, want):
|
| 44 |
+
return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold()
|
| 45 |
+
|
| 46 |
+
def eq_item(got, want, kind, tol):
|
| 47 |
+
if kind == "int": return eq_int(got, want)
|
| 48 |
+
if kind == "number": return eq_num(got, want, tol)
|
| 49 |
+
return eq_str(got, want)
|
| 50 |
+
|
| 51 |
+
def run_check(answer, chk, validators):
|
| 52 |
+
t = chk["type"]
|
| 53 |
+
if t == "custom":
|
| 54 |
+
fn = getattr(validators, chk["name"], None) if validators else None
|
| 55 |
+
if fn is None: return 0.0
|
| 56 |
+
try: r = fn(answer, chk.get("args", {}))
|
| 57 |
+
except Exception: return 0.0
|
| 58 |
+
if r is True: return 1.0
|
| 59 |
+
if r is False or r is None: return 0.0
|
| 60 |
+
return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0
|
| 61 |
+
got = lookup(answer, chk["path"])
|
| 62 |
+
if got is MISSING: return 0.0
|
| 63 |
+
want, tol = chk["value"], chk.get("abs_tol", 1e-9)
|
| 64 |
+
if t == "int": ok = eq_int(got, want)
|
| 65 |
+
elif t == "number": ok = eq_num(got, want, tol)
|
| 66 |
+
elif t == "str": ok = eq_str(got, want)
|
| 67 |
+
elif t == "bool": ok = isinstance(got, bool) and got is want
|
| 68 |
+
elif t == "list":
|
| 69 |
+
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))
|
| 70 |
+
elif t == "set":
|
| 71 |
+
kind = chk.get("item", "str")
|
| 72 |
+
if not isinstance(got, list): ok = False
|
| 73 |
+
else:
|
| 74 |
+
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)
|
| 75 |
+
g = [norm(x) for x in got]; w = [norm(x) for x in want]
|
| 76 |
+
ok = None not in g and len(g) == len(set(g)) and set(g) == set(w)
|
| 77 |
+
else: ok = False
|
| 78 |
+
return 1.0 if ok else 0.0
|
| 79 |
+
|
| 80 |
+
def grade(answer, checks, validators=None):
|
| 81 |
+
"""Return (reward, details). answer is the parsed JSON, or None if it could not be read."""
|
| 82 |
+
if not isinstance(answer, dict):
|
| 83 |
+
return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks]
|
| 84 |
+
total = sum(c.get("weight", 1.0) for c in checks)
|
| 85 |
+
got, details = 0.0, []
|
| 86 |
+
for c in checks:
|
| 87 |
+
s = run_check(answer, c, validators)
|
| 88 |
+
got += s * c.get("weight", 1.0)
|
| 89 |
+
details.append({"check": c.get("path", c.get("name")), "score": s})
|
| 90 |
+
return (round(got / total, 6) if total else 0.0), details
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
ap = argparse.ArgumentParser()
|
| 94 |
+
ap.add_argument("--answer", default="/app/answer.json")
|
| 95 |
+
ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json"))
|
| 96 |
+
ap.add_argument("--out", default="/logs/verifier")
|
| 97 |
+
a = ap.parse_args()
|
| 98 |
+
checks = json.load(open(a.checks))
|
| 99 |
+
try:
|
| 100 |
+
with open(a.answer) as f: answer = json.load(f)
|
| 101 |
+
except Exception:
|
| 102 |
+
answer = None
|
| 103 |
+
validators = None
|
| 104 |
+
here = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
if os.path.exists(os.path.join(here, "validators.py")):
|
| 106 |
+
sys.path.insert(0, here)
|
| 107 |
+
import validators # noqa
|
| 108 |
+
reward, details = grade(answer, checks, validators)
|
| 109 |
+
os.makedirs(a.out, exist_ok=True)
|
| 110 |
+
with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward))
|
| 111 |
+
with open(os.path.join(a.out, "reward.json"), "w") as f:
|
| 112 |
+
json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f)
|
| 113 |
+
print("reward", reward)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
main()
|
tasks/psychometrics/psychometrics-norms-d1-e0012/environment/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY workdir/ /app/
|
tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/clients.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
client_id,sex,age,raw_score,referral
|
| 2 |
+
C235,F,31,29,self
|
| 3 |
+
C267,M,56,37,self
|
| 4 |
+
C216,M,50,41,GP
|
| 5 |
+
C330,F,44,24,occupational health
|
| 6 |
+
C168,M,51,14,GP
|
| 7 |
+
C217,M,65,23,GP
|
tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/norms.csv
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scale,scale_name,age_band,sex,n,mean,sd
|
| 2 |
+
IRR,Irritability Scale,45-59,M,828,25.2,7.8
|
| 3 |
+
IRR,Irritability Scale,45-59,F,287,27.1,5.7
|
| 4 |
+
IRR,Irritability Scale,60-74,F,612,30.2,6.8
|
| 5 |
+
IRR,Irritability Scale,60-74,M,246,31.6,7.3
|
| 6 |
+
IRR,Irritability Scale,18-29,M,670,26.4,8.1
|
| 7 |
+
IRR,Irritability Scale,30-44,F,664,26.0,4.4
|
| 8 |
+
IRR,Irritability Scale,30-44,M,295,26.1,4.4
|
| 9 |
+
IRR,Irritability Scale,18-29,F,652,27.0,4.4
|
tasks/psychometrics/psychometrics-norms-d1-e0012/environment/workdir/norms_guide.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using the norm table
|
| 2 |
+
|
| 3 |
+
The scales and norms are invented for practice.
|
| 4 |
+
|
| 5 |
+
`norms.csv` gives the mean and standard deviation (SD) of the raw score in each norm group. A norm group is an age band and a sex. The age bands are inclusive: `30-44` covers ages 30 to 44.
|
| 6 |
+
|
| 7 |
+
For a client with raw score `x` in a norm group with mean `M` and standard deviation `SD`:
|
| 8 |
+
|
| 9 |
+
z = (x - M) / SD
|
| 10 |
+
T score = 50 + 10 * z
|
| 11 |
+
percentile = 100 * Phi(z), Phi(z) = 0.5 * (1 + erf(z / sqrt(2)))
|
| 12 |
+
|
| 13 |
+
`erf` is the error function (`math.erf` in Python). Use the unrounded z in the T score and the percentile.
|
tasks/psychometrics/psychometrics-norms-d1-e0012/instruction.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Norm-referenced scores
|
| 2 |
+
|
| 3 |
+
`clients.csv` holds raw scores on the Irritability Scale. `norms.csv` is the norm table and `norms_guide.md` says how to use it.
|
| 4 |
+
|
| 5 |
+
Convert every raw score using the norm group that matches the client's age and sex.
|
| 6 |
+
|
| 7 |
+
Write `answer.json` in this folder, a JSON object with:
|
| 8 |
+
|
| 9 |
+
- `clients`: an object keyed by client id. Each value has
|
| 10 |
+
- `z`: the z score, 2 decimals.
|
| 11 |
+
- `t_score`: the T score, 1 decimal.
|
| 12 |
+
- `percentile`: the percentile rank, 1 decimal.
|
| 13 |
+
|
| 14 |
+
Numbers are accepted within one unit of the last decimal place asked for.
|
tasks/psychometrics/psychometrics-norms-d1-e0012/solution/oracle.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reference solution for Psychometrics. Works only from the files in the current directory."""
|
| 3 |
+
import csv, json, math, os, re
|
| 4 |
+
from decimal import Decimal, ROUND_HALF_UP
|
| 5 |
+
from fractions import Fraction
|
| 6 |
+
|
| 7 |
+
def rows(name):
|
| 8 |
+
with open(name, newline="") as f: return list(csv.DictReader(f))
|
| 9 |
+
|
| 10 |
+
def text(name):
|
| 11 |
+
with open(name) as f: return f.read()
|
| 12 |
+
|
| 13 |
+
def nums(s): return [int(x) for x in re.findall(r"\d+", s)]
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------------------- scoring
|
| 16 |
+
def scoring():
|
| 17 |
+
man = text("scoring_manual.md")
|
| 18 |
+
top = int(re.search(r"scale from 0 to (\d+)", man).group(1))
|
| 19 |
+
reverse = set(nums(re.search(r"^Reverse-keyed items:(.*)$", man, re.M).group(1)))
|
| 20 |
+
subs, bands = [], []
|
| 21 |
+
for line in man.splitlines():
|
| 22 |
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
| 23 |
+
if len(cells) == 3 and re.match(r"^[A-Z]{3}$", cells[1]): subs.append((cells[1], nums(cells[2])))
|
| 24 |
+
if len(cells) == 2 and re.match(r"^\d+ to \d+$", cells[1]): bands.append((cells[0], nums(cells[1])))
|
| 25 |
+
missing_codes = {""}
|
| 26 |
+
if "code 9" in man: missing_codes.add("9")
|
| 27 |
+
m = re.search(r"Item (\d+) is the risk item", man)
|
| 28 |
+
risk = int(m.group(1)) if m else None
|
| 29 |
+
latest = {}
|
| 30 |
+
for r in rows("responses.csv"):
|
| 31 |
+
if r["client_id"] not in latest or r["administered"] > latest[r["client_id"]]["administered"]: latest[r["client_id"]] = r
|
| 32 |
+
ans = {"clients": {}, "moderate_or_severe": [], "unscorable": [], "risk_flagged": []}
|
| 33 |
+
for cid, r in latest.items():
|
| 34 |
+
out = {"subscales": {}}
|
| 35 |
+
ok = True
|
| 36 |
+
for code, items in subs:
|
| 37 |
+
vals = []
|
| 38 |
+
for i in items:
|
| 39 |
+
cell = r["item_%02d" % i].strip()
|
| 40 |
+
if cell in missing_codes: continue
|
| 41 |
+
v = int(cell)
|
| 42 |
+
vals.append(top - v if i in reverse else v)
|
| 43 |
+
n_miss = len(items) - len(vals)
|
| 44 |
+
if Fraction(n_miss, len(items)) > Fraction(20, 100):
|
| 45 |
+
ok = False; ans["unscorable"].append("%s:%s" % (cid, code)); continue
|
| 46 |
+
if n_miss == 0: out["subscales"][code] = sum(vals)
|
| 47 |
+
else:
|
| 48 |
+
pro = Decimal(sum(vals)) * Decimal(len(items)) / Decimal(len(vals))
|
| 49 |
+
out["subscales"][code] = int(pro.quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
| 50 |
+
if ok:
|
| 51 |
+
total = sum(out["subscales"].values())
|
| 52 |
+
out["total"] = total
|
| 53 |
+
for name, (lo, hi) in bands:
|
| 54 |
+
if lo <= total <= hi: out["band"] = name
|
| 55 |
+
if out["band"] in ("moderate", "severe"): ans["moderate_or_severe"].append(cid)
|
| 56 |
+
if risk is not None:
|
| 57 |
+
cell = r["item_%02d" % risk].strip()
|
| 58 |
+
if cell not in missing_codes and int(cell) >= 2: ans["risk_flagged"].append(cid)
|
| 59 |
+
ans["clients"][cid] = out
|
| 60 |
+
return ans
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------------------------------- reliable change
|
| 63 |
+
def reliable_change():
|
| 64 |
+
inst = dict((r["code"], r) for r in rows("instruments.csv"))
|
| 65 |
+
ans = {"instruments": {}, "results": {}, "recovered": [], "deteriorated": [], "no_post": []}
|
| 66 |
+
counts = {}
|
| 67 |
+
for r in rows("outcomes.csv"):
|
| 68 |
+
code, cid = r["instrument"], r["client_id"]
|
| 69 |
+
i = inst[code]
|
| 70 |
+
se = float(i["sd"]) * math.sqrt(1 - float(i["reliability"]))
|
| 71 |
+
sdiff = math.sqrt(2 * se * se)
|
| 72 |
+
ans["instruments"].setdefault(code, {})["s_diff"] = round(sdiff, 2)
|
| 73 |
+
if r["post_score"].strip() == "":
|
| 74 |
+
ans["no_post"].append("%s:%s" % (cid, code)); continue
|
| 75 |
+
pre, post, cut = float(r["pre_score"]), float(r["post_score"]), float(i["clinical_cutoff"])
|
| 76 |
+
x = (post - pre) / sdiff
|
| 77 |
+
up_is_bad = i.get("direction", "higher_is_worse") == "higher_is_worse"
|
| 78 |
+
if up_is_bad: pre_clin, post_clin, better = pre >= cut, post >= cut, post < pre
|
| 79 |
+
else: pre_clin, post_clin, better = pre <= cut, post <= cut, post > pre
|
| 80 |
+
if not (x > 1.96 or x < -1.96): status = "unchanged"
|
| 81 |
+
elif not better: status = "deteriorated"
|
| 82 |
+
elif pre_clin and not post_clin: status = "recovered"
|
| 83 |
+
else: status = "improved"
|
| 84 |
+
ans["results"].setdefault(cid, {})[code] = {"rci": round(x, 2), "status": status}
|
| 85 |
+
if status in ("recovered", "deteriorated"): ans[status].append("%s:%s" % (cid, code))
|
| 86 |
+
c = counts.setdefault(code, [0, 0]); c[0] += 1; c[1] += status == "recovered"
|
| 87 |
+
for code, (n, rec) in counts.items():
|
| 88 |
+
ans["instruments"][code]["recovered_pct"] = round(100.0 * rec / n, 1)
|
| 89 |
+
return ans
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------------------- norms
|
| 92 |
+
def norms():
|
| 93 |
+
table = {}
|
| 94 |
+
for r in rows("norms.csv"):
|
| 95 |
+
lo, hi = r["age_band"].split("-")
|
| 96 |
+
table.setdefault((r["scale"], r["sex"]), []).append((int(lo), int(hi), float(r["mean"]), float(r["sd"])))
|
| 97 |
+
only_scale = sorted(set(k[0] for k in table))[0]
|
| 98 |
+
guide = text("norms_guide.md")
|
| 99 |
+
with_ranges = "## Ranges" in guide
|
| 100 |
+
ans = {"clients": {}, "no_norm_group": [], "high_or_above": []}
|
| 101 |
+
for r in rows("clients.csv"):
|
| 102 |
+
cid = r["client_id"]
|
| 103 |
+
if "age" in r: age = int(r["age"])
|
| 104 |
+
else:
|
| 105 |
+
by, bm, bd = [int(x) for x in r["date_of_birth"].split("-")]
|
| 106 |
+
ty, tm, td = [int(x) for x in r["test_date"].split("-")]
|
| 107 |
+
age = ty - by
|
| 108 |
+
if tm < bm or (tm == bm and td < bd): age -= 1
|
| 109 |
+
scale = r.get("scale") or only_scale
|
| 110 |
+
group = [g for g in table[(scale, r["sex"])] if g[0] <= age <= g[1]]
|
| 111 |
+
if not group:
|
| 112 |
+
if cid not in ans["no_norm_group"]: ans["no_norm_group"].append(cid)
|
| 113 |
+
continue
|
| 114 |
+
z = (float(r["raw_score"]) - group[0][2]) / group[0][3]
|
| 115 |
+
t = 50 + 10 * z
|
| 116 |
+
res = {"z": round(z, 2), "t_score": round(t, 1), "percentile": round(50 * (1 + math.erf(z / math.sqrt(2))), 1)}
|
| 117 |
+
if with_ranges:
|
| 118 |
+
res["range"] = "very-high" if t >= 70 else "high" if t >= 60 else "average" if t >= 40 else "low"
|
| 119 |
+
if t >= 60: ans["high_or_above"].append("%s:%s" % (cid, scale))
|
| 120 |
+
c = ans["clients"].setdefault(cid, {"age": age, "scales": {}})
|
| 121 |
+
c["scales"][scale] = res
|
| 122 |
+
else:
|
| 123 |
+
res["age"] = age
|
| 124 |
+
ans["clients"][cid] = res
|
| 125 |
+
return ans
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------- reliability
|
| 128 |
+
def variance(xs):
|
| 129 |
+
m = sum(xs) / len(xs)
|
| 130 |
+
return sum((x - m) * (x - m) for x in xs) / (len(xs) - 1)
|
| 131 |
+
|
| 132 |
+
def corr(xs, ys):
|
| 133 |
+
n = len(xs); mx = sum(xs) / n; my = sum(ys) / n
|
| 134 |
+
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / (n - 1)
|
| 135 |
+
return cov / math.sqrt(variance(xs) * variance(ys))
|
| 136 |
+
|
| 137 |
+
def alpha_of(cols):
|
| 138 |
+
k = len(cols)
|
| 139 |
+
totals = [sum(c[i] for c in cols) for i in range(len(cols[0]))]
|
| 140 |
+
return k / (k - 1) * (1 - sum(variance(c) for c in cols) / variance(totals))
|
| 141 |
+
|
| 142 |
+
def reliability():
|
| 143 |
+
notes = text("reliability_notes.md")
|
| 144 |
+
m = re.search(r"^Reverse-keyed items:([^.]*)\.", notes, re.M)
|
| 145 |
+
reverse = set(re.findall(r"q\d+", m.group(1))) if m else set()
|
| 146 |
+
data = rows("item_responses.csv")
|
| 147 |
+
qs = sorted([c for c in data[0] if re.match(r"^q\d+$", c)], key=lambda c: int(c[1:]))
|
| 148 |
+
complete = [r for r in data if all(r[q].strip() != "" for q in qs)]
|
| 149 |
+
cols = dict((q, [(6 - int(r[q])) if q in reverse else int(r[q]) for r in complete]) for q in qs)
|
| 150 |
+
alpha = alpha_of([cols[q] for q in qs])
|
| 151 |
+
totals = [sum(cols[q][i] for q in qs) for i in range(len(complete))]
|
| 152 |
+
ans = {"items": {}, "total_variance": round(variance(totals), 3), "alpha": round(alpha, 3), "weak_items": [], "n_used": len(complete)}
|
| 153 |
+
best, best_alpha = None, None
|
| 154 |
+
for q in qs:
|
| 155 |
+
rest = [t - x for t, x in zip(totals, cols[q])]
|
| 156 |
+
rr = corr(cols[q], rest)
|
| 157 |
+
a_del = alpha_of([cols[p] for p in qs if p != q])
|
| 158 |
+
ans["items"][q] = {"mean": round(sum(cols[q]) / len(cols[q]), 2), "variance": round(variance(cols[q]), 3),
|
| 159 |
+
"item_rest_r": round(rr, 3), "alpha_if_deleted": round(a_del, 3)}
|
| 160 |
+
if rr < 0.30: ans["weak_items"].append(q)
|
| 161 |
+
if best is None or a_del > best_alpha: best, best_alpha = q, a_del
|
| 162 |
+
ans["best_item_to_drop"] = best
|
| 163 |
+
ans["alpha_gain"] = round(best_alpha - alpha, 3)
|
| 164 |
+
return ans
|
| 165 |
+
|
| 166 |
+
if os.path.exists("scoring_manual.md"): answer = scoring()
|
| 167 |
+
elif os.path.exists("rci_rules.md"): answer = reliable_change()
|
| 168 |
+
elif os.path.exists("norms.csv"): answer = norms()
|
| 169 |
+
else: answer = reliability()
|
| 170 |
+
with open("answer.json", "w") as f: json.dump(answer, f, indent=1)
|
tasks/psychometrics/psychometrics-norms-d1-e0012/solution/solve.sh
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
+
cd /app
|
| 4 |
+
python3 /solution/oracle.py
|
tasks/psychometrics/psychometrics-norms-d1-e0012/task.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version = "1.0"
|
| 2 |
+
|
| 3 |
+
[metadata]
|
| 4 |
+
author_name = "Renaissance Labs"
|
| 5 |
+
author_email = "unknown"
|
| 6 |
+
difficulty = "easy"
|
| 7 |
+
category = "psychometrics"
|
| 8 |
+
tags = ["psychometrics", "norms", "isced-0313", "eval"]
|
| 9 |
+
isced_f = "0313"
|
| 10 |
+
family = "norms"
|
| 11 |
+
split = "eval"
|
| 12 |
+
seed = 10012
|
| 13 |
+
|
| 14 |
+
[verifier]
|
| 15 |
+
timeout_sec = 120.0
|
| 16 |
+
|
| 17 |
+
[agent]
|
| 18 |
+
timeout_sec = 900.0
|
| 19 |
+
|
| 20 |
+
[environment]
|
| 21 |
+
build_timeout_sec = 600.0
|
| 22 |
+
cpus = 1
|
| 23 |
+
memory_mb = 1024
|
| 24 |
+
storage_mb = 2048
|
| 25 |
+
allow_internet = false
|
tasks/psychometrics/psychometrics-norms-d1-e0012/tests/checks.json
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"path": "clients.C216.z",
|
| 4 |
+
"type": "number",
|
| 5 |
+
"value": 2.03,
|
| 6 |
+
"abs_tol": 0.01,
|
| 7 |
+
"weight": 1.0
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"path": "clients.C216.t_score",
|
| 11 |
+
"type": "number",
|
| 12 |
+
"value": 70.3,
|
| 13 |
+
"abs_tol": 0.1,
|
| 14 |
+
"weight": 1.0
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"path": "clients.C216.percentile",
|
| 18 |
+
"type": "number",
|
| 19 |
+
"value": 97.9,
|
| 20 |
+
"abs_tol": 0.1,
|
| 21 |
+
"weight": 1.0
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"path": "clients.C168.z",
|
| 25 |
+
"type": "number",
|
| 26 |
+
"value": -1.44,
|
| 27 |
+
"abs_tol": 0.01,
|
| 28 |
+
"weight": 1.0
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "clients.C168.t_score",
|
| 32 |
+
"type": "number",
|
| 33 |
+
"value": 35.6,
|
| 34 |
+
"abs_tol": 0.1,
|
| 35 |
+
"weight": 1.0
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"path": "clients.C168.percentile",
|
| 39 |
+
"type": "number",
|
| 40 |
+
"value": 7.6,
|
| 41 |
+
"abs_tol": 0.1,
|
| 42 |
+
"weight": 1.0
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"path": "clients.C267.z",
|
| 46 |
+
"type": "number",
|
| 47 |
+
"value": 1.51,
|
| 48 |
+
"abs_tol": 0.01,
|
| 49 |
+
"weight": 1.0
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "clients.C267.t_score",
|
| 53 |
+
"type": "number",
|
| 54 |
+
"value": 65.1,
|
| 55 |
+
"abs_tol": 0.1,
|
| 56 |
+
"weight": 1.0
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "clients.C267.percentile",
|
| 60 |
+
"type": "number",
|
| 61 |
+
"value": 93.5,
|
| 62 |
+
"abs_tol": 0.1,
|
| 63 |
+
"weight": 1.0
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"path": "clients.C330.z",
|
| 67 |
+
"type": "number",
|
| 68 |
+
"value": -0.45,
|
| 69 |
+
"abs_tol": 0.01,
|
| 70 |
+
"weight": 1.0
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"path": "clients.C330.t_score",
|
| 74 |
+
"type": "number",
|
| 75 |
+
"value": 45.5,
|
| 76 |
+
"abs_tol": 0.1,
|
| 77 |
+
"weight": 1.0
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"path": "clients.C330.percentile",
|
| 81 |
+
"type": "number",
|
| 82 |
+
"value": 32.5,
|
| 83 |
+
"abs_tol": 0.1,
|
| 84 |
+
"weight": 1.0
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"path": "clients.C217.z",
|
| 88 |
+
"type": "number",
|
| 89 |
+
"value": -1.18,
|
| 90 |
+
"abs_tol": 0.01,
|
| 91 |
+
"weight": 1.0
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"path": "clients.C217.t_score",
|
| 95 |
+
"type": "number",
|
| 96 |
+
"value": 38.2,
|
| 97 |
+
"abs_tol": 0.1,
|
| 98 |
+
"weight": 1.0
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"path": "clients.C217.percentile",
|
| 102 |
+
"type": "number",
|
| 103 |
+
"value": 11.9,
|
| 104 |
+
"abs_tol": 0.1,
|
| 105 |
+
"weight": 1.0
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"path": "clients.C235.z",
|
| 109 |
+
"type": "number",
|
| 110 |
+
"value": 0.68,
|
| 111 |
+
"abs_tol": 0.01,
|
| 112 |
+
"weight": 1.0
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"path": "clients.C235.t_score",
|
| 116 |
+
"type": "number",
|
| 117 |
+
"value": 56.8,
|
| 118 |
+
"abs_tol": 0.1,
|
| 119 |
+
"weight": 1.0
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"path": "clients.C235.percentile",
|
| 123 |
+
"type": "number",
|
| 124 |
+
"value": 75.2,
|
| 125 |
+
"abs_tol": 0.1,
|
| 126 |
+
"weight": 1.0
|
| 127 |
+
}
|
| 128 |
+
]
|
tasks/psychometrics/psychometrics-norms-d1-e0012/tests/test.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
mkdir -p /logs/verifier
|
| 3 |
+
python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt
|
tasks/psychometrics/psychometrics-norms-d1-e0012/tests/verify.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Verifier for Renaissance Labs practice environments. Standard library only.
|
| 3 |
+
|
| 4 |
+
Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1].
|
| 5 |
+
The reward is the weighted share of checks that pass. A missing, unreadable or non-object
|
| 6 |
+
answer scores 0. Each check addresses one value in the answer by a dotted path.
|
| 7 |
+
|
| 8 |
+
Check types
|
| 9 |
+
int exact integer (a float with integral value is accepted; booleans are not)
|
| 10 |
+
number within abs_tol of value (default 1e-9); booleans and strings are rejected
|
| 11 |
+
str equal after trimming and case-folding
|
| 12 |
+
bool exact boolean
|
| 13 |
+
list ordered list; items compared as int/number/str according to "item" (default str)
|
| 14 |
+
set unordered collection of distinct strings (or ints)
|
| 15 |
+
custom a function in validators.py: name(answer, args) -> bool or float in [0, 1]
|
| 16 |
+
"""
|
| 17 |
+
import argparse, json, math, os, sys
|
| 18 |
+
|
| 19 |
+
MISSING = object()
|
| 20 |
+
|
| 21 |
+
def lookup(obj, path):
|
| 22 |
+
cur = obj
|
| 23 |
+
for part in path.split("."):
|
| 24 |
+
if isinstance(cur, dict):
|
| 25 |
+
if part not in cur: return MISSING
|
| 26 |
+
cur = cur[part]
|
| 27 |
+
elif isinstance(cur, list):
|
| 28 |
+
try: cur = cur[int(part)]
|
| 29 |
+
except (ValueError, IndexError): return MISSING
|
| 30 |
+
else:
|
| 31 |
+
return MISSING
|
| 32 |
+
return cur
|
| 33 |
+
|
| 34 |
+
def is_num(x):
|
| 35 |
+
return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x)))
|
| 36 |
+
|
| 37 |
+
def eq_int(got, want):
|
| 38 |
+
return is_num(got) and float(got) == float(int(got)) and int(got) == want
|
| 39 |
+
|
| 40 |
+
def eq_num(got, want, tol):
|
| 41 |
+
return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12
|
| 42 |
+
|
| 43 |
+
def eq_str(got, want):
|
| 44 |
+
return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold()
|
| 45 |
+
|
| 46 |
+
def eq_item(got, want, kind, tol):
|
| 47 |
+
if kind == "int": return eq_int(got, want)
|
| 48 |
+
if kind == "number": return eq_num(got, want, tol)
|
| 49 |
+
return eq_str(got, want)
|
| 50 |
+
|
| 51 |
+
def run_check(answer, chk, validators):
|
| 52 |
+
t = chk["type"]
|
| 53 |
+
if t == "custom":
|
| 54 |
+
fn = getattr(validators, chk["name"], None) if validators else None
|
| 55 |
+
if fn is None: return 0.0
|
| 56 |
+
try: r = fn(answer, chk.get("args", {}))
|
| 57 |
+
except Exception: return 0.0
|
| 58 |
+
if r is True: return 1.0
|
| 59 |
+
if r is False or r is None: return 0.0
|
| 60 |
+
return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0
|
| 61 |
+
got = lookup(answer, chk["path"])
|
| 62 |
+
if got is MISSING: return 0.0
|
| 63 |
+
want, tol = chk["value"], chk.get("abs_tol", 1e-9)
|
| 64 |
+
if t == "int": ok = eq_int(got, want)
|
| 65 |
+
elif t == "number": ok = eq_num(got, want, tol)
|
| 66 |
+
elif t == "str": ok = eq_str(got, want)
|
| 67 |
+
elif t == "bool": ok = isinstance(got, bool) and got is want
|
| 68 |
+
elif t == "list":
|
| 69 |
+
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))
|
| 70 |
+
elif t == "set":
|
| 71 |
+
kind = chk.get("item", "str")
|
| 72 |
+
if not isinstance(got, list): ok = False
|
| 73 |
+
else:
|
| 74 |
+
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)
|
| 75 |
+
g = [norm(x) for x in got]; w = [norm(x) for x in want]
|
| 76 |
+
ok = None not in g and len(g) == len(set(g)) and set(g) == set(w)
|
| 77 |
+
else: ok = False
|
| 78 |
+
return 1.0 if ok else 0.0
|
| 79 |
+
|
| 80 |
+
def grade(answer, checks, validators=None):
|
| 81 |
+
"""Return (reward, details). answer is the parsed JSON, or None if it could not be read."""
|
| 82 |
+
if not isinstance(answer, dict):
|
| 83 |
+
return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks]
|
| 84 |
+
total = sum(c.get("weight", 1.0) for c in checks)
|
| 85 |
+
got, details = 0.0, []
|
| 86 |
+
for c in checks:
|
| 87 |
+
s = run_check(answer, c, validators)
|
| 88 |
+
got += s * c.get("weight", 1.0)
|
| 89 |
+
details.append({"check": c.get("path", c.get("name")), "score": s})
|
| 90 |
+
return (round(got / total, 6) if total else 0.0), details
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
ap = argparse.ArgumentParser()
|
| 94 |
+
ap.add_argument("--answer", default="/app/answer.json")
|
| 95 |
+
ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json"))
|
| 96 |
+
ap.add_argument("--out", default="/logs/verifier")
|
| 97 |
+
a = ap.parse_args()
|
| 98 |
+
checks = json.load(open(a.checks))
|
| 99 |
+
try:
|
| 100 |
+
with open(a.answer) as f: answer = json.load(f)
|
| 101 |
+
except Exception:
|
| 102 |
+
answer = None
|
| 103 |
+
validators = None
|
| 104 |
+
here = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
if os.path.exists(os.path.join(here, "validators.py")):
|
| 106 |
+
sys.path.insert(0, here)
|
| 107 |
+
import validators # noqa
|
| 108 |
+
reward, details = grade(answer, checks, validators)
|
| 109 |
+
os.makedirs(a.out, exist_ok=True)
|
| 110 |
+
with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward))
|
| 111 |
+
with open(os.path.join(a.out, "reward.json"), "w") as f:
|
| 112 |
+
json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f)
|
| 113 |
+
print("reward", reward)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
main()
|
tasks/psychometrics/psychometrics-norms-d1-t0000/environment/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY workdir/ /app/
|
tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/clients.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
client_id,sex,age,raw_score,referral
|
| 2 |
+
C134,M,48,19,GP
|
| 3 |
+
C117,M,70,30,self
|
| 4 |
+
C341,F,32,34,occupational health
|
| 5 |
+
C195,F,69,24,occupational health
|
| 6 |
+
C129,M,32,24,GP
|
| 7 |
+
C283,M,48,29,self
|
tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/norms.csv
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scale,scale_name,age_band,sex,n,mean,sd
|
| 2 |
+
IRR,Irritability Scale,18-29,M,235,20.7,8.4
|
| 3 |
+
IRR,Irritability Scale,30-44,F,126,18.8,8.0
|
| 4 |
+
IRR,Irritability Scale,18-29,F,503,22.9,6.7
|
| 5 |
+
IRR,Irritability Scale,60-74,M,270,21.5,7.3
|
| 6 |
+
IRR,Irritability Scale,45-59,M,775,21.6,4.4
|
| 7 |
+
IRR,Irritability Scale,45-59,F,194,17.6,6.3
|
| 8 |
+
IRR,Irritability Scale,60-74,F,617,21.5,4.5
|
| 9 |
+
IRR,Irritability Scale,30-44,M,401,17.8,8.1
|
tasks/psychometrics/psychometrics-norms-d1-t0000/environment/workdir/norms_guide.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using the norm table
|
| 2 |
+
|
| 3 |
+
The scales and norms are invented for practice.
|
| 4 |
+
|
| 5 |
+
`norms.csv` gives the mean and standard deviation (SD) of the raw score in each norm group. A norm group is an age band and a sex. The age bands are inclusive: `30-44` covers ages 30 to 44.
|
| 6 |
+
|
| 7 |
+
For a client with raw score `x` in a norm group with mean `M` and standard deviation `SD`:
|
| 8 |
+
|
| 9 |
+
z = (x - M) / SD
|
| 10 |
+
T score = 50 + 10 * z
|
| 11 |
+
percentile = 100 * Phi(z), Phi(z) = 0.5 * (1 + erf(z / sqrt(2)))
|
| 12 |
+
|
| 13 |
+
`erf` is the error function (`math.erf` in Python). Use the unrounded z in the T score and the percentile.
|
tasks/psychometrics/psychometrics-norms-d1-t0000/instruction.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Norm-referenced scores
|
| 2 |
+
|
| 3 |
+
`clients.csv` holds raw scores on the Irritability Scale. `norms.csv` is the norm table and `norms_guide.md` says how to use it.
|
| 4 |
+
|
| 5 |
+
Convert every raw score using the norm group that matches the client's age and sex.
|
| 6 |
+
|
| 7 |
+
Write `answer.json` in this folder, a JSON object with:
|
| 8 |
+
|
| 9 |
+
- `clients`: an object keyed by client id. Each value has
|
| 10 |
+
- `z`: the z score, 2 decimals.
|
| 11 |
+
- `t_score`: the T score, 1 decimal.
|
| 12 |
+
- `percentile`: the percentile rank, 1 decimal.
|
| 13 |
+
|
| 14 |
+
Numbers are accepted within one unit of the last decimal place asked for.
|
tasks/psychometrics/psychometrics-norms-d1-t0000/solution/oracle.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reference solution for Psychometrics. Works only from the files in the current directory."""
|
| 3 |
+
import csv, json, math, os, re
|
| 4 |
+
from decimal import Decimal, ROUND_HALF_UP
|
| 5 |
+
from fractions import Fraction
|
| 6 |
+
|
| 7 |
+
def rows(name):
|
| 8 |
+
with open(name, newline="") as f: return list(csv.DictReader(f))
|
| 9 |
+
|
| 10 |
+
def text(name):
|
| 11 |
+
with open(name) as f: return f.read()
|
| 12 |
+
|
| 13 |
+
def nums(s): return [int(x) for x in re.findall(r"\d+", s)]
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------------------- scoring
|
| 16 |
+
def scoring():
|
| 17 |
+
man = text("scoring_manual.md")
|
| 18 |
+
top = int(re.search(r"scale from 0 to (\d+)", man).group(1))
|
| 19 |
+
reverse = set(nums(re.search(r"^Reverse-keyed items:(.*)$", man, re.M).group(1)))
|
| 20 |
+
subs, bands = [], []
|
| 21 |
+
for line in man.splitlines():
|
| 22 |
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
| 23 |
+
if len(cells) == 3 and re.match(r"^[A-Z]{3}$", cells[1]): subs.append((cells[1], nums(cells[2])))
|
| 24 |
+
if len(cells) == 2 and re.match(r"^\d+ to \d+$", cells[1]): bands.append((cells[0], nums(cells[1])))
|
| 25 |
+
missing_codes = {""}
|
| 26 |
+
if "code 9" in man: missing_codes.add("9")
|
| 27 |
+
m = re.search(r"Item (\d+) is the risk item", man)
|
| 28 |
+
risk = int(m.group(1)) if m else None
|
| 29 |
+
latest = {}
|
| 30 |
+
for r in rows("responses.csv"):
|
| 31 |
+
if r["client_id"] not in latest or r["administered"] > latest[r["client_id"]]["administered"]: latest[r["client_id"]] = r
|
| 32 |
+
ans = {"clients": {}, "moderate_or_severe": [], "unscorable": [], "risk_flagged": []}
|
| 33 |
+
for cid, r in latest.items():
|
| 34 |
+
out = {"subscales": {}}
|
| 35 |
+
ok = True
|
| 36 |
+
for code, items in subs:
|
| 37 |
+
vals = []
|
| 38 |
+
for i in items:
|
| 39 |
+
cell = r["item_%02d" % i].strip()
|
| 40 |
+
if cell in missing_codes: continue
|
| 41 |
+
v = int(cell)
|
| 42 |
+
vals.append(top - v if i in reverse else v)
|
| 43 |
+
n_miss = len(items) - len(vals)
|
| 44 |
+
if Fraction(n_miss, len(items)) > Fraction(20, 100):
|
| 45 |
+
ok = False; ans["unscorable"].append("%s:%s" % (cid, code)); continue
|
| 46 |
+
if n_miss == 0: out["subscales"][code] = sum(vals)
|
| 47 |
+
else:
|
| 48 |
+
pro = Decimal(sum(vals)) * Decimal(len(items)) / Decimal(len(vals))
|
| 49 |
+
out["subscales"][code] = int(pro.quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
| 50 |
+
if ok:
|
| 51 |
+
total = sum(out["subscales"].values())
|
| 52 |
+
out["total"] = total
|
| 53 |
+
for name, (lo, hi) in bands:
|
| 54 |
+
if lo <= total <= hi: out["band"] = name
|
| 55 |
+
if out["band"] in ("moderate", "severe"): ans["moderate_or_severe"].append(cid)
|
| 56 |
+
if risk is not None:
|
| 57 |
+
cell = r["item_%02d" % risk].strip()
|
| 58 |
+
if cell not in missing_codes and int(cell) >= 2: ans["risk_flagged"].append(cid)
|
| 59 |
+
ans["clients"][cid] = out
|
| 60 |
+
return ans
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------------------------------- reliable change
|
| 63 |
+
def reliable_change():
|
| 64 |
+
inst = dict((r["code"], r) for r in rows("instruments.csv"))
|
| 65 |
+
ans = {"instruments": {}, "results": {}, "recovered": [], "deteriorated": [], "no_post": []}
|
| 66 |
+
counts = {}
|
| 67 |
+
for r in rows("outcomes.csv"):
|
| 68 |
+
code, cid = r["instrument"], r["client_id"]
|
| 69 |
+
i = inst[code]
|
| 70 |
+
se = float(i["sd"]) * math.sqrt(1 - float(i["reliability"]))
|
| 71 |
+
sdiff = math.sqrt(2 * se * se)
|
| 72 |
+
ans["instruments"].setdefault(code, {})["s_diff"] = round(sdiff, 2)
|
| 73 |
+
if r["post_score"].strip() == "":
|
| 74 |
+
ans["no_post"].append("%s:%s" % (cid, code)); continue
|
| 75 |
+
pre, post, cut = float(r["pre_score"]), float(r["post_score"]), float(i["clinical_cutoff"])
|
| 76 |
+
x = (post - pre) / sdiff
|
| 77 |
+
up_is_bad = i.get("direction", "higher_is_worse") == "higher_is_worse"
|
| 78 |
+
if up_is_bad: pre_clin, post_clin, better = pre >= cut, post >= cut, post < pre
|
| 79 |
+
else: pre_clin, post_clin, better = pre <= cut, post <= cut, post > pre
|
| 80 |
+
if not (x > 1.96 or x < -1.96): status = "unchanged"
|
| 81 |
+
elif not better: status = "deteriorated"
|
| 82 |
+
elif pre_clin and not post_clin: status = "recovered"
|
| 83 |
+
else: status = "improved"
|
| 84 |
+
ans["results"].setdefault(cid, {})[code] = {"rci": round(x, 2), "status": status}
|
| 85 |
+
if status in ("recovered", "deteriorated"): ans[status].append("%s:%s" % (cid, code))
|
| 86 |
+
c = counts.setdefault(code, [0, 0]); c[0] += 1; c[1] += status == "recovered"
|
| 87 |
+
for code, (n, rec) in counts.items():
|
| 88 |
+
ans["instruments"][code]["recovered_pct"] = round(100.0 * rec / n, 1)
|
| 89 |
+
return ans
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------------------- norms
|
| 92 |
+
def norms():
|
| 93 |
+
table = {}
|
| 94 |
+
for r in rows("norms.csv"):
|
| 95 |
+
lo, hi = r["age_band"].split("-")
|
| 96 |
+
table.setdefault((r["scale"], r["sex"]), []).append((int(lo), int(hi), float(r["mean"]), float(r["sd"])))
|
| 97 |
+
only_scale = sorted(set(k[0] for k in table))[0]
|
| 98 |
+
guide = text("norms_guide.md")
|
| 99 |
+
with_ranges = "## Ranges" in guide
|
| 100 |
+
ans = {"clients": {}, "no_norm_group": [], "high_or_above": []}
|
| 101 |
+
for r in rows("clients.csv"):
|
| 102 |
+
cid = r["client_id"]
|
| 103 |
+
if "age" in r: age = int(r["age"])
|
| 104 |
+
else:
|
| 105 |
+
by, bm, bd = [int(x) for x in r["date_of_birth"].split("-")]
|
| 106 |
+
ty, tm, td = [int(x) for x in r["test_date"].split("-")]
|
| 107 |
+
age = ty - by
|
| 108 |
+
if tm < bm or (tm == bm and td < bd): age -= 1
|
| 109 |
+
scale = r.get("scale") or only_scale
|
| 110 |
+
group = [g for g in table[(scale, r["sex"])] if g[0] <= age <= g[1]]
|
| 111 |
+
if not group:
|
| 112 |
+
if cid not in ans["no_norm_group"]: ans["no_norm_group"].append(cid)
|
| 113 |
+
continue
|
| 114 |
+
z = (float(r["raw_score"]) - group[0][2]) / group[0][3]
|
| 115 |
+
t = 50 + 10 * z
|
| 116 |
+
res = {"z": round(z, 2), "t_score": round(t, 1), "percentile": round(50 * (1 + math.erf(z / math.sqrt(2))), 1)}
|
| 117 |
+
if with_ranges:
|
| 118 |
+
res["range"] = "very-high" if t >= 70 else "high" if t >= 60 else "average" if t >= 40 else "low"
|
| 119 |
+
if t >= 60: ans["high_or_above"].append("%s:%s" % (cid, scale))
|
| 120 |
+
c = ans["clients"].setdefault(cid, {"age": age, "scales": {}})
|
| 121 |
+
c["scales"][scale] = res
|
| 122 |
+
else:
|
| 123 |
+
res["age"] = age
|
| 124 |
+
ans["clients"][cid] = res
|
| 125 |
+
return ans
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------- reliability
|
| 128 |
+
def variance(xs):
|
| 129 |
+
m = sum(xs) / len(xs)
|
| 130 |
+
return sum((x - m) * (x - m) for x in xs) / (len(xs) - 1)
|
| 131 |
+
|
| 132 |
+
def corr(xs, ys):
|
| 133 |
+
n = len(xs); mx = sum(xs) / n; my = sum(ys) / n
|
| 134 |
+
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / (n - 1)
|
| 135 |
+
return cov / math.sqrt(variance(xs) * variance(ys))
|
| 136 |
+
|
| 137 |
+
def alpha_of(cols):
|
| 138 |
+
k = len(cols)
|
| 139 |
+
totals = [sum(c[i] for c in cols) for i in range(len(cols[0]))]
|
| 140 |
+
return k / (k - 1) * (1 - sum(variance(c) for c in cols) / variance(totals))
|
| 141 |
+
|
| 142 |
+
def reliability():
|
| 143 |
+
notes = text("reliability_notes.md")
|
| 144 |
+
m = re.search(r"^Reverse-keyed items:([^.]*)\.", notes, re.M)
|
| 145 |
+
reverse = set(re.findall(r"q\d+", m.group(1))) if m else set()
|
| 146 |
+
data = rows("item_responses.csv")
|
| 147 |
+
qs = sorted([c for c in data[0] if re.match(r"^q\d+$", c)], key=lambda c: int(c[1:]))
|
| 148 |
+
complete = [r for r in data if all(r[q].strip() != "" for q in qs)]
|
| 149 |
+
cols = dict((q, [(6 - int(r[q])) if q in reverse else int(r[q]) for r in complete]) for q in qs)
|
| 150 |
+
alpha = alpha_of([cols[q] for q in qs])
|
| 151 |
+
totals = [sum(cols[q][i] for q in qs) for i in range(len(complete))]
|
| 152 |
+
ans = {"items": {}, "total_variance": round(variance(totals), 3), "alpha": round(alpha, 3), "weak_items": [], "n_used": len(complete)}
|
| 153 |
+
best, best_alpha = None, None
|
| 154 |
+
for q in qs:
|
| 155 |
+
rest = [t - x for t, x in zip(totals, cols[q])]
|
| 156 |
+
rr = corr(cols[q], rest)
|
| 157 |
+
a_del = alpha_of([cols[p] for p in qs if p != q])
|
| 158 |
+
ans["items"][q] = {"mean": round(sum(cols[q]) / len(cols[q]), 2), "variance": round(variance(cols[q]), 3),
|
| 159 |
+
"item_rest_r": round(rr, 3), "alpha_if_deleted": round(a_del, 3)}
|
| 160 |
+
if rr < 0.30: ans["weak_items"].append(q)
|
| 161 |
+
if best is None or a_del > best_alpha: best, best_alpha = q, a_del
|
| 162 |
+
ans["best_item_to_drop"] = best
|
| 163 |
+
ans["alpha_gain"] = round(best_alpha - alpha, 3)
|
| 164 |
+
return ans
|
| 165 |
+
|
| 166 |
+
if os.path.exists("scoring_manual.md"): answer = scoring()
|
| 167 |
+
elif os.path.exists("rci_rules.md"): answer = reliable_change()
|
| 168 |
+
elif os.path.exists("norms.csv"): answer = norms()
|
| 169 |
+
else: answer = reliability()
|
| 170 |
+
with open("answer.json", "w") as f: json.dump(answer, f, indent=1)
|
tasks/psychometrics/psychometrics-norms-d1-t0000/solution/solve.sh
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
+
cd /app
|
| 4 |
+
python3 /solution/oracle.py
|
tasks/psychometrics/psychometrics-norms-d1-t0000/task.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version = "1.0"
|
| 2 |
+
|
| 3 |
+
[metadata]
|
| 4 |
+
author_name = "Renaissance Labs"
|
| 5 |
+
author_email = "unknown"
|
| 6 |
+
difficulty = "easy"
|
| 7 |
+
category = "psychometrics"
|
| 8 |
+
tags = ["psychometrics", "norms", "isced-0313", "train"]
|
| 9 |
+
isced_f = "0313"
|
| 10 |
+
family = "norms"
|
| 11 |
+
split = "train"
|
| 12 |
+
seed = 0
|
| 13 |
+
|
| 14 |
+
[verifier]
|
| 15 |
+
timeout_sec = 120.0
|
| 16 |
+
|
| 17 |
+
[agent]
|
| 18 |
+
timeout_sec = 900.0
|
| 19 |
+
|
| 20 |
+
[environment]
|
| 21 |
+
build_timeout_sec = 600.0
|
| 22 |
+
cpus = 1
|
| 23 |
+
memory_mb = 1024
|
| 24 |
+
storage_mb = 2048
|
| 25 |
+
allow_internet = false
|
tasks/psychometrics/psychometrics-norms-d1-t0000/tests/checks.json
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"path": "clients.C129.z",
|
| 4 |
+
"type": "number",
|
| 5 |
+
"value": 0.77,
|
| 6 |
+
"abs_tol": 0.01,
|
| 7 |
+
"weight": 1.0
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"path": "clients.C129.t_score",
|
| 11 |
+
"type": "number",
|
| 12 |
+
"value": 57.7,
|
| 13 |
+
"abs_tol": 0.1,
|
| 14 |
+
"weight": 1.0
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"path": "clients.C129.percentile",
|
| 18 |
+
"type": "number",
|
| 19 |
+
"value": 77.8,
|
| 20 |
+
"abs_tol": 0.1,
|
| 21 |
+
"weight": 1.0
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"path": "clients.C195.z",
|
| 25 |
+
"type": "number",
|
| 26 |
+
"value": 0.56,
|
| 27 |
+
"abs_tol": 0.01,
|
| 28 |
+
"weight": 1.0
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "clients.C195.t_score",
|
| 32 |
+
"type": "number",
|
| 33 |
+
"value": 55.6,
|
| 34 |
+
"abs_tol": 0.1,
|
| 35 |
+
"weight": 1.0
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"path": "clients.C195.percentile",
|
| 39 |
+
"type": "number",
|
| 40 |
+
"value": 71.1,
|
| 41 |
+
"abs_tol": 0.1,
|
| 42 |
+
"weight": 1.0
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"path": "clients.C134.z",
|
| 46 |
+
"type": "number",
|
| 47 |
+
"value": -0.59,
|
| 48 |
+
"abs_tol": 0.01,
|
| 49 |
+
"weight": 1.0
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "clients.C134.t_score",
|
| 53 |
+
"type": "number",
|
| 54 |
+
"value": 44.1,
|
| 55 |
+
"abs_tol": 0.1,
|
| 56 |
+
"weight": 1.0
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "clients.C134.percentile",
|
| 60 |
+
"type": "number",
|
| 61 |
+
"value": 27.7,
|
| 62 |
+
"abs_tol": 0.1,
|
| 63 |
+
"weight": 1.0
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"path": "clients.C283.z",
|
| 67 |
+
"type": "number",
|
| 68 |
+
"value": 1.68,
|
| 69 |
+
"abs_tol": 0.01,
|
| 70 |
+
"weight": 1.0
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"path": "clients.C283.t_score",
|
| 74 |
+
"type": "number",
|
| 75 |
+
"value": 66.8,
|
| 76 |
+
"abs_tol": 0.1,
|
| 77 |
+
"weight": 1.0
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"path": "clients.C283.percentile",
|
| 81 |
+
"type": "number",
|
| 82 |
+
"value": 95.4,
|
| 83 |
+
"abs_tol": 0.1,
|
| 84 |
+
"weight": 1.0
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"path": "clients.C341.z",
|
| 88 |
+
"type": "number",
|
| 89 |
+
"value": 1.9,
|
| 90 |
+
"abs_tol": 0.01,
|
| 91 |
+
"weight": 1.0
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"path": "clients.C341.t_score",
|
| 95 |
+
"type": "number",
|
| 96 |
+
"value": 69.0,
|
| 97 |
+
"abs_tol": 0.1,
|
| 98 |
+
"weight": 1.0
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"path": "clients.C341.percentile",
|
| 102 |
+
"type": "number",
|
| 103 |
+
"value": 97.1,
|
| 104 |
+
"abs_tol": 0.1,
|
| 105 |
+
"weight": 1.0
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"path": "clients.C117.z",
|
| 109 |
+
"type": "number",
|
| 110 |
+
"value": 1.16,
|
| 111 |
+
"abs_tol": 0.01,
|
| 112 |
+
"weight": 1.0
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"path": "clients.C117.t_score",
|
| 116 |
+
"type": "number",
|
| 117 |
+
"value": 61.6,
|
| 118 |
+
"abs_tol": 0.1,
|
| 119 |
+
"weight": 1.0
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"path": "clients.C117.percentile",
|
| 123 |
+
"type": "number",
|
| 124 |
+
"value": 87.8,
|
| 125 |
+
"abs_tol": 0.1,
|
| 126 |
+
"weight": 1.0
|
| 127 |
+
}
|
| 128 |
+
]
|
tasks/psychometrics/psychometrics-norms-d1-t0000/tests/test.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
mkdir -p /logs/verifier
|
| 3 |
+
python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt
|
tasks/psychometrics/psychometrics-norms-d1-t0000/tests/verify.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Verifier for Renaissance Labs practice environments. Standard library only.
|
| 3 |
+
|
| 4 |
+
Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1].
|
| 5 |
+
The reward is the weighted share of checks that pass. A missing, unreadable or non-object
|
| 6 |
+
answer scores 0. Each check addresses one value in the answer by a dotted path.
|
| 7 |
+
|
| 8 |
+
Check types
|
| 9 |
+
int exact integer (a float with integral value is accepted; booleans are not)
|
| 10 |
+
number within abs_tol of value (default 1e-9); booleans and strings are rejected
|
| 11 |
+
str equal after trimming and case-folding
|
| 12 |
+
bool exact boolean
|
| 13 |
+
list ordered list; items compared as int/number/str according to "item" (default str)
|
| 14 |
+
set unordered collection of distinct strings (or ints)
|
| 15 |
+
custom a function in validators.py: name(answer, args) -> bool or float in [0, 1]
|
| 16 |
+
"""
|
| 17 |
+
import argparse, json, math, os, sys
|
| 18 |
+
|
| 19 |
+
MISSING = object()
|
| 20 |
+
|
| 21 |
+
def lookup(obj, path):
|
| 22 |
+
cur = obj
|
| 23 |
+
for part in path.split("."):
|
| 24 |
+
if isinstance(cur, dict):
|
| 25 |
+
if part not in cur: return MISSING
|
| 26 |
+
cur = cur[part]
|
| 27 |
+
elif isinstance(cur, list):
|
| 28 |
+
try: cur = cur[int(part)]
|
| 29 |
+
except (ValueError, IndexError): return MISSING
|
| 30 |
+
else:
|
| 31 |
+
return MISSING
|
| 32 |
+
return cur
|
| 33 |
+
|
| 34 |
+
def is_num(x):
|
| 35 |
+
return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x)))
|
| 36 |
+
|
| 37 |
+
def eq_int(got, want):
|
| 38 |
+
return is_num(got) and float(got) == float(int(got)) and int(got) == want
|
| 39 |
+
|
| 40 |
+
def eq_num(got, want, tol):
|
| 41 |
+
return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12
|
| 42 |
+
|
| 43 |
+
def eq_str(got, want):
|
| 44 |
+
return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold()
|
| 45 |
+
|
| 46 |
+
def eq_item(got, want, kind, tol):
|
| 47 |
+
if kind == "int": return eq_int(got, want)
|
| 48 |
+
if kind == "number": return eq_num(got, want, tol)
|
| 49 |
+
return eq_str(got, want)
|
| 50 |
+
|
| 51 |
+
def run_check(answer, chk, validators):
|
| 52 |
+
t = chk["type"]
|
| 53 |
+
if t == "custom":
|
| 54 |
+
fn = getattr(validators, chk["name"], None) if validators else None
|
| 55 |
+
if fn is None: return 0.0
|
| 56 |
+
try: r = fn(answer, chk.get("args", {}))
|
| 57 |
+
except Exception: return 0.0
|
| 58 |
+
if r is True: return 1.0
|
| 59 |
+
if r is False or r is None: return 0.0
|
| 60 |
+
return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0
|
| 61 |
+
got = lookup(answer, chk["path"])
|
| 62 |
+
if got is MISSING: return 0.0
|
| 63 |
+
want, tol = chk["value"], chk.get("abs_tol", 1e-9)
|
| 64 |
+
if t == "int": ok = eq_int(got, want)
|
| 65 |
+
elif t == "number": ok = eq_num(got, want, tol)
|
| 66 |
+
elif t == "str": ok = eq_str(got, want)
|
| 67 |
+
elif t == "bool": ok = isinstance(got, bool) and got is want
|
| 68 |
+
elif t == "list":
|
| 69 |
+
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))
|
| 70 |
+
elif t == "set":
|
| 71 |
+
kind = chk.get("item", "str")
|
| 72 |
+
if not isinstance(got, list): ok = False
|
| 73 |
+
else:
|
| 74 |
+
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)
|
| 75 |
+
g = [norm(x) for x in got]; w = [norm(x) for x in want]
|
| 76 |
+
ok = None not in g and len(g) == len(set(g)) and set(g) == set(w)
|
| 77 |
+
else: ok = False
|
| 78 |
+
return 1.0 if ok else 0.0
|
| 79 |
+
|
| 80 |
+
def grade(answer, checks, validators=None):
|
| 81 |
+
"""Return (reward, details). answer is the parsed JSON, or None if it could not be read."""
|
| 82 |
+
if not isinstance(answer, dict):
|
| 83 |
+
return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks]
|
| 84 |
+
total = sum(c.get("weight", 1.0) for c in checks)
|
| 85 |
+
got, details = 0.0, []
|
| 86 |
+
for c in checks:
|
| 87 |
+
s = run_check(answer, c, validators)
|
| 88 |
+
got += s * c.get("weight", 1.0)
|
| 89 |
+
details.append({"check": c.get("path", c.get("name")), "score": s})
|
| 90 |
+
return (round(got / total, 6) if total else 0.0), details
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
ap = argparse.ArgumentParser()
|
| 94 |
+
ap.add_argument("--answer", default="/app/answer.json")
|
| 95 |
+
ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json"))
|
| 96 |
+
ap.add_argument("--out", default="/logs/verifier")
|
| 97 |
+
a = ap.parse_args()
|
| 98 |
+
checks = json.load(open(a.checks))
|
| 99 |
+
try:
|
| 100 |
+
with open(a.answer) as f: answer = json.load(f)
|
| 101 |
+
except Exception:
|
| 102 |
+
answer = None
|
| 103 |
+
validators = None
|
| 104 |
+
here = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
if os.path.exists(os.path.join(here, "validators.py")):
|
| 106 |
+
sys.path.insert(0, here)
|
| 107 |
+
import validators # noqa
|
| 108 |
+
reward, details = grade(answer, checks, validators)
|
| 109 |
+
os.makedirs(a.out, exist_ok=True)
|
| 110 |
+
with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward))
|
| 111 |
+
with open(os.path.join(a.out, "reward.json"), "w") as f:
|
| 112 |
+
json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f)
|
| 113 |
+
print("reward", reward)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
main()
|
tasks/psychometrics/psychometrics-norms-d1-t0012/environment/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY workdir/ /app/
|
tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/clients.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
client_id,sex,age,raw_score,referral
|
| 2 |
+
C104,F,25,32,self
|
| 3 |
+
C232,F,65,28,self
|
| 4 |
+
C312,M,50,36,self
|
| 5 |
+
C292,M,52,15,GP
|
| 6 |
+
C207,M,49,38,self
|
| 7 |
+
C202,M,50,22,GP
|
tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/norms.csv
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scale,scale_name,age_band,sex,n,mean,sd
|
| 2 |
+
ATT,Attention Difficulties Scale,18-29,M,296,19.7,6.6
|
| 3 |
+
ATT,Attention Difficulties Scale,60-74,M,585,21.6,5.1
|
| 4 |
+
ATT,Attention Difficulties Scale,30-44,F,786,19.2,7.8
|
| 5 |
+
ATT,Attention Difficulties Scale,60-74,F,878,17.3,5.0
|
| 6 |
+
ATT,Attention Difficulties Scale,18-29,F,671,21.2,5.1
|
| 7 |
+
ATT,Attention Difficulties Scale,45-59,F,865,25.1,4.5
|
| 8 |
+
ATT,Attention Difficulties Scale,45-59,M,584,23.8,6.7
|
| 9 |
+
ATT,Attention Difficulties Scale,30-44,M,593,18.2,7.1
|
tasks/psychometrics/psychometrics-norms-d1-t0012/environment/workdir/norms_guide.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using the norm table
|
| 2 |
+
|
| 3 |
+
The scales and norms are invented for practice.
|
| 4 |
+
|
| 5 |
+
`norms.csv` gives the mean and standard deviation (SD) of the raw score in each norm group. A norm group is an age band and a sex. The age bands are inclusive: `30-44` covers ages 30 to 44.
|
| 6 |
+
|
| 7 |
+
For a client with raw score `x` in a norm group with mean `M` and standard deviation `SD`:
|
| 8 |
+
|
| 9 |
+
z = (x - M) / SD
|
| 10 |
+
T score = 50 + 10 * z
|
| 11 |
+
percentile = 100 * Phi(z), Phi(z) = 0.5 * (1 + erf(z / sqrt(2)))
|
| 12 |
+
|
| 13 |
+
`erf` is the error function (`math.erf` in Python). Use the unrounded z in the T score and the percentile.
|
tasks/psychometrics/psychometrics-norms-d1-t0012/instruction.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Norm-referenced scores
|
| 2 |
+
|
| 3 |
+
`clients.csv` holds raw scores on the Attention Difficulties Scale. `norms.csv` is the norm table and `norms_guide.md` says how to use it.
|
| 4 |
+
|
| 5 |
+
Convert every raw score using the norm group that matches the client's age and sex.
|
| 6 |
+
|
| 7 |
+
Write `answer.json` in this folder, a JSON object with:
|
| 8 |
+
|
| 9 |
+
- `clients`: an object keyed by client id. Each value has
|
| 10 |
+
- `z`: the z score, 2 decimals.
|
| 11 |
+
- `t_score`: the T score, 1 decimal.
|
| 12 |
+
- `percentile`: the percentile rank, 1 decimal.
|
| 13 |
+
|
| 14 |
+
Numbers are accepted within one unit of the last decimal place asked for.
|
tasks/psychometrics/psychometrics-norms-d1-t0012/solution/oracle.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reference solution for Psychometrics. Works only from the files in the current directory."""
|
| 3 |
+
import csv, json, math, os, re
|
| 4 |
+
from decimal import Decimal, ROUND_HALF_UP
|
| 5 |
+
from fractions import Fraction
|
| 6 |
+
|
| 7 |
+
def rows(name):
|
| 8 |
+
with open(name, newline="") as f: return list(csv.DictReader(f))
|
| 9 |
+
|
| 10 |
+
def text(name):
|
| 11 |
+
with open(name) as f: return f.read()
|
| 12 |
+
|
| 13 |
+
def nums(s): return [int(x) for x in re.findall(r"\d+", s)]
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------------------- scoring
|
| 16 |
+
def scoring():
|
| 17 |
+
man = text("scoring_manual.md")
|
| 18 |
+
top = int(re.search(r"scale from 0 to (\d+)", man).group(1))
|
| 19 |
+
reverse = set(nums(re.search(r"^Reverse-keyed items:(.*)$", man, re.M).group(1)))
|
| 20 |
+
subs, bands = [], []
|
| 21 |
+
for line in man.splitlines():
|
| 22 |
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
| 23 |
+
if len(cells) == 3 and re.match(r"^[A-Z]{3}$", cells[1]): subs.append((cells[1], nums(cells[2])))
|
| 24 |
+
if len(cells) == 2 and re.match(r"^\d+ to \d+$", cells[1]): bands.append((cells[0], nums(cells[1])))
|
| 25 |
+
missing_codes = {""}
|
| 26 |
+
if "code 9" in man: missing_codes.add("9")
|
| 27 |
+
m = re.search(r"Item (\d+) is the risk item", man)
|
| 28 |
+
risk = int(m.group(1)) if m else None
|
| 29 |
+
latest = {}
|
| 30 |
+
for r in rows("responses.csv"):
|
| 31 |
+
if r["client_id"] not in latest or r["administered"] > latest[r["client_id"]]["administered"]: latest[r["client_id"]] = r
|
| 32 |
+
ans = {"clients": {}, "moderate_or_severe": [], "unscorable": [], "risk_flagged": []}
|
| 33 |
+
for cid, r in latest.items():
|
| 34 |
+
out = {"subscales": {}}
|
| 35 |
+
ok = True
|
| 36 |
+
for code, items in subs:
|
| 37 |
+
vals = []
|
| 38 |
+
for i in items:
|
| 39 |
+
cell = r["item_%02d" % i].strip()
|
| 40 |
+
if cell in missing_codes: continue
|
| 41 |
+
v = int(cell)
|
| 42 |
+
vals.append(top - v if i in reverse else v)
|
| 43 |
+
n_miss = len(items) - len(vals)
|
| 44 |
+
if Fraction(n_miss, len(items)) > Fraction(20, 100):
|
| 45 |
+
ok = False; ans["unscorable"].append("%s:%s" % (cid, code)); continue
|
| 46 |
+
if n_miss == 0: out["subscales"][code] = sum(vals)
|
| 47 |
+
else:
|
| 48 |
+
pro = Decimal(sum(vals)) * Decimal(len(items)) / Decimal(len(vals))
|
| 49 |
+
out["subscales"][code] = int(pro.quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
| 50 |
+
if ok:
|
| 51 |
+
total = sum(out["subscales"].values())
|
| 52 |
+
out["total"] = total
|
| 53 |
+
for name, (lo, hi) in bands:
|
| 54 |
+
if lo <= total <= hi: out["band"] = name
|
| 55 |
+
if out["band"] in ("moderate", "severe"): ans["moderate_or_severe"].append(cid)
|
| 56 |
+
if risk is not None:
|
| 57 |
+
cell = r["item_%02d" % risk].strip()
|
| 58 |
+
if cell not in missing_codes and int(cell) >= 2: ans["risk_flagged"].append(cid)
|
| 59 |
+
ans["clients"][cid] = out
|
| 60 |
+
return ans
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------------------------------- reliable change
|
| 63 |
+
def reliable_change():
|
| 64 |
+
inst = dict((r["code"], r) for r in rows("instruments.csv"))
|
| 65 |
+
ans = {"instruments": {}, "results": {}, "recovered": [], "deteriorated": [], "no_post": []}
|
| 66 |
+
counts = {}
|
| 67 |
+
for r in rows("outcomes.csv"):
|
| 68 |
+
code, cid = r["instrument"], r["client_id"]
|
| 69 |
+
i = inst[code]
|
| 70 |
+
se = float(i["sd"]) * math.sqrt(1 - float(i["reliability"]))
|
| 71 |
+
sdiff = math.sqrt(2 * se * se)
|
| 72 |
+
ans["instruments"].setdefault(code, {})["s_diff"] = round(sdiff, 2)
|
| 73 |
+
if r["post_score"].strip() == "":
|
| 74 |
+
ans["no_post"].append("%s:%s" % (cid, code)); continue
|
| 75 |
+
pre, post, cut = float(r["pre_score"]), float(r["post_score"]), float(i["clinical_cutoff"])
|
| 76 |
+
x = (post - pre) / sdiff
|
| 77 |
+
up_is_bad = i.get("direction", "higher_is_worse") == "higher_is_worse"
|
| 78 |
+
if up_is_bad: pre_clin, post_clin, better = pre >= cut, post >= cut, post < pre
|
| 79 |
+
else: pre_clin, post_clin, better = pre <= cut, post <= cut, post > pre
|
| 80 |
+
if not (x > 1.96 or x < -1.96): status = "unchanged"
|
| 81 |
+
elif not better: status = "deteriorated"
|
| 82 |
+
elif pre_clin and not post_clin: status = "recovered"
|
| 83 |
+
else: status = "improved"
|
| 84 |
+
ans["results"].setdefault(cid, {})[code] = {"rci": round(x, 2), "status": status}
|
| 85 |
+
if status in ("recovered", "deteriorated"): ans[status].append("%s:%s" % (cid, code))
|
| 86 |
+
c = counts.setdefault(code, [0, 0]); c[0] += 1; c[1] += status == "recovered"
|
| 87 |
+
for code, (n, rec) in counts.items():
|
| 88 |
+
ans["instruments"][code]["recovered_pct"] = round(100.0 * rec / n, 1)
|
| 89 |
+
return ans
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------------------- norms
|
| 92 |
+
def norms():
|
| 93 |
+
table = {}
|
| 94 |
+
for r in rows("norms.csv"):
|
| 95 |
+
lo, hi = r["age_band"].split("-")
|
| 96 |
+
table.setdefault((r["scale"], r["sex"]), []).append((int(lo), int(hi), float(r["mean"]), float(r["sd"])))
|
| 97 |
+
only_scale = sorted(set(k[0] for k in table))[0]
|
| 98 |
+
guide = text("norms_guide.md")
|
| 99 |
+
with_ranges = "## Ranges" in guide
|
| 100 |
+
ans = {"clients": {}, "no_norm_group": [], "high_or_above": []}
|
| 101 |
+
for r in rows("clients.csv"):
|
| 102 |
+
cid = r["client_id"]
|
| 103 |
+
if "age" in r: age = int(r["age"])
|
| 104 |
+
else:
|
| 105 |
+
by, bm, bd = [int(x) for x in r["date_of_birth"].split("-")]
|
| 106 |
+
ty, tm, td = [int(x) for x in r["test_date"].split("-")]
|
| 107 |
+
age = ty - by
|
| 108 |
+
if tm < bm or (tm == bm and td < bd): age -= 1
|
| 109 |
+
scale = r.get("scale") or only_scale
|
| 110 |
+
group = [g for g in table[(scale, r["sex"])] if g[0] <= age <= g[1]]
|
| 111 |
+
if not group:
|
| 112 |
+
if cid not in ans["no_norm_group"]: ans["no_norm_group"].append(cid)
|
| 113 |
+
continue
|
| 114 |
+
z = (float(r["raw_score"]) - group[0][2]) / group[0][3]
|
| 115 |
+
t = 50 + 10 * z
|
| 116 |
+
res = {"z": round(z, 2), "t_score": round(t, 1), "percentile": round(50 * (1 + math.erf(z / math.sqrt(2))), 1)}
|
| 117 |
+
if with_ranges:
|
| 118 |
+
res["range"] = "very-high" if t >= 70 else "high" if t >= 60 else "average" if t >= 40 else "low"
|
| 119 |
+
if t >= 60: ans["high_or_above"].append("%s:%s" % (cid, scale))
|
| 120 |
+
c = ans["clients"].setdefault(cid, {"age": age, "scales": {}})
|
| 121 |
+
c["scales"][scale] = res
|
| 122 |
+
else:
|
| 123 |
+
res["age"] = age
|
| 124 |
+
ans["clients"][cid] = res
|
| 125 |
+
return ans
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------- reliability
|
| 128 |
+
def variance(xs):
|
| 129 |
+
m = sum(xs) / len(xs)
|
| 130 |
+
return sum((x - m) * (x - m) for x in xs) / (len(xs) - 1)
|
| 131 |
+
|
| 132 |
+
def corr(xs, ys):
|
| 133 |
+
n = len(xs); mx = sum(xs) / n; my = sum(ys) / n
|
| 134 |
+
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / (n - 1)
|
| 135 |
+
return cov / math.sqrt(variance(xs) * variance(ys))
|
| 136 |
+
|
| 137 |
+
def alpha_of(cols):
|
| 138 |
+
k = len(cols)
|
| 139 |
+
totals = [sum(c[i] for c in cols) for i in range(len(cols[0]))]
|
| 140 |
+
return k / (k - 1) * (1 - sum(variance(c) for c in cols) / variance(totals))
|
| 141 |
+
|
| 142 |
+
def reliability():
|
| 143 |
+
notes = text("reliability_notes.md")
|
| 144 |
+
m = re.search(r"^Reverse-keyed items:([^.]*)\.", notes, re.M)
|
| 145 |
+
reverse = set(re.findall(r"q\d+", m.group(1))) if m else set()
|
| 146 |
+
data = rows("item_responses.csv")
|
| 147 |
+
qs = sorted([c for c in data[0] if re.match(r"^q\d+$", c)], key=lambda c: int(c[1:]))
|
| 148 |
+
complete = [r for r in data if all(r[q].strip() != "" for q in qs)]
|
| 149 |
+
cols = dict((q, [(6 - int(r[q])) if q in reverse else int(r[q]) for r in complete]) for q in qs)
|
| 150 |
+
alpha = alpha_of([cols[q] for q in qs])
|
| 151 |
+
totals = [sum(cols[q][i] for q in qs) for i in range(len(complete))]
|
| 152 |
+
ans = {"items": {}, "total_variance": round(variance(totals), 3), "alpha": round(alpha, 3), "weak_items": [], "n_used": len(complete)}
|
| 153 |
+
best, best_alpha = None, None
|
| 154 |
+
for q in qs:
|
| 155 |
+
rest = [t - x for t, x in zip(totals, cols[q])]
|
| 156 |
+
rr = corr(cols[q], rest)
|
| 157 |
+
a_del = alpha_of([cols[p] for p in qs if p != q])
|
| 158 |
+
ans["items"][q] = {"mean": round(sum(cols[q]) / len(cols[q]), 2), "variance": round(variance(cols[q]), 3),
|
| 159 |
+
"item_rest_r": round(rr, 3), "alpha_if_deleted": round(a_del, 3)}
|
| 160 |
+
if rr < 0.30: ans["weak_items"].append(q)
|
| 161 |
+
if best is None or a_del > best_alpha: best, best_alpha = q, a_del
|
| 162 |
+
ans["best_item_to_drop"] = best
|
| 163 |
+
ans["alpha_gain"] = round(best_alpha - alpha, 3)
|
| 164 |
+
return ans
|
| 165 |
+
|
| 166 |
+
if os.path.exists("scoring_manual.md"): answer = scoring()
|
| 167 |
+
elif os.path.exists("rci_rules.md"): answer = reliable_change()
|
| 168 |
+
elif os.path.exists("norms.csv"): answer = norms()
|
| 169 |
+
else: answer = reliability()
|
| 170 |
+
with open("answer.json", "w") as f: json.dump(answer, f, indent=1)
|
tasks/psychometrics/psychometrics-norms-d1-t0012/solution/solve.sh
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
set -e
|
| 3 |
+
cd /app
|
| 4 |
+
python3 /solution/oracle.py
|
tasks/psychometrics/psychometrics-norms-d1-t0012/task.toml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version = "1.0"
|
| 2 |
+
|
| 3 |
+
[metadata]
|
| 4 |
+
author_name = "Renaissance Labs"
|
| 5 |
+
author_email = "unknown"
|
| 6 |
+
difficulty = "easy"
|
| 7 |
+
category = "psychometrics"
|
| 8 |
+
tags = ["psychometrics", "norms", "isced-0313", "train"]
|
| 9 |
+
isced_f = "0313"
|
| 10 |
+
family = "norms"
|
| 11 |
+
split = "train"
|
| 12 |
+
seed = 12
|
| 13 |
+
|
| 14 |
+
[verifier]
|
| 15 |
+
timeout_sec = 120.0
|
| 16 |
+
|
| 17 |
+
[agent]
|
| 18 |
+
timeout_sec = 900.0
|
| 19 |
+
|
| 20 |
+
[environment]
|
| 21 |
+
build_timeout_sec = 600.0
|
| 22 |
+
cpus = 1
|
| 23 |
+
memory_mb = 1024
|
| 24 |
+
storage_mb = 2048
|
| 25 |
+
allow_internet = false
|
tasks/psychometrics/psychometrics-norms-d1-t0012/tests/checks.json
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"path": "clients.C202.z",
|
| 4 |
+
"type": "number",
|
| 5 |
+
"value": -0.27,
|
| 6 |
+
"abs_tol": 0.01,
|
| 7 |
+
"weight": 1.0
|
| 8 |
+
},
|
| 9 |
+
{
|
| 10 |
+
"path": "clients.C202.t_score",
|
| 11 |
+
"type": "number",
|
| 12 |
+
"value": 47.3,
|
| 13 |
+
"abs_tol": 0.1,
|
| 14 |
+
"weight": 1.0
|
| 15 |
+
},
|
| 16 |
+
{
|
| 17 |
+
"path": "clients.C202.percentile",
|
| 18 |
+
"type": "number",
|
| 19 |
+
"value": 39.4,
|
| 20 |
+
"abs_tol": 0.1,
|
| 21 |
+
"weight": 1.0
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"path": "clients.C292.z",
|
| 25 |
+
"type": "number",
|
| 26 |
+
"value": -1.31,
|
| 27 |
+
"abs_tol": 0.01,
|
| 28 |
+
"weight": 1.0
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"path": "clients.C292.t_score",
|
| 32 |
+
"type": "number",
|
| 33 |
+
"value": 36.9,
|
| 34 |
+
"abs_tol": 0.1,
|
| 35 |
+
"weight": 1.0
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"path": "clients.C292.percentile",
|
| 39 |
+
"type": "number",
|
| 40 |
+
"value": 9.5,
|
| 41 |
+
"abs_tol": 0.1,
|
| 42 |
+
"weight": 1.0
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"path": "clients.C104.z",
|
| 46 |
+
"type": "number",
|
| 47 |
+
"value": 2.12,
|
| 48 |
+
"abs_tol": 0.01,
|
| 49 |
+
"weight": 1.0
|
| 50 |
+
},
|
| 51 |
+
{
|
| 52 |
+
"path": "clients.C104.t_score",
|
| 53 |
+
"type": "number",
|
| 54 |
+
"value": 71.2,
|
| 55 |
+
"abs_tol": 0.1,
|
| 56 |
+
"weight": 1.0
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"path": "clients.C104.percentile",
|
| 60 |
+
"type": "number",
|
| 61 |
+
"value": 98.3,
|
| 62 |
+
"abs_tol": 0.1,
|
| 63 |
+
"weight": 1.0
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"path": "clients.C312.z",
|
| 67 |
+
"type": "number",
|
| 68 |
+
"value": 1.82,
|
| 69 |
+
"abs_tol": 0.01,
|
| 70 |
+
"weight": 1.0
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"path": "clients.C312.t_score",
|
| 74 |
+
"type": "number",
|
| 75 |
+
"value": 68.2,
|
| 76 |
+
"abs_tol": 0.1,
|
| 77 |
+
"weight": 1.0
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"path": "clients.C312.percentile",
|
| 81 |
+
"type": "number",
|
| 82 |
+
"value": 96.6,
|
| 83 |
+
"abs_tol": 0.1,
|
| 84 |
+
"weight": 1.0
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"path": "clients.C232.z",
|
| 88 |
+
"type": "number",
|
| 89 |
+
"value": 2.14,
|
| 90 |
+
"abs_tol": 0.01,
|
| 91 |
+
"weight": 1.0
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"path": "clients.C232.t_score",
|
| 95 |
+
"type": "number",
|
| 96 |
+
"value": 71.4,
|
| 97 |
+
"abs_tol": 0.1,
|
| 98 |
+
"weight": 1.0
|
| 99 |
+
},
|
| 100 |
+
{
|
| 101 |
+
"path": "clients.C232.percentile",
|
| 102 |
+
"type": "number",
|
| 103 |
+
"value": 98.4,
|
| 104 |
+
"abs_tol": 0.1,
|
| 105 |
+
"weight": 1.0
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"path": "clients.C207.z",
|
| 109 |
+
"type": "number",
|
| 110 |
+
"value": 2.12,
|
| 111 |
+
"abs_tol": 0.01,
|
| 112 |
+
"weight": 1.0
|
| 113 |
+
},
|
| 114 |
+
{
|
| 115 |
+
"path": "clients.C207.t_score",
|
| 116 |
+
"type": "number",
|
| 117 |
+
"value": 71.2,
|
| 118 |
+
"abs_tol": 0.1,
|
| 119 |
+
"weight": 1.0
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"path": "clients.C207.percentile",
|
| 123 |
+
"type": "number",
|
| 124 |
+
"value": 98.3,
|
| 125 |
+
"abs_tol": 0.1,
|
| 126 |
+
"weight": 1.0
|
| 127 |
+
}
|
| 128 |
+
]
|
tasks/psychometrics/psychometrics-norms-d1-t0012/tests/test.sh
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
mkdir -p /logs/verifier
|
| 3 |
+
python3 /tests/verify.py --answer /app/answer.json --checks /tests/checks.json --out /logs/verifier || echo 0 > /logs/verifier/reward.txt
|
tasks/psychometrics/psychometrics-norms-d1-t0012/tests/verify.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Verifier for Renaissance Labs practice environments. Standard library only.
|
| 3 |
+
|
| 4 |
+
Reads the agent's answer (a JSON object) and a list of checks, and writes a reward in [0, 1].
|
| 5 |
+
The reward is the weighted share of checks that pass. A missing, unreadable or non-object
|
| 6 |
+
answer scores 0. Each check addresses one value in the answer by a dotted path.
|
| 7 |
+
|
| 8 |
+
Check types
|
| 9 |
+
int exact integer (a float with integral value is accepted; booleans are not)
|
| 10 |
+
number within abs_tol of value (default 1e-9); booleans and strings are rejected
|
| 11 |
+
str equal after trimming and case-folding
|
| 12 |
+
bool exact boolean
|
| 13 |
+
list ordered list; items compared as int/number/str according to "item" (default str)
|
| 14 |
+
set unordered collection of distinct strings (or ints)
|
| 15 |
+
custom a function in validators.py: name(answer, args) -> bool or float in [0, 1]
|
| 16 |
+
"""
|
| 17 |
+
import argparse, json, math, os, sys
|
| 18 |
+
|
| 19 |
+
MISSING = object()
|
| 20 |
+
|
| 21 |
+
def lookup(obj, path):
|
| 22 |
+
cur = obj
|
| 23 |
+
for part in path.split("."):
|
| 24 |
+
if isinstance(cur, dict):
|
| 25 |
+
if part not in cur: return MISSING
|
| 26 |
+
cur = cur[part]
|
| 27 |
+
elif isinstance(cur, list):
|
| 28 |
+
try: cur = cur[int(part)]
|
| 29 |
+
except (ValueError, IndexError): return MISSING
|
| 30 |
+
else:
|
| 31 |
+
return MISSING
|
| 32 |
+
return cur
|
| 33 |
+
|
| 34 |
+
def is_num(x):
|
| 35 |
+
return isinstance(x, (int, float)) and not isinstance(x, bool) and not (isinstance(x, float) and (math.isnan(x) or math.isinf(x)))
|
| 36 |
+
|
| 37 |
+
def eq_int(got, want):
|
| 38 |
+
return is_num(got) and float(got) == float(int(got)) and int(got) == want
|
| 39 |
+
|
| 40 |
+
def eq_num(got, want, tol):
|
| 41 |
+
return is_num(got) and abs(float(got) - float(want)) <= tol + 1e-12
|
| 42 |
+
|
| 43 |
+
def eq_str(got, want):
|
| 44 |
+
return isinstance(got, str) and got.strip().casefold() == str(want).strip().casefold()
|
| 45 |
+
|
| 46 |
+
def eq_item(got, want, kind, tol):
|
| 47 |
+
if kind == "int": return eq_int(got, want)
|
| 48 |
+
if kind == "number": return eq_num(got, want, tol)
|
| 49 |
+
return eq_str(got, want)
|
| 50 |
+
|
| 51 |
+
def run_check(answer, chk, validators):
|
| 52 |
+
t = chk["type"]
|
| 53 |
+
if t == "custom":
|
| 54 |
+
fn = getattr(validators, chk["name"], None) if validators else None
|
| 55 |
+
if fn is None: return 0.0
|
| 56 |
+
try: r = fn(answer, chk.get("args", {}))
|
| 57 |
+
except Exception: return 0.0
|
| 58 |
+
if r is True: return 1.0
|
| 59 |
+
if r is False or r is None: return 0.0
|
| 60 |
+
return max(0.0, min(1.0, float(r))) if is_num(r) else 0.0
|
| 61 |
+
got = lookup(answer, chk["path"])
|
| 62 |
+
if got is MISSING: return 0.0
|
| 63 |
+
want, tol = chk["value"], chk.get("abs_tol", 1e-9)
|
| 64 |
+
if t == "int": ok = eq_int(got, want)
|
| 65 |
+
elif t == "number": ok = eq_num(got, want, tol)
|
| 66 |
+
elif t == "str": ok = eq_str(got, want)
|
| 67 |
+
elif t == "bool": ok = isinstance(got, bool) and got is want
|
| 68 |
+
elif t == "list":
|
| 69 |
+
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))
|
| 70 |
+
elif t == "set":
|
| 71 |
+
kind = chk.get("item", "str")
|
| 72 |
+
if not isinstance(got, list): ok = False
|
| 73 |
+
else:
|
| 74 |
+
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)
|
| 75 |
+
g = [norm(x) for x in got]; w = [norm(x) for x in want]
|
| 76 |
+
ok = None not in g and len(g) == len(set(g)) and set(g) == set(w)
|
| 77 |
+
else: ok = False
|
| 78 |
+
return 1.0 if ok else 0.0
|
| 79 |
+
|
| 80 |
+
def grade(answer, checks, validators=None):
|
| 81 |
+
"""Return (reward, details). answer is the parsed JSON, or None if it could not be read."""
|
| 82 |
+
if not isinstance(answer, dict):
|
| 83 |
+
return 0.0, [{"check": c.get("path", c.get("name")), "score": 0.0} for c in checks]
|
| 84 |
+
total = sum(c.get("weight", 1.0) for c in checks)
|
| 85 |
+
got, details = 0.0, []
|
| 86 |
+
for c in checks:
|
| 87 |
+
s = run_check(answer, c, validators)
|
| 88 |
+
got += s * c.get("weight", 1.0)
|
| 89 |
+
details.append({"check": c.get("path", c.get("name")), "score": s})
|
| 90 |
+
return (round(got / total, 6) if total else 0.0), details
|
| 91 |
+
|
| 92 |
+
def main():
|
| 93 |
+
ap = argparse.ArgumentParser()
|
| 94 |
+
ap.add_argument("--answer", default="/app/answer.json")
|
| 95 |
+
ap.add_argument("--checks", default=os.path.join(os.path.dirname(os.path.abspath(__file__)), "checks.json"))
|
| 96 |
+
ap.add_argument("--out", default="/logs/verifier")
|
| 97 |
+
a = ap.parse_args()
|
| 98 |
+
checks = json.load(open(a.checks))
|
| 99 |
+
try:
|
| 100 |
+
with open(a.answer) as f: answer = json.load(f)
|
| 101 |
+
except Exception:
|
| 102 |
+
answer = None
|
| 103 |
+
validators = None
|
| 104 |
+
here = os.path.dirname(os.path.abspath(__file__))
|
| 105 |
+
if os.path.exists(os.path.join(here, "validators.py")):
|
| 106 |
+
sys.path.insert(0, here)
|
| 107 |
+
import validators # noqa
|
| 108 |
+
reward, details = grade(answer, checks, validators)
|
| 109 |
+
os.makedirs(a.out, exist_ok=True)
|
| 110 |
+
with open(os.path.join(a.out, "reward.txt"), "w") as f: f.write(str(reward))
|
| 111 |
+
with open(os.path.join(a.out, "reward.json"), "w") as f:
|
| 112 |
+
json.dump({"reward": reward, "solved": reward == 1.0, "passed": sum(1 for d in details if d["score"] == 1.0), "checks": len(details)}, f)
|
| 113 |
+
print("reward", reward)
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
main()
|
tasks/psychometrics/psychometrics-norms-d1-t0024/environment/Dockerfile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.12-slim
|
| 2 |
+
WORKDIR /app
|
| 3 |
+
COPY workdir/ /app/
|
tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/clients.csv
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
client_id,sex,age,raw_score,referral
|
| 2 |
+
C363,F,53,44,self
|
| 3 |
+
C102,M,38,28,self
|
| 4 |
+
C368,M,60,48,self
|
| 5 |
+
C259,M,48,40,GP
|
| 6 |
+
C246,F,52,26,GP
|
| 7 |
+
C238,F,53,35,self
|
tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/norms.csv
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scale,scale_name,age_band,sex,n,mean,sd
|
| 2 |
+
FAT,Fatigue Scale,60-74,M,625,32.2,5.9
|
| 3 |
+
FAT,Fatigue Scale,30-44,F,559,26.3,4.1
|
| 4 |
+
FAT,Fatigue Scale,45-59,F,504,30.9,4.8
|
| 5 |
+
FAT,Fatigue Scale,30-44,M,643,29.0,8.2
|
| 6 |
+
FAT,Fatigue Scale,45-59,M,131,26.2,6.3
|
| 7 |
+
FAT,Fatigue Scale,60-74,F,284,26.8,7.2
|
| 8 |
+
FAT,Fatigue Scale,18-29,M,155,28.1,6.5
|
| 9 |
+
FAT,Fatigue Scale,18-29,F,288,27.1,5.1
|
tasks/psychometrics/psychometrics-norms-d1-t0024/environment/workdir/norms_guide.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Using the norm table
|
| 2 |
+
|
| 3 |
+
The scales and norms are invented for practice.
|
| 4 |
+
|
| 5 |
+
`norms.csv` gives the mean and standard deviation (SD) of the raw score in each norm group. A norm group is an age band and a sex. The age bands are inclusive: `30-44` covers ages 30 to 44.
|
| 6 |
+
|
| 7 |
+
For a client with raw score `x` in a norm group with mean `M` and standard deviation `SD`:
|
| 8 |
+
|
| 9 |
+
z = (x - M) / SD
|
| 10 |
+
T score = 50 + 10 * z
|
| 11 |
+
percentile = 100 * Phi(z), Phi(z) = 0.5 * (1 + erf(z / sqrt(2)))
|
| 12 |
+
|
| 13 |
+
`erf` is the error function (`math.erf` in Python). Use the unrounded z in the T score and the percentile.
|
tasks/psychometrics/psychometrics-norms-d1-t0024/instruction.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Norm-referenced scores
|
| 2 |
+
|
| 3 |
+
`clients.csv` holds raw scores on the Fatigue Scale. `norms.csv` is the norm table and `norms_guide.md` says how to use it.
|
| 4 |
+
|
| 5 |
+
Convert every raw score using the norm group that matches the client's age and sex.
|
| 6 |
+
|
| 7 |
+
Write `answer.json` in this folder, a JSON object with:
|
| 8 |
+
|
| 9 |
+
- `clients`: an object keyed by client id. Each value has
|
| 10 |
+
- `z`: the z score, 2 decimals.
|
| 11 |
+
- `t_score`: the T score, 1 decimal.
|
| 12 |
+
- `percentile`: the percentile rank, 1 decimal.
|
| 13 |
+
|
| 14 |
+
Numbers are accepted within one unit of the last decimal place asked for.
|
tasks/psychometrics/psychometrics-norms-d1-t0024/solution/oracle.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Reference solution for Psychometrics. Works only from the files in the current directory."""
|
| 3 |
+
import csv, json, math, os, re
|
| 4 |
+
from decimal import Decimal, ROUND_HALF_UP
|
| 5 |
+
from fractions import Fraction
|
| 6 |
+
|
| 7 |
+
def rows(name):
|
| 8 |
+
with open(name, newline="") as f: return list(csv.DictReader(f))
|
| 9 |
+
|
| 10 |
+
def text(name):
|
| 11 |
+
with open(name) as f: return f.read()
|
| 12 |
+
|
| 13 |
+
def nums(s): return [int(x) for x in re.findall(r"\d+", s)]
|
| 14 |
+
|
| 15 |
+
# ---------------------------------------------------------------- scoring
|
| 16 |
+
def scoring():
|
| 17 |
+
man = text("scoring_manual.md")
|
| 18 |
+
top = int(re.search(r"scale from 0 to (\d+)", man).group(1))
|
| 19 |
+
reverse = set(nums(re.search(r"^Reverse-keyed items:(.*)$", man, re.M).group(1)))
|
| 20 |
+
subs, bands = [], []
|
| 21 |
+
for line in man.splitlines():
|
| 22 |
+
cells = [c.strip() for c in line.strip().strip("|").split("|")]
|
| 23 |
+
if len(cells) == 3 and re.match(r"^[A-Z]{3}$", cells[1]): subs.append((cells[1], nums(cells[2])))
|
| 24 |
+
if len(cells) == 2 and re.match(r"^\d+ to \d+$", cells[1]): bands.append((cells[0], nums(cells[1])))
|
| 25 |
+
missing_codes = {""}
|
| 26 |
+
if "code 9" in man: missing_codes.add("9")
|
| 27 |
+
m = re.search(r"Item (\d+) is the risk item", man)
|
| 28 |
+
risk = int(m.group(1)) if m else None
|
| 29 |
+
latest = {}
|
| 30 |
+
for r in rows("responses.csv"):
|
| 31 |
+
if r["client_id"] not in latest or r["administered"] > latest[r["client_id"]]["administered"]: latest[r["client_id"]] = r
|
| 32 |
+
ans = {"clients": {}, "moderate_or_severe": [], "unscorable": [], "risk_flagged": []}
|
| 33 |
+
for cid, r in latest.items():
|
| 34 |
+
out = {"subscales": {}}
|
| 35 |
+
ok = True
|
| 36 |
+
for code, items in subs:
|
| 37 |
+
vals = []
|
| 38 |
+
for i in items:
|
| 39 |
+
cell = r["item_%02d" % i].strip()
|
| 40 |
+
if cell in missing_codes: continue
|
| 41 |
+
v = int(cell)
|
| 42 |
+
vals.append(top - v if i in reverse else v)
|
| 43 |
+
n_miss = len(items) - len(vals)
|
| 44 |
+
if Fraction(n_miss, len(items)) > Fraction(20, 100):
|
| 45 |
+
ok = False; ans["unscorable"].append("%s:%s" % (cid, code)); continue
|
| 46 |
+
if n_miss == 0: out["subscales"][code] = sum(vals)
|
| 47 |
+
else:
|
| 48 |
+
pro = Decimal(sum(vals)) * Decimal(len(items)) / Decimal(len(vals))
|
| 49 |
+
out["subscales"][code] = int(pro.quantize(Decimal(1), rounding=ROUND_HALF_UP))
|
| 50 |
+
if ok:
|
| 51 |
+
total = sum(out["subscales"].values())
|
| 52 |
+
out["total"] = total
|
| 53 |
+
for name, (lo, hi) in bands:
|
| 54 |
+
if lo <= total <= hi: out["band"] = name
|
| 55 |
+
if out["band"] in ("moderate", "severe"): ans["moderate_or_severe"].append(cid)
|
| 56 |
+
if risk is not None:
|
| 57 |
+
cell = r["item_%02d" % risk].strip()
|
| 58 |
+
if cell not in missing_codes and int(cell) >= 2: ans["risk_flagged"].append(cid)
|
| 59 |
+
ans["clients"][cid] = out
|
| 60 |
+
return ans
|
| 61 |
+
|
| 62 |
+
# ---------------------------------------------------------------- reliable change
|
| 63 |
+
def reliable_change():
|
| 64 |
+
inst = dict((r["code"], r) for r in rows("instruments.csv"))
|
| 65 |
+
ans = {"instruments": {}, "results": {}, "recovered": [], "deteriorated": [], "no_post": []}
|
| 66 |
+
counts = {}
|
| 67 |
+
for r in rows("outcomes.csv"):
|
| 68 |
+
code, cid = r["instrument"], r["client_id"]
|
| 69 |
+
i = inst[code]
|
| 70 |
+
se = float(i["sd"]) * math.sqrt(1 - float(i["reliability"]))
|
| 71 |
+
sdiff = math.sqrt(2 * se * se)
|
| 72 |
+
ans["instruments"].setdefault(code, {})["s_diff"] = round(sdiff, 2)
|
| 73 |
+
if r["post_score"].strip() == "":
|
| 74 |
+
ans["no_post"].append("%s:%s" % (cid, code)); continue
|
| 75 |
+
pre, post, cut = float(r["pre_score"]), float(r["post_score"]), float(i["clinical_cutoff"])
|
| 76 |
+
x = (post - pre) / sdiff
|
| 77 |
+
up_is_bad = i.get("direction", "higher_is_worse") == "higher_is_worse"
|
| 78 |
+
if up_is_bad: pre_clin, post_clin, better = pre >= cut, post >= cut, post < pre
|
| 79 |
+
else: pre_clin, post_clin, better = pre <= cut, post <= cut, post > pre
|
| 80 |
+
if not (x > 1.96 or x < -1.96): status = "unchanged"
|
| 81 |
+
elif not better: status = "deteriorated"
|
| 82 |
+
elif pre_clin and not post_clin: status = "recovered"
|
| 83 |
+
else: status = "improved"
|
| 84 |
+
ans["results"].setdefault(cid, {})[code] = {"rci": round(x, 2), "status": status}
|
| 85 |
+
if status in ("recovered", "deteriorated"): ans[status].append("%s:%s" % (cid, code))
|
| 86 |
+
c = counts.setdefault(code, [0, 0]); c[0] += 1; c[1] += status == "recovered"
|
| 87 |
+
for code, (n, rec) in counts.items():
|
| 88 |
+
ans["instruments"][code]["recovered_pct"] = round(100.0 * rec / n, 1)
|
| 89 |
+
return ans
|
| 90 |
+
|
| 91 |
+
# ---------------------------------------------------------------- norms
|
| 92 |
+
def norms():
|
| 93 |
+
table = {}
|
| 94 |
+
for r in rows("norms.csv"):
|
| 95 |
+
lo, hi = r["age_band"].split("-")
|
| 96 |
+
table.setdefault((r["scale"], r["sex"]), []).append((int(lo), int(hi), float(r["mean"]), float(r["sd"])))
|
| 97 |
+
only_scale = sorted(set(k[0] for k in table))[0]
|
| 98 |
+
guide = text("norms_guide.md")
|
| 99 |
+
with_ranges = "## Ranges" in guide
|
| 100 |
+
ans = {"clients": {}, "no_norm_group": [], "high_or_above": []}
|
| 101 |
+
for r in rows("clients.csv"):
|
| 102 |
+
cid = r["client_id"]
|
| 103 |
+
if "age" in r: age = int(r["age"])
|
| 104 |
+
else:
|
| 105 |
+
by, bm, bd = [int(x) for x in r["date_of_birth"].split("-")]
|
| 106 |
+
ty, tm, td = [int(x) for x in r["test_date"].split("-")]
|
| 107 |
+
age = ty - by
|
| 108 |
+
if tm < bm or (tm == bm and td < bd): age -= 1
|
| 109 |
+
scale = r.get("scale") or only_scale
|
| 110 |
+
group = [g for g in table[(scale, r["sex"])] if g[0] <= age <= g[1]]
|
| 111 |
+
if not group:
|
| 112 |
+
if cid not in ans["no_norm_group"]: ans["no_norm_group"].append(cid)
|
| 113 |
+
continue
|
| 114 |
+
z = (float(r["raw_score"]) - group[0][2]) / group[0][3]
|
| 115 |
+
t = 50 + 10 * z
|
| 116 |
+
res = {"z": round(z, 2), "t_score": round(t, 1), "percentile": round(50 * (1 + math.erf(z / math.sqrt(2))), 1)}
|
| 117 |
+
if with_ranges:
|
| 118 |
+
res["range"] = "very-high" if t >= 70 else "high" if t >= 60 else "average" if t >= 40 else "low"
|
| 119 |
+
if t >= 60: ans["high_or_above"].append("%s:%s" % (cid, scale))
|
| 120 |
+
c = ans["clients"].setdefault(cid, {"age": age, "scales": {}})
|
| 121 |
+
c["scales"][scale] = res
|
| 122 |
+
else:
|
| 123 |
+
res["age"] = age
|
| 124 |
+
ans["clients"][cid] = res
|
| 125 |
+
return ans
|
| 126 |
+
|
| 127 |
+
# ---------------------------------------------------------------- reliability
|
| 128 |
+
def variance(xs):
|
| 129 |
+
m = sum(xs) / len(xs)
|
| 130 |
+
return sum((x - m) * (x - m) for x in xs) / (len(xs) - 1)
|
| 131 |
+
|
| 132 |
+
def corr(xs, ys):
|
| 133 |
+
n = len(xs); mx = sum(xs) / n; my = sum(ys) / n
|
| 134 |
+
cov = sum((x - mx) * (y - my) for x, y in zip(xs, ys)) / (n - 1)
|
| 135 |
+
return cov / math.sqrt(variance(xs) * variance(ys))
|
| 136 |
+
|
| 137 |
+
def alpha_of(cols):
|
| 138 |
+
k = len(cols)
|
| 139 |
+
totals = [sum(c[i] for c in cols) for i in range(len(cols[0]))]
|
| 140 |
+
return k / (k - 1) * (1 - sum(variance(c) for c in cols) / variance(totals))
|
| 141 |
+
|
| 142 |
+
def reliability():
|
| 143 |
+
notes = text("reliability_notes.md")
|
| 144 |
+
m = re.search(r"^Reverse-keyed items:([^.]*)\.", notes, re.M)
|
| 145 |
+
reverse = set(re.findall(r"q\d+", m.group(1))) if m else set()
|
| 146 |
+
data = rows("item_responses.csv")
|
| 147 |
+
qs = sorted([c for c in data[0] if re.match(r"^q\d+$", c)], key=lambda c: int(c[1:]))
|
| 148 |
+
complete = [r for r in data if all(r[q].strip() != "" for q in qs)]
|
| 149 |
+
cols = dict((q, [(6 - int(r[q])) if q in reverse else int(r[q]) for r in complete]) for q in qs)
|
| 150 |
+
alpha = alpha_of([cols[q] for q in qs])
|
| 151 |
+
totals = [sum(cols[q][i] for q in qs) for i in range(len(complete))]
|
| 152 |
+
ans = {"items": {}, "total_variance": round(variance(totals), 3), "alpha": round(alpha, 3), "weak_items": [], "n_used": len(complete)}
|
| 153 |
+
best, best_alpha = None, None
|
| 154 |
+
for q in qs:
|
| 155 |
+
rest = [t - x for t, x in zip(totals, cols[q])]
|
| 156 |
+
rr = corr(cols[q], rest)
|
| 157 |
+
a_del = alpha_of([cols[p] for p in qs if p != q])
|
| 158 |
+
ans["items"][q] = {"mean": round(sum(cols[q]) / len(cols[q]), 2), "variance": round(variance(cols[q]), 3),
|
| 159 |
+
"item_rest_r": round(rr, 3), "alpha_if_deleted": round(a_del, 3)}
|
| 160 |
+
if rr < 0.30: ans["weak_items"].append(q)
|
| 161 |
+
if best is None or a_del > best_alpha: best, best_alpha = q, a_del
|
| 162 |
+
ans["best_item_to_drop"] = best
|
| 163 |
+
ans["alpha_gain"] = round(best_alpha - alpha, 3)
|
| 164 |
+
return ans
|
| 165 |
+
|
| 166 |
+
if os.path.exists("scoring_manual.md"): answer = scoring()
|
| 167 |
+
elif os.path.exists("rci_rules.md"): answer = reliable_change()
|
| 168 |
+
elif os.path.exists("norms.csv"): answer = norms()
|
| 169 |
+
else: answer = reliability()
|
| 170 |
+
with open("answer.json", "w") as f: json.dump(answer, f, indent=1)
|