import numpy as np
from scorer_kappa_next import aggregate,fast_score_subject,weighted_accuracy_by_samples

def agg(acc,L):return aggregate([dict(acc=acc,L=L,sdt=0.,nSwitches=1,nUndetected=0)])
for acc in (0,49,50):assert agg(acc,.8)['Kappa']==0 and agg(acc,.8)['H']==0
assert abs(agg(75,1)['H']-2/3)<1e-12
assert abs(agg(100,1)['H']-1)<1e-12
assert abs(weighted_accuracy_by_samples([.9,.5],[1,3])-.6)<1e-12
fs=10;gt=np.r_[np.zeros(20),np.ones(20),np.zeros(20),np.ones(40)];trial=np.zeros(len(gt))
z=fast_score_subject(gt,gt,trial,fs=fs);assert np.all(z['sdtPerSwitch']==0) and z['L']==1
# First target switch is missed before the next ground-truth switch. Predictions
# become target label only later: must not be credited to the first switch.
pred=np.r_[np.zeros(60),np.ones(40)];z=fast_score_subject(pred,gt,trial,fs=fs)
assert np.isinf(z['sdtPerSwitch'][0]) and z['sdtPerSwitch'][2]==0
# A short new-label run that extends across a following ground-truth switch
# does not satisfy sustain before the switch in this provisional interpretation.
pred=np.zeros(len(gt));pred[37:45]=1;z=fast_score_subject(pred,gt,trial,fs=fs)
assert np.isinf(z['sdtPerSwitch'][0])
print('PASS kappa, perfect, missed-next-switch, bounded-sustain')
