"""Educational synthetic-data analysis; Python 3 stdlib, no upload/network calls."""
from pathlib import Path
import csv,json
from collections import defaultdict
ROOT=Path(__file__).resolve().parent
def load(name):
 with (ROOT/name).open(newline='',encoding='utf8') as f:return list(csv.DictReader(f))
def summarize():
 rows=load('observations.csv');follow=load('followup.csv');doe=load('doe-confirmation.csv')
 assert len({r['wafer_id'] for r in rows})==len(rows),'duplicate wafer key'
 for r in rows:
  assert int(r['first_pass'])+int(r['bin_contact'])+int(r['bin_other'])==int(r['tested_unique'])
  assert int(r['edge_tested'])+int(r['inner_tested'])==int(r['tested_unique'])
  assert int(r['edge_fail'])+int(r['inner_fail'])==int(r['tested_unique'])-int(r['first_pass'])
 def rate(rs):
  n=sum(int(r['tested_unique']) for r in rs);p=sum(int(r['first_pass']) for r in rs);return dict(tested=n,passed=p,yield_pct=100*p/n)
 phases={phase:rate([r for r in rows if r['phase']==phase]) for phase in ['before','after']}
 groups=defaultdict(list)
 for r in rows:groups[(r['phase'],r['product'],r['chamber'])].append(r)
 strata=[dict(phase=k[0],product=k[1],chamber=k[2],**rate(v)) for k,v in sorted(groups.items())]
 regions={}
 for phase in ['before','after']:
  rs=[r for r in rows if r['phase']==phase];regions[phase]={}
  for region in ['edge','inner']:
   n=sum(int(r[region+'_tested']) for r in rs);f=sum(int(r[region+'_fail']) for r in rs);regions[phase][region]=dict(tested=n,failed=f,fail_pct=100*f/n)
 # Followup rows are a targeted selection of initial fails; never add them to original unique denominator.
 assert {r['wafer_id'] for r in follow}=={r['wafer_id'] for r in rows}
 screen=[r for r in doe if r['stage']=='screen'];means={}
 for a in [-1,1]:
  for b in [-1,1]:
   rs=[r for r in screen if int(r['factor_a'])==a and int(r['factor_b'])==b];assert len(rs)==3;means[f'{a},{b}']=rate(rs)['yield_pct']
 def effect(f):
  high=[r for r in screen if f(r)==1];low=[r for r in screen if f(r)==-1];return rate(high)['yield_pct']-rate(low)['yield_pct']
 return dict(phases=phases,drop_percentage_points=phases['before']['yield_pct']-phases['after']['yield_pct'],strata=strata,regions=regions,doe_means_pct=means,effects_pp=dict(A=effect(lambda r:int(r['factor_a'])),B=effect(lambda r:int(r['factor_b'])),AB=effect(lambda r:int(r['factor_a'])*int(r['factor_b']))),confirmation=rate([r for r in doe if r['stage']=='confirmation']),monitor=rate([r for r in doe if r['stage']=='monitor']))
if __name__=='__main__':print(json.dumps(summarize(),ensure_ascii=False,indent=2))
