82 lines
3.2 KiB
Python
82 lines
3.2 KiB
Python
"""Process-conditioned target, hierarchy weights and scoring; no fitting I/O."""
|
|
import numpy as np
|
|
|
|
from .features import finite_array
|
|
|
|
|
|
def positive_scale(value, name):
|
|
v = float(value)
|
|
if not np.isfinite(v) or v <= 0:
|
|
raise ValueError(f"{name} must be finite and positive")
|
|
return v
|
|
|
|
|
|
def dimensionless_target(seconds, t_set):
|
|
t = finite_array(seconds, name="seconds")
|
|
if np.any(t < 0):
|
|
raise ValueError("negative arrival time")
|
|
return t / positive_scale(t_set, "t_set")
|
|
|
|
|
|
def prediction_seconds(t_star, t_set):
|
|
"""Direct target per Claude 1794667074.550589: no log transform or tau.
|
|
|
|
Predictions are not clipped; invalid output fails closed and is reported.
|
|
"""
|
|
t = finite_array(t_star, name="prediction t_star")
|
|
return finite_array(t * positive_scale(t_set, "t_set"), name="predicted seconds")
|
|
|
|
|
|
def b0_seconds(relative_volume, t_set):
|
|
vf = finite_array(relative_volume, name="relative flow volume")
|
|
if np.any((vf < 0) | (vf > 1)):
|
|
raise ValueError("relative volume outside [0,1]")
|
|
return vf * positive_scale(t_set, "t_set")
|
|
|
|
|
|
def hierarchy_weights(draw_counts, part_ids, component_ids):
|
|
"""Sum=1; equal components, equal parts in each, count-weighted nodes.
|
|
|
|
Call on the full learning pool, not on one minibatch. A batch-1-part
|
|
objective sums each part's weighted contribution over the epoch; if
|
|
sampling parts uniformly with replacement multiply each contribution by
|
|
the number of learning parts for an unbiased per-step objective.
|
|
"""
|
|
c = finite_array(draw_counts, name="draw counts")
|
|
p, g = np.asarray(part_ids), np.asarray(component_ids)
|
|
if c.ndim != 1 or not len(c) or p.shape != c.shape or g.shape != c.shape:
|
|
raise ValueError("invalid hierarchy shape")
|
|
if np.any(c <= 0) or np.any(c != np.floor(c)):
|
|
raise ValueError("only eligible positive draw counts accepted")
|
|
groups = np.unique(g)
|
|
w = np.empty_like(c)
|
|
for part in np.unique(p):
|
|
mask = p == part
|
|
if len(np.unique(g[mask])) != 1:
|
|
raise ValueError("one part belongs to multiple components")
|
|
for group in groups:
|
|
mask_g = g == group
|
|
parts = np.unique(p[mask_g])
|
|
for part in parts:
|
|
mask = mask_g & (p == part)
|
|
w[mask] = c[mask] / c[mask].sum() / len(parts) / len(groups)
|
|
return w
|
|
|
|
|
|
def relative_l2(prediction, truth, draw_counts, eligible):
|
|
p, t, c = np.asarray(prediction), np.asarray(truth), np.asarray(draw_counts)
|
|
e = np.asarray(eligible)
|
|
if p.ndim != 1 or t.shape != p.shape or c.shape != p.shape or e.shape != p.shape or e.dtype.kind != "b":
|
|
raise ValueError("invalid score arrays")
|
|
# Mask before any arithmetic: ineligible NaNs must never enter 0*NaN.
|
|
p, t, c = (finite_array(v[e], name="eligible score input") for v in (p, t, c))
|
|
if not len(c) or np.any(c <= 0) or np.any(c != np.floor(c)):
|
|
raise ValueError("no eligible positive integer draw counts")
|
|
denominator = np.sum(c*t*t)
|
|
if denominator <= 0:
|
|
return {"value": None, "reason": "zero truth norm"}
|
|
value = float(np.sqrt(np.sum(c*(p-t)**2)/denominator))
|
|
if not np.isfinite(value):
|
|
raise ValueError("nonfinite relative L2")
|
|
return {"value": value, "reason": None}
|