Files
MeltMind/tools/route_followup_checks.py

45 lines
3.7 KiB
Python

"""Additional diagnostics: saved cuts and a symmetric rod counterexample."""
import json
from pathlib import Path
import time
import numpy as np
from scipy.sparse.csgraph import connected_components
from frame_analytic_probe import box_mesh
from route_feasibility import node_tensor,graph_from_edges,spectral_features,matched_acceptance
out=Path('artifacts/routes-2026-11-13');start=time.monotonic();rows=[]
for p in sorted(out.glob('*.routes.json')):
r=json.loads(p.read_text());a=np.load(out/(r['case']+'.route_arrays.npz'))
d=np.load(Path('artifacts/intake-2026-11-13/samples')/(r['case']+'.npz'))
x=d['points_raw'];faces=d['boundary_triangles'];mass=d['node_volume_raw'];q=x[faces]
areas=np.linalg.norm(np.cross(q[:,1]-q[:,0],q[:,2]-q[:,0]),axis=1)/2
vertex_area=np.bincount(faces.ravel(),weights=np.repeat(areas/3,3),minlength=len(x))
cuts={}
for name in r['partitions']:
labels=a[name];v=np.bincount(labels,weights=mass);surface=np.bincount(labels,weights=vertex_area)
cuts[name]=dict(exterior_area_fraction=(surface/surface.sum()).tolist(),dimensionless_exterior_exposure=(surface/v**(2/3)).tolist())
# Exact pooling residual reconstruction, before any learned nonlinear map.
mean=np.stack([np.bincount(labels,weights=mass*x[:,j])/v for j in range(3)],axis=1)
residual=x-mean[labels]
cuts[name]['pool_plus_residual_reconstruction_error']=float(abs(residual+mean[labels]-x).max())
for alpha in [1,4]:
type_name=f'type_alpha{alpha}';ctrl=f'control_alpha{alpha}'
cuts[type_name]['vs_control_existing_screen']=matched_acceptance(r['partitions'][type_name],r['partitions'][ctrl])
for other in ['generic',ctrl]:
qt=np.quantile(cuts[type_name]['dimensionless_exterior_exposure'],[.1,.5,.9]);qo=np.quantile(cuts[other]['dimensionless_exterior_exposure'],[.1,.5,.9])
cuts[type_name]['exterior_exposure_log_ratio_vs_'+other]=float(abs(np.log(np.maximum(qt,1e-30)/np.maximum(qo,1e-30))).max())
kind=a['type_proxy'];edges=d['edges'];permutation=np.array([2,0,3,1]);null=np.array_equal(kind[edges[:,0]]!=kind[edges[:,1]],permutation[kind[edges[:,0]]]!=permutation[kind[edges[:,1]]])
rows.append(dict(case=r['case'],cuts=cuts,type_id_relabel_edge_cost_invariant=bool(null),status='Boundary exposure measured after initial run: descriptive, not a preregistered pass. No exclusions or repair were applied. Full v0.1 acceptance remains open.'))
# Same symmetric physical rod, different symmetry-related tetra discretizations.
x,tet,mass,edges=box_mesh(np.array([.2,.2,8.]),12)
g=graph_from_edges(len(x),edges,np.linalg.norm(x[edges[:,0]]-x[edges[:,1]],axis=1))
c=np.array([.1,.1,4.]);R=np.array([[0.,-1,0],[1,0,0],[0,0,1.]])
x2=(x-c)@R.T+c
v1=np.minimum(x,np.array([.2,.2,8.])-x).min(1);v2=np.minimum(x2,np.array([.2,.2,8.])-x2).min(1)
t1,_,_=node_tensor(x,tet,mass,v1,g);t2,_,_=node_tensor(x2,tet,mass,v2,g);i=int(np.argmin(np.linalg.norm(x-c,axis=1)))
example=dict(point=c.tolist(),tensor_original=t1[i].tolist(),tensor_symmetry_related_mesh=t2[i].tolist(),same_geometry_tensor_difference=float(abs(t1[i]-t2[i]).max()),expected_rotated_tensor_error=float(abs(t2[i]-R@t1[i]@R.T).max()),spectral_gate_original=bool(spectral_features(t1)['full'][i]),spectral_gate_rotated=bool(spectral_features(t2)['full'][i]),meaning='Pose equivariance passes but geometry-only identifiability fails: symmetric rod centre acquires a preferred transverse direction from tetra discretization. Eigenvalue gap alone must NOT certify a physical frame.')
result=dict(partition_followup=rows,symmetric_rod_counterexample=example,wall_seconds=time.monotonic()-start)
target=out/'route_followup_checks.json'
if target.exists():raise FileExistsError(target)
target.write_text(json.dumps(result,indent=2)+'\n');print(json.dumps(example,indent=2))