Files
MeltMind/tools/s2_tail_diagnostic_math.py
T

110 lines
5.7 KiB
Python

"""05344bac discrete volume tail and same-domain peak certificates; CPU only."""
import math
import numpy as np
ALPHA = .01
RADIUS = .10
MASS_TOL = 1e-12
def weights(mass):
m = np.asarray(mass, np.float64)
if m.ndim != 1 or not len(m) or not np.isfinite(m).all() or np.any(m <= 0):
raise ValueError('Finite positive lumped mass required')
total = math.fsum(m)
if not math.isfinite(total) or total <= 0:
raise ValueError('Invalid volume denominator')
return m / total
def tail_membership(values, w):
s = np.asarray(values, np.float64)
if s.ndim != 1 or s.shape != w.shape or not np.isfinite(s).all():
raise ValueError('Finite full-domain field required')
# Locate a candidate boundary; all final plateau masses use compensated sums.
order = np.argsort(-s, kind='stable')
at = min(int(np.searchsorted(np.cumsum(w[order]), ALPHA)), len(s)-1)
q = float(s[order[at]])
above, tied = s > q, s == q
high, equal = math.fsum(w[above]), math.fsum(w[tied])
remainder = ALPHA-high
fixed = min(max(remainder, 0.), equal)
correction = fixed-remainder
if equal <= 0 or abs(correction) > MASS_TOL:
raise ValueError('Tail boundary exceeds fixed mass correction tolerance')
fraction = fixed/equal
a = above.astype(np.float64)
a[tied] = fraction
actual = math.fsum(w*a)
if abs(actual-ALPHA) > MASS_TOL:
raise ValueError('Tail volume differs from alpha')
return a, dict(threshold=q, plateau_volume=equal, plateau_nodes=int(np.sum(tied)), plateau_fraction=fraction,
mass_correction=correction, tail_volume=actual,
constant_field=bool(np.all(s == s[0])))
def tail_error(a, b, w):
intersection = math.fsum(w*np.minimum(a, b))
miss = math.fsum(w*np.maximum(a-b, 0.))/ALPHA
false = math.fsum(w*np.maximum(b-a, 0.))/ALPHA
# Mean directional loss is numerically robust at identical fractional tails.
error = (miss+false)/2
identity = 1-intersection/ALPHA
if abs(error-identity) > 2*MASS_TOL/ALPHA or abs(miss-false) > 2*MASS_TOL/ALPHA:
raise ValueError('Equal tail mass / intersection identity')
if not 0 <= error <= 1+2*MASS_TOL/ALPHA:
raise ValueError('Tail error outside range')
return dict(E_tail=error, Miss=miss, False_alarm=false, intersection=intersection,
intersection_formula=identity, finite_precision_identity_difference=error-identity)
def peak_diagnostic(truth, pred, points, raw, rg, *, w=None, true_tail=None):
t, p, x = np.asarray(truth, np.float64), np.asarray(pred, np.float64), np.asarray(points, np.float64)
raw = np.asarray(raw)
if t.ndim != 1 or p.shape != t.shape or raw.shape != t.shape or x.shape != (len(t),3) or not len(t):
raise ValueError('Peak domain shape')
if not all(np.isfinite(v).all() for v in [t,p,x]) or not math.isfinite(rg) or rg <= 0:
raise ValueError('Peak finite domain and positive Rg')
# Query duplicate records must not change maxima, epsilon or distances.
ids, first, inverse = np.unique(raw, return_index=True, return_inverse=True)
if len(ids) != len(raw):
if w is not None or true_tail is not None:
raise ValueError('Full volume domain requires unique IDs')
if not np.array_equal(t, t[first][inverse]) or not np.array_equal(p, p[first][inverse]) or not np.array_equal(x, x[first][inverse]):
raise ValueError('Conflicting duplicated query identity')
t,p,x,raw = t[first],p[first],x[first],ids
top, predicted_top = np.flatnonzero(t == t.max()), np.flatnonzero(p == p.max())
y = int(top[np.argmin(raw[top])]); yh = int(predicted_top[np.argmin(raw[predicted_top])])
epsilon = float(np.max(np.abs(p-t)))
distances = np.linalg.norm(x-x[y], axis=1)/rg
exterior = distances > RADIUS
trivial = not bool(exterior.any())
gamma = None if trivial else float(t[y]-np.max(t[exterior]))
margin = None if trivial else gamma-2*epsilon
eta = float(64*np.finfo(np.float64).eps*max(1.,float(np.max(np.abs(t))),float(np.max(np.abs(p))),2*epsilon))
certified = trivial or gamma > 2*epsilon
near = bool(not trivial and abs(margin) <= eta)
distance = float(distances[yh])
if certified and distance > RADIUS:
raise ValueError('Certificate contradicts observed peak distance')
peakset = float(np.min(np.linalg.norm(x[top]-x[yh], axis=1))/rg)
taildist = None if true_tail is None else float(np.min(np.linalg.norm(x[true_tail > 0]-x[yh], axis=1))/rg)
if distance > RADIUS and peakset == 0:
category = 'OTHER_EXACT_TRUE_PEAK'
elif peakset <= RADIUS:
category = 'NEAR_TRUE_PEAK'
elif taildist is None:
category = 'FAR_TRUE_PEAK_QUERY'
elif taildist <= RADIUS:
category = 'NEAR_TRUE_TAIL_AWAY_FROM_PEAK'
else:
category = 'FAR_TRUE_TAIL'
return dict(nodes=len(t),epsilon=epsilon,gamma=gamma,gamma_positive_infinity=trivial,
gamma_minus_2epsilon=margin,eta=eta,near_boundary=near,trivial_radius=trivial,
certified=certified,stable_certificate=certified and not near,
peak_distance_over_Rg=distance,d_peakset_over_Rg=peakset,d_tail_over_Rg=taildist,
true_peak_raw_id=int(raw[y]),predicted_peak_raw_id=int(raw[yh]),
true_peak_count=len(top),predicted_peak_count=len(predicted_top),
true_peak_volume=None if w is None else math.fsum(w[top]),
predicted_peak_volume=None if w is None else math.fsum(w[predicted_top]),
true_time_drop_at_predicted_peak=float(t[y]-t[yh]),
predicted_minus_true_peak_time=float(p[yh]-t[y]),
constant_truth=bool(np.all(t==t[0])),category=category,
topology_branch_identified=False)