Skip to main content

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. Its reference_date is "today", for resolving date_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 a medication-type criteria_tree node.
  • code: As _BareCodeAddressedLeaf.code; omit it for a text-only criterion.
  • text: As _TextAddressedLeaf.text, searched across the entry's code_text and every codings[].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 (see MedicationDoseBound). 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 one MedicationCriterion leaf per bound under an AndGroup: 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 like CodeCriterion: 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 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 a dose_quantity bound.
  • dose_type: Which dose_and_rate reading to use — "ordered" (the prescriber's own instruction, sometimes per-weight) or "calculated" (a separately computed absolute amount, which also covers a MedicationAdministration-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 an OrGroup rather than picking one.
  • op: The comparison operator. "in"/"not in" are deliberately excluded, unlike CodeValueBound.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 one OrGroup leaf per strength rather than a membership list.
  • value: The threshold value.
  • unit: The unit dose_unit must equal. Required rather than optional, unlike CodeValueBound.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 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 a frequency bound.
  • op: The comparison operator, as MedicationDoseQuantityBound.op — "in"/"not in" excluded for the same reason: "twice or three times daily" is an OrGroup of two == leaves, not a membership list.
  • value: The threshold value.
  • frequency_period: The period an entry's own frequency must be measured over. frequency alone is not a clinically meaningful
  • bound: "at least twice" could mean twice daily or twice weekly, and those are different prescriptions. This pins the comparison to one instruction's own period, exactly (not compared via op), the same way dose_type pins one dose_and_rate reading.
  • frequency_period_unit: The period_unit an entry's own frequency must be measured over, matched exactly alongside frequency_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