utils
TrajectoryBatch
dataclass
Initialize a batch of trajectories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time |
Tensor
|
Tensor of shape (batch_size, sequence_length) containing days after prediction time. Values must be monotonically increasing within each sequence. |
required |
code |
Tensor
|
Tensor of shape (batch_size, sequence_length) containing event code vocabulary indices |
required |
mask |
Tensor
|
Tensor of shape (batch_size, sequence_length) indicates valid measurements/codes |
required |
numeric_value |
Tensor
|
Tensor of shape (batch_size, sequence_length) containing numeric values |
required |
numeric_value_mask |
Tensor
|
Tensor of shape (batch_size, sequence_length) indicating valid numeric values |
required |
metadata_df |
DataFrame
|
DataFrame containing code vocabulary mapping with ‘code’ and ‘code/vocab_index’ columns |
required |
time_scale |
str
|
scale of the time, by default it is ‘Y’ (years). Any numpy datetime units can be used, see https://numpy.org/doc/2.1/reference/arrays.datetime.html#datetime-units |
'Y'
|
Source code in meds_torch/models/components/utils.py
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 | |
to_meds(prediction_time, subject_id)
Convert the trajectory batch to MEDS format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction_time |
list[datetime]
|
List of prediction times for each trajectory in the batch |
required |
subject_id |
list[str | int]
|
List of subject IDs for each trajectory in the batch |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pl.DataFrame: MEDS format DataFrame with columns: - time: Absolute timestamp of the event - code: The medical code string - numeric_value: The numeric value associated with the code (if any) - subject_id: ID of the subject - prediction_time: The prediction time for this trajectory |
Example:
metadata_df = pl.DataFrame({ … ‘code’: [‘A1’, ‘A2’, ‘A3’, ‘A4’, ‘A5’, ‘A6’], … ‘code/vocab_index’: [1, 2, 3, 4, 5, 6] … }) batch = TrajectoryBatch( … time=torch.tensor([[0, .5, 2], [0, 3, 5]]), … code=torch.tensor([[1, 2, 3], [4, 5, 6]]), … mask=torch.tensor([[1, 1, 1], [1, 1, 0]]), … numeric_value=torch.tensor([[0.5, 1.0, 1.5], [2.0, 2.5, 3.0]]), … numeric_value_mask=torch.tensor([[1, 1, 0], [1, 0, 0]]), … metadata_df=metadata_df … ) prediction_times = [datetime(2024, 1, 1), datetime(2024, 1, 1)] subject_ids = [1, 2] df = batch.to_meds(prediction_times, subject_ids) df.sort(“subject_id”, “code”) shape: (5, 6) ┌────────────┬─────────────────────┬─────────────────────┬──────┬──────────────────┬───────────────┐ │ subject_id ┆ prediction_time ┆ time ┆ code ┆ code/vocab_index ┆ numeric_value │ │ — ┆ — ┆ — ┆ — ┆ — ┆ — │ │ i32 ┆ datetime[ns] ┆ datetime[ns] ┆ str ┆ i64 ┆ f32 │ ╞════════════╪═════════════════════╪═════════════════════╪══════╪══════════════════╪═══════════════╡ │ 1 ┆ 2024-01-01 00:00:00 ┆ 2024-01-01 00:00:00 ┆ A1 ┆ 1 ┆ 0.5 │ │ 1 ┆ 2024-01-01 00:00:00 ┆ 2024-07-01 14:54:36 ┆ A2 ┆ 2 ┆ 1.0 │ │ 1 ┆ 2024-01-01 00:00:00 ┆ 2025-12-31 11:38:24 ┆ A3 ┆ 3 ┆ NaN │ │ 2 ┆ 2024-01-01 00:00:00 ┆ 2024-01-01 00:00:00 ┆ A4 ┆ 4 ┆ 2.0 │ │ 2 ┆ 2024-01-01 00:00:00 ┆ 2026-12-31 17:27:36 ┆ A5 ┆ 5 ┆ NaN │ └────────────┴─────────────────────┴─────────────────────┴──────┴──────────────────┴───────────────┘
Source code in meds_torch/models/components/utils.py
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 | |
get_last_token(output, mask)
Get the last non-masked token from the output tensor.
Args: output (torch.Tensor): The output tensor of shape (batch_size, seq_len, hidden_dim) mask (torch.Tensor): The mask tensor of shape (batch_size, seq_len) where True indicates masked positions
Returns: torch.Tensor: The last non-masked token for each sequence in the batch
Source code in meds_torch/models/components/utils.py
get_time_days_delta(pred_times, input_end_times, device=torch.device('cpu'))
Convert lists of prediction times and generation start times to the continuous number of days (including fractional days) between each pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pred_times |
List[datetime]
|
List of prediction times |
required |
input_end_times |
List[datetime]
|
List of generation start times, one for each prediction time |
required |
device |
device
|
Device to place the output tensor on. Defaults to CPU. |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Tensor of shape (len(pred_times),) containing the number of days (including fractional parts) after each generation start time for each prediction time. |
Examples:
>>> from datetime import datetime
>>> import torch
>>> pred_times = [
... datetime(2022, 1, 1, 12, 0), # Noon on Jan 1
... datetime(2022, 1, 2, 0, 0) # Midnight on Jan 2
... ]
>>> end_times = [
... datetime(2022, 1, 1, 0, 0), # Midnight on Jan 1
... datetime(2022, 1, 1, 0, 0) # Midnight on Jan 1
... ]
>>> get_time_days_delta(pred_times, end_times)
tensor([0.5000, 1.0000])
>>> pred_times = [
... datetime(2022, 1, 1, 6, 0), # 6 AM on Jan 1
... datetime(2022, 1, 1, 18, 0) # 6 PM on Jan 1
... ]
>>> end_times = [
... datetime(2022, 1, 1, 0, 0), # Midnight on Jan 1
... datetime(2022, 1, 1, 12, 0) # Noon on Jan 1
... ]
>>> get_time_days_delta(pred_times, end_times)
tensor([0.2500, 0.2500])