| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | from __future__ import annotations |
| 24 | |
| 25 | import argparse |
| 26 | import json |
| 27 | import math |
| 28 | import pathlib |
| 29 | import re |
| 30 | import statistics |
| 31 | import sys |
| 32 | |
| 33 | ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 34 | |
| 35 | |
| 36 | MIN_RESPONSES = 30 |
| 37 | |
| 38 | _ID = re.compile(r"(\w+):⟨([^⟩]+)⟩") |
| 39 | |
| 40 | |
| 41 | def _field(line: str, name: str) -> str | None: |
| 42 | |
| 43 | i = line.find(" %s: " % name) |
| 44 | if i < 0: |
| 45 | return None |
| 46 | i += len(name) + 3 |
| 47 | if line[i] == "'": |
| 48 | j = line.find("'", i + 1) |
| 49 | return line[i + 1:j] |
| 50 | j = i |
| 51 | while j < len(line) and line[j] not in ",}": |
| 52 | j += 1 |
| 53 | return line[i:j].strip() |
| 54 | |
| 55 | |
| 56 | def _rid(raw: str | None) -> str | None: |
| 57 | if not raw: |
| 58 | return None |
| 59 | m = _ID.match(raw) |
| 60 | return "%s:%s" % (m.group(1), m.group(2)) if m else None |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | def _norm_sf(z: float) -> float: |
| 68 | |
| 69 | return 0.5 * math.erfc(abs(z) / math.sqrt(2.0)) |
| 70 | |
| 71 | |
| 72 | def welch(a: list[float], b: list[float]) -> dict: |
| 73 | |
| 74 | na, nb = len(a), len(b) |
| 75 | if na < 2 or nb < 2: |
| 76 | return {"n_a": na, "n_b": nb, "t": None, "p": None, "d": None} |
| 77 | ma, mb = statistics.fmean(a), statistics.fmean(b) |
| 78 | va, vb = statistics.variance(a), statistics.variance(b) |
| 79 | se = math.sqrt(va / na + vb / nb) |
| 80 | t = (ma - mb) / se if se else 0.0 |
| 81 | pooled = math.sqrt(((na - 1) * va + (nb - 1) * vb) / (na + nb - 2)) |
| 82 | return { |
| 83 | "n_a": na, "n_b": nb, |
| 84 | "ort_a": round(ma, 4), "ort_b": round(mb, 4), |
| 85 | "fark": round(ma - mb, 4), |
| 86 | "t": round(t, 3), |
| 87 | "p": _fmt_p(2.0 * _norm_sf(t)), |
| 88 | "d": round((ma - mb) / pooled, 3) if pooled else None, |
| 89 | } |
| 90 | |
| 91 | |
| 92 | def two_prop(k1: int, n1: int, k2: int, n2: int) -> dict: |
| 93 | |
| 94 | if n1 == 0 or n2 == 0: |
| 95 | return {"oran_1": None, "oran_2": None, "fark": None, "z": None, "p": None} |
| 96 | p1, p2 = k1 / n1, k2 / n2 |
| 97 | pool = (k1 + k2) / (n1 + n2) |
| 98 | se = math.sqrt(pool * (1 - pool) * (1 / n1 + 1 / n2)) |
| 99 | z = (p1 - p2) / se if se else 0.0 |
| 100 | return {"oran_1": round(p1, 4), "oran_2": round(p2, 4), |
| 101 | "fark": round(p1 - p2, 4), "n_1": n1, "n_2": n2, |
| 102 | "z": round(z, 2), "p": _fmt_p(2.0 * _norm_sf(z))} |
| 103 | |
| 104 | |
| 105 | def _fmt_p(p: float) -> float | str: |
| 106 | return "<1e-12" if p < 1e-12 else round(p, 6) |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | def load(seed_dir: pathlib.Path) -> dict: |
| 114 | man = json.loads((seed_dir / "_seed_manifest.json").read_text(encoding="utf-8")) |
| 115 | items = {i["question"]: i for i in man["items"]} |
| 116 | students = {s["user"]: s for s in man["students"]} |
| 117 | |
| 118 | |
| 119 | correct: dict[str, str] = {} |
| 120 | for line in (seed_dir / "09_sorular.surql").open(encoding="utf-8"): |
| 121 | if not line.startswith("{ id: exam_question:"): |
| 122 | continue |
| 123 | qid = _rid(_field(line, "id")) |
| 124 | key = _field(line, "correct") |
| 125 | if qid and key and key != "NONE": |
| 126 | correct[qid] = key |
| 127 | |
| 128 | |
| 129 | per_item: dict[str, dict] = {} |
| 130 | per_student: dict[str, dict] = {} |
| 131 | for line in (seed_dir / "11_exam_answer.surql").open(encoding="utf-8"): |
| 132 | if not line.startswith("{ id: exam_answer:"): |
| 133 | continue |
| 134 | sel = _field(line, "selected") |
| 135 | if not sel or sel == "NONE": |
| 136 | continue |
| 137 | qid = _rid(_field(line, "question")) |
| 138 | uid = _rid(_field(line, "user")) |
| 139 | if qid not in correct: |
| 140 | continue |
| 141 | row = per_item.setdefault(qid, {"n": 0, "sec": {}}) |
| 142 | row["n"] += 1 |
| 143 | row["sec"][sel] = row["sec"].get(sel, 0) + 1 |
| 144 | dims = (items.get(qid) or {}).get("dims") |
| 145 | if not dims or uid is None: |
| 146 | continue |
| 147 | ok = 1 if sel == correct[qid] else 0 |
| 148 | st = per_student.setdefault(uid, {}) |
| 149 | for dim, label in dims.items(): |
| 150 | cell = st.setdefault((dim, label), [0, 0]) |
| 151 | cell[0] += 1 |
| 152 | cell[1] += ok |
| 153 | return {"items": items, "students": students, "correct": correct, |
| 154 | "per_item": per_item, "per_student": per_student} |
| 155 | |
| 156 | |
| 157 | def item_rows(data: dict) -> list[dict]: |
| 158 | |
| 159 | out = [] |
| 160 | for qid, row in data["per_item"].items(): |
| 161 | if row["n"] < MIN_RESPONSES: |
| 162 | continue |
| 163 | gold = data["items"].get(qid) |
| 164 | if not gold or not gold.get("dims"): |
| 165 | continue |
| 166 | key = data["correct"][qid] |
| 167 | n = row["n"] |
| 168 | n_ok = row["sec"].get(key, 0) |
| 169 | dist = {c: k for c, k in row["sec"].items() if c != key} |
| 170 | trap = gold.get("trap_choice") |
| 171 | out.append({ |
| 172 | "question": qid, |
| 173 | "dims": gold["dims"], |
| 174 | "n": n, |
| 175 | "p": n_ok / n, |
| 176 | "tuzak_pay": (row["sec"].get(trap, 0) / n) if trap else None, |
| 177 | "en_cekici_celdirici_pay": (max(dist.values()) / n) if dist else 0.0, |
| 178 | }) |
| 179 | return out |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | def measure(data: dict) -> dict: |
| 187 | rows = item_rows(data) |
| 188 | by = lambda dim, label: [r["p"] for r in rows if r["dims"][dim] == label] |
| 189 | |
| 190 | |
| 191 | lv = {l: by("bilissel_talep", l) for l in ("hatirlama", "uygulama", "analiz")} |
| 192 | m1 = { |
| 193 | "madde_sayisi": {l: len(v) for l, v in lv.items()}, |
| 194 | "ortalama_p": {l: round(statistics.fmean(v), 4) for l, v in lv.items() if v}, |
| 195 | "analiz_vs_hatirlama": welch(lv["analiz"], lv["hatirlama"]), |
| 196 | "analiz_vs_uygulama": welch(lv["analiz"], lv["uygulama"]), |
| 197 | "uygulama_vs_hatirlama": welch(lv["uygulama"], lv["hatirlama"]), |
| 198 | } |
| 199 | |
| 200 | |
| 201 | trap_rows = [r for r in rows if r["dims"]["dikkat_tuzagi"] == "var" |
| 202 | and r["tuzak_pay"] is not None] |
| 203 | plain_rows = [r for r in rows if r["dims"]["dikkat_tuzagi"] == "yok"] |
| 204 | trap_share = [r["tuzak_pay"] for r in trap_rows] |
| 205 | plain_best = [r["en_cekici_celdirici_pay"] for r in plain_rows] |
| 206 | m2 = { |
| 207 | "tuzakli_madde": len(trap_rows), |
| 208 | "tuzaksiz_madde": len(plain_rows), |
| 209 | "tuzak_sik_ortalama_pay": round(statistics.fmean(trap_share), 4) if trap_share else None, |
| 210 | "tuzaksizda_en_cekici_celdirici_ortalama_pay": |
| 211 | round(statistics.fmean(plain_best), 4) if plain_best else None, |
| 212 | "karsilastirma": welch(trap_share, plain_best), |
| 213 | "tuzak_sik_en_cekici_oldugu_madde_pct": round(100.0 * sum( |
| 214 | 1 for r in trap_rows |
| 215 | if r["tuzak_pay"] >= r["en_cekici_celdirici_pay"]) / max(1, len(trap_rows)), 2), |
| 216 | } |
| 217 | |
| 218 | |
| 219 | ry = {l: by("okuma_yuku", l) for l in ("dusuk", "yuksek")} |
| 220 | m3 = { |
| 221 | "madde_sayisi": {l: len(v) for l, v in ry.items()}, |
| 222 | "ortalama_p": {l: round(statistics.fmean(v), 4) for l, v in ry.items() if v}, |
| 223 | "yuksek_vs_dusuk": welch(ry["yuksek"], ry["dusuk"]), |
| 224 | } |
| 225 | |
| 226 | ad = {l: by("adim_sayisi", l) for l in ("tek_adim", "cok_adim")} |
| 227 | m3["adim_sayisi_ortalama_p"] = {l: round(statistics.fmean(v), 4) |
| 228 | for l, v in ad.items() if v} |
| 229 | m3["cok_adim_vs_tek_adim"] = welch(ad["cok_adim"], ad["tek_adim"]) |
| 230 | |
| 231 | |
| 232 | per_arch: dict[str, dict] = {} |
| 233 | for uid, cells in data["per_student"].items(): |
| 234 | arch = (data["students"].get(uid) or {}).get("archetype") |
| 235 | if not arch: |
| 236 | continue |
| 237 | tgt = per_arch.setdefault(arch, {}) |
| 238 | for key, (n, k) in cells.items(): |
| 239 | cell = tgt.setdefault(key, [0, 0]) |
| 240 | cell[0] += n |
| 241 | cell[1] += k |
| 242 | |
| 243 | contrasts = [ |
| 244 | ("bilissel_talep", "hatirlama", "analiz"), |
| 245 | ("dikkat_tuzagi", "var", "yok"), |
| 246 | ("okuma_yuku", "yuksek", "dusuk"), |
| 247 | ] |
| 248 | table = {} |
| 249 | for arch in sorted(per_arch): |
| 250 | cells = per_arch[arch] |
| 251 | rowd = {"profil": "sapma yok (kontrol)"} |
| 252 | prof = ARCH_PROFILE.get(arch) |
| 253 | if prof: |
| 254 | rowd["profil"] = prof |
| 255 | for dim, a, b in contrasts: |
| 256 | na, ka = cells.get((dim, a), [0, 0]) |
| 257 | nb, kb = cells.get((dim, b), [0, 0]) |
| 258 | rowd["%s: %s - %s" % (dim, a, b)] = two_prop(ka, na, kb, nb) |
| 259 | table[arch] = rowd |
| 260 | |
| 261 | kontrol = [a for a in table if a in CONTROL_ARCHETYPES] |
| 262 | sapmali = [a for a in table if a not in CONTROL_ARCHETYPES] |
| 263 | |
| 264 | |
| 265 | |
| 266 | ozet = {} |
| 267 | for dim, a, b in contrasts: |
| 268 | key = "%s: %s - %s" % (dim, a, b) |
| 269 | ks = [table[x][key]["fark"] for x in kontrol if table[x][key]["fark"] is not None] |
| 270 | base = statistics.fmean(ks) if ks else None |
| 271 | ozet[key] = { |
| 272 | "kontrol_ortalama_fark": round(base, 4) if base is not None else None, |
| 273 | "kontrol_azami_mutlak_sapma": round( |
| 274 | max(abs(v - base) for v in ks), 4) if ks else None, |
| 275 | } |
| 276 | if base is None: |
| 277 | continue |
| 278 | for arch in table: |
| 279 | cell = table[arch][key] |
| 280 | if cell["fark"] is not None: |
| 281 | cell["kontrole_gore_sapma"] = round(cell["fark"] - base, 4) |
| 282 | |
| 283 | return { |
| 284 | "olcum_1_bilissel_talep_p": m1, |
| 285 | "olcum_2_dikkat_tuzagi": m2, |
| 286 | "olcum_3_okuma_yuku_p": m3, |
| 287 | "olcum_4_arketip_boyut": {"tablo": table, "sapmali": sapmali}, |
| 288 | "olcum_5_kontrol": {"kontrol_arketipler": kontrol, "ozet": ozet}, |
| 289 | "kapsam": {"madde_esigi": MIN_RESPONSES, "olculen_madde": len(rows), |
| 290 | "toplam_madde": len(data["per_item"])}, |
| 291 | } |
| 292 | |
| 293 | |
| 294 | ARCH_PROFILE = { |
| 295 | "A4": "aceleci: analizde guclu, dikkat tuzagina yakalanan", |
| 296 | "A7": "okuma guclugu: yuksek okuma yukunde dusen", |
| 297 | "A8": "ezberci: hatirlamada guclu, analizde zayif", |
| 298 | } |
| 299 | CONTROL_ARCHETYPES = {"A1", "A2", "A3", "A5", "A6"} |
| 300 | |
| 301 | |
| 302 | def main(argv=None) -> int: |
| 303 | ap = argparse.ArgumentParser() |
| 304 | ap.add_argument("--seed-dir", default=str(ROOT / "seed")) |
| 305 | ap.add_argument("--json", default=None) |
| 306 | args = ap.parse_args(argv) |
| 307 | |
| 308 | data = load(pathlib.Path(args.seed_dir)) |
| 309 | out = measure(data) |
| 310 | text = json.dumps(out, ensure_ascii=False, indent=1) |
| 311 | if args.json: |
| 312 | pathlib.Path(args.json).write_text(text, encoding="utf-8") |
| 313 | sys.stdout.reconfigure(encoding="utf-8") |
| 314 | print(text) |
| 315 | return 0 |
| 316 | |
| 317 | |
| 318 | if __name__ == "__main__": |
| 319 | raise SystemExit(main()) |