Files
MeltMind/tools/phase2_gate_math.py

54 lines
3.5 KiB
Python

"""B3 quantization/birth records and B4 weighted monotone projection."""
import numpy as np
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import connected_components
def quantize_lower_tie(values,step=.001,offset=0.):
y=np.asarray(values,dtype=np.float64)
if not np.isfinite(y).all() or not np.isfinite([step,offset]).all() or step<=0:raise ValueError('Invalid quantization input')
q=(y-offset)/step;lo=np.floor(q)
return (lo+((q-lo)>.5))*step+offset
def monotone_projection(potential,target,weights):
s=np.asarray(potential,dtype=np.float64);y=np.asarray(target,dtype=np.float64);w=np.asarray(weights,dtype=np.float64)
if s.ndim!=1 or not len(s) or s.shape!=y.shape or s.shape!=w.shape or not np.isfinite([s,y,w]).all() or np.any(w<=0):raise ValueError('Projection domain mismatch')
distinct,inverse=np.unique(s,return_inverse=True);W=np.bincount(inverse,weights=w);means=np.bincount(inverse,weights=w*y)/W
block_means=[];block_weight=[];block_length=[]
for mean,weight in zip(means,W):
block_means.append(float(mean));block_weight.append(float(weight));block_length.append(1)
while len(block_means)>1 and block_means[-2]>block_means[-1]:
weight=block_weight[-2]+block_weight[-1]
mean=(block_weight[-2]*block_means[-2]+block_weight[-1]*block_means[-1])/weight
length=block_length[-2]+block_length[-1]
block_means[-2:]=[mean];block_weight[-2:]=[weight];block_length[-2:]=[length]
fitted=np.repeat(block_means,block_length);prediction=fitted[inverse]
within=float(np.sum(w*(y-means[inverse])**2));between=float(np.sum(W*(means-fitted)**2))
direct=float(np.sum(w*(y-prediction)**2));denom=float(np.sum(w*y*y))
if not np.isclose(within+between,direct,rtol=1e-10,atol=1e-12*max(denom,1.)):raise ValueError('PAVA decomposition identity failed')
info=dict(nodes=len(y),potential_groups=len(distinct),fitted_blocks=len(block_means),within_tie_SSE=within,
between_order_SSE=between,total_SSE=direct,denominator=denom,
normalized_tie=within/denom if denom>0 else None,normalized_order=between/denom if denom>0 else None,
relative_l2=float(np.sqrt(direct/denom)) if denom>0 else None,
tie_share=within/direct if direct>0 else None,deployable=False)
return info,dict(potential=distinct,group_weights=W,group_mean=means,fitted=fitted)
def birth_platforms(field,mass,raw,points,adjacency):
s=np.asarray(field,dtype=np.float64);m=np.asarray(mass,dtype=np.float64);n=len(s)
src=np.repeat(np.arange(n),np.diff(adjacency.indptr));dst=adjacency.indices
equal=s[src]==s[dst]
ties=csr_matrix((np.ones(np.count_nonzero(equal)),(src[equal],dst[equal])),shape=(n,n))
count,labels=connected_components(ties,directed=False)
rejected=np.zeros(count,bool);rejected[labels[src[s[dst]>s[src]]]]=True
order=np.argsort(labels,kind='stable');bounds=np.r_[0,np.flatnonzero(np.diff(labels[order]))+1,n];out=[]
for lo,hi in zip(bounds[:-1],bounds[1:]):
ids=order[lo:hi];g=labels[ids[0]]
if rejected[g]:continue
center=np.sum(points[ids]*m[ids,None],axis=0)/m[ids].sum();d=np.linalg.norm(points[ids]-center,axis=1)
rep=int(ids[np.lexsort((raw[ids],d))[0]])
out.append(dict(birth_min_raw_id=int(raw[ids].min()),representative_raw_id=int(raw[rep]),nodes=len(ids),
volume_mass=float(m[ids].sum()),scalar=float(s[rep]),position=list(map(float,points[rep]))))
return sorted(out,key=lambda r:r['birth_min_raw_id'])