Database connections, tables and mappers
Tables
Phenex provides certain table types on which it knows how to operate. For instance, Phenex implements a CodeTable, which is an event table containing codes. Phenex has abstracted operations for each table type. For instance, given a CodeTable, Phenex knows how to filter this table based on the presence of codes within that table. Phenex doesn't care if the code table is actually a diagnosis code table or a procedure code table or a medication code table.
In onboarding a new data model to Phenex, the tables must be mapped into Phenex table types by subclassing the appropriate PhenexTable. When subclassing a PhenexTable, you must define:
1. COLUMN_MAPPING: a mapping of the input table columns to the fields on the chosen PhenexTable type (e.g. 'CD' maps to 'CODE' in a CodeTable).
2. JOIN_KEYS: if you want to use the autojoin functionality of PhenexTable, you must specify what keys to use to join pairs of tables
3. PATHS: if you want to use the autojoin functionality of PhenexTable for more complex joins, you must specify join paths to take to get from one table to another
Note that for each table type, there are REQUIRED_FIELDS, i.e., fields that MUST be defined for Phenex to work with such a table and KNOWN_FIELDS, i.e., fields that Phenex internally understands what to do with (there is a Phenotype that knows how to work with that field). For instance, in a PhenexPersonTable, one MUST define PERSON_ID, but DATE_OF_BIRTH is an optional field that PhenEx can process if given and transform into AGE. These are fixed for each table type and should not be overridden.
JOIN_KEYS and PATHS Documentation:
JOIN_KEYS defines direct relationships between tables. The key is the CLASS NAME of the target table, and the value is a list of join keys. Each join key can be: - A string: symmetric join (column has same name in both tables) - A 2-element tuple/list: asymmetric join (left_col, right_col) with different names
PATHS defines multi-hop join paths. The key is the CLASS NAME of the final target table, and the value is a list of CLASS NAMES for intermediate tables to traverse.
IMPORTANT: JOIN_KEYS should be defined symmetrically - if TableA can join to TableB, then TableB should also define how to join back to TableA.
Example 1: Symmetric joins (same column names)
class DummyConditionOccurrenceTable(CodeTable):
NAME_TABLE = "DIAGNOSIS"
JOIN_KEYS = {
"DummyPersonTable": ["PERSON_ID"], # Join using PERSON_ID in both tables
"DummyEncounterTable": ["PERSON_ID", "ENCID"], # Compound join: both keys must match
}
PATHS = {
"DummyVisitDetailTable": ["DummyEncounterTable"] # To reach VisitDetail, go through Encounter
}
class DummyEncounterTable(PhenexTable):
NAME_TABLE = "ENCOUNTER"
JOIN_KEYS = {
"DummyPersonTable": ["PERSON_ID"],
"DummyConditionOccurrenceTable": ["PERSON_ID", "ENCID"], # Symmetric!
"DummyVisitDetailTable": ["PERSON_ID", "VISITID"],
}
class DummyVisitDetailTable(PhenexTable):
NAME_TABLE = "VISIT"
JOIN_KEYS = {
"DummyPersonTable": ["PERSON_ID"],
"DummyEncounterTable": ["PERSON_ID", "VISITID"], # Symmetric!
}
Example 2: Asymmetric joins (different column names)
class EventTable(CodeTable):
NAME_TABLE = "EVENT"
JOIN_KEYS = {
"EventMappingTable": [("ID", "EVENTID")], # EventTable.ID joins to EventMappingTable.EVENTID
}
PATHS = {
"ConceptTable": ["EventMappingTable"],
}
DEFAULT_MAPPING = {
"PERSON_ID": "PERSON_ID",
"ID": "ID", # Must include ID in mapping for it to exist
}
class EventMappingTable(PhenexTable):
NAME_TABLE = "EVENT_MAPPING"
JOIN_KEYS = {
"EventTable": [("EVENTID", "ID")], # Symmetric: reverse the tuple
"ConceptTable": [("CONCEPTID", "ID")], # Maps to ConceptTable.ID
}
DEFAULT_MAPPING = {
"EVENTID": "EVENTID",
"CONCEPTID": "CONCEPTID",
}
class ConceptTable(CodeTable):
NAME_TABLE = "CONCEPT"
JOIN_KEYS = {
"EventMappingTable": [("ID", "CONCEPTID")], # Symmetric: reverse the tuple
}
DEFAULT_MAPPING = {
"ID": "ID",
"CODE": "CONCEPT_CODE",
"CODE_TYPE": "VOCABULARY_ID",
}
Example 3: Mixed symmetric and asymmetric joins
class PatientEventTable(CodeTable):
JOIN_KEYS = {
"EventMappingTable": [
"PERSON_ID", # Symmetric: PERSON_ID in both tables
("EVENT_ID", "EVENTID") # Asymmetric: different column names
],
}
In all examples: - Symmetric relationships use strings: ["COLUMN_NAME"] - Asymmetric relationships use tuples: [("LEFT_COL", "RIGHT_COL")] - Compound joins use multiple elements: ["COL1", "COL2"] or [("L1", "R1"), ("L2", "R2")] - All relationships should be symmetric (both tables define the join) - ALL join columns must be in DEFAULT_MAPPING for them to exist in the mapped table
Source code in phenex/tables.py
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | |
__init__(table, name=None, column_mapping={})
Instantiate a PhenexTable, possibly overriding NAME_TABLE and COLUMN_MAPPING.
Source code in phenex/tables.py
filter(expr)
Filter the table by an Ibis Expression or using a PhenExFilter.
Source code in phenex/tables.py
from_dict(data)
classmethod
Reconstruct a PhenexTable class reference from serialized data.
Note: This returns the class itself, not an instance, since we cannot reconstruct the actual table data without a database connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
Serialized class configuration |
required |
Returns:
| Type | Description |
|---|---|
|
The PhenexTable subclass |
Source code in phenex/tables.py
join(other, *args, domains=None, **kwargs)
The join method performs a join of PhenexTables, using autojoin functionality if Phenex is able to find the table types specified in PATHS.
Source code in phenex/tables.py
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 | |
to_dict()
classmethod
Serialize the PhenexTable class configuration (not the data).
This serializes the class-level attributes that define the table mapping, but not the actual ibis table data which cannot be serialized.
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Class configuration including NAME_TABLE, JOIN_KEYS, DEFAULT_MAPPING, etc. |