Converter Pretrain¶
twinweaver.pretrain.converter_pretrain ¶
Classes¶
ConverterPretrain ¶
Bases: ConverterBase
Implements bidirectional conversion between structured patient data and a textual representation.
This class provides the core logic for transforming pandas DataFrames containing patient
events and constant information into a human-readable text format (forward_conversion)
format (forward_conversion) and parsing this text format back into DataFrames (reverse_conversion). It inherits
base functionalities and configuration handling from ConverterBase.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
Config
|
Configuration object storing settings like column names, date formats, etc. |
preamble_text |
str
|
Inherited/configured text added at the beginning of the output string. |
constant_text |
str
|
Inherited/configured text marking the start of the constant data section. |
first_day_text |
str
|
Inherited/configured text marking the start of the event data section. |
constant_description |
DataFrame
|
DataFrame describing the columns in the constant DataFrame. Other attributes related to formatting and separators inherited from base class.¶ |
Source code in twinweaver/pretrain/converter_pretrain.py
9 10 11 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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
Functions¶
__init__ ¶
Initializes the ConverterPretrain instance.
Sets up the converter using the provided configuration object, primarily by
calling the initializer of the base class ConverterBase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
A configuration object containing necessary settings for data processing, such as standard column names, text separators, and formatting details. |
required |
dm
|
DataManager
|
A DataManager object containing the data frames, e.g. constant_description. |
required |
Source code in twinweaver/pretrain/converter_pretrain.py
forward_conversion ¶
Converts structured patient data (events, constant info) into a textual representation.
This method takes patient data as DataFrames, preprocesses them (e.g., calculating age from birthdate), formats the constant information and time-series events into predefined textual structures, and combines them into a single string. It returns the generated text along with metadata containing both the original and processed DataFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
DataFrame
|
DataFrame containing the time-series event data for the patient. Expected columns
are defined in the |
required |
constant
|
DataFrame
|
DataFrame containing constant (non-time-varying) information for the patient. Expected to have a single row. Columns represent different attributes. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the conversion results: { "text": str, # The full textual representation of the patient's data. "meta": { "raw_constant": pd.DataFrame, # Original input constant DataFrame. "processed_constant": pd.DataFrame, # Constant DataFrame after preprocessing. "raw_events": pd.DataFrame, # Original input events DataFrame. "events": pd.DataFrame, # Events DataFrame after preprocessing. "constant_description": pd.DataFrame # The constant description used. } } |
Source code in twinweaver/pretrain/converter_pretrain.py
reverse_conversion ¶
Converts a textual representation of patient data back into structured DataFrames.
Parses the input text string, attempting to extract constant patient information and
time-series events based on the formatting conventions used by forward_conversion.
Uses helper methods (_extract_constant_data, _extract_event_data) and metadata
(like constant descriptions and original event structure hints) to reconstruct the
DataFrames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The textual representation of patient data, as generated by |
required |
meta_data
|
dict
|
The metadata dictionary associated with the text, typically containing necessary
context like 'constant_description' and potentially 'raw_events' or other hints.
This usually comes from the 'internal_meta' part of the |
required |
unique_events
|
DataFrame
|
A DataFrame listing all possible unique events (e.g., names, categories) in the dataset. Used to help identify and parse events correctly from the text. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing the reconstructed data: { "constant": pd.DataFrame, # DataFrame of the extracted constant information. "events": pd.DataFrame # DataFrame of the extracted event data. } |