Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/a4d/clean/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -366,22 +359,30 @@ 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")
.map_elements(fix_value, return_dtype=pl.String)
.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


Expand Down
4 changes: 2 additions & 2 deletions src/a4d/findings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions tests/test_clean/test_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
)
Expand All @@ -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"],
}
)
Expand Down Expand Up @@ -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"],
}
)
Expand All @@ -697,6 +700,7 @@ def test_fix_testing_frequency_large_range():
df = pl.DataFrame(
{
"patient_id": ["P1"],
"sheet_name": "Jan26",
"testing_frequency": ["0-10"],
}
)
Expand All @@ -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"],
}
Expand Down Expand Up @@ -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")]
Loading