"""Bounded O1 review: region volume scale, unchanged matching, controlled ablations.""" import argparse import hashlib import heapq import json from pathlib import Path import resource import time import numpy as np from scipy.sparse.csgraph import connected_components if __package__: from .route_feasibility import graph_from_edges,farthest_seeds,connected_voronoi,partition_report,matched_acceptance else: from route_feasibility import graph_from_edges,farthest_seeds,connected_voronoi,partition_report,matched_acceptance def regions(kind,edges,mass): selected=edges[kind[edges[:,0]]==kind[edges[:,1]]] count,labels=connected_components(graph_from_edges(len(kind),selected),directed=False) volume=np.bincount(labels,weights=mass,minlength=count) first=np.full(count,len(kind),dtype=int);np.minimum.at(first,labels,np.arange(len(kind))) return labels,volume,kind[first] def region_stats(kind,edges,mass): labels,volume,types=regions(kind,edges,mass) return dict(regions=len(volume),total_volume_raw=float(mass.sum()),volume_share_by_type=(np.bincount(kind,weights=mass,minlength=4)/mass.sum()).tolist(),region_volume_quantiles=np.quantile(volume,[0,.1,.5,.9,1]).tolist(),region_volumes_raw=volume.tolist(),region_types=types.tolist()) def merge_small_regions(kind,edges,length,mass,min_volume): labels,volume,types=regions(kind,edges,mass);nr=len(volume);parent=np.arange(nr);contacts=[{} for _ in range(nr)] crossing=labels[edges[:,0]]!=labels[edges[:,1]] pairs=np.sort(labels[edges[crossing]],axis=1) contact_weight=(mass[edges[crossing,0]]+mass[edges[crossing,1]])/(2*length[crossing]**2) if len(pairs): unique,index=np.unique(pairs,axis=0,return_inverse=True);weight=np.bincount(index,weights=contact_weight) for (i,j),w in zip(unique,weight):contacts[i][int(j)]=float(w);contacts[j][int(i)]=float(w) heap=[(float(v),i) for i,v in enumerate(volume) if v=min_volume:continue if not contacts[i]:raise ValueError('Small isolated component: cannot merge without violating topology') j=min(contacts[i],key=lambda x:(-contacts[i][x],x)) history.append(dict(source=i,destination=j,source_volume=float(volume[i]),destination_volume_before=float(volume[j]),source_type=int(types[i]),destination_type=int(types[j]))) parent[i]=j;volume[j]+=volume[i] for neighbor,w in list(contacts[i].items()): contacts[neighbor].pop(i,None) if neighbor==j:continue contacts[j][neighbor]=contacts[j].get(neighbor,0.)+w;contacts[neighbor][j]=contacts[neighbor].get(j,0.)+w contacts[i].clear() if volume[j]1024:raise ValueError('Declared bounded K limit exceeded; no silent scale change') contract=dict(type_source='Frozen previous 3-hop spectral proxy, not validated wall/rib semantics. This intervention fixes connected-region/aggregation volume scale only, not descriptor support.',minimum_region_volume_raw=float(minimum),minimum_fraction=1/512,S_nominal_raw=float(nominal),S_rule='Half of count-median connected same-type region volume AFTER fixed minimum-volume merge; node count not used',K_floor=8,K=k,S_realized_raw=float(mass.sum()/k),volume_unit='raw coordinate cubed; multiply by 1e-9 for hypothesized m^3 only',thresholds_unchanged=dict(sorted_volume_fraction_l1=.1,aspect_quantile_ratio=1.25,cross_edge_fraction_delta=.05),alphas=[1,4,16,64],control='Exact actual edge-cost multiset permutation within 20 edge-length quantile strata',repair_rule='Same pre-partition region cleanup for every arm; no per-arm volume/shape repair and no relaxed matching',decision_timing='Written before generating ANY new partitions',seed=811) (out/'scale_contract.json').write_text(json.dumps(contract,indent=2)+'\n');contract_hash=hashlib.sha256((out/'scale_contract.json').read_bytes()).hexdigest() print(a.case+' scale fixed '+json.dumps({key:contract[key] for key in ['K','S_nominal_raw','S_realized_raw','minimum_region_volume_raw']}),flush=True) seeds=farthest_seeds(x,mass,k);records={};cuts={} rng=np.random.default_rng(1729);rng.normal(size=(3,3));perm=rng.permutation(len(kind));random_kind=cleaned[perm] cross=cleaned[edges[:,0]]!=cleaned[edges[:,1]] def run(name,weights,use_seeds): tick=time.monotonic();labels=connected_voronoi(graph_from_edges(len(x),edges,weights),use_seeds) rec=partition_report(labels,graph,edges,x,mass,cleaned);rec['seconds']=time.monotonic()-tick rec['type_boundary_cut_recall']=float((labels[edges[cross,0]]!=labels[edges[cross,1]]).mean()) if cross.any() else None records[name]=rec;cuts[name]=labels run('generic_S',length,seeds) # Isolate field cleanup at old K, reusing archived generic cut/seeds. records['generic_K32']=partition_report(saved['generic'],graph,edges,x,mass,cleaned);cuts['generic_K32']=saved['generic'] run('type_K32_alpha4',length*(1+4*cross),saved['seeds']) run('old_control_K32_alpha4',length*(1+4*(random_kind[edges[:,0]]!=random_kind[edges[:,1]])),saved['seeds']) run('old_control_S_alpha4',length*(1+4*(random_kind[edges[:,0]]!=random_kind[edges[:,1]])),seeds) comparisons={} for alpha in contract['alphas']: weights=length*(1+alpha*cross);controlled=histogram_control(weights,length) run(f'type_S_alpha{alpha}',weights,seeds);run(f'control_S_alpha{alpha}',controlled,seeds) t=records[f'type_S_alpha{alpha}'];c=records[f'control_S_alpha{alpha}'];g=records['generic_S'] tg=matched_acceptance(t,g);cg=matched_acceptance(c,g);tc=matched_acceptance(t,c) comparisons[str(alpha)]=dict(type_vs_generic=tg,control_vs_generic=cg,type_vs_control=tc,all_three_pass_partial_screen=all(r['pass_provisional'] for r in [tg,cg,tc]),type_entropy_reduction_relative=1-t['within_region_type_entropy']/g['within_region_type_entropy'] if g['within_region_type_entropy'] else None,edge_cost_histogram_exact=bool(np.array_equal(np.sort(weights),np.sort(controlled)))) ablations={name:matched_acceptance(records[name],records[ref]) for name,ref in [('type_K32_alpha4','generic_K32'),('old_control_K32_alpha4','generic_K32'),('old_control_S_alpha4','generic_S')]} old_random=kind[perm];old_boundary=kind[edges[:,0]]!=kind[edges[:,1]];old_control_boundary=old_random[edges[:,0]]!=old_random[edges[:,1]] result=dict(case=a.case,contract=contract,contract_sha256=contract_hash,raw_regions=before,cleaned_regions=after,cleanup=merges,old_boundary_edge_fraction=float(old_boundary.mean()),old_control_boundary_edge_fraction=float(old_control_boundary.mean()),archived_old_control_alpha4_matching=old['partitions']['control_alpha4']['matching'],records=records,comparisons=comparisons,ablations=ablations,wall_seconds=time.monotonic()-start,peak_rss_kib=resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,interpretation='Partial matching diagnostic, NOT full H1 acceptance. Old tolerance function reused unchanged; boundary exposure and pool inference remain outside that function. No partitions selected using prediction labels, no training.') np.savez_compressed(out/'partitions.npz',cleaned_type=cleaned,seeds=seeds,**cuts) (out/'result.json').write_text(json.dumps(result,indent=2)+'\n') print(json.dumps({'case':a.case,'raw_regions':before['regions'],'cleaned_regions':after['regions'],'K':k,'changed_volume_fraction':merges['changed_volume_fraction'],'matching_at_alphas':{a:b['all_three_pass_partial_screen'] for a,b in comparisons.items()},'wall_seconds':result['wall_seconds'],'peak_rss_kib':result['peak_rss_kib']},indent=2),flush=True) if __name__=='__main__':main()