#!/usr/bin/env python3
"""FlashMemory pattern-transfer classroom model. Not physical process or real recipe."""
import csv
import json
from collections import defaultdict
from pathlib import Path

file=Path(__file__).with_name("pattern-transfer-synthetic.csv")
with file.open(encoding="utf-8",newline="") as h:
    rows=list(csv.DictReader(h))
assert len(rows)==18
groups=defaultdict(list)
for r in rows:
    dose=int(r["relative_dose_index"])
    focus=int(r["relative_focus_index"])
    kind=r["etch_case"]
    p=float(r["photo_opening_nm"])
    depth=float(r["vertical_depth_nm"])
    lateral=float(r["lateral_undercut_per_side_nm"])
    post=float(r["post_etch_opening_nm"])
    transfer=float(r["transfer_bias_nm"])
    proxy=float(r["anisotropy_proxy"])
    assert dose in (-1,0,1) and focus in (-1,0,1)
    assert kind in ("Directional","MoreLateral")
    assert p==100+4*dose+3*abs(focus)
    assert depth==100 and lateral==(4 if kind=="Directional" else 16)
    assert post==p+2*lateral and transfer==post-p
    assert abs(proxy-(1-lateral/depth))<1e-10
    # Fictional classroom window only. Not a fab acceptance condition.
    photo_pass=abs(p-100)<=5
    final_pass=abs(post-108)<=5
    groups[kind].append((dose,focus,photo_pass,final_pass,post))
assert all(len(groups[k])==9 for k in ("Directional","MoreLateral"))
report={"dataset":"SYNTHETIC_TRAINING_ONLY",
        "target_photo_nm":100,
        "fictional_photo_window_nm":[95,105],
        "fictional_final_window_nm":[103,113],
        "cases":len(rows),"etch_conditions":{}}
for kind in ("Directional","MoreLateral"):
    vals=groups[kind]
    report["etch_conditions"][kind]={
        "n":len(vals),
        "photo_window_pass":sum(x[2] for x in vals),
        "post_etch_window_pass":sum(x[3] for x in vals),
        "mean_final_opening_nm":round(sum(x[4] for x in vals)/len(vals),4),
        "anisotropy_proxy":0.96 if kind=="Directional" else 0.84,
        "transfer_bias_nm":8 if kind=="Directional" else 32,
    }
assert report["etch_conditions"]["Directional"]["photo_window_pass"]==7
assert report["etch_conditions"]["Directional"]["post_etch_window_pass"]==7
assert report["etch_conditions"]["MoreLateral"]["post_etch_window_pass"]==0
report["limitations"]="Numbers, dose/focus indices, widths, and windows are intentionally fictional; proxy 1-L/D is a simplified geometry ratio, not a validated etch anisotropy spec. No replicates or independent wafer observations; no process causality or actual yield is established."
print(json.dumps(report,ensure_ascii=False,indent=2))
