From dfa6e4e0d9f58dc9d6d120988cc4aca12df220d0 Mon Sep 17 00:00:00 2001 From: Tatiana Tchouakam Date: Fri, 23 Oct 2026 11:18:34 +0200 Subject: [PATCH] feat(findings): report each testing frequency range with sheet, patient and value (ticket 4) --- src/a4d/clean/transformers.py | 35 ++++++++++++++------------- src/a4d/findings.py | 4 +-- tests/test_clean/test_transformers.py | 28 +++++++++++++++++++++ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/a4d/clean/transformers.py b/src/a4d/clean/transformers.py index 3531893..2b9cd8b 100644 --- a/src/a4d/clean/transformers.py +++ b/src/a4d/clean/transformers.py @@ -324,7 +324,7 @@ def fix_testing_frequency(df: pl.DataFrame) -> pl.DataFrame: - Replaces ranges like "0-2" with mean "1" - Preserves null and empty values as null - - Logs warning when ranges are detected + - Reports one finding per range, with its sheet, patient, value and mean Args: df: Input DataFrame @@ -341,19 +341,12 @@ def fix_testing_frequency(df: pl.DataFrame) -> pl.DataFrame: if "testing_frequency" not in df.columns: return df - # Track if we logged warnings - has_ranges = False - def fix_value(value: str | None) -> str | None: """Fix a single testing_frequency value.""" - nonlocal has_ranges - if value is None or value == "": return None if "-" in value: - has_ranges = True - try: mean_value = replace_range_with_mean(value) # Return as string, remove trailing .0 for whole numbers @@ -366,6 +359,23 @@ def fix_value(value: str | None) -> str | None: return value + # One finding per cell written as a range, with its sheet, patient, the value the + # clinic wrote and the mean published instead, so the cell can be found (ticket 4). + ranges = df.filter(pl.col("testing_frequency").str.contains("-", literal=True)) + for row in ranges.iter_rows(named=True): + value = row["testing_frequency"] + report_finding( + error_code="testing_frequency_averaged", + message=( + f"Testing frequency {value!r} is a range; {fix_value(value)!r} was published." + ), + patient_id=row.get("patient_id") or "unknown", + column="testing_frequency", + original_value=value, + **sheet_context(row), + function_name="convert_testing_frequency", + ) + # Apply transformation df = df.with_columns( pl.col("testing_frequency") @@ -373,15 +383,6 @@ def fix_value(value: str | None) -> str | None: .alias("testing_frequency") ) - # Log warning if any ranges were found - if has_ranges: - report_finding( - error_code="testing_frequency_averaged", - message="Found ranges in testing_frequency column. Replacing with mean values.", - column="testing_frequency", - function_name="convert_testing_frequency", - ) - return df diff --git a/src/a4d/findings.py b/src/a4d/findings.py index 8d91c21..9e99b8b 100644 --- a/src/a4d/findings.py +++ b/src/a4d/findings.py @@ -159,7 +159,6 @@ # at the whole column before deciding there is anything to say. "tracker_layout_changed": "tracker_column", "glucose_unit_swapped": "tracker_column", - "testing_frequency_averaged": "tracker_column", # One patient, deduplicated across the months they appear in. A diagnosis # date before a date of birth is a property of the patient; saying it once # per monthly row would say it 58 times for 8 patients. @@ -177,8 +176,9 @@ "excel_error_patient_id": "row", "missing_required_field": "row", "patient_id_unrepairable": "row", - # Ticket 4: one finding per unparseable cell, so the cell can be found. + # Ticket 4: one finding per cell, so the cell can be found. "blood_pressure_unparseable": "row", + "testing_frequency_averaged": "row", "patient_id_recovered": "row", "source_formula_error": "row", "glucose_unit_suspect": "row", diff --git a/tests/test_clean/test_transformers.py b/tests/test_clean/test_transformers.py index f27b926..dfac86d 100644 --- a/tests/test_clean/test_transformers.py +++ b/tests/test_clean/test_transformers.py @@ -627,6 +627,7 @@ def test_fix_testing_frequency_range_replacement(): df = pl.DataFrame( { "patient_id": ["P1", "P2", "P3"], + "sheet_name": "Jan26", "testing_frequency": ["0-2", "2-3", "1-5"], } ) @@ -641,6 +642,7 @@ def test_fix_testing_frequency_mixed(): df = pl.DataFrame( { "patient_id": ["P1", "P2", "P3", "P4"], + "sheet_name": "Jan26", "testing_frequency": ["2", "0-2", "1.5", "2-3"], } ) @@ -671,6 +673,7 @@ def test_fix_testing_frequency_whole_numbers(): df = pl.DataFrame( { "patient_id": ["P1", "P2"], + "sheet_name": "Jan26", "testing_frequency": ["0-2", "1-3"], } ) @@ -697,6 +700,7 @@ def test_fix_testing_frequency_large_range(): df = pl.DataFrame( { "patient_id": ["P1"], + "sheet_name": "Jan26", "testing_frequency": ["0-10"], } ) @@ -711,6 +715,7 @@ def test_fix_testing_frequency_preserves_other_columns(): df = pl.DataFrame( { "patient_id": ["P1", "P2"], + "sheet_name": "Jan26", "testing_frequency": ["0-2", "3"], "other_col": ["A", "B"], } @@ -954,3 +959,26 @@ def test_split_bp_reports_one_finding_per_invalid_cell(collector): if f.error_code == "blood_pressure_unparseable" ] assert found == [("Jan26", "XX_QA001", "120-80"), ("Feb26", "XX_QA003", "high")] + + +def test_fix_testing_frequency_reports_one_finding_per_range(collector): + """Ticket 4: each range gets its own finding, with the sheet, the patient, the + value the clinic wrote and the mean published. A single number and an empty + cell give no finding.""" + df = pl.DataFrame( + { + "sheet_name": ["Jan26", "Feb26", "Feb26", "Mar26"], + "patient_id": ["XX_QA001", "XX_QA002", "XX_QA003", "XX_QA004"], + "testing_frequency": ["3-4", "2", "0-2", None], + } + ) + + result = fix_testing_frequency(df) + + assert result["testing_frequency"].to_list() == ["3.5", "2", "1", None] + found = [ + (f.sheet_name, f.patient_id, f.original_value) + for f in collector.findings + if f.error_code == "testing_frequency_averaged" + ] + assert found == [("Jan26", "XX_QA001", "3-4"), ("Feb26", "XX_QA003", "0-2")]