Files
MeltMind/tools/naive_ordering_math.py

85 lines
4.2 KiB
Python

"""Array-only ordering diagnostics; no learning, archives or test selection."""
from collections import defaultdict
import numpy as np
from scipy.sparse import coo_matrix
from scipy.sparse.csgraph import dijkstra
def cdf_interval(scores, weights):
s = np.asarray(scores, dtype=np.float64)
w = np.asarray(weights, dtype=np.float64)
if s.ndim != 1 or s.shape != w.shape or not len(s) or not np.isfinite(s).all() or not np.isfinite(w).all() or np.any(w <= 0):
raise ValueError('CDF requires finite scores and positive weights')
order = np.argsort(s, kind='stable')
prefix = np.r_[0., np.cumsum(w[order])]
total = w.sum()
return prefix[np.searchsorted(s[order], s, side='left')]/total, prefix[np.searchsorted(s[order], s, side='right')]/total
def pps_cdf(scores, multiplicity):
counts = np.asarray(multiplicity, dtype=np.float64)
if not np.isfinite(counts).all() or np.any(counts != np.floor(counts)):
raise ValueError('PPS multiplicity must be an integer count, not node volume')
return cdf_interval(scores, counts)
def training_clock(rows, fold, pool, end_times):
"""Component/part-equal mean of full VTU T_end / pre-solve t_set."""
groups = defaultdict(list)
access = []
for row in rows:
a = row['assignments'][fold]
if not row['included'] or a['role'] != 'learning' or str(pool) not in a['learning_pools']:
continue
# Do not substitute a query maximum or an available validation/test case.
end = end_times[row['case']]
ratio = end/row['preparation']['t_set']
if not np.isfinite(ratio) or ratio <= 0:
raise ValueError('Invalid learning full-node end time')
groups[row['component_id']].append(float(ratio))
access.append(dict(case=row['case'], role='learning', full_end_over_t_set=float(ratio)))
if not groups:
raise ValueError('Empty original learning pool')
return float(np.mean([np.mean(x) for x in groups.values()])), access
def squared_wall_distance(points, edges, dw, gate):
x = np.asarray(points, dtype=np.float64)
e = np.asarray(edges)
d = np.asarray(dw, dtype=np.float64)
g = np.asarray(gate, dtype=np.float64)
if x.shape != (len(d), 3) or e.ndim != 2 or e.shape[1] != 2 or e.dtype.kind not in 'iu' or g.shape != (3,):
raise ValueError('Geometry shape')
if not len(d) or not len(e) or e.min() < 0 or e.max() >= len(d) or not np.isfinite(x).all() or not np.isfinite(d).all() or np.any(d < 0):
raise ValueError('Invalid geometry/distance')
source = int(np.argmin(np.linalg.norm(x-g, axis=1)))
# Piecewise-linear D_w at edge midpoint, then square; NOT mean(D_w**2).
midpoint = d[e].mean(axis=1)
keep = midpoint > 0
selected = e[keep]
cost = np.linalg.norm(x[selected[:,1]]-x[selected[:,0]], axis=1)/midpoint[keep]**2
if not np.isfinite(cost).all() or np.any(cost <= 0):
raise ValueError('Invalid positive-edge costs; no epsilon floor')
graph = coo_matrix((np.tile(cost, 2), (np.r_[selected[:,0],selected[:,1]], np.r_[selected[:,1],selected[:,0]])), shape=(len(x),len(x))).tocsr()
out = dijkstra(graph, directed=False, indices=source)
if not np.isfinite(out).all():
raise ValueError('Unreachable full tet node; keep failure, do not add zero-wall edges')
return out, source, int(np.count_nonzero(~keep))
def volume_clock_curve(truth, mass, end_full, t_set):
t = np.asarray(truth, dtype=np.float64)
m = np.asarray(mass, dtype=np.float64)
left, right = cdf_interval(t, m)
if not np.isfinite([end_full,t_set]).all() or min(end_full,t_set) <= 0:
raise ValueError('Positive seconds scales required')
grid = np.linspace(0,1,101)
order = np.argsort(t,kind='stable')
cumulative = np.cumsum(m[order])/m.sum()
indices = np.minimum(np.searchsorted(cumulative,grid,side='left'),len(t)-1)
times = t[order[indices]]
gap = np.maximum(np.maximum(left-t/end_full,t/end_full-right),0.)
return dict(volume_quantiles=grid.tolist(),seconds=times.tolist(),over_t_set=(times/t_set).tolist(),
over_full_end=(times/end_full).tolist(),max_distance_from_linear_time_to_CDF_tie_interval=float(gap.max()),
max_tie_volume=float(np.max(right-left)),tet_end=float(t.max()),full_VTU_end=float(end_full))