Bits & Flames bitsandflames/fyron

Phenotyping

fyron.phenotyping builds transparent rule-based clinical phenotypes from tabular FHIR-derived or registry-style data. Use it when diagnosis codes, procedure codes, lab thresholds, or medication exposure rows need to become patient-level cohort flags.

Phenotyping rules are explicit definitions, not validated clinical truth. Keep the rule inputs, code lists, thresholds, and evidence counts with the analysis so clinicians and reviewers can inspect what happened.

For broad ICD-10 disease groups, German OPS procedure categories, delimited code parsing, and date interval columns, see DataFrame Operations. Use fyron.phenotyping when the study needs explicit rule definitions with evidence counts and patient-level flags.

Imports

python
from fyron import phenotyping as fph

Function Reference

FunctionRequiredOptionalReturns
define_code_phenotypetable, code column, codespatient/date/code-system columns, phenotype namepatient-level phenotype table
define_lab_threshold_phenotypetable, value column, thresholddirection, patient/date columns, phenotype namepatient-level phenotype table
define_medication_exposuretable, medication column, medicationspatient/start/end columns, phenotype namepatient-level exposure table
combine_phenotypesphenotype tablesstrategy="any" or "all"combined patient-level table
phenotype_summaryphenotype table, phenotype columnpatient/group columnsprevalence summary table

Code Phenotype

python
condition_flag = fph.define_code_phenotype(
    conditions,
    code_col="code",
    codes=["I50", "I50.1"],
    patient_col="patient_id",
    date_col="recorded_date",
    code_system_col="system",
    code_systems=["ICD-10"],
    phenotype_name="heart_failure",
)

Outputs include patient_id, heart_failure, evidence_count, evidence_values, and optional first_date / last_date.

Lab Threshold Phenotype

python
anemia = fph.define_lab_threshold_phenotype(
    observations,
    value_col="hemoglobin",
    threshold=12.0,
    direction="<",
    date_col="effective_datetime",
    phenotype_name="anemia",
)

Medication Exposure

python
exposure = fph.define_medication_exposure(
    medications,
    medication_col="medication_name",
    medications=["cisplatin", "carboplatin"],
    start_col="start_date",
    end_col="end_date",
    phenotype_name="platinum_exposure",
)

Combine Rules

python
case_definition = fph.combine_phenotypes(
    [condition_flag, anemia],
    strategy="any",
    phenotype_name="study_case",
)

summary = fph.phenotype_summary(
    case_definition.merge(cohort[["patient_id", "sex"]], on="patient_id"),
    phenotype_col="study_case",
    group_col="sex",
)

Common Pitfalls

  • Store the exact code lists used for a study.
  • Review date windows before interpreting first/last evidence dates.
  • Treat medication names/codes as site-specific unless they come from normalized terminology.
  • Validate rule outputs on a small manually reviewed sample before modeling.