from pathlib import Path
import json,sys,math
import numpy as np,pandas as pd,statsmodels.api as sm
from scipy import stats
sys.path.insert(0,'code');import rhythm as cv
orig=cv.person_metrics

def metrics(g):
 r=orig(g);n=len(g);consec=np.diff(g['t'].to_numpy())==1;q=consec.sum()
 r['IV_gap_adjusted']=r['IV']*n/(q+1)
 r['IV_gap_factor']=n/(q+1);r['nonconsecutive_boundaries']=n-1-q
 x=np.log1p(g['mims'].to_numpy());hod=g['hod'].to_numpy();day=g['day'].to_numpy();res=x-pd.Series(x).groupby(hod).transform('mean').to_numpy()
 res=res-pd.Series(res).groupby(day).transform('mean').to_numpy()
 for name,keep in [('AR1_day_centered',consec),('AR1_day_centered_within_day',consec & (day[:-1]==day[1:]))]:
  r[name]=float(np.corrcoef(res[:-1][keep],res[1:][keep])[0,1])
 return r
cv.person_metrics=metrics
m=cv.build('data/nhanes-pax');m['age2']=m['RIDAGEYR']**2;m['phq2']=m['DPQ010']+m['DPQ020']
base=['RIDAGEYR','age2','male','cycleH','exam_nov_apr'];out={'n':len(m),'gap_summary':{'participants_with_gaps':int((m['nonconsecutive_boundaries']>0).sum()),'maximum_boundaries':float(m['nonconsecutive_boundaries'].max()),'mean_IV_gap_factor':float(m['IV_gap_factor'].mean()),'maximum_IV_gap_factor':float(m['IV_gap_factor'].max())},'primary_sensitivities':{},'interactions':{}}
for y,sign in [('IV',1),('IV_gap_adjusted',1),('AR1',1),('AR1_day_centered',1),('AR1_day_centered_within_day',1),('IS',-1),('RA',-1)]:
 out['primary_sensitivities'][y]={}
 for x in ['phq','phq2']:
  a=cv.rank_reg(m,y,x,base,sign);b=cv.rank_reg(m,y,x,base+['mean_activity'],sign)
  out['primary_sensitivities'][y][x]={'base':a,'activity_adjusted':b}
# Conventional centered product interactions, preserving the named main effects.
for y in ['IV','IV_gap_adjusted','IS']:
 Y=stats.rankdata(m[y]);out['interactions'][y]={}
 for kind in ['submitted_rank_product_raw_main','rank_product_rank_main','rank_PHQ_product','raw_PHQ_product']:
  p=m['phq'].to_numpy();a=m['RIDAGEYR'].to_numpy();rawprod=(p-p.mean())*(a-a.mean())
  if kind.startswith('submitted'):main=p;it=stats.rankdata(rawprod)
  elif kind=='rank_product_rank_main':main=stats.rankdata(p);it=stats.rankdata(rawprod)
  elif kind=='rank_PHQ_product':main=stats.rankdata(p);it=(main-main.mean())*(a-a.mean())
  else:main=p;it=rawprod
  X=m[base].copy();X['PHQ_main']=main;X['interaction']=it;fit=sm.OLS(Y,sm.add_constant(X)).fit(cov_type='HC3')
  scale=np.std(it)/np.std(Y);b=float(fit.params['interaction']*scale);se=float(fit.bse['interaction']*scale)
  out['interactions'][y][kind]={'beta_std':b,'se_std':se,'ci95_std':[b-1.959964*se,b+1.959964*se],'p_two_sided':float(fit.pvalues['interaction'])}
# Primary IV interval, a sampling bound rather than proof of absence.
Y=stats.rankdata(m['IV']);X=m[base].copy();X['phq_rank']=stats.rankdata(m['phq']);fit=sm.OLS(Y,sm.add_constant(X)).fit(cov_type='HC3');scale=np.std(X['phq_rank'])/np.std(Y);b=float(fit.params['phq_rank']*scale);se=float(fit.bse['phq_rank']*scale)
out['IV_primary_ci95']=[b-1.959964*se,b+1.959964*se]
Path('/out/sensitivity-results.json').write_text(json.dumps(out,indent=2,allow_nan=False)+'\n');print(json.dumps(out,indent=2))
