Core¶
Trajectories and datasets are the shared representation every other part of
ehr2rl reads and returns. See the synthetic quickstart
for a walkthrough.
- class ehr2rl.PatientTrajectory(subject_id, admission_id, timestamps, states, actions, rewards, terminals, metadata=<factory>)¶
One patient’s episode as aligned per-timestep arrays.
Tis the number of timesteps,Dthe number of state features, andAthe number of action columns. Arrays are converted to NumPy and their shapes validated on construction; a one-dimensionalactionsarray is reshaped to(T, 1).- Parameters:
subject_id (
str) – MIMIC-IVsubject_id, as a string.admission_id (
str) – MIMIC-IVhadm_id, as a string.timestamps (
ndarray) – Shape(T,). Unix time in seconds for each step.states (
ndarray) – Shape(T, D), float. Column names are inmetadata["feature_names"]when known.actions (
ndarray) – Shape(T, A).rewards (
ndarray) – Shape(T,), float.terminals (
ndarray) – Shape(T,), bool.Truewhere the episode ends.metadata (
dict) – Per-trajectory information such asdied,feature_names,action_names,action_sizes,sofa_scores, andprovenance.
- Raises:
ValueError – If there are no timesteps or an array has the wrong shape.
- class ehr2rl.EHRDataset(root=None, trajectories=None)¶
A collection of patient trajectories.
Supports
len(), iteration, and integer indexing over its trajectories. Theload_*andfeaturizemethods implement the frozen local CSV path. They modify the dataset in place and return it, so calls can be chained.- Parameters:
- Variables:
- copy_with(trajectories)¶
Return a new dataset with this one’s root and tables but other trajectories.
- Parameters:
trajectories (
Iterable[PatientTrajectory]) – Trajectories for the new dataset.- Returns:
A new dataset. The
tablesmapping is copied; the tables themselves are shared.- Return type:
- load_admissions(filename='hosp/admissions.csv.gz')¶
Load the admissions table from
root.- Parameters:
filename (
str) – Path relative toroot. If it does not exist,admissions.csvis tried instead.- Returns:
This dataset, for chaining.
- Return type:
- Raises:
EHRValidationError – If the file is missing or lacks required columns.
ValueError – If
rootis not set.
- load_vitals(filename='icu/chartevents.csv.gz', resample='1h')¶
Load ICU chart events from
rootand average them into time bins.- Parameters:
- Returns:
This dataset, for chaining.
- Return type:
- Raises:
EHRValidationError – If the file is missing or lacks required columns.
ValueError – If
rootis not set or a value is not numeric.
- load_labs(filename='hosp/labevents.csv.gz', codes=None)¶
Load lab events from
root.- Parameters:
- Returns:
This dataset, for chaining.
- Return type:
- Raises:
EHRValidationError – If the file is missing or lacks required columns.
ValueError – If
rootis not set or a value is not numeric.
- featurize(pipeline='standard')¶
Build trajectories from the loaded tables, replacing
trajectories.Does nothing when no tables are loaded.
- Parameters:
pipeline (
str) – Featurization pipeline."standard"is the only one.- Returns:
This dataset, for chaining.
- Return type:
- Raises:
ValueError – If
pipelineis not"standard".
The load_* and featurize methods implement the frozen
local CSV path.
- exception ehr2rl.EHRValidationError¶
Raised when input tables, an itemid map, or a feature preset is invalid.