"""Surrogate PK/PD transition.""" from __future__ import annotations from app.models.dosing.pkpd_state import PKPDState def step_pkpd( state: PKPDState, dose_delta: float, organ_factor: float = 0.0, interaction_factor: float = 0.0, ) -> PKPDState: # Effect benefits modestly from dose increases, but toxicity amplifies with organ stress + interactions. effective_delta = dose_delta * (1.0 - min(0.6, organ_factor * 0.4)) new_effect = max(0.0, min(1.0, state.effect_level + 0.28 * effective_delta - 0.05 * interaction_factor)) toxicity_gain = max(0.0, dose_delta) * (0.35 + organ_factor * 0.25 + interaction_factor * 0.2) new_toxicity = max(0.0, min(1.0, (state.toxicity_level * 0.85) + toxicity_gain)) new_underdose = max(0.0, min(1.0, 1.0 - new_effect + max(0.0, -dose_delta) * 0.15)) return PKPDState( effect_level=new_effect, toxicity_level=new_toxicity, underdose_risk=new_underdose, organ_stress=max(0.0, min(1.0, organ_factor)), interaction_load=max(0.0, min(1.0, interaction_factor)), )