| import re, json, os |
| from datasets import load_dataset |
| HOME=os.environ["HOME"] |
| MEDX=f"{HOME}/pentabrid/datasets/MedXpertQA/Text/test.jsonl" |
| OUT=f"{HOME}/pentabrid/datasets/mcr_clinician_baseline.jsonl" |
| def dequote(s): |
| s=str(s) |
| for ch in ['\u201c','\u201d','\u201e','\u201f','\u2033','"']: s=s.replace(ch,'') |
| return re.sub(r'[ \t]+',' ',s).strip() |
| def words(s): return re.findall(r"[a-z0-9]+", str(s).lower()) |
| def grams(t,n): return set(tuple(t[i:i+n]) for i in range(len(t)-n+1)) if len(t)>=n else set() |
| G13=set(); nref=0 |
| with open(MEDX) as f: |
| for line in f: |
| line=line.strip() |
| if not line: continue |
| r=json.loads(line); parts=[str(v) for v in r.values() if isinstance(v,str)] |
| for v in r.values(): |
| if isinstance(v,dict): parts+=[str(x) for x in v.values()] |
| G13|=grams(words(" ".join(parts)),13); nref+=1 |
| print(f"decontam ref: {nref} MedXpertQA questions, {len(G13)} 13-grams") |
| mcr=load_dataset("zou-lab/MedCaseReasoning",split="train") |
| kept=[]; dc=0 |
| for ex in mcr: |
| cp=str(ex.get("case_prompt","")).strip(); dr=dequote(ex.get("diagnostic_reasoning","")); dx=str(ex.get("final_diagnosis","")).strip() |
| if not cp or not dr or not dx: continue |
| if grams(words(cp),13)&G13: dc+=1; continue |
| instr=cp+"\n\nReason through the differential diagnosis step by step, then give the single most likely diagnosis on a final line as 'Diagnosis: <name>'." |
| output="<think>\n"+dr+"\n</think>\n\nDiagnosis: "+dx |
| kept.append({"instruction":instr,"input":"","output":output,"source":"medcasereasoning"}) |
| with open(OUT,"w") as f: |
| for r in kept: f.write(json.dumps(r,ensure_ascii=False)+"\n") |
| print(f"kept={len(kept)} dropped_contam={dc} -> {OUT} ({os.path.getsize(OUT)/1e6:.1f} MB)") |
|
|