|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +# Example script: load rules + data, run harmonization, and write output. |
| 4 | +# |
| 5 | +# This file is intentionally verbose and commented so a new user can |
| 6 | +# understand the minimal steps required to use the framework. |
| 7 | + |
| 8 | +from harmonization_framework.harmonize import harmonize_file |
| 9 | +from harmonization_framework.rule_registry import RuleRegistry |
| 10 | + |
| 11 | + |
| 12 | +def main() -> None: |
| 13 | + # Find this demo directory so all paths are absolute and stable. |
| 14 | + base_dir = Path(__file__).resolve().parent |
| 15 | + |
| 16 | + input_path = base_dir / "input.csv" |
| 17 | + print("input_path: ", input_path) |
| 18 | + |
| 19 | + rules_path = base_dir / "rules.json" |
| 20 | + print("rules_path: ", rules_path) |
| 21 | + |
| 22 | + output_path = base_dir / "output.csv" |
| 23 | + print("output_path: ", output_path) |
| 24 | + |
| 25 | + # The RuleRegistry is the in-memory container for all harmonization rules. |
| 26 | + # Loading the JSON file builds a registry of source/target rules that |
| 27 | + # harmonize values from the source column into the target column. |
| 28 | + rules = RuleRegistry() |
| 29 | + rules.load(str(rules_path), clean=True) |
| 30 | + |
| 31 | + # Each pair is (source_column, target_column). These names must match |
| 32 | + # the rules in rules.json. The framework will: |
| 33 | + # 1) rename the column to the target name |
| 34 | + # 2) apply the rule for that source/target to each value |
| 35 | + harmonization_pairs = [ |
| 36 | + ("age", "age_years"), |
| 37 | + ("weight_lbs", "weight_kg"), |
| 38 | + ("name", "given_name"), |
| 39 | + ("name", "family_name"), |
| 40 | + ("visit_type_code", "visit_type_label"), |
| 41 | + ] |
| 42 | + |
| 43 | + # Run the harmonization and save the output CSV. |
| 44 | + # dataset_name becomes the value of the "source dataset" column. |
| 45 | + harmonized = harmonize_file( |
| 46 | + input_path=str(input_path), |
| 47 | + output_path=str(output_path), |
| 48 | + harmonization_pairs=harmonization_pairs, |
| 49 | + rules=rules, |
| 50 | + dataset_name="demo", |
| 51 | + ) |
| 52 | + |
| 53 | + # Reorder columns for readability in the demo output. |
| 54 | + preferred_order = [ |
| 55 | + "family_name", |
| 56 | + "given_name", |
| 57 | + "age_years", |
| 58 | + "weight_kg", |
| 59 | + "visit_type_label", |
| 60 | + "source dataset", |
| 61 | + "original_id", |
| 62 | + ] |
| 63 | + ordered = [col for col in preferred_order if col in harmonized.columns] |
| 64 | + ordered += [col for col in harmonized.columns if col not in ordered] |
| 65 | + harmonized = harmonized[ordered] |
| 66 | + harmonized.to_csv(output_path, index=False) |
| 67 | + |
| 68 | + # The output file contains the transformed columns plus: |
| 69 | + # - "source dataset": the dataset_name value for each row |
| 70 | + # - "original_id": the original row index from the input file |
| 71 | + print(f"Wrote harmonized CSV to: {output_path}") |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + main() |
0 commit comments