File size: 3,241 Bytes
ea29ada 8d23ae4 e8598c4 ea29ada e8598c4 ea29ada 4895777 ea29ada cb32d35 ea29ada | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """
Post-hoc data-quality flags for TreasureHuntForTwo. Reads exported *_data.csv files and
surfaces participants worth a closer look. It does NOT auto-exclude anyone, it just flags.
Flags (all heuristic, tune the thresholds to your study):
incomplete < 12 main houses finished
fast_rt median choice RT < 350 ms
many_fast > 40% of choices faster than 250 ms
side_bias chose the same side (left/right) on > 90% of trials
uv run python quality_flags.py [folder ...] # default: downloaded_data + data
Writes quality_flags.csv.
"""
import csv
import glob
import os
import statistics
import sys
FAST_MS, MANY_FAST_PCT, SIDE_BIAS = 350, 40, 0.90
def _int(x):
return int(x) if str(x).lstrip("-").isdigit() else None
def analyze(path):
rows = list(csv.DictReader(open(path)))
trials = [r for r in rows if r["row_type"] == "trial" and r["blockIndex"] != "practice"]
if not trials:
return None
summ = next((r for r in rows if r["row_type"] == "summary"), {})
rts = [v for r in trials if (v := _int(r["choice_rt_ms"])) is not None]
sides = [r["chosen_side"] for r in trials]
n_house = len(set(r["blockIndex"] for r in trials))
med_rt = round(statistics.median(rts)) if rts else 0
pct_fast = round(100 * sum(1 for x in rts if x < 250) / len(rts)) if rts else 0
side_bias = round(max(sides.count("left"), sides.count("right")) / len(sides), 2) if sides else 0
dur = float(summ.get("duration_min") or 0)
flags = []
if str(summ.get("dev_mode")) == "1":
flags.append("DEV(exclude)")
if n_house < 12:
flags.append(f"incomplete({n_house}/12)")
if med_rt and med_rt < FAST_MS:
flags.append(f"fast_rt({med_rt}ms)")
if pct_fast > MANY_FAST_PCT:
flags.append(f"many_fast({pct_fast}%)")
if side_bias > SIDE_BIAS:
flags.append(f"side_bias({int(side_bias*100)}%)")
return dict(pid=summ.get("pid") or trials[0]["pid"], gid=summ.get("gid", ""),
houses=n_house, med_rt=med_rt, pct_fast=pct_fast, side_bias=side_bias,
dur_min=round(dur, 1),
flags=";".join(flags) or "ok")
def main(folders):
paths = []
for fo in folders:
paths += glob.glob(os.path.join(fo, "**", "*_data.csv"), recursive=True)
paths = sorted(set(paths))
if not paths:
print("No *_data.csv files found in:", folders)
return
results = [r for r in (analyze(p) for p in paths) if r]
print(f"{'pid':7}{'gid':4}{'houses':>7}{'medRT':>7}{'%fast':>6}{'sideB':>7}{'attn':>6}{'min':>6} flags")
for r in results:
print(f"{r['pid']:7}{str(r['gid']):4}{r['houses']:>7}{r['med_rt']:>7}{r['pct_fast']:>6}"
f"{r['side_bias']:>7}{r['attn']:>6}{r['dur_min']:>6} {r['flags']}")
flagged = [r for r in results if r["flags"] != "ok"]
print(f"\n{len(flagged)}/{len(results)} participant(s) flagged for review (not auto-excluded).")
with open("quality_flags.csv", "w", newline="") as f:
wr = csv.DictWriter(f, fieldnames=list(results[0].keys()))
wr.writeheader()
wr.writerows(results)
print("wrote quality_flags.csv")
if __name__ == "__main__":
main(sys.argv[1:] or ["downloaded_data", "data"])
|