BinPhenotype
Bases: Phenotype
BinPhenotype converts values into categorical bin labels. Supports both continuous numeric binning and discrete value mapping.
For continuous values: Takes a phenotype that returns numeric values (like age, measurements, etc.) and converts the VALUE column into bin labels like "[10-20)", "[20-30)", etc.
For discrete values: Takes a phenotype that returns discrete values (like codes from CodelistPhenotype) and maps them to categorical labels using a bin mapping dictionary.
DATE: The event date selected from the input phenotype VALUE: A categorical variable representing the bin label
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
The name of the phenotype. |
required | |
phenotype
|
Phenotype
|
The phenotype that returns values of interest (AgePhenotype, MeasurementPhenotype, CodelistPhenotype, etc.) |
required |
bins
|
Optional[List[Union[int, float]]]
|
List of bin edges for continuous binning. Default is [0, 10, 20, ..., 100] for age ranges. |
None
|
value_mapping
|
Optional[Dict[str, List[str]]]
|
Dictionary mapping bin names to lists of values or Codelist objects (e.g., {"Heart Disease": ["I21", "I22", "I23"]} or {"Heart Disease": heart_disease_codelist}) |
None
|
Examples:
Example: Binning on continuous value
# Continuous binning example
from phenex.phenotypes import AgePhenotype
age = AgePhenotype()
binned_age = BinPhenotype(
name="age_groups",
phenotype=age,
bins=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
)
Example: Binning on discrete value
# Discrete mapping example
from phenex.phenotypes import CodelistPhenotype
from phenex.codelists import Codelist
diagnosis_codelist = Codelist(
name="diagnosis_codes",
codelist=["I21", "I22", "I23", "I24", "I25"]
)
diagnosis_codes = CodelistPhenotype(
name="diagnosis",
domain="CONDITION_OCCURRENCE",
codelist=diagnosis_codelist,
return_value="all"
)
diagnosis_categories = BinPhenotype(
name="diagnosis_categories",
phenotype=diagnosis_codes,
value_mapping={
"Acute MI": ["I21", "I22"],
"MI Complications": ["I23", "I24"],
"Chronic Heart Disease": ["I25"]
}
)
Example: Binning on discrete value
# Alternative: Using Codelist objects in value_mapping
acute_mi_codelist = Codelist(name="acute_mi", codelist=["I21", "I22"])
mi_complications_codelist = Codelist(name="mi_complications", codelist=["I23", "I24"])
chronic_hd_codelist = Codelist(name="chronic_hd", codelist=["I25"])
diagnosis_categories_v2 = BinPhenotype(
name="diagnosis_categories_v2",
phenotype=diagnosis_codes,
value_mapping={
"Acute MI": acute_mi_codelist,
"MI Complications": mi_complications_codelist,
"Chronic Heart Disease": chronic_hd_codelist
}
)
tables = {"PERSON": example_person_table}
result_table = binned_age.execute(tables)
# Result will have VALUE column with labels like "[20-30)", "[30-40)", etc.
result_table = diagnosis_categories.execute(tables)
# Result will have VALUE column with labels like "Acute MI", "MI Complications", etc.
Source code in phenex/phenotypes/bin_phenotype.py
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 |
|
dependencies
property
Recursively collect all dependencies of a node (including dependencies of dependencies).
Returns:
Type | Description |
---|---|
Set[Node]
|
List[Node]: A list of Node objects on which this Node depends. |
dependency_graph
property
namespaced_table
property
A PhenotypeTable has generic column names 'person_id', 'boolean', 'event_date', and 'value'. The namespaced_table prepends the phenotype name to all of these columns. This is useful when joining multiple phenotype tables together.
Returns:
Name | Type | Description |
---|---|---|
table |
Table
|
The namespaced table for the current phenotype. |
reverse_dependency_graph
property
execute(tables=None, con=None, overwrite=False, lazy_execution=False, n_threads=1)
Executes the Node computation for the current node and its dependencies. Supports lazy execution using hash-based change detection to avoid recomputing Node's that have already executed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tables
|
Dict[str, Table]
|
A dictionary mapping domains to Table objects. |
None
|
con
|
Optional[object]
|
Connection to database for materializing outputs. If provided, outputs from the node and all children nodes will be materialized (written) to the database using the connector. |
None
|
overwrite
|
bool
|
If True, will overwrite any existing tables found in the database while writing. If False, will throw an error when an existing table is found. Has no effect if con is not passed. |
False
|
lazy_execution
|
bool
|
If True, only re-executes if the node's definition has changed. Defaults to False. You should pass overwrite=True with lazy_execution as lazy_execution is intended precisely for iterative updates to a node definition. You must pass a connector (to cache results) for lazy_execution to work. |
False
|
n_threads
|
int
|
Max number of Node's to execute simultaneously when this node has multiple children. |
1
|
Returns:
Name | Type | Description |
---|---|---|
Table |
Table
|
The resulting table for this node. Also accessible through self.table after calling self.execute(). |
Source code in phenex/node.py
visualize_dependencies()
Create a text visualization of the dependency graph for this node and its dependencies.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A text representation of the dependency graph |