Rewards¶
See Rewards for the metadata each reward needs and how to write your own.
- class ehr2rl.reward.BaseReward¶
Base class for reward functions.
Subclasses implement
compute;shapeapplies it to every trajectory in a dataset.- abstractmethod compute(trajectory)¶
Return one reward per timestep for a trajectory.
- Parameters:
trajectory (
PatientTrajectory) – Trajectory to score. It must not be modified.- Returns:
Shape
(T,).- Return type:
- shape(dataset, policy=None)¶
Return a copy of a dataset with rewards computed for every trajectory.
- Parameters:
dataset (
EHRDataset) – Input dataset. It is not modified.policy (
object|None) – Reserved for policy-dependent rewards. Ignored by all built-in rewards.
- Returns:
A new dataset with new trajectories.
- Return type:
- class ehr2rl.MortalityReward(survival_reward=1.0, death_penalty=-1.0)¶
Terminal reward for in-hospital survival.
Reads
metadata["died"]; a missing key counts as survival.- Parameters:
- compute(trajectory)¶
Place the survival reward or death penalty at the last terminal step.
- Parameters:
trajectory (
PatientTrajectory) – Trajectory with at least oneTrueterminal.- Returns:
Shape
(T,), zero except at the last terminal step.- Return type:
- class ehr2rl.SofaReward(clip=(-5.0, 5.0))¶
Dense reward for change in SOFA score; improvement is positive.
- Parameters:
clip (
tuple[float,float] |None) –(low, high)bounds for each step’s reward, orNonefor no clipping.
- compute(trajectory)¶
Return
sofa[t - 1] - sofa[t]per step, with0at the first step.- Parameters:
trajectory (
PatientTrajectory) – Trajectory withmetadata["sofa_scores"]of shape(T,).- Returns:
Shape
(T,), clipped toclip.- Return type:
- Raises:
ValueError – If
sofa_scoresis missing or has the wrong shape.
- class ehr2rl.ReadmissionReward(window_days=30, readmitted_reward=-1.0, not_readmitted_reward=1.0, censored_value=nan)¶
Terminal reward for 30-day hospital readmission.
Reads
metadata["readmitted_within_30_days"](True,False, orNonewhen follow-up is censored). Patients who died get0.- Parameters:
window_days (
int) – Stored for reference but not used; the window is set by the metadata.readmitted_reward (
float) – Reward at the final step when readmitted.not_readmitted_reward (
float) – Reward at the final step when not readmitted.censored_value (
float) – Reward at the final step when follow-up is censored. The default,NaN, must be replaced or filtered out before training.
- compute(trajectory)¶
Place the readmission reward at the final step.
- Parameters:
trajectory (
PatientTrajectory) – Trajectory withmetadata["readmitted_within_30_days"].- Returns:
Shape
(T,), zero except at the final step.- Return type:
- Raises:
ValueError – If
readmitted_within_30_daysis missing.
- class ehr2rl.CompositeReward(components)¶
Weighted sum of other reward functions.
- Parameters:
components (
Sequence[tuple[BaseReward,float]]) –(reward, weight)pairs. At least one is required.- Raises:
ValueError – If
componentsis empty.TypeError – If a component is not a
BaseReward.
- compute(trajectory)¶
Return the weighted sum of every component’s rewards.
- Parameters:
trajectory (
PatientTrajectory) – Trajectory with whatever metadata the components need.- Returns:
Shape
(T,).- Return type:
- Raises:
ValueError – If a component returns the wrong shape.