Skip to main content

element_shapes

Report FHIR elements a reader could not use, by their structure alone.

A reader that meets an element in a shape it does not handle keeps going with an empty value, which is the right call for one resource and the wrong one to make silently: a whole site whose server states the eye, a date or a code some other way reads as nothing, and nothing says so. ModMed's bodySite, which states the eye only as a reference to a BodyStructure, went unread like this.

note_unread_element records the element's shape: its nested keys, with every value dropped. A shape says how a server structures the element, never what it holds for a patient. The one value kept is an extension's url, which names the extension's definition (http://hl7.org/fhir/StructureDefinition/...) rather than anything about the patient.

The first time a run meets each resource type, element and shape, it logs a warning naming them. After that it only counts, so a site where every resource has the same quirk logs it once, not once per resource. The counts reach telemetry once per run (report_element_reads), as do note_recovered_element's counts of elements read through a fallback such as the BodyStructure reference.

Module​

Functions​

choice_elements​

def choice_elements(resource: Any, prefix: str) ‑> dict[str, typing.Any] | None:

A resource's elements whose names start with prefix, extensions included.

For a choice element such as performed[x] this is every variant the resource carries (performedDateTime, performedPeriod, ...) along with any primitive extension FHIR states one by (_performedDateTime, for example a data-absent reason), so the shape shows how the server stated the element when no variant could be read.

Arguments

  • resource: The resource as JSON, or a deserialised model.
  • prefix: The element's name, without [x].

Returns The matching elements by name, or None when there are none.

collecting_element_reads​

def collecting_element_reads(    ,) ‑> Iterator[ElementReads]:

Collect what every note inside the block records.

element_shape​

def element_shape(element: Any) ‑> str:

The nested keys of a FHIR element, with every value dropped.

Lists show each distinct item shape once, joined by |. A scalar shows as *, and an extension's url keeps its value (see the module docstring). For example, ModMed's bodySite is {extension:[{url=http://hl7.org/fhir/StructureDefinition/bodySite,valueReference:{reference}}]}.

Arguments

  • element: The element as JSON (a dict, a list or a scalar), or a deserialised model with model_dump, or None when absent.

Returns The shape, at most SHAPE_MAX_LENGTH characters long, or ABSENT.

note_absent_or_unread​

def note_absent_or_unread(    resource_type: str,    element: str,    resource: Any,    prefix: str,    *,    detail: str | None = None,) ‑> None:

Record a reader's empty read of prefix[x] under the right counter.

The EHR stating there is no value is an answer, counted as stated absence. Anything else is a shape the reader did not handle, counted as unread with its shape logged once a run.

Arguments

  • resource_type: The FHIR resource type.
  • element: The element's path within the resource.
  • resource: The resource the element came from.
  • prefix: The element's name without [x].
  • detail: See note_unread_element.

note_recovered_element​

def note_recovered_element(resource_type: str, element: str, how: str) ‑> None:

Record that a reader read an element through a fallback.

Arguments

  • resource_type: The FHIR resource type.
  • element: The element's path within the resource.
  • how: The fallback, for example "body_structure_reference".

note_stated_absence​

def note_stated_absence(resource_type: str, element: str, reason: str) ‑> None:

Record that the EHR stated element has no value, and why.

Counted apart from note_unread_element, which is for elements the reader could not make sense of. Keeping them apart is what lets a misread stand out on a site whose EHR states absence constantly.

Arguments

  • resource_type: The FHIR resource type.
  • element: The element's path within the resource.
  • reason: The data-absent-reason code, or STATED_ABSENT when the resource does not carry the element.

note_unread_element​

def note_unread_element(    resource_type: str, element: str, value: Any, *, detail: str | None = None,) ‑> None:

Record that a reader read an element as empty, by its shape.

Arguments

  • resource_type: The FHIR resource type, for example "Observation".
  • element: The element's path within the resource, for example "bodySite" or "performed[x]".
  • value: The element as served, or None when the resource did not carry it. Only its shape is kept.
  • detail: A value that is safe to log and says more than the shape, such as a BodyStructure ID. Appended to the shape.

stated_absence​

def stated_absence(resource: Any, prefix: str) ‑> str | None:

The reason resource gives for prefix[x] having no value.

A server states absence two ways: it leaves the element out, or it carries a primitive extension naming a data-absent-reason. Both are answers, so a reader that reads the element as empty for either counts it here rather than as unread.

Arguments

  • resource: The resource as JSON, or a deserialised model.
  • prefix: The element's name without [x], for example "performed".

Returns The valueCode the server gave; STATED_ABSENT when it carries no variant of the element at all; None when the element holds something this cannot read as a statement of absence, which is a reading failure and belongs in unread.

Global variables​

  • ABSENT : Final[str] - The shape of an element the resource does not carry at all.
  • DATA_ABSENT_REASONS : Final[frozenset[str]] - The codes FHIR defines for data-absent-reason.

    A reason outside this set is not stated absence but something the reader has not met, so it is counted as unread instead of being absorbed.

  • DATA_ABSENT_REASON_URL : Final[str] - The extension FHIR uses to state why a primitive has no value.
  • ElementKey - (resource type, element, shape), the unit an unread element is counted in.
  • RecoveryKey - (resource type, element, how), the unit a recovered element is counted in.
  • SHAPE_MAX_LENGTH : Final[int] - Longer shapes are cut short, so one odd resource cannot flood a log line.
  • STATED_ABSENT : Final[str] - The reason recorded for an element the server left out altogether.

Classes​

ElementReads​

class ElementReads(    unread: Counter[ElementKey] = Counter(),    recovered: Counter[RecoveryKey] = Counter(),    stated_absent: Counter[ElementKey] = Counter(),):

What one run's readers could not use, and what they recovered.

Attributes

  • unread: Elements read as empty, by resource type, element and shape.
  • recovered: Elements read through a fallback, by resource type, element and how.
  • stated_absent: Elements the EHR stated there is no value for, by resource type, element and the reason it gave.

Variables​