API
pycomorb.comorbidity(score, df, id_col='id', code_col='code', age_col='age', year_col='year', icd_version='icd10', icd_version_col=None, icd_modification=None, icd_modification_target_year=None, implementation=None, weights=None, definition_data=None, definition_file_path=None, weight_col_name='weights', score_col_name='Custom Comorbidity Score', mutual_exclusion_rules=None, return_categories=False)
Calculate a comorbidity or frailty score from ICD-coded data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score
|
str
|
Name of the score to calculate. Supported values are |
required |
df
|
pl.DataFrame | pd.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
age_col
|
str
|
Column name containing patient ages (required for Charlson calculations). Defaults to |
'age'
|
year_col
|
str
|
Column name containing the year of the ICD code (used when applying ICD modification transfer files). Defaults to |
'year'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str
|
Column name with ICD version labels (required for |
None
|
icd_modification
|
str
|
ICD modification to apply ( |
None
|
icd_modification_target_year
|
int
|
Target year for ICD modification mappings (ICD-10-GM: 2004-2026, ICD-10-CM: 2015-2027). Defaults to the earliest year of the chosen modification ( |
None
|
implementation
|
str
|
Implementation variant for the selected score (for example |
None
|
weights
|
str
|
Weighting scheme for the Charlson and Elixhauser indices (e.g. |
None
|
definition_data
|
pl.DataFrame | pd.DataFrame | Path | str
|
Definition table for custom indices, either as a DataFrame or a path to a CSV file. Defaults to |
None
|
definition_file_path
|
str
|
Path to a CSV containing custom index definitions. Ignored when |
None
|
weight_col_name
|
str
|
Column name with weights in |
'weights'
|
score_col_name
|
str
|
Column name assigned to the calculated score for custom indices. Defaults to |
'Custom Comorbidity Score'
|
mutual_exclusion_rules
|
list[tuple[str, str]]
|
Mutually exclusive category pairs for custom indices, where the second entry is suppressed when the first is present. Both categories must exist in the definitions. Defaults to |
None
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame | pd.DataFrame
|
DataFrame containing the calculated score and, when |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the requested |
Examples:
Compute a Charlson score with category indicators and automatic ICD-10-GM harmonization:
>>> import polars as pl
>>> from pycomorb import comorbidity
>>> diagnoses = pl.DataFrame(
... {
... "id": [1, 1, 2],
... "code": ["I21", "E119", "C349"],
... "age": [66, 66, 81],
... "year": [2012, 2012, 2010],
... }
... )
>>> result = comorbidity(
... score="charlson",
... df=diagnoses,
... icd_version="icd10",
... icd_modification="icd10gm",
... return_categories=True,
... )
>>> result.select("id", "Age Score", "Charlson Age-Comorbidity Score").sort("id")
shape: (2, 3)
┌─────┬───────────┬────────────────────────────────┐
│ id ┆ Age Score ┆ Charlson Age-Comorbidity Score │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═══════════╪════════════════════════════════╡
│ 1 ┆ 2 ┆ 4 │
│ 2 ┆ 4 ┆ 6 │
└─────┴───────────┴────────────────────────────────┘
Source code in pycomorb/__init__.py
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 | |
pycomorb.CharlsonComorbidityIndex
CharlsonComorbidityIndex(df, id_col='id', code_col='code', age_col='age', icd_version='icd10', icd_version_col=None, year_col=None, implementation='quan', weights='charlson', return_categories=False)
Calculate the Charlson Comorbidity Index (CCI) using ICD codes and age.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
age_col
|
str
|
Column name containing patient ages (age points are only added for |
'age'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str
|
Column name with ICD version labels when |
None
|
year_col
|
str
|
Column name with each record's ICD-10-GM catalogue year. Required when |
None
|
implementation
|
str
|
Definition set to use; |
'quan'
|
weights
|
str
|
Weighting scheme; one of |
'charlson'
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
DataFrame containing |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pycomorb/CharlsonComorbidityIndex.py
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 | |
CharlsonComorbidity_10year_survival(df=None, id_col='id', code_col='code', age_col='age', icd_version='icd10', icd_version_col=None, implementation='quan', precalculated_df=False)
Estimate 10-year survival probabilities from the Charlson Comorbidity Index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame | None
|
Source data with ICD codes, ages, and identifiers, or a precomputed Charlson score table when |
None
|
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
age_col
|
str
|
Column name containing patient ages. Defaults to |
'age'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str | None
|
Column name with ICD version labels when |
None
|
implementation
|
str
|
Charlson definition set passed to :func: |
'quan'
|
precalculated_df
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
DataFrame with two columns: |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If required columns are missing when |
Source code in pycomorb/CharlsonComorbidityIndex.py
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 | |
pycomorb.ElixhauserComorbidityIndex
ElixhauserComorbidityIndex(df, id_col='id', code_col='code', icd_version='icd10', icd_version_col=None, year_col=None, implementation='quan', weights='van_walraven', return_categories=False)
Calculate the Elixhauser Comorbidity Index (ECI) using ICD codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str
|
Column name with ICD version labels when |
None
|
year_col
|
str
|
Column name with each record's diagnosis year. Required when |
None
|
implementation
|
str
|
Definition set to use; |
'quan'
|
weights
|
str
|
Weighting scheme; one of |
'van_walraven'
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
DataFrame containing |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
ValueError
|
If |
Source code in pycomorb/ElixhauserComorbidityIndex.py
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 | |
pycomorb.CombinedComorbidityScore
CombinedComorbidityScore(df, id_col='id', code_col='code', icd_version='icd10', icd_version_col=None, return_categories=False)
Calculate the Combined Comorbidity Score using ICD codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str
|
Column name with ICD version labels when |
None
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
DataFrame containing |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If required columns are missing. |
Source code in pycomorb/CombinedComorbidityScore.py
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 | |
pycomorb.HospitalFrailtyRiskScore
HospitalFrailtyRiskScore(df, id_col='id', code_col='code', icd_version='icd10', return_categories=False)
Calculate the Hospital Frailty Risk Score (HFRS) using ICD-10 codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame | pd.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
icd_version
|
str
|
ICD version; must be |
'icd10'
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame | pd.DataFrame
|
DataFrame containing |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pycomorb/HospitalFrailtyRiskScore.py
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 | |
pycomorb.CustomComorbidityIndex
CustomComorbidityIndex(df, id_col='id', code_col='code', icd_version='icd10', icd_version_col=None, definition_data=None, weight_col_name=None, score_col_name=None, mutual_exclusion_rules=None, year_col=None, return_categories=False)
Compute a custom comorbidity score from ICD-coded input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
pl.DataFrame
|
Input data containing at least |
required |
id_col
|
str
|
Column name containing unique identifiers. Defaults to |
'id'
|
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
icd_version
|
str
|
ICD version; one of |
'icd10'
|
icd_version_col
|
str
|
Column name with ICD version labels when |
None
|
definition_data
|
Path | pl.DataFrame
|
Path to a CSV file or DataFrame containing category definitions and weights. May optionally include an |
None
|
weight_col_name
|
str
|
Column name with weights inside |
None
|
score_col_name
|
str
|
Column name assigned to the calculated score in the result. Defaults to |
None
|
mutual_exclusion_rules
|
list[tuple[str, str]]
|
Pairs of mutually exclusive categories where the second entry is suppressed when the first is present. Defaults to |
None
|
year_col
|
str
|
Column name with each record's definition-catalogue year (e.g. ICD-10-GM catalogue year). When provided, |
None
|
return_categories
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
DataFrame containing |
Patients without a usable code are kept with all categories 0.
Raises:
| Type | Description |
|---|---|
AssertionError
|
If required columns or definitions are missing, or if |
ValueError
|
If |
Source code in pycomorb/CustomComorbidityIndex.py
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 | |
coerce_icd_version(icd_version, implementation, icd9_only, icd10_only)
Replace an ICD version the implementation cannot use, with a warning.
Source code in pycomorb/CustomComorbidityIndex.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
weighted_definitions(prefix, definition_file, implementation, canonical_rules)
Definitions of a Charlson/Elixhauser implementation joined with its weights, plus its exclusion rules.
prefix ("CHARLSON" or "ELIXHAUSER") names the weight and alias files; canonical_rules are
(more severe, milder) pairs in canonical category names, translated to the implementation's own names.
Source code in pycomorb/CustomComorbidityIndex.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
pycomorb.comorbidity_reporting(score, implementation=None, weights=None, icd_version='icd10', icd_modification=None, path=None, **study_info)
Reporting template for a comorbidity score, with the information needed to reproduce it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score
|
str
|
|
required |
implementation
|
str
|
As in the score function. Defaults to its default (none for |
None
|
weights
|
str
|
As in the score function. Defaults to its default (none for |
None
|
icd_version
|
str
|
ICD version of the data; replaced by the one the implementation uses if it supports a single version only. Defaults to |
'icd10'
|
icd_modification
|
str
|
ICD modification, e.g. |
None
|
path
|
Path | str
|
Directory to write the tables to, as |
None
|
**study_info
|
str
|
Answers for the domains that depend on the study data: |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[pl.DataFrame, pl.DataFrame, pl.DataFrame, pl.DataFrame]
|
|
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pycomorb/Reporting.py
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 | |
pycomorb.read_reporting(path)
Read the tables written by comorbidity_reporting back in.
The result holds the arguments of a custom score that reproduces the reported one::
comorbidity("custom", df, **read_reporting(path))
icd_version is the one the report used, so do not pass it again; icd_version_col is still needed for "icd9_10".
Records need a year column (year_col, default "year") if the codes have a year.
Returns:
| Type | Description |
|---|---|
dict
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If one of the tables is missing in |
ValueError
|
If the tables do not belong together. |
Source code in pycomorb/Reporting.py
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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | |
pycomorb.get_icd10gm(data, code_col='code', year_col='year', target_year=2024)
Map ICD-10-GM codes to a target year using official transfer files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
pl.DataFrame
|
Input data containing at least |
required |
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
year_col
|
str
|
Column name containing the year of each ICD code. Defaults to |
'year'
|
target_year
|
int
|
Target ICD-10-GM year. Defaults to |
2024
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
Input DataFrame with an additional |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pycomorb/ICDModifications.py
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 | |
pycomorb.get_icd10cm(data, code_col='code', year_col='year', target_year=2024)
Map ICD-10-CM codes to a target year using official transfer files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
pl.DataFrame
|
Input data containing at least |
required |
code_col
|
str
|
Column name containing ICD codes. Defaults to |
'code'
|
year_col
|
str
|
Column name containing the year of each ICD code. Defaults to |
'year'
|
target_year
|
int
|
Target ICD-10-CM year. Defaults to |
2024
|
Returns:
| Type | Description |
|---|---|
pl.DataFrame
|
Input DataFrame with an additional |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in pycomorb/ICDModifications.py
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 | |