Actions

See Medication actions for how doses are aggregated and binned.

class ehr2rl.actions.ActionConfig(vasopressor_bins, fluid_bins, timestep='1h', status_include=('FinishedRunning', 'Changed'))

Configuration for medication-derived actions.

Parameters:
  • vasopressor_bins (DoseBins) – Bins for norepinephrine-equivalent dose in mcg/kg/min.

  • fluid_bins (DoseBins) – Bins for IV fluid volume in mL per timestep.

  • timestep (str) – Aggregation window, as a pandas frequency string.

  • status_include (tuple[str, ...]) – statusdescription values to keep. Other rows are ignored.

class ehr2rl.actions.DoseBins(edges, labels=None)

Assign continuous dose values to integer bins.

Bin 0 is reserved for exactly zero or lower values. Positive values are assigned with numpy.searchsorted(..., side="right") so edge values fall into the higher bin.

Parameters:
  • edges (tuple[float, ...]) – Bin edges in ascending order. At least one is required.

  • labels (tuple[int, ...] | None) – Optional output label for each bin, with len(edges) + 1 values.

Raises:

ValueError – If edges is empty or unsorted, or labels has the wrong length.

property n_bins: int

Number of discrete values assign can return (largest label + 1).

assign(values)

Return one bin label per dose value.

Parameters:

values (ndarray | Series) – Doses, in any shape.

Returns:

Integer labels with the same shape as values.

Return type:

ndarray

ehr2rl.actions.build_medication_actions(inputevents, timestamps, *, config)

Build (vasopressor_bin, fluid_bin) actions aligned to state timesteps.

Vasopressor exposure is aggregated with max norepinephrine-equivalent dose per timestep so short high-intensity infusion intervals are not diluted. Fluid exposure is allocated by overlap duration when an event spans multiple timesteps, so one recorded amount is not counted in full more than once.

Parameters:
  • inputevents (DataFrame) – Rows with starttime, endtime, itemid, rate, and amount, and optionally rateuom, patientweight, and statusdescription.

  • timestamps (ndarray) – Shape (T,). State times in Unix seconds.

  • config (ActionConfig) – Bins and aggregation settings.

Returns:

Shape (T, 2) integer array of (vasopressor_bin, fluid_bin).

Return type:

ndarray

Raises:

ValueError – If a vasopressor row’s rate unit cannot be converted.

Norepinephrine equivalents

class ehr2rl.actions.VasopressorConversion(name, itemids, factor, rate_unit='mcg/kg/min')

Conversion factor for norepinephrine-equivalent dose.

Parameters:
  • name (str) – Drug name, used in error messages.

  • itemids (tuple[int, ...]) – MIMIC-IV inputevents itemids for the drug.

  • factor (float) – Multiplier from a rate in rate_unit to norepinephrine mcg/kg/min.

  • rate_unit (str) – Unit factor applies to. Recorded rates are converted to it first.

ehr2rl.actions.DEFAULT_NEE_CONVERSIONS

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable’s items.

If the argument is a tuple, the return value is the same object.

ehr2rl.actions.norepinephrine_equivalent(row, conversions=(VasopressorConversion(name='norepinephrine', itemids=(221906,), factor=1.0, rate_unit='mcg/kg/min'), VasopressorConversion(name='epinephrine', itemids=(221289,), factor=1.0, rate_unit='mcg/kg/min'), VasopressorConversion(name='vasopressin', itemids=(222315,), factor=2.5, rate_unit='units/min'), VasopressorConversion(name='phenylephrine', itemids=(221749,), factor=0.1, rate_unit='mcg/kg/min'), VasopressorConversion(name='dopamine', itemids=(221662,), factor=0.01, rate_unit='mcg/kg/min')))

Return norepinephrine-equivalent dose for an inputevents row.

The row’s rateuom is converted to the conversion’s rate_unit before factor is applied. Rows without rateuom are assumed to already be in rate_unit.

Parameters:
  • row (Series) – One inputevents row with itemid and rate, and optionally rateuom and patientweight.

  • conversions (Iterable[VasopressorConversion]) – Conversions to match itemid against.

Returns:

Dose in norepinephrine mcg/kg/min. 0.0 for non-vasopressor rows and rows without a numeric rate.

Return type:

float

Raises:

ValueError – If a vasopressor row’s unit cannot be converted, or a per-minute rate has no positive patientweight. This is raised rather than returning a dose in the wrong unit.