From 0527266bf86b0a79779709fda855421c46699993 Mon Sep 17 00:00:00 2001 From: kert Date: Wed, 11 Nov 2026 12:54:16 -0500 Subject: [PATCH] fix(bcda): leave race and sex null when the source sends a null flavor UNK/ASKU race and FHIR gender other/unknown were written as CCW code 0, so the patient table showed an 'unknown' the source never asserted. --- src/bcda/express/v3.py | 23 +++++++++++------------ tests/bcda/test_v3.py | 7 ++++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/bcda/express/v3.py b/src/bcda/express/v3.py index 0074e4c..4f54eec 100644 --- a/src/bcda/express/v3.py +++ b/src/bcda/express/v3.py @@ -72,9 +72,10 @@ POS = "Place_of_Service_Code_Set" NUBC_TOB = "https://www.nubc.org/CodeSystem/TypeOfBill" # CCW BENE_RACE_CD from the US Core OMB race/ethnicity codes -# (CCW codebook: 0 Unknown, 1 White, 2 Black, 3 Other, 4 Asian/Pacific -# Islander, 5 Hispanic, 6 North American Native). Hispanic ethnicity -# wins when present, as in the CCW RTI race code derivation. +# (CCW codebook: 1 White, 2 Black, 3 Other, 4 Asian/Pacific Islander, +# 5 Hispanic, 6 North American Native). Hispanic ethnicity wins when +# present, as in the CCW RTI race code derivation. The codebook's +# 0 = Unknown is not written: a null flavor stays null. OMB_TO_CCW_RACE = { "2106-3": "1", # White "2054-5": "2", # Black or African American @@ -82,9 +83,10 @@ OMB_TO_CCW_RACE = { "2076-8": "4", # Native Hawaiian or Other Pacific Islander "1002-5": "6", # American Indian or Alaska Native "2131-1": "3", # Other Race - "UNK": "0", - "ASKU": "0", } +# Null flavors are "not reported", not the CCW unknown code. Writing "0" +# made the patient table show a zero for race the source never sent. +NULL_FLAVOR_RACE = {"UNK", "ASKU"} HISPANIC_OMB = "2135-2" # SNOMED sex (us-core-sex) → CCW BENE_SEX_IDENT_CD @@ -447,8 +449,8 @@ def _race(extensions: list[dict]) -> tuple[str | None, str | None]: hispanic = True if hispanic: return omb_race, "5" - if omb_race is None: - return None, None + if omb_race is None or omb_race in NULL_FLAVOR_RACE: + return omb_race, None return omb_race, OMB_TO_CCW_RACE.get(omb_race, "3") @@ -486,11 +488,8 @@ def flatten_patient(resource: dict) -> Patient: lang = _coding(comm.get("language"), "urn:ietf:bcp:47") or lang gender = resource.get("gender") - bene_sex_cd = ( - SNOMED_SEX.get(sex_snomed or "") - or GENDER_TO_CCW.get(gender or "") - or ("0" if gender else None) - ) + # male/female only. "other" and "unknown" are not the CCW code 0. + bene_sex_cd = SNOMED_SEX.get(sex_snomed or "") or GENDER_TO_CCW.get(gender or "") return Patient( id=resource.get("id"), diff --git a/tests/bcda/test_v3.py b/tests/bcda/test_v3.py index 1dce0b9..1f7b3e0 100644 --- a/tests/bcda/test_v3.py +++ b/tests/bcda/test_v3.py @@ -454,13 +454,14 @@ class TestFlattenPatientV3: } ] ) - assert flatten_patient(r).bene_race_cd == "0" + assert flatten_patient(r).bene_race_cd is None + assert flatten_patient(r).us_core_race == "UNK" assert flatten_patient(_patient(extension=[])).bene_race_cd is None def test_sex_fallbacks(self) -> None: assert flatten_patient(_patient(extension=[], gender="male")).bene_sex_cd == "1" assert ( - flatten_patient(_patient(extension=[], gender="other")).bene_sex_cd == "0" + flatten_patient(_patient(extension=[], gender="other")).bene_sex_cd is None ) assert flatten_patient(_patient(extension=[], gender=None)).bene_sex_cd is None @@ -1284,7 +1285,7 @@ class TestOfficialSamples: assert all(p.bene_mbi_id for p in rows) assert all(p.bene_id == p.id for p in rows) assert {p.bene_sex_cd for p in rows} == {"1", "2"} - assert {p.bene_race_cd for p in rows} <= set("0123456") + assert {p.bene_race_cd for p in rows} <= set("123456") | {None} assert all(p.cntct_lang_cd for p in rows) def test_coverages(self, sample_coverages) -> None: