42161eb612
Stages 01-06 (depuration, MCA balancing and categorization, bootstrap indices, stepwise LR and hybrid Bayesian network, survival curves, risk factors), settings in model.toml, run.py writing each stage's files and pipeline.py as the runner that replaces ejecutar_pipeline.py. New extras seer-survival and seer-survival-dca; experiments/ excluded from the wheel.
27 lines
1.2 KiB
Python
27 lines
1.2 KiB
Python
"""Finding a covariate's column: by its recoded `<name>_cat` column or, failing that, by keywords in the column's label.
|
|
Plain functions shared by the balancing and categorization stages (not Hamilton nodes)."""
|
|
|
|
import pandas as pd
|
|
|
|
|
|
def find_col(labels: dict, keywords, exclude=()) -> str | None:
|
|
"""The first column whose lower-cased label has any of `keywords` and none of `exclude`."""
|
|
for col, desc in labels.items():
|
|
d = str(desc).lower()
|
|
if any(k in d for k in keywords) and not any(e in d for e in exclude):
|
|
return col
|
|
return None
|
|
|
|
|
|
def resolve_column(df: pd.DataFrame, name: str, labels: dict, candidate_keywords: dict, direct=()) -> str | None:
|
|
"""`<name>_cat` if the frame has it, a `direct` name that is itself a column (e.g. `Age_group`), else the column
|
|
found by the covariate's keywords; None when there is none."""
|
|
if name in direct:
|
|
return name if name in df.columns else None
|
|
cat_col = f"{name}_cat"
|
|
if cat_col in df.columns:
|
|
return cat_col
|
|
kw = candidate_keywords.get(name, {"include": [], "exclude": []})
|
|
col = find_col(labels, kw["include"], kw["exclude"])
|
|
return col if col in df.columns else None
|