F2b: No evaluable carries the reason ported from the tea library
Study level: Validation.reasons, built from the three gates that decide validity (triage-not-analyzable, few-clean-minutes, few-electrodes; the library's run_study reasons plus its usable-electrode gate) and invalid-file for an unreadable file. Analysis.reasons sits next to valid, level and clean_fraction. Window level: the spike detector's 2 s gate now records every reason a window is rejected (gap, state, mixed-states, muscle, frozen) and the minutes rejected by each, kept in Spikes.rejected. A window can count under more than one reason, as in the library. The platform has no transient rejection, so there is no transient reason. Metric level: biomarkers.measure returns a Measured result with the reason per unmeasured metric: few-wake-windows and no-dominant-peak (posterior rhythm), no-n2 and no-n3 (under one minute of the stage or no events), no-sleep and few-electrodes (gradient, spikes). A study that is not valid now returns its five metrics with no value, each carrying invalid-eeg. Curve scoring returns a reason next to z: no-age, age-out-of-range and no-exposure, because only scoring knows the baseline's age coverage. The reasons and the rejected minutes travel in the features JSON under metric-reasons and rejected-minutes, outside the scenario keys so the reference tables and curve training do not see them.
This commit is contained in:
@@ -12,6 +12,7 @@ MIN_EFFECTIVE = 30
|
||||
COUNT_AGES = (16.0, 37.0)
|
||||
Z_MAX = 8.0
|
||||
EXPOSURE = "minutes"
|
||||
SCORE_FIELDS = ("z", "outside", "p5", "median", "p95")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -93,6 +94,10 @@ def train(features: pd.DataFrame, scenario: str) -> dict:
|
||||
return out
|
||||
|
||||
|
||||
def _unscored(reason: str | None = None) -> dict:
|
||||
return {**dict.fromkeys(SCORE_FIELDS), "reason": reason}
|
||||
|
||||
|
||||
def _number(v) -> float | None:
|
||||
return float(v) if v is not None and math.isfinite(v) else None
|
||||
|
||||
@@ -112,7 +117,7 @@ def centiles(baseline_feature: dict, age_months: float) -> dict:
|
||||
def _score_curve(baseline: dict, value: float, age: float) -> dict:
|
||||
q = centiles(baseline, age)
|
||||
if q["median"] is None:
|
||||
return dict.fromkeys(("z", "outside", "p5", "median", "p95"))
|
||||
return _unscored("age-out-of-range")
|
||||
side = q["median"] - q["p5"] if value < q["median"] else q["p95"] - q["median"]
|
||||
z = (value - q["median"]) / (side / 1.645) if side > 0 else None
|
||||
outside = -1 if value < q["p5"] else 1 if value > q["p95"] else 0
|
||||
@@ -122,29 +127,40 @@ def _score_curve(baseline: dict, value: float, age: float) -> dict:
|
||||
"p5": q["p5"],
|
||||
"median": q["median"],
|
||||
"p95": q["p95"],
|
||||
"reason": None,
|
||||
}
|
||||
|
||||
|
||||
def _score_count(baseline: dict, value: float, age: float, exposure: float | None) -> dict:
|
||||
out = dict.fromkeys(("z", "outside", "p5", "median", "p95"))
|
||||
rate, k = baseline["rate"], baseline["k"]
|
||||
lo, hi = baseline["ages"]
|
||||
if exposure is None or not math.isfinite(rate) or not (exposure > 0 and lo <= age < hi):
|
||||
return out
|
||||
if not lo <= age < hi:
|
||||
return _unscored("age-out-of-range")
|
||||
if exposure is None or not math.isfinite(rate) or not exposure > 0:
|
||||
return _unscored("no-exposure")
|
||||
n, mu = float(np.round(value * exposure)), rate * exposure
|
||||
law = stats.poisson(mu) if k is None else stats.nbinom(k, k / (k + mu))
|
||||
tail, at = law.sf(n - 1), law.pmf(n)
|
||||
z = float(np.clip(stats.norm.ppf(1 - (tail - at / 2)), -Z_MAX, Z_MAX))
|
||||
p5, median, p95 = (float(law.ppf(p)) / exposure for p in (0.05, 0.5, 0.95))
|
||||
return {"z": z, "outside": 1 if tail < 0.05 else 0, "p5": p5, "median": median, "p95": p95}
|
||||
return {
|
||||
"z": z,
|
||||
"outside": 1 if tail < 0.05 else 0,
|
||||
"p5": p5,
|
||||
"median": median,
|
||||
"p95": p95,
|
||||
"reason": None,
|
||||
}
|
||||
|
||||
|
||||
def score(
|
||||
baseline: dict, value: float | None, age_months: float | None, exposure: float | None = None
|
||||
) -> dict:
|
||||
value, age_months = _number(value), _number(age_months)
|
||||
if value is None or age_months is None:
|
||||
return dict.fromkeys(("z", "outside", "p5", "median", "p95"))
|
||||
if value is None:
|
||||
return _unscored()
|
||||
if age_months is None:
|
||||
return _unscored("no-age")
|
||||
if baseline["kind"] == "count_rate":
|
||||
return _score_count(baseline, value, age_months, _number(exposure))
|
||||
return _score_curve(baseline, value, age_months)
|
||||
|
||||
Reference in New Issue
Block a user