custom_tokenization
Functions for tokenizing MEDS datasets.
Here, tokenization refers specifically to the process of converting a longitudinal, irregularly sampled, continuous time sequence into a temporal sequence at the level that will be consumed by deep-learning models.
All these functions take in normalized data – meaning data where there are no longer any code modifiers,
as those have been normalized alongside codes into integer indices (in the output code column). The only
columns of concern here thus are subject_id, time, code, numeric_value.
extract_seq_of_subject_events(df)
This function extracts sequences of subject events, which are sequences of measurements.
The result of this can be naturally tensorized into a JointNestedRaggedTensorDict object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
The input data. |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
A |
Examples:
>>> from datetime import datetime
>>> df = pl.DataFrame({
... "subject_id": [1, 1, 1, 1, 2, 2, 2],
... "time": [
... None, datetime(2021, 1, 1), datetime(2021, 1, 1), datetime(2021, 1, 13),
... None, datetime(2021, 1, 2), datetime(2021, 1, 2)],
... "code": [100, 101, 102, 103, 200, 201, 202],
... "numeric_value": pl.Series([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=pl.Float32)
... }).lazy()
>>> extract_seq_of_subject_events(df).collect()
shape: (2, 4)
┌────────────┬─────────────────┬─────────────────────┬─────────────────────┐
│ subject_id ┆ time_delta_days ┆ code ┆ numeric_value │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ list[f32] ┆ list[list[i64]] ┆ list[list[f32]] │
╞════════════╪═════════════════╪═════════════════════╪═════════════════════╡
│ 1 ┆ [NaN, 12.0] ┆ [[101, 102], [103]] ┆ [[2.0, 3.0], [4.0]] │
│ 2 ┆ [NaN] ┆ [[201, 202]] ┆ [[6.0, 7.0]] │
└────────────┴─────────────────┴─────────────────────┴─────────────────────┘
Source code in meds_torch/utils/custom_tokenization.py
extract_statics_and_schema(df)
This function extracts static data and schema information (sequence of subject unique times).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
The input data. |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
A |
LazyFrame
|
by subject as lists, in the same order as the subject IDs occurred in the original file. |
Examples:
>>> from datetime import datetime
>>> df = pl.DataFrame({
... "subject_id": [1, 1, 1, 1, 2, 2, 2],
... "time": [
... None, datetime(2021, 1, 1), datetime(2021, 1, 1), datetime(2021, 1, 13),
... None, datetime(2021, 1, 2), datetime(2021, 1, 2)],
... "code": [100, 101, 102, 103, 200, 201, 202],
... "numeric_value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
... }).lazy()
>>> df = extract_statics_and_schema(df).collect()
>>> df.drop("time")
shape: (2, 4)
┌────────────┬───────────┬───────────────┬─────────────────────┐
│ subject_id ┆ code ┆ numeric_value ┆ start_time │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ list[i64] ┆ list[f64] ┆ datetime[μs] │
╞════════════╪═══════════╪═══════════════╪═════════════════════╡
│ 1 ┆ [100] ┆ [1.0] ┆ 2021-01-01 00:00:00 │
│ 2 ┆ [200] ┆ [5.0] ┆ 2021-01-02 00:00:00 │
└────────────┴───────────┴───────────────┴─────────────────────┘
>>> df.select("subject_id", "time").explode("time")
shape: (3, 2)
┌────────────┬─────────────────────┐
│ subject_id ┆ time │
│ --- ┆ --- │
│ i64 ┆ datetime[μs] │
╞════════════╪═════════════════════╡
│ 1 ┆ 2021-01-01 00:00:00 │
│ 1 ┆ 2021-01-13 00:00:00 │
│ 2 ┆ 2021-01-02 00:00:00 │
└────────────┴─────────────────────┘
>>> df = pl.DataFrame({
... "subject_id": [1, 1, 1, 1, 2, 2, 2],
... "time": [
... datetime(2020, 1, 1), datetime(2021, 1, 1), datetime(2021, 1, 1), datetime(2021, 1, 13),
... datetime(2020, 1, 1), datetime(2021, 1, 2), datetime(2021, 1, 2)],
... "code": [100, 101, 102, 103, 200, 201, 202],
... "numeric_value": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
... }).lazy()
>>> df = extract_statics_and_schema(df).collect()
>>> df.drop("time")
shape: (2, 4)
┌────────────┬───────────┬───────────────┬─────────────────────┐
│ subject_id ┆ code ┆ numeric_value ┆ start_time │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ list[i64] ┆ list[f64] ┆ datetime[μs] │
╞════════════╪═══════════╪═══════════════╪═════════════════════╡
│ 1 ┆ null ┆ null ┆ 2020-01-01 00:00:00 │
│ 2 ┆ null ┆ null ┆ 2020-01-01 00:00:00 │
└────────────┴───────────┴───────────────┴─────────────────────┘
>>> df.select("subject_id", "time").explode("time")
shape: (5, 2)
┌────────────┬─────────────────────┐
│ subject_id ┆ time │
│ --- ┆ --- │
│ i64 ┆ datetime[μs] │
╞════════════╪═════════════════════╡
│ 1 ┆ 2020-01-01 00:00:00 │
│ 1 ┆ 2021-01-01 00:00:00 │
│ 1 ┆ 2021-01-13 00:00:00 │
│ 2 ┆ 2020-01-01 00:00:00 │
│ 2 ┆ 2021-01-02 00:00:00 │
└────────────┴─────────────────────┘
Source code in meds_torch/utils/custom_tokenization.py
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 | |
fill_to_nans(col)
This function fills infinite and null values with NaN.
This enables the downstream functions to naturally tensorize data into numpy or Torch tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col |
str | Expr
|
The input column. |
required |
Returns:
| Type | Description |
|---|---|
Expr
|
A |
Examples:
>>> print(fill_to_nans("value"))
.when([(col("value").is_infinite()) |
(col("value").is_null())]).then(dyn float: NaN).otherwise(col("value"))
>>> print(fill_to_nans(pl.col("time_delta")))
.when([(col("time_delta").is_infinite()) |
(col("time_delta").is_null())]).then(dyn float: NaN).otherwise(col("time_delta"))
>>> df = pl.DataFrame({"value": [1.0, float("inf"), None, -float("inf"), 2.0]})
>>> df.select(fill_to_nans("value").alias("value"))["value"].to_list()
[1.0, nan, nan, nan, 2.0]
Source code in meds_torch/utils/custom_tokenization.py
main(cfg)
TODO.
Source code in meds_torch/utils/custom_tokenization.py
split_static_and_dynamic(df)
This function splits the input data into static and dynamic data.
Static data is data that has a null time, and dynamic data is everything else.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
The input data. |
required |
Returns:
| Type | Description |
|---|---|
LazyFrame
|
A tuple of two |
LazyFrame
|
dynamic data. |
Examples:
>>> from datetime import datetime
>>> df = pl.DataFrame({
... "subject_id": [1, 1, 2, 2],
... "time": [None, datetime(2021, 1, 1), None, datetime(2021, 1, 2)],
... "code": [100, 101, 200, 201],
... "numeric_value": [1.0, 2.0, 3.0, 4.0]
... }).lazy()
>>> static, dynamic = split_static_and_dynamic(df)
>>> static.collect()
shape: (2, 3)
┌────────────┬──────┬───────────────┐
│ subject_id ┆ code ┆ numeric_value │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞════════════╪══════╪═══════════════╡
│ 1 ┆ 100 ┆ 1.0 │
│ 2 ┆ 200 ┆ 3.0 │
└────────────┴──────┴───────────────┘
>>> dynamic.collect()
shape: (2, 4)
┌────────────┬─────────────────────┬──────┬───────────────┐
│ subject_id ┆ time ┆ code ┆ numeric_value │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ datetime[μs] ┆ i64 ┆ f64 │
╞════════════╪═════════════════════╪══════╪═══════════════╡
│ 1 ┆ 2021-01-01 00:00:00 ┆ 101 ┆ 2.0 │
│ 2 ┆ 2021-01-02 00:00:00 ┆ 201 ┆ 4.0 │
└────────────┴─────────────────────┴──────┴───────────────┘