File size: 2,437 Bytes
04ae9bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | """Process features from arrived, label-free test events only.
This module has no clinical-table input or cohort lookup. Learned model and
preprocessing parameters may be loaded separately by the inference process.
"""
from __future__ import annotations
import numpy as np
class StreamProcessFeatures:
FIELDS = {"visit_id", "patient_id", "visit_month"}
def __init__(self, months, mode="observed"):
if mode not in {"observed", "current_only"}:
raise ValueError(mode)
self.months = [int(m) for m in months]
self.mode = mode
self.history = {}
self.last_month = -1
self.seen_visits = set()
@property
def names(self):
return [f"v{m}" for m in self.months] + ["control", "gap", "prior_count", "visit_month"]
def update(self, event):
if set(event) != self.FIELDS:
raise ValueError("Inference events must contain exactly visit_id, patient_id, visit_month")
patient, month = int(event["patient_id"]), int(event["visit_month"])
visit = str(event["visit_id"])
if month < self.last_month:
raise ValueError("Test events must arrive in chronological order")
if visit in self.seen_visits:
raise ValueError("Duplicate test event")
self.last_month = month
self.seen_visits.add(visit)
if self.mode == "current_only":
flags = [1.0 if m == month else -1.0 for m in self.months]
return np.asarray(flags + [-1., -1., -1., float(month)], dtype=np.float32)
previous = self.history.get(patient, set())
past = sorted(m for m in previous if m < month)
available = previous | {month}
self.history[patient] = available
flags = [-1. if m > month else float(m in available) for m in self.months]
first_nonzero = min((m for m in available if m > 0), default=None)
control = -1. if month < 12 else float(first_nonzero == 12)
gap = float(month - past[-1]) if past else -1.
return np.asarray(flags + [control, gap, float(len(past)), float(month)], dtype=np.float32)
def features_for_events(events, months, mode="observed"):
builder = StreamProcessFeatures(months, mode)
rows, visits = [], []
for event in events:
rows.append(builder.update(event))
visits.append(event["visit_id"])
return np.asarray(rows, dtype=np.float32), visits, builder.names
|