ccda_acuity
Read visual acuity observations out of a C-CDA (CCD) document.
The parser is pure: bytes in, Observation objects and masked counters out,
with no I/O and no logging. It walks every observation element in the
document whatever section it sits in, matching elements by local name so a
document with a missing namespace still reads.
An observation is an acuity candidate when one of its codings is a code the
caller supplies, or LOINC 28631-0, or when its display, original text or
inline text reads like an acuity. Each candidate with a readable value becomes
an Observation whose codings end with the CCD_ACUITY_SYSTEM marker, so a
reader can tell a CCD-sourced reading from another provider's.
Documents are parsed with defusedxml, which refuses DTDs, entity
declarations and external references, so a document carrying any is a
parse_error.
Nothing a document says about a patient leaves the parser except through
CCDAcuityParse.observations. The counters and shapes are built from codes,
tag names and text_shape masks only.
Module
Functions
decode_document_bytes
def decode_document_bytes(raw: bytes) ‑> bytes | None:Get XML bytes out of what a document download returned.
Tries, in order: a FHIR Binary JSON resource whose data is base64 XML;
raw XML; base64 XML (whitespace and line wrapping ignored, padding
repaired).
Arguments
raw: The bytes as downloaded.
Returns
Bytes the XML parser can read, which start with < once any BOM and
leading whitespace are put aside, or None if none of the routes
reaches XML.
parse_ccd_acuity
def parse_ccd_acuity( document: bytes, *, acuity_codes: Collection[tuple[str, str]], document_date: datetime | None,) ‑> CCDAcuityParse:Read the visual acuity observations from a CCD.
Arguments
document: The document as XML bytes (seedecode_document_bytes).acuity_codes:(full system URI, code)pairs that identify an acuity.document_date: The document's own date, given to a reading that has no usableeffectiveTimeof its own.
Returns
The readings and masked telemetry. A document that is not XML, not a
ClinicalDocument, or not well formed (or that carries a DTD,
entity declarations or external references) gives no readings and
says why in status.
reading_eye
def reading_eye(observation: Observation) ‑> Literal['L', 'R', 'B'] | None:The eye a reading from parse_ccd_acuity is for, read from its bodysite.
The site code is looked up directly, not through the shared laterality
inference, which reads the both-eyes site as left and right: a binocular
reading is "B" here, and says nothing about either eye on its own.
Arguments
observation: A readingparse_ccd_acuityreturned.
Returns
"L", "R" or "B", or None if the reading names no eye.
Global variables
CCDParseStatus- How far a document got:parsedmeans a CDA root was read.
COUNT_KEYS- Every key ofCCDAcuityParse.counts.
SHAPE_KEEP_TOKENS- Upper-case tokens left readable in a display shape: abbreviations that name an eye or a correction, not anything about a person.
Classes
CCDAcuityParse
class CCDAcuityParse( status: CCDParseStatus, observations: tuple[Observation, ...], unextractable: int, counts: Mapping[str, int], shapes: Mapping[str, int],):The outcome of parsing one document.
Attributes
status: How far the document got.observations: The acuity readings found, in document order, each with the CCD marker coding last and its eye, if the document names one, as itsbodysite(seereading_eye).unextractable: Acuity candidates from which no value could be read.counts: Telemetry counters, always carrying every key inCOUNT_KEYS.shapes: Masked shapes to their occurrence counts: of display names under an unknown code (display_unknown_code:), of the section a candidate sits in (element_path:) and of the LOINC and SNOMED codes on a candidate (candidate_code:). None carries document text.
Variables
-
counts : Mapping[str, int]
-
observations : tuple[Observation, ...]
-
shapes : Mapping[str, int]
-
status : Literal['parsed', 'not_xml', 'not_cda', 'parse_error']
-
unextractable : int