custom_text_normalization
Transformations for normalizing MEDS datasets, across both categorical and continuous dimensions.
main(cfg)
Calls the normalize function on a polars dataframe.
Source code in meds_torch/utils/custom_text_normalization.py
normalize(df, code_metadata, code_modifiers=None)
Normalize a MEDS dataset across both categorical and continuous dimensions.
This function expects a MEDS dataset in flattened form, with columns for:
- subject_id
- time
- code
- numeric_value
- text_value
In addition, the code_metadata dataset should contain information about the codes in the MEDS dataset,
including the mandatory columns:
- code (categorical)
- code/vocab_index (int)
- Any code_modifiers columns, if specified
Additionally, it must either have:
- Pre-computed means and standard deviations for the numeric values of the codes in the MEDS dataset,
via:
- values/mean (float)
- values/std (float)
- Or the necessary statistics to compute the per-occurrence mean and standard deviation of the numeric
values of the codes in the MEDS dataset, via:
- values/n_occurrences (int)
- values/sum (float)
- values/sum_sqd (float)
The values/* functions will be used to normalize the code numeric values to have a mean of 0 and a
standard deviation of 1. The output dataframe will further be filtered to only contain rows where the
code in the MEDS dataset appears in the code_metadata dataset, and the output code column will be
converted to the code/vocab_index integral ID from the code_metadata dataset.
This function can further be customized by specifying additional columns to join on, via the
code_modifiers parameter, which must appear in both the MEDS dataset and the code metadata. These
columns will be discarded from the output dataframe, which will only contain the four expected input
columns, though normalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
The MEDS dataset to normalize. See above for the expected schema. |
required |
code_metadata |
DataFrame
|
Metadata about the codes in the MEDS dataset. See above for the expected schema. |
required |
code_modifiers |
list[str] | None
|
Additional columns to join on, which will be discarded from the output dataframe. |
None
|
Returns:
| Type | Description |
|---|---|
LazyFrame
|
The normalized MEDS dataset, with the schema described above. |
Examples:
>>> from datetime import datetime
>>> MEDS_df = pl.DataFrame(
... {
... "subject_id": [1, 1, 1, 2, 2, 2, 3],
... "time": [
... datetime(2021, 1, 1),
... datetime(2021, 1, 1),
... datetime(2021, 1, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... ],
... "code": ["lab//A", "lab//A", "dx//B", "lab//A", "dx//D", "lab//C", "lab//F"],
... "text_value": [None, "fever", None, None, "cough", None, None],
... "numeric_value": [1, 3, None, 3, None, None, None],
... "unit": ["mg/dL", "g/dL", None, "mg/dL", None, None, None],
... },
... schema = {
... "subject_id": pl.UInt32,
... "time": pl.Datetime,
... "code": pl.Utf8,
... "text_value": pl.String,
... "numeric_value": pl.Float64,
... "unit": pl.Utf8,
... },
... )
>>> code_metadata = pl.DataFrame(
... {
... "code": ["lab//A", "lab//C", "dx//B", "dx//E", "lab//F"],
... "code/vocab_index": [0, 2, 3, 4, 5],
... "values/mean": [2.0, None, None, None, 3],
... "values/std": [0.5, None, None, None, 0.2],
... },
... schema = {
... "code": pl.Utf8,
... "code/vocab_index": pl.UInt32,
... "values/mean": pl.Float64,
... "values/std": pl.Float64,
... },
... )
>>> normalize(MEDS_df.lazy(), code_metadata).collect()
shape: (6, 5)
┌────────────┬─────────────────────┬──────┬───────────────┬────────────┐
│ subject_id ┆ time ┆ code ┆ numeric_value ┆ text_value │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ datetime[μs] ┆ u32 ┆ f64 ┆ str │
╞════════════╪═════════════════════╪══════╪═══════════════╪════════════╡
│ 1 ┆ 2021-01-01 00:00:00 ┆ 0 ┆ -2.0 ┆ null │
│ 1 ┆ 2021-01-01 00:00:00 ┆ 0 ┆ 2.0 ┆ fever │
│ 1 ┆ 2021-01-02 00:00:00 ┆ 3 ┆ null ┆ null │
│ 2 ┆ 2022-10-02 00:00:00 ┆ 0 ┆ 2.0 ┆ null │
│ 2 ┆ 2022-10-02 00:00:00 ┆ 2 ┆ null ┆ null │
│ 3 ┆ 2022-10-02 00:00:00 ┆ 5 ┆ null ┆ null │
└────────────┴─────────────────────┴──────┴───────────────┴────────────┘
>>> MEDS_df = pl.DataFrame(
... {
... "subject_id": [1, 1, 1, 2, 2, 2, 3],
... "time": [
... datetime(2021, 1, 1),
... datetime(2021, 1, 1),
... datetime(2021, 1, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... datetime(2022, 10, 2),
... ],
... "code": ["lab//A", "lab//A", "dx//B", "lab//A", "dx//D", "lab//C", "lab//F"],
... "text_value": [None, "fever", None, None, "cough", None, None],
... "numeric_value": [1, 3, None, 3, None, None, None],
... "unit": ["mg/dL", "g/dL", None, "mg/dL", None, None, None],
... },
... schema = {
... "subject_id": pl.UInt32,
... "time": pl.Datetime,
... "code": pl.Utf8,
... "text_value": pl.String,
... "numeric_value": pl.Float64,
... "unit": pl.Utf8,
... },
... )
>>> code_metadata = pl.DataFrame(
... {
... "code": ["lab//A", "lab//A", "lab//C", "dx//B", "dx//E", "lab//F"],
... "unit": ["mg/dL", "g/dL", None, None, None, None],
... "code/vocab_index": [0, 1, 2, 3, 4, 5],
... "values/mean": [2.0, 3.0, None, None, None, 3],
... "values/std": [0.5, 2.0, None, None, None, 0.2],
... },
... schema = {
... "code": pl.Utf8,
... "unit": pl.Utf8,
... "code/vocab_index": pl.UInt32,
... "values/mean": pl.Float64,
... "values/std": pl.Float64,
... },
... )
>>> normalize(MEDS_df.lazy(), code_metadata, ["unit"]).collect()
shape: (6, 5)
┌────────────┬─────────────────────┬──────┬───────────────┬────────────┐
│ subject_id ┆ time ┆ code ┆ numeric_value ┆ text_value │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ u32 ┆ datetime[μs] ┆ u32 ┆ f64 ┆ str │
╞════════════╪═════════════════════╪══════╪═══════════════╪════════════╡
│ 1 ┆ 2021-01-01 00:00:00 ┆ 0 ┆ -2.0 ┆ null │
│ 1 ┆ 2021-01-01 00:00:00 ┆ 1 ┆ 0.0 ┆ fever │
│ 1 ┆ 2021-01-02 00:00:00 ┆ 3 ┆ null ┆ null │
│ 2 ┆ 2022-10-02 00:00:00 ┆ 0 ┆ 2.0 ┆ null │
│ 2 ┆ 2022-10-02 00:00:00 ┆ 2 ┆ null ┆ null │
│ 3 ┆ 2022-10-02 00:00:00 ┆ 5 ┆ null ┆ null │
└────────────┴─────────────────────┴──────┴───────────────┴────────────┘
Source code in meds_torch/utils/custom_text_normalization.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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 216 217 218 | |