clinops.temporal¶
clinops.temporal.windower.TemporalWindower
¶
TemporalWindower(
window_hours=24.0,
step_hours=6.0,
imputation=ImputationStrategy.FORWARD_FILL,
min_observations=1,
aggregations=None,
label_col=None,
label_fn=None,
)
Extract fixed-size feature windows from long-format clinical time-series.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_hours
|
float
|
Duration of each window in hours. |
24.0
|
step_hours
|
float
|
Step between window starts in hours. Use same value as window_hours for non-overlapping (tumbling) windows. |
6.0
|
imputation
|
ImputationStrategy
|
Imputation strategy for within-window missing values. |
FORWARD_FILL
|
min_observations
|
int
|
Drop windows with fewer non-null observations than this threshold. |
1
|
aggregations
|
dict[str, str | Callable[..., Any]] | None
|
Column → aggregation function mapping. If empty, mean is used for all numeric columns. |
None
|
label_col
|
str | None
|
Column name of binary/multi-class outcome labels (optional). |
None
|
label_fn
|
Callable[..., Any] | None
|
How to derive the label for a window. Default: last non-null value. |
None
|
Examples:
>>> windower = TemporalWindower(window_hours=24, step_hours=6)
>>> windows = windower.fit_transform(
... df=chartevents,
... id_col="subject_id",
... time_col="charttime",
... feature_cols=["heart_rate", "spo2", "resp_rate", "map"],
... )
>>> windows.shape
(4820, 6) # (n_windows, n_features + id + window_start)
Source code in clinops/temporal/windower.py
fit_transform
¶
Extract windows from a long-format clinical DataFrame.
Supports two input formats:
Wide format (one column per feature):
df has columns like heart_rate, spo2, map.
Pass feature_cols to select which columns to use.
Long format (item × value pairs):
df has an item_col (e.g. itemid) and a
value_col (e.g. valuenum). Data is pivoted before
windowing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input DataFrame in long or wide format. |
required |
id_col
|
str
|
Column identifying each patient/subject. |
required |
time_col
|
str
|
Datetime column for temporal ordering. |
required |
feature_cols
|
list[str] | None
|
Columns to include as features (wide format). |
None
|
value_col
|
str | None
|
Numeric value column (long format with item_col). |
None
|
item_col
|
str | None
|
Item identifier column (long format, e.g. itemid). |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per (patient, window_start). Columns:
|
Source code in clinops/temporal/windower.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
clinops.temporal.windower.WindowConfig
dataclass
¶
WindowConfig(
window_hours=24.0,
step_hours=6.0,
min_observations=1,
imputation=ImputationStrategy.FORWARD_FILL,
aggregations=dict(),
label_col=None,
label_fn=None,
)
Configuration for temporal window extraction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window_hours
|
float
|
Duration of each feature window in hours. |
24.0
|
step_hours
|
float
|
Step size between consecutive windows (< window_hours = overlapping). Set equal to window_hours for tumbling (non-overlapping) windows. |
6.0
|
min_observations
|
int
|
Minimum number of non-null observations required per window. Windows below this threshold are dropped. |
1
|
imputation
|
ImputationStrategy
|
Strategy used to fill gaps within each window. |
FORWARD_FILL
|
aggregations
|
dict[str, str | Callable[..., Any]]
|
Dict mapping feature column names to aggregation functions. Defaults to mean for all numeric columns. |
dict()
|
label_col
|
str | None
|
Optional column name containing outcome labels. If provided, labels are extracted per window using label_fn. |
None
|
label_fn
|
Callable[..., Any] | None
|
Function (window_df) → label applied to each window's rows within the label_col. Defaults to last observed value. |
None
|
clinops.temporal.imputation.Imputer
¶
Imputer(
strategy=ImputationStrategy.FORWARD_FILL,
max_gap_hours=None,
time_col=None,
per_patient=False,
id_col=None,
)
Apply a chosen imputation strategy to a DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
ImputationStrategy
|
Imputation strategy to apply. |
FORWARD_FILL
|
max_gap_hours
|
float | None
|
For FORWARD_FILL and BACKWARD_FILL: maximum gap (in hours) to
fill across. Gaps larger than this are left as NaN to avoid
propagating stale values across long time periods. Requires a
|
None
|
time_col
|
str | None
|
Name of the datetime column (used with max_gap_hours). |
None
|
per_patient
|
bool
|
If True and strategy is MEAN/MEDIAN, compute statistics per patient group rather than globally. |
False
|
id_col
|
str | None
|
Patient identifier column (required when per_patient=True). |
None
|
Source code in clinops/temporal/imputation.py
fit
¶
Compute imputation statistics from a reference DataFrame (training set).
Source code in clinops/temporal/imputation.py
transform
¶
Apply imputation to df. Call fit() first for MEAN/MEDIAN strategies.
Source code in clinops/temporal/imputation.py
clinops.temporal.imputation.ImputationStrategy
¶
Bases: StrEnum
Supported imputation strategies.
FORWARD_FILL Carry the last observed value forward in time. Appropriate for slowly-changing vitals (heart rate, SpO2) where repeated measurements are assumed stable until updated.
BACKWARD_FILL Fill from the next observed value backward. Useful when a measurement is known to have been taken but not yet recorded.
LINEAR Linear interpolation between surrounding observations. Use for continuous physiological signals with regular sampling.
MEAN
Replace missing values with the column mean (global, per patient,
or per cohort depending on fit scope).
MEDIAN Replace with column median. More robust than mean for skewed lab values.
ZERO Fill with zero. Use only for count-based features where absence genuinely means zero (e.g. number of interventions).
INDICATOR
Add a binary missingness indicator column ({col}_missing)
and fill values with zero. Lets the model learn from
missingness patterns directly.
NONE Do not impute — leave NaN values in place.
clinops.temporal.features.LagFeatureBuilder
¶
Add lag and rolling-statistics features to windowed clinical data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lags
|
list[int] | None
|
List of lag steps (in window units) to include. e.g. [1, 2, 4] adds features at t-1, t-2, t-4 windows back. |
None
|
rolling_windows
|
list[int] | None
|
List of rolling window sizes to compute mean/std over. |
None
|
feature_cols
|
list[str] | None
|
Columns to create lags for. If None, all numeric columns are used. |
None
|
id_col
|
str
|
Patient identifier column — lags are computed within each patient. |
'subject_id'
|
Examples:
>>> builder = LagFeatureBuilder(lags=[1, 2], rolling_windows=[4])
>>> enriched = builder.fit_transform(windows_df, id_col="subject_id")
Source code in clinops/temporal/features.py
fit_transform
¶
Add lag and rolling features. Returns enriched DataFrame.
Source code in clinops/temporal/features.py
clinops.temporal.cohort.CohortAligner
¶
CohortAligner(
anchor_col="icu_intime",
id_col="subject_id",
max_hours_before=0.0,
max_hours_after=48.0,
time_col="charttime",
)
Align multiple patients' time-series to a common reference event.
In clinical research it's common to align patients relative to an anchor event (e.g. ICU admission, ventilation start, first sepsis flag) rather than using wall-clock time. This class handles the realignment so downstream models see time-relative-to-event rather than absolute timestamps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor_col
|
str
|
Column containing the anchor event timestamp for each patient. |
'icu_intime'
|
id_col
|
str
|
Patient identifier column. |
'subject_id'
|
max_hours_before
|
float
|
Include data up to this many hours before the anchor event. |
0.0
|
max_hours_after
|
float
|
Include data up to this many hours after the anchor event. |
48.0
|
Examples:
>>> aligner = CohortAligner(anchor_col="icu_intime", max_hours_after=48)
>>> aligned = aligner.align(chartevents, admissions)
Source code in clinops/temporal/features.py
align
¶
Align events to anchor timestamps from a reference DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events_df
|
DataFrame
|
Long-format events with id_col and time_col. |
required |
anchor_df
|
DataFrame
|
One row per patient with id_col and anchor_col. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
events_df filtered to the alignment window with a new
|