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
30 changes: 30 additions & 0 deletions src/a4d/clean/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@
from a4d.extract.common import EXCEL_ERROR_STRINGS
from a4d.findings import PLACE_COLUMNS, ErrorCode, report_finding, sheet_context

# The tracker columns computed by a formula, and the inputs that formula reads.
# When one of these inputs is blank on the row, the formula had nothing to
# compute from: the gap is the missing measurement, not the formula.
FORMULA_INPUTS: dict[str, tuple[str, ...]] = {
"bmi": ("weight", "height"),
"t1d_diagnosis_age": ("dob", "t1d_diagnosis_date"),
}


def _formula_input_missing(row: dict, column: str) -> bool:
"""True when a formula input of ``column`` is in the frame and blank on this row.

An input column that is not in the frame says nothing, so the finding stays.
Blank means null, empty, or a missing-value marker such as "-" or "NA".
"""
for name in FORMULA_INPUTS.get(column, ()):
if name not in row:
continue
value = row[name]
if value is None or str(value).strip().lower() in MISSING_VALUE_MARKERS:
return True
return False


def normalize_excel_formula_errors(
df: pl.DataFrame,
Expand All @@ -51,6 +74,11 @@ def normalize_excel_formula_errors(
distinction between "source formula could not compute this" and "field
was simply blank" survives into the error log, even though both end up
``null`` in the data.

Not logged when the computed column's own inputs are blank on that row
(FORMULA_INPUTS: bmi from weight and height, t1d_diagnosis_age from dob and
t1d_diagnosis_date): the formula had nothing to compute from, so the gap is
the missing measurement, not the formula. The cell is still nulled.
"""
data_cols = [col for col in df.columns if df.schema[col] == pl.String]
if not data_cols:
Expand All @@ -65,6 +93,8 @@ def normalize_excel_formula_errors(
if offenders.is_empty():
continue
for row in offenders.iter_rows(named=True):
if _formula_input_missing(row, col):
continue
report_finding(
file_name=row.get(file_name_col) or None,
patient_id=row.get(patient_id_col) or "unknown",
Expand Down
41 changes: 41 additions & 0 deletions tests/test_clean/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,3 +1025,44 @@ def test_parse_date_flexible_treats_new_absence_words_as_missing():
including the template's example text left in a cell."""
for marker in ("???", "_", "Miss", "missing date", "mmm-yy", "e.g. xxx"):
assert parse_date_flexible(marker) is None, marker


def test_normalize_excel_formula_errors_skips_when_formula_inputs_are_blank(collector):
"""A BMI error with weight or height blank is a missing measurement, not a
formula problem: the cell is nulled without a finding. With both inputs
recorded, the error is still reported."""
df = pl.DataFrame(
{
"file_name": ["test.xlsx"] * 3,
"sheet_name": "Jan26",
"patient_id": ["XX_QA001", "XX_QA002", "XX_QA003"],
"weight": [None, "40", "40"],
"height": [None, "-", "1.5"],
"bmi": ["#DIV/0!", "#DIV/0!", "#DIV/0!"],
}
)

result = normalize_excel_formula_errors(df)

assert result["bmi"].to_list() == [None, None, None]
assert [f.patient_id for f in collector.findings] == ["XX_QA003"]


def test_normalize_excel_formula_errors_age_at_diagnosis_needs_a_diagnosis_date(collector):
"""An age-at-diagnosis error without a diagnosis date is not reported; with
both dates recorded, it is."""
df = pl.DataFrame(
{
"file_name": ["test.xlsx"] * 2,
"sheet_name": "Patient List",
"patient_id": ["XX_QA001", "XX_QA002"],
"dob": ["2015-03-01", "2015-03-01"],
"t1d_diagnosis_date": [None, "2020-06-01"],
"t1d_diagnosis_age": ["#NUM!", "#NUM!"],
}
)

result = normalize_excel_formula_errors(df)

assert result["t1d_diagnosis_age"].to_list() == [None, None]
assert [f.patient_id for f in collector.findings] == ["XX_QA002"]
Loading