from itertools import product,combinations_with_replacement from collections import defaultdict def run(load,read,check): reg=load('C286_DATA.json')['target_register'];allpairs=load('C438_DATA.json')['labelled_pairs'] rolemap={('Noah birth','Noah death'):'A_Noah_complement',('Noah birth','Shem death'):'B_Noah_Shem', ('Flood start','Shem birth'):'C_Shem_Flood',('Flood close','Shem birth'):'C_Shem_Flood', ('Flood close','Flood start'):'D_Flood_boundaries',('Flood close','Noah birth'):'E_Noah_Flood_alias',('Noah birth','Noah birth'):'F_MT_Noah_Gears'} groups=defaultdict(list) for z in allpairs:groups[rolemap[tuple(z['roles'])]].append(z) check('six disjoint labelled classes',[len(groups[k]) for k in sorted(groups)]==[128,40,48,32,4,6]) def key(c,i,j): a,b=sorted([i,j],key=lambda v:reg[v]['center']);return(c,a,b) classes={name:{(z['center'],z['lower_record'],z['upper_record']) for z in rows} for name,rows in groups.items()} nb=[i for i,r in enumerate(reg) if r['role']=='Noah birth'];fs=[i for i,r in enumerate(reg) if r['role']=='Flood start'];fc=[i for i,r in enumerate(reg) if r['role']=='Flood close'] derived=defaultdict(set);trialcounts=defaultdict(int) for g,t in product([1,2],[0,1]): c=3188+2*(g-1)+60*t for i,j in product(fs,fc): a,b=reg[i],reg[j];trialcounts['D']+=1 rule=(t==0 and a['tradition']!='MT' and b['tradition']!='MT' and a['Cainan']==b['Cainan']==1 and a['Terah60']==b['Terah60']==1 and a['full430']+b['full430']==1 and a['gear']+b['gear']==2*g) if rule:derived['D_Flood_boundaries'].add(key(c,i,j)) assert rule==(a['center']+b['center']==2*c) check(f'Flood boundary rule complete at{c}',{k for k in derived['D_Flood_boundaries'] if k[0]==c}=={k for k in classes['D_Flood_boundaries'] if k[0]==c}) for i,j in product(nb,fc): a,b=reg[i],reg[j];trialcounts['E']+=1 rule=(g==2 and a['tradition']=='SP' and a.get('rail')=='companion' and b['tradition']!='MT' and a['gear']==b['gear']==1 and a['Cainan']==b['Cainan']==0 and a['full430']==b['full430']==0 and a['Terah60']==b['Terah60']==t) if rule:derived['E_Noah_Flood_alias'].add(key(c,i,j)) assert rule==(a['center']+b['center']==2*c) check(f'Noah Flood rule complete at{c}',{k for k in derived['E_Noah_Flood_alias'] if k[0]==c}=={k for k in classes['E_Noah_Flood_alias'] if k[0]==c}) for i,j in combinations_with_replacement(nb,2): a,b=reg[i],reg[j];trialcounts['F']+=1 rule=(a['tradition']==b['tradition']=='MT' and a['Cainan']==b['Cainan']==1 and a['full430']==b['full430']==1 and a['Terah60']==b['Terah60']==t and a['gear']+b['gear']==2*(g+1)) if rule:derived['F_MT_Noah_Gears'].add(key(c,i,j)) assert rule==(a['center']+b['center']==2*c) check(f'MT Noah Gear rule complete at{c}',{k for k in derived['F_MT_Noah_Gears'] if k[0]==c}=={k for k in classes['F_MT_Noah_Gears'] if k[0]==c}) check('Flood sum base',2891+2890+2*130+2*60+215==6376) check('Noah companion and Flood-close600',3490-2890==600 and (3490+2890)//2==3190) check('Noah-death Flood-close alias uses349',2541+130+215+4==2890 and 130+215+4==349) coordgroups={k:{(z['center'],z['lower'],z['upper']) for z in v} for k,v in groups.items()} coordunion=set().union(*coordgroups.values()) overlaps=[{'center':c,'lower':a,'upper':b,'classes':[k for k,v in coordgroups.items() if (c,a,b) in v]} for c,a,b in sorted(coordunion) if sum((c,a,b) in v for v in coordgroups.values())>1] check('112 class memberships reduce to106 distinct orbits',sum(map(len,coordgroups.values()))==112 and len(coordunion)==106 and len(overlaps)==6 and all(len(z['classes'])==2 for z in overlaps)) check('source labelled partition is exhaustive',len(set().union(*classes.values()))==sum(map(len,classes.values()))==258) return {'step':'C441','classes':[{'id':k,'labelled_count':len(v),'coordinate_count':len(coordgroups[k]),'pairs':v} for k,v in sorted(groups.items())], 'residual_rule_trials':dict(trialcounts),'residual_rule_hits':{k:[list(z) for z in sorted(v)] for k,v in derived.items()},'coordinate_class_overlaps':overlaps, 'counts':{'classes':6,'labelled_pairs':258,'coordinate_class_memberships':112,'coordinate_orbit_union':106,'double_memberships':6,'residual_labels_explained':42}, 'limits':'The grammar partitions labelled source comparisons, not biographies. Six coordinate overlaps have distinct event identities; Flood close is not an SP/LXX person rail.'}