Files
MeltMind/tools/extract_endpoint_review.py

209 lines
18 KiB
Python

"""CSV-only review extraction from saved tensors; no estimators, references or solves."""
import argparse
import ast
import csv
import hashlib
import json
from pathlib import Path
import re
import numpy as np
TEST_MEANINGS = {
'test_actual_exchange_and_purity_are_distinct_metrics':'用小例区分锚点区域跨区量与片内最大类型体积纯度。',
'test_four_point_rule_quadratic_barycentric_moments':'核对四点四面体求积的重心坐标一次、二次矩;不证明截断核乘张量积分精确。',
'test_many_samples_on_two_layers_do_not_pass_support':'即使求积点很多,仅两个有效轴向层仍不能通过三层支撑门。',
'test_quota_relocation_keeps_existing_slots_and_unique_nodes':'小例核对精确锚点配额、迁移数量、保留槽位和节点唯一性。',
'test_same_constant_p1_field_preserved_by_both_rules':'两种体积求积对同一常量P1张量场保持常量。',
'test_scalar_kernel_reference_symmetry_and_order_agreement':'既有运行检查核积分的端部半量、反射对称和24/48阶一致;本次不重新积分。',
'test_adjacent_fixed_seeds_do_not_fake_capacity':'相邻固定种子的链例不能伪报容量通过。',
'test_connected_chain_capacity_certificate':'不等节点质量链例得到连通等容量分配,且输入距离不改变。',
'test_indivisible_mass_not_claimed_feasible':'节点质量不可分时保留失败,不宣称可行或全局最优。',
'test_grouped_plane_and_axis_avoid_transverse_axis':'退化平面按子空间投影表示,检查幂等性、完备性及符号不变性。',
'test_primary_gap_exposes_grouping_difference':'0.1硬gap将不同谱分为三单轴/平面加轴,并识别零张量;不证明真实退化。',
'test_box_distance_analytic_across_surface_refinements':'长方体、薄板、细杆在不同表面细分下的距离与解析值比较。',
'test_log_unit_cannot_spill_into_next_line':'日志数字的单位不能串到下一行,缺失字段保持缺失。',
'test_multiblock_binary_with_distinct_header_padding':'多块压缩VTU数组解码及分量数错误拒绝。',
'test_single_block_header_padding_and_bad_payload':'单块压缩VTU解码及截断载荷拒绝。',
'test_tetra_volume_weights_and_orientation':'四面体体积、集总权重、负定向、边界及非法索引检查。',
'test_triangle_interior_edge_vertex_and_degenerate':'点到三角形面内、边、顶点和退化情形的距离。',
'test_control_preserves_exact_weight_multiset_and_seed':'控制边代价置换保持精确多重集合且随机种子可复现。',
'test_merge_preserves_geometry_and_minimum_connected_region':'小区域合并保持输入类型不变、总体积及最小连通区域体积要求。',
'test_small_disconnected_component_is_not_bridged':'不能靠虚构连接合并不连通小区域。',
'test_changed_pattern_never_counted_as_zero_projector_error':'模式不同应记录翻转和空比较集,不能写零投影误差。',
'test_constant_tensor_preserved_with_unequal_masses':'物理邻域平均在不等质量下仍保持常量张量。',
'test_junction_priority_and_rod_ends':'检查接头优先级、杆端和侧壁/内区划分。',
'test_mass_split_at_same_quadrature_position_is_invariant':'同一点质量拆成多个样本后体积求积不变。',
'test_physical_kernel_rotation_translation_covariance':'固定物理核在旋转平移下保持张量协变。',
'test_affine_gradient_tensor':'线性距离场的四面体梯度生成已知方向张量。',
'test_bottleneck_matches_sublevel_flood':'瓶颈路径值与逐层淹没可达性对照。',
'test_bottleneck_plateau_counterexample':'平台/非单调到达时间反例中的瓶颈值检查。',
'test_connected_cut_uses_true_graph':'固定种子的链分区遵守真实图连接。',
'test_matching_rejects_volume_difference':'匹配检查拒绝明显体积分布差并接受自身对照。',
'test_symmetries_cannot_supply_unique_full_frame':'对称谱例不能被完整三轴门当作唯一坐标系;仅是给定谱例。',
}
def write_csv(path, rows):
assert rows
with path.open('w',encoding='utf-8-sig',newline='') as stream:
writer=csv.DictWriter(stream,fieldnames=list(rows[0]))
writer.writeheader();writer.writerows(rows)
def spectrum(tensor):
values,basis=np.linalg.eigh(tensor)
values,basis=values[:,::-1],basis[:,:,::-1]
denominator=np.maximum(values[:,0],1e-12)
gap=(values[:,:-1]-values[:,1:])/denominator[:,None]
code=(gap[:,0]>.1).astype(int)+2*(gap[:,1]>.1).astype(int)
code[values.sum(1)<=1e-10]=-1
projections=np.zeros((len(tensor),3,3,3));ranks=np.zeros((len(tensor),3),int)
for c,groups in {0:[(0,3)],1:[(0,1),(1,3)],2:[(0,2),(2,3)],3:[(0,1),(1,2),(2,3)]}.items():
ids=np.flatnonzero(code==c)
for j,(left,right) in enumerate(groups):
sub=basis[ids,:,left:right]
projections[ids,j]=sub@sub.transpose(0,2,1)
ranks[ids,j]=right-left
return values,denominator,gap,code,projections,ranks
def main():
p=argparse.ArgumentParser();p.add_argument('output',type=Path);out=p.parse_args().output
out.mkdir(parents=True,exist_ok=True)
with (out/'extraction.started').open('x') as f:f.write('saved tensors and tables only\n')
source=Path('artifacts/approved-quadrature-seeds-2026-11-14')
contract=json.loads((source/'execution_contract.json').read_text())
previous=json.loads((source/'O2_result.json').read_text())
reference=np.load(source/'kernel_mass_reference.npz')
records={};point_rows=[];centers=[];inventory=[];spectral_summary=[]
labels={-1:'unsupported',0:'3',1:'1+2',2:'2+1',3:'1+1+1'}
for name in previous['estimates']:
saved=np.load(source/(name+'.npz'));q=saved['query'];t=saved['tensor']
if not inventory:
for i,xyz in enumerate(q):
end=min(xyz[2],8-xyz[2])<=.5+1e-12
wall=min(xyz[0],.2-xyz[0],xyz[1],.2-xyz[1])<=.05+1e-12
inventory.append(dict(anchor_id=i,x=xyz[0],y=xyz[1],z=xyz[2],region='end' if end else 'wall' if wall else 'interior'))
assert np.array_equal(q,reference['query']) and len(q)==2197
endpoint=np.minimum(q[:,2],8-q[:,2])<=.5+1e-12
ids=np.flatnonzero(endpoint);assert len(ids)==338
value,den,gap,code,projector,ranks=spectrum(t)
assert np.array_equal(code,saved['mode'])
layers=saved['effective_axial_layers']>=3
spatial=saved['minimum_relative_support_eigenvalue']>1e-10
effective=saved['effective_samples']>=16
mass=saved['relative_kernel_mass_error']<=.05
reference_pass=reference['relative_agreement']<=1e-8
assert np.array_equal(layers&spatial,saved['support_resolved'])
assert np.array_equal(effective&mass&reference_pass,saved['quadrature_quality'])
for i in ids:
row=dict(estimate=name,anchor_id=int(i),x=q[i,0],y=q[i,1],z=q[i,2],end_side='z0' if q[i,2]<4 else 'z8',
support_resolved=int(saved['support_resolved'][i]),quality_pass=int(saved['quadrature_quality'][i]),
support_and_quality=int(saved['support_resolved'][i]&saved['quadrature_quality'][i]),
effective_axial_layers=int(saved['effective_axial_layers'][i]),layer_gate=int(layers[i]),
min_relative_support_eigenvalue=saved['minimum_relative_support_eigenvalue'][i],spatial_rank_gate=int(spatial[i]),
effective_samples=saved['effective_samples'][i],effective_sample_gate=int(effective[i]),
kernel_mass=saved['kernel_mass'][i],reference_kernel_mass_saved=saved['reference_kernel_mass'][i],
kernel_mass_relative_error=saved['relative_kernel_mass_error'][i],kernel_mass_gate=int(mass[i]),
reference_agreement_saved=reference['relative_agreement'][i],reference_gate=int(reference_pass[i]),
individual_quadrature_weights='NOT_SAVED',lambda1=value[i,0],lambda2=value[i,1],lambda3=value[i,2],
gap12_numerator=value[i,0]-value[i,1],gap23_numerator=value[i,1]-value[i,2],gap_denominator=den[i],
gap12=gap[i,0],gap23=gap[i,1],hard_gap=.1,gap12_gt_threshold=int(gap[i,0]>.1),gap23_gt_threshold=int(gap[i,1]>.1),
trace=value[i].sum(),mode_code=int(code[i]),mode=labels[code[i]],spectral_source='eigh_of_saved_raw_tensor_unscaled')
row.update({f'T{j}{k}':t[i,j,k] for j in range(3) for k in range(3)})
for group in range(3):
row[f'P{group+1}_rank']=int(ranks[i,group])
row.update({f'P{group+1}_{j}{k}':projector[i,group,j,k] if ranks[i,group] else None for j in range(3) for k in range(3)})
point_rows.append(row)
records[name]=dict(saved=saved,ids=ids,code=code,P=projector,rank=ranks)
c=previous['estimates'][name]['center']
centers.append(dict(estimate=name,mesh='six_tets_Freudenthal',nx=12,ny=12,nz=previous['estimates'][name]['z_cells'],
x=.1,y=.1,z=4.,existing_reference='diag(0.5,0.5,0); reused prior record only',
raw_tensor_frobenius_error=c['tensor_frobenius_error_to_analytic'],eigenvalue_max_error=c['eigenvalue_max_error_to_analytic'],
axis_projector_error=c['axis_projector_error_to_analytic'],source='O2_result.json:estimates/'+name+'/center'))
spectral_summary.append(dict(estimate=name,lambda_min=value[ids].min(0).tolist(),lambda_max=value[ids].max(0).tolist(),
gap_min=gap[ids].min(0).tolist(),gap_max=gap[ids].max(0).tolist()))
write_csv(out/'anchors.csv',inventory);write_csv(out/'endpoint_estimates.csv',point_rows);write_csv(out/'center_error_provenance.csv',centers)
pairs=[];summaries=[]
for label,comparison in previous['comparisons'].items():
first,second=comparison['first'],comparison['second'];a,b=records[first],records[second]
errors=[];qualified_errors=[];transition={};bs=bq=br=compatible_count=0
for i in a['ids']:
sa,sb=a['saved'],b['saved']
support=bool(sa['support_resolved'][i]&sb['support_resolved'][i])
quality=bool(sa['quadrature_quality'][i]&sb['quadrature_quality'][i])
resolved=support and quality
compatible=bool(a['code'][i]==b['code'][i] and a['code'][i] in [1,2])
error=float(np.linalg.norm(a['P'][i]-b['P'][i],axis=(1,2)).max()) if compatible else None
transition_key=labels[a['code'][i]]+' -> '+labels[b['code'][i]]
transition[transition_key]=transition.get(transition_key,0)+1
bs+=support;bq+=quality;br+=resolved;compatible_count+=compatible
if compatible:errors.append(error)
if compatible and resolved:qualified_errors.append(error)
xyz=sa['query'][i]
pairs.append(dict(comparison=label,anchor_id=int(i),x=xyz[0],y=xyz[1],z=xyz[2],
from_estimate=first,to_estimate=second,from_mode=labels[a['code'][i]],to_mode=labels[b['code'][i]],
from_support=int(sa['support_resolved'][i]),to_support=int(sb['support_resolved'][i]),
from_quality=int(sa['quadrature_quality'][i]),to_quality=int(sb['quadrature_quality'][i]),
bilateral_support_resolved=int(support),bilateral_quality=int(quality),bilateral_resolved=int(resolved),
mode_flip=int(a['code'][i]!=b['code'][i]),compatible_two_cluster_modes=int(compatible),
projector_frobenius_max=error,qualified_projector_error=error if compatible and resolved else None,
comparison_status='compatible' if compatible else 'incompatible_mode_NO_PROJECTOR_COMPARISON',
from_projectors_key=f'{first}|{i}',to_projectors_key=f'{second}|{i}'))
old=comparison['regions']['end'];flips=sum(n for key,n in transition.items() if key.split(' -> ')[0]!=key.split(' -> ')[1])
assert br==old['support_quality_both_count'] and np.isclose(flips/338,old['rates']['all_mode_flips'])
for arr,key in [(errors,'projector_difference_both_emit_same_mode'),(qualified_errors,'physical_gates_and_same_two_group_projector')]:
assert len(arr)==old[key]['count']
if arr:assert np.isclose(max(arr),old[key]['maximum']) and np.isclose(np.quantile(arr,.95),old[key]['p95'])
summaries.append(dict(comparison=label,denominator=338,bilateral_support=bs,bilateral_quality=bq,bilateral_resolved=br,
mode_flips=flips,mode_flip_rate=flips/338,compatible_count=compatible_count,incompatible_count=338-compatible_count,
transitions=json.dumps(transition,ensure_ascii=False),compatible_Pmax=max(errors) if errors else None,compatible_P95=float(np.quantile(errors,.95)) if errors else None,
qualified_count=len(qualified_errors),qualified_Pmax=max(qualified_errors) if qualified_errors else None,qualified_P95=float(np.quantile(qualified_errors,.95)) if qualified_errors else None))
write_csv(out/'endpoint_pairs.csv',pairs);write_csv(out/'endpoint_summary.csv',summaries)
o1=json.loads((source/'O1_result.json').read_text());o1rows=[]
for name,r in o1['records'].items():
quantile=np.quantile(r['aspect'],[.1,.5,.9])
o1rows.append(dict(arm=name,capacity_pass=int(r['capacity_connectivity_anchor_pass']),max_capacity_relative_error=r['capacity']['maximum_relative_volume_error'],
actual_anchor_region_exchange_mass_fraction=r['purity']['anchor_region_exchange_mass_fraction'],
dominant_type_volume_purity=r['purity']['dominant_type_volume_purity'],dominant_region_volume_purity=r['purity']['dominant_region_volume_purity'],
aspect_q10=quantile[0],aspect_q50=quantile[1],aspect_q90=quantile[2],capacity_termination=r['capacity']['termination'],
seed_count_quota_exact=1,region_partition_quota_in_solver=0,zero_crossing_hard_constraint=0))
write_csv(out/'O1_summary.csv',o1rows)
write_csv(out/'O1_matching.csv',[dict(pair=k,**{n:v for n,v in r.items() if n not in ['limits','status']}) for k,r in o1['matching'].items()])
seed=np.load(source/'common_quota_anchors.npz');actual=np.bincount(seed['region'][seed['new']],minlength=10)
assert np.array_equal(actual,seed['quota'])
write_csv(out/'O1_quota.csv',[dict(region=i,quota=int(seed['quota'][i]),actual_seed_count=int(actual[i]),solver_region_partition_quota='NOT_ENFORCED') for i in range(10)])
tests=[];log=(source/'tests.txt').read_text()
for name,fullid in re.findall(r'^(test_\w+) \(([^)]+)\) \.\.\. ok$',log,re.M):
module,cls=fullid.split('.')
path=Path('tests')/(module+'.py');tree=ast.parse(path.read_text())
line=next(n.lineno for c in tree.body if isinstance(c,ast.ClassDef) and c.name==cls for n in c.body if isinstance(n,ast.FunctionDef) and n.name==name)
tests.append(dict(test_name=name,class_id=fullid,status_in_saved_run='ok',meaning=TEST_MEANINGS[name],source=str(path),source_line=line,rerun_this_extraction=0))
assert len(tests)==31
write_csv(out/'test_catalog.csv',tests);(out/'existing_test_log.txt').write_text(log)
metadata=dict(scope='Extraction only: no mesh, tensor estimator, quadrature reference, partition, or test rerun.',
anchor_id='Zero-based row in original 2197-anchor query array; identity preserved.',
joins='endpoint_pairs.{from,to}_projectors_key joins endpoint_estimates.(estimate|anchor_id). Projectors are matrix entries P1_00..P3_22 in descending spectral-cluster order.',
eigenvalues='Raw, unnormalized saved tensor eigenvalues descending lambda1>=lambda2>=lambda3; eigenvalues/projectors were not separately saved and are recovered by numpy.linalg.eigh, not re-estimated.',
gap_formula='d=max(lambda1,1e-12); g12=(lambda1-lambda2)/d; g23=(lambda2-lambda3)/d. Strict >0.1 sets split; equality stays merged. trace<=1e-10 overrides to unsupported.',
modes={'-1':'unsupported','0':'3-dimensional isotropic cluster','1':'1+2 (lambda1 axis, lambda2/3 plane)','2':'2+1 (lambda1/2 plane, lambda3 axis)','3':'1+1+1'},
projector='For each cluster [a,b], P=sum u_j u_j^T. No choice of a basis axis inside a grouped plane. Unused P slots are blank.',
bilateral_resolved='AND of support_resolved and quality for BOTH estimates; bilateral_support_resolved separately omits quality.',
compatible_error='Only matching two-cluster modes: max over corresponding cluster Frobenius norms. Incompatible modes have blank/NA error, never zero.',
end_definition='min(z,8-z)<=0.5+1e-12, priority over wall. R=0.5; cross-section thickness=0.2; cutoff=R=2.5 thicknesses, R=5 half-thicknesses. Retained original anchors yield only z=0 and z=8 at the ends, 169 each; no anchors in 0<z<=0.5 or 7.5<=z<8.',
threshold_interpretation='Proximity to the artificial normalized gap 0.1 is not equality of eigenvalues and not proof of true spectral degeneracy.',
missing=['Individual quadrature locations/weights and full neighborhood covariance matrices are not saved in estimate NPZ; no per-point individual-weight audit recreated.',
'Independent per-point continuum tensor/projector reference is absent. Only existing center reference/error records are quoted.',
'Original eigenvalue/eigenvector arrays were not persisted separately; derived spectral columns are explicitly tagged.'],
support_gates='effective_axial_layers>=3; minimum_relative_support_eigenvalue>1e-10. Effective layer and moment values are copied from NPZ.',
quality_gates='effective_samples>=16; saved relative kernel-mass error<=0.05; saved reference agreement<=1e-8. No reference integration rerun.',
O1_constraint='Exact quota enforced at anchor construction only. Solver has no regional partition-count/volume or zero-crossing hard constraint; capacity is an output feasibility check in the existing dual search.',
prior_test_scope='31 saved tests passed before prior experiment; they are implementation checks, not a physical convergence certificate; not rerun here.',
endpoint_spectral_ranges=spectral_summary)
(out/'data_dictionary.json').write_text(json.dumps(metadata,ensure_ascii=False,indent=2)+'\n')
print(json.dumps({'anchor_rows':len(inventory),'endpoint_estimate_rows':len(point_rows),'endpoint_pair_rows':len(pairs),'saved_tests':len(tests),'summaries':summaries},ensure_ascii=False,indent=2))
if __name__=='__main__':main()