pyhealth.tasks.InHospitalMortalityMEDS#

class pyhealth.tasks.in_hospital_mortality_meds.InHospitalMortalityMEDS(observation_window='full_stay', window_hours=48.0, code_mapping=None)[source]#

Bases: BaseTask

In-hospital mortality prediction for MEDS datasets.

One sample per completed hospital stay. The observation window is the half-open interval [admission, prediction_time) and the binary label is whether the stay ended in death (discharge code HOSPITAL_DISCHARGE//DIED). MEDS codes observed during the window, excluding the terminating discharge event and any MEDS_DEATH, form the input sequence. See the module docstring for the full definition.

Parameters:
  • observation_window (str) – "full_stay" (default) observes the entire stay, i.e. [admission, discharge). "first_hours" observes only [admission, admission + window_hours) and keeps stays whose length exceeds window_hours (an early-warning setup with a strictly future outcome).

  • window_hours (float) – Observation length used when observation_window="first_hours". Ignored for "full_stay". Defaults to 48.0, matching InHospitalMortalityMIMIC4.

  • code_mapping (Optional[Dict[str, Tuple[str, str]]]) – Optional vocab mapping forwarded to BaseTask (e.g. {"codes": ("ICD10CM", "CCSCM")}).

task_name#

The name of the task.

Type:

str

input_schema#

codes — the sequence of MEDS codes observed during the window.

Type:

Dict[str, str]

output_schema#

mortality — binary in-hospital mortality.

Type:

Dict[str, str]

Raises:

ValueError – If observation_window is not one of "full_stay"/"first_hours", or if window_hours is not positive.

Examples

>>> from pathlib import Path
>>> import pyhealth.datasets.configs as meds_configs
>>> from pyhealth.datasets import MEDSDataset
>>> from pyhealth.tasks import InHospitalMortalityMEDS
>>> # A bundled stay-aware config exposes hadm_id (not a core MEDS
>>> # field, so it is kept out of the default configs/meds.yaml):
>>> cfg = Path(meds_configs.__file__).parent / "meds_with_hadm.yaml"
>>> dataset = MEDSDataset(
...     root="/path/to/mimic-iv-demo-meds/0.0.1",
...     config_path=str(cfg),
... )
>>> samples = dataset.set_task(InHospitalMortalityMEDS())
>>> # Early-warning variant: first 48h, stays longer than 48h only
>>> early = InHospitalMortalityMEDS(observation_window="first_hours")
task_name: str = 'InHospitalMortalityMEDS'#
input_schema: ClassVar[dict[str, str]] = {'codes': 'sequence'}#
output_schema: ClassVar[dict[str, str]] = {'mortality': 'binary'}#
pre_filter(df)[source]#

Restricts the global scan to MEDS events before per-patient calls.

All MEDS data lives in a single meds event type, so this narrows the frame once rather than per patient.

Return type:

LazyFrame