custom_text_tensorization
Functions for tensorizing MEDS datasets.
convert_to_NRT(df)
This converts a tokenized dataframe into a nested ragged tensor.
Most of the work for this function is actually done in tokenize – this function is just a wrapper
to convert the output into a nested ragged tensor using polars’ built-in to_dict method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
LazyFrame
|
The tokenized dataframe. |
required |
Returns:
| Type | Description |
|---|---|
JointNestedRaggedTensorDict
|
A |
JointNestedRaggedTensorDict
|
many levels of ragged nesting are present among the codes and numeric values. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If there are no time delta columns or if there are multiple time delta columns. |
Examples:
>>> df = pl.DataFrame({
... "subject_id": [1, 2],
... "time_delta_days": [[float("nan"), 12.0], [float("nan")]],
... "code": [[[101, 102], [103]], [[201, 202]]],
... "modality_idx": [[[0., float("nan")], [2.]], [[float("nan"), float("nan")]]],
... "numeric_value": [[[2.0, 3.0], [4.0]], [[6.0, 7.0]]]
... })
>>> nrt = convert_to_NRT(df.lazy())
>>> for k, v in sorted(list(nrt.to_dense().items())):
... print(k)
... print(v)
code
[[[101 102]
[103 0]]
[[201 202]
[ 0 0]]]
dim1/mask
[[ True True]
[ True False]]
dim2/mask
[[[ True True]
[ True False]]
[[ True True]
[False False]]]
modality_idx
[[[ 0. nan]
[ 2. 0.]]
[[nan nan]
[ 0. 0.]]]
numeric_value
[[[2. 3.]
[4. 0.]]
[[6. 7.]
[0. 0.]]]
time_delta_days
[[nan 12.]
[nan 0.]]
Source code in meds_torch/utils/custom_text_tensorization.py
main(cfg)
TODO.