"""Analyze existing records only; an unparsed result is non-credit, not wrong.""" import json,math,random,statistics,sys from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parent)) from pilot_core import read_jsonl,completed def wilson(k,n): if not n:return None z=1.95996398454;p=k/n;den=1+z*z/n;center=(p+z*z/(2*n))/den;half=z*math.sqrt(p*(1-p)/n+z*z/(4*n*n))/den;return [max(0,center-half),min(1,center+half)] def analyze(records): if not records:raise ValueError('No records') completed(records,records[0]['run_hash']); groups={} for r in records:groups.setdefault(r['condition'],[]).append(r) out={'n_records':len(records),'conditions':{},'paired':{}} for c,rs in groups.items(): valid=[r for r in rs if not r.get('error')];n=len(valid);correct=sum(r['score']['correct'] is True for r in valid) lengths=[r['token_count'] for r in valid] out['conditions'][c]={'n':n,'generation_errors':len(rs)-n,'correct':correct,'accuracy_noncredit_abstentions':correct/n if n else None,'accuracy_wilson_95':wilson(correct,n),'unparsed_or_abstained':sum(r['score']['correct'] is None for r in valid),'loop_count':sum(r['loop']['loop_detected'] for r in valid),'truncation_count':sum(r['truncated'] for r in valid),'tokens':{'min':min(lengths,default=None),'median':statistics.median(lengths) if lengths else None,'max':max(lengths,default=None),'all':lengths},'latency_total_s':sum(r['latency_seconds'] for r in valid)} base={r['source_row']:r for r in groups.get('edited',[]) if not r.get('error')} for c in ('base_thinking','base_no_thinking','emulation','constant','random'): cmp={r['source_row']:r for r in groups.get(c,[]) if not r.get('error')}; keys=sorted(base.keys() & cmp.keys()); differences=[int(base[k]['score']['correct'] is True)-int(cmp[k]['score']['correct'] is True) for k in keys] if not differences:continue rng=random.Random(14353); n=len(differences);samples=sorted(sum(rng.choice(differences) for _ in range(n))/n for _ in range(10000)) out['paired']['edited_minus_'+c]={'n':n,'difference':sum(differences)/n,'bootstrap_percentile_95':[samples[249],samples[9749]],'edited_only':sum(v==1 for v in differences),'comparison_only':sum(v==-1 for v in differences),'per_question':[{'source_row':k,'edited_correct':base[k]['score']['correct'],'comparison_correct':cmp[k]['score']['correct']} for k in keys]} return out if __name__=='__main__': p=Path(sys.argv[1] if len(sys.argv)>1 else 'results/pilot_001/records.jsonl'); result=analyze(read_jsonl(p)); out=p.parent/'analysis.json';out.write_text(json.dumps(result,indent=2)); print(json.dumps(result['conditions'],indent=2))