split_quantiles
Transformations for splitting quantile codes back into their components.
main(cfg)
Split quantile codes in MEDS dataset.
Source code in meds_torch/utils/split_quantiles.py
split_quantile_codes(df, ignore_pattern=None)
Split codes with quantile suffixes (e.g., ‘lab//A//_Q_1’) into separate code and quantile columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
LazyFrame with columns including ‘code’ that may contain quantile suffixes |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
LazyFrame with original code and extracted quantile value |
Examples:
from datetime import datetime df = pl.DataFrame({ … “subject_id”: [1, 1, 2, 2], … “time”: [ … datetime(2021, 1, 1), … datetime(2021, 1, 2), … datetime(2022, 10, 2), … datetime(2022, 10, 2), … ], … “code”: [“lab//A//_Q_1”, “dx//B”, “lab//A//_Q_4”, “lab//C”], … “numeric_value”: [1.0, None, 3.0, None], … }) result = split_quantile_codes(df.lazy()).collect() result shape: (6, 5) ┌────────────┬─────────────────────┬────────┬───────────────┬──────────┐ │ subject_id ┆ time ┆ code ┆ numeric_value ┆ quantile │ │ — ┆ — ┆ — ┆ — ┆ — │ │ i64 ┆ datetime[μs] ┆ str ┆ f64 ┆ i64 │ ╞════════════╪═════════════════════╪════════╪═══════════════╪══════════╡ │ 1 ┆ 2021-01-01 00:00:00 ┆ lab//A ┆ 1.0 ┆ 1 │ │ 1 ┆ 2021-01-01 00:00:00 ┆ 1 ┆ 1.0 ┆ 1 │ │ 1 ┆ 2021-01-02 00:00:00 ┆ dx//B ┆ null ┆ null │ │ 2 ┆ 2022-10-02 00:00:00 ┆ lab//A ┆ 3.0 ┆ 4 │ │ 2 ┆ 2022-10-02 00:00:00 ┆ 4 ┆ 3.0 ┆ 4 │ │ 2 ┆ 2022-10-02 00:00:00 ┆ lab//C ┆ null ┆ null │ └────────────┴─────────────────────┴────────┴───────────────┴──────────┘ result = split_quantile_codes(df.lazy(), “^lab.*$”).collect() result shape: (4, 5) ┌────────────┬─────────────────────┬──────────────┬───────────────┬──────────┐ │ subject_id ┆ time ┆ code ┆ numeric_value ┆ quantile │ │ — ┆ — ┆ — ┆ — ┆ — │ │ i64 ┆ datetime[μs] ┆ str ┆ f64 ┆ i64 │ ╞════════════╪═════════════════════╪══════════════╪═══════════════╪══════════╡ │ 1 ┆ 2021-01-01 00:00:00 ┆ lab//A//_Q_1 ┆ 1.0 ┆ 1 │ │ 1 ┆ 2021-01-02 00:00:00 ┆ dx//B ┆ null ┆ null │ │ 2 ┆ 2022-10-02 00:00:00 ┆ lab//A//_Q_4 ┆ 3.0 ┆ 4 │ │ 2 ┆ 2022-10-02 00:00:00 ┆ lab//C ┆ null ┆ null │ └────────────┴─────────────────────┴──────────────┴───────────────┴──────────┘