"""Reproduce pre-assignment recency bands from Hillstrom's public email experiment.

Run: python holdout_sensitivity.py hillstorm_no_indices.csv.gz
No record-level data is printed. Uses only the Python standard library.
"""
from collections import defaultdict
import csv
from decimal import Decimal
import gzip
from hashlib import sha256
import json
from math import sqrt
from pathlib import Path
import sys


def summarize(path):
    groups=defaultdict(lambda:[0,0,0,Decimal(0)])
    with gzip.open(path,'rt',encoding='utf-8',newline='') as source:
        for row in csv.DictReader(source):
            recency=int(row['recency'])
            band='1–3' if recency<=3 else '4–6' if recency<=6 else '7–12'
            assert 1<=recency<=12
            for group in (band,'All'):
                counts=groups[(group,row['segment'])]
                counts[0]+=1
                counts[1]+=int(row['visit'])
                counts[2]+=int(row['conversion'])
                counts[3]+=Decimal(row['spend'])
    assert sum(x[0] for (band,_),x in groups.items() if band=='All')==64000
    result={}
    for (band,arm),(n,v,b,s) in sorted(groups.items()):
        result[f'{band} | {arm}']={'assigned':n,'visited':v,'buyers':b,'spend':float(s)}
    return {'source_sha256':sha256(Path(path).read_bytes()).hexdigest(),'groups':result}


if __name__=='__main__':
    print(json.dumps(summarize(sys.argv[1]),indent=2,ensure_ascii=False))
