medication
Criteria on a patient's medications (FHIR MedicationRequest, MedicationStatement).
The medication leaf (MedicationCriterion): a medication of a matching code
or text is recorded for the patient, in its date window and for its eye. An
optional dose bound (MedicationDoseQuantityBound,
MedicationFrequencyBound) also requires a matching amount or frequency in
one of the record's dosage instructions. The evidence and findings carry each
matched record's course dates and doses.
Module
Functions
medication_leaf_filter
def medication_leaf_filter( criterion: MedicationCriterion, ctx: FilterBuildContext,) ‑> MethodFilter:Build a MethodFilter for a MedicationCriterion criteria_tree leaf.
Mirrors observation_leaf_filter's code/date/laterality/selector
handling exactly (see its docstring). It reads against the fixed
MEDICATIONS_COLUMN rather than a resource-keyed one.
MedicationCriterion has no condition-vs-procedure resource choice to
make, the same reason ObservationCriterion has none. Additionally,
when criterion.dose is set, _select_matching_candidates checks it
(_medication_entry_matches_dose) against each code/date/laterality-
qualifying entry as selector reduces them, rather than pre-filtering
candidates by it — see observation_leaf_filter's docstring for why
that distinction matters for "all"/"latest"/"earliest".
Arguments
criterion: The leaf to compile.ctx: What the filter builders share. Itsreference_dateis "today", for resolvingdate_range.
Returns
A MethodFilter degrading to unknown when MEDICATIONS_COLUMN is
absent from the row, or the row's cell is unreadable — the same
missing-data convention code_leaf_filter uses.
Classes
MedicationCriterion
class MedicationCriterion(**data: Any):An EHR medication, matched by code or text, optionally bounded by dosage.
Arguments
node: Discriminator identifying this as amedication-typecriteria_treenode.code: As_BareCodeAddressedLeaf.code; omit it for a text-only criterion.text: As_TextAddressedLeaf.text, searched across the entry'scode_textand everycodings[].display. A medication is not always coded, and a drug recorded as free text carries only its name.dose: An optional bound (or list of bounds) on the medication's own dosage (seeMedicationDoseBound). When set, a matching code entry additionally qualifies only if any of its dosage instructions satisfies every given bound. A list is not the same as oneMedicationCriterionleaf per bound under anAndGroup: each leaf there would find its own qualifying entry independently, so e.g. a dose_quantity leaf and a frequency leaf could each be satisfied by a different instruction on the same entry. A list here pins every bound to one instruction at once — e.g. "40mg, given at least twice daily," not "40mg on some instruction, and at least twice daily on some (possibly different) instruction." When unset, behaves exactly likeCodeCriterion: presence/date/laterality only, dosage ignored.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
Variables
- static
criterion_noun : ClassVar[str]
- static
dose : MedicationDoseQuantityBound | MedicationFrequencyBound | list[MedicationDoseQuantityBound | MedicationFrequencyBound] | None
- static
model_config
- static
node : Literal['medication']
Methods
model_post_init
def model_post_init(self: BaseModel, context: Any, /) ‑> None:This function is meant to behave like a BaseModel method to initialise private attributes.
It takes context as an argument since that's what pydantic-core passes when calling it.
Arguments
self: The BaseModel instance.context: The context.
MedicationDoseQuantityBound
class MedicationDoseQuantityBound(**data: Any):A comparison against one dose_and_rate reading's own dose_quantity.
Arguments
field: Discriminator identifying this as adose_quantitybound.dose_type: Whichdose_and_ratereading to use —"ordered"(the prescriber's own instruction, sometimes per-weight) or"calculated"(a separately computed absolute amount, which also covers aMedicationAdministration-sourced reading: an exact amount actually given, not a prescriber's own instruction). These are different measurements, not interchangeable variants of one fact. A config author who wants "either reading qualifies" writes two leaves under anOrGrouprather than picking one.op: The comparison operator."in"/"not in"are deliberately excluded, unlikeCodeValueBound.op's full set: a dose is a continuous quantity, not a small enumerated set of values to check membership against, and "one of these exact strengths" (the one real case for it) is a config author writing oneOrGroupleaf per strength rather than a membership list.value: The threshold value.unit: The unitdose_unitmust equal. Required rather than optional, unlikeCodeValueBound.unit: a dose reading in the wrong unit is not a "probably fine" comparison the way a lab value might be."ordered"and"calculated"readings routinely differ by exactly a unit (mg/kg vs mg).
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
Variables
- static
dose_type : Literal['ordered', 'calculated']
- static
field : Literal['dose_quantity']
- static
model_config
- static
op : Literal['>', '>=', '<', '<=', '==', '!=']
- static
unit : UnitBound
- static
value : bool | int | float | str
MedicationFrequencyBound
class MedicationFrequencyBound(**data: Any):A comparison against an instruction's own frequency, pinned to one period.
Arguments
field: Discriminator identifying this as afrequencybound.op: The comparison operator, asMedicationDoseQuantityBound.op—"in"/"not in"excluded for the same reason: "twice or three times daily" is anOrGroupof two==leaves, not a membership list.value: The threshold value.frequency_period: Theperiodan entry's ownfrequencymust be measured over.frequencyalone is not a clinically meaningfulbound: "at least twice" could mean twice daily or twice weekly, and those are different prescriptions. This pins the comparison to one instruction's ownperiod, exactly (not compared viaop), the same waydose_typepins onedose_and_ratereading.frequency_period_unit: Theperiod_unitan entry's ownfrequencymust be measured over, matched exactly alongsidefrequency_period.
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
Variables
- static
field : Literal['frequency']
- static
frequency_period : float
- static
frequency_period_unit : str
- static
model_config
- static
op : Literal['>', '>=', '<', '<=', '==', '!=']
- static
value : bool | int | float | str