repository
The patients domain's repository methods.
The cohort listings, a patient's summary and the trial queue.
Mixed into CacheBackedPatientDataRepository, which supplies the cache handle
they read through.
Classes
PatientsRepositoryMixin
class PatientsRepositoryMixin():One domain's methods, mixed into the repository.
Ancestors
Subclasses
Methods
get_eligibility_counts
def get_eligibility_counts( self, trial_ids: list[str],) ‑> dict[str, EligibilityCounts]:Return per-trial status counts, zeroed for trials with neither source.
The counts are over each trial's effective cohort, so they match the
review-aware list_patients filter: a reviewed patient counts under their
review's verdict, and a review for a trial with no analysis row counts on
its own.
Arguments
trial_ids: The trial (project) IDs to count.
Returns
A map of trial ID → EligibilityCounts.
get_patient
def get_patient( self, bitfount_patient_id: str, project_id: str | None = None,) ‑> PatientSummary | None:Return a single patient summary, or None.
Resolves the identity row and eligible-trial set with targeted
primary-key lookups rather than materialising every patient, scoped to
the same published identity partitions as list_patients.
Arguments
bitfount_patient_id: The Bitfount patient ID.project_id: When given, scopeslast_analysed_image_dateto that trial; omitted, it spans every trial the pod publishes.
Returns
The PatientSummary, or None if the patient is unknown.
get_trial_candidate_counts
def get_trial_candidate_counts( self, project_id: str,) ‑> CandidateCounts:Return one trial's candidate counts, bucketed by review status.
The queue count comes from the same read the listing uses — the unreviewed candidates (eligible or unknown, with an identity and no deciding review) — and each review bucket from the review store's own count, restricted to patients with an identity, so each count matches what a listing of that bucket serves.
Arguments
project_id: The trial (project) ID.
Returns
A CandidateCounts: to_review plus a count per review status.
list_patients
def list_patients( self, *, search: str | None = None, trials: list[str] | None = None, statuses: Collection[EligibilityStatus] | None = None, project_id: str | None = None, page: int = 1, page_size: int = 50,) ‑> PatientList:Return one page of patients, filtered and searched in the database.
Filtering, search and windowing run in SQL (name/ehr_patient_id are
plaintext columns), so only the requested page is materialised. Each
returned patient keeps its full trial_statuses regardless of the
trials/statuses filter, so a listing narrowed to one trial still
reports each patient's verdicts across every trial the pod publishes.
Arguments
search: Case-insensitive substring matched against name or EHR id.trials: If given, restrict to patients with a verdict for any of these trials (union). Blank entries are dropped. A trial's site reviews are part of its cohort, so a filter naming an unpublished trial restricts to the patients reviewed for it.statuses: Which verdictstrialsadmits, read from the review's verdict where one is in force and the analysis verdict otherwise.Noneadmits all three. Ignored whentrialsisNone.project_id: When given,last_analysed_image_dateis the most recent scan-processing time for that trial. Omitted, it is the most recent across every trial the pod publishes, which next to trial-scoped neighbours reads as a contradiction — a trial-aware caller should pass it.page: 1-indexed page number.page_size: Page size (number of items per page).
Returns
A PatientList whose items are the requested page and whose
total is the count of all matching patients before pagination.
list_trial_patients
def list_trial_patients( self, project_id: str, *, statuses: Collection[EligibilityStatus] | None = None, review_state: ReviewState = unreviewed, review_statuses: Collection[ReviewStatus] | None = None, page: int = 1, page_size: int = 50,) ‑> TrialPatientList:Return one page of a trial's patients, reviewed first, then the queue.
Two groups (review_state.groups), each read by the query that already
serves it, and stitched into one list (reviewed first, so a caller
paging the whole list meets the site's decisions before the queue):
reviewed: the patients with a deciding review, aslist_trial_reviewsserves them (most recent review first), each with itsreview. Needs no publishedpatient_eligibilitypartition.unreviewed: the review queue, driven off the trial'spatient_eligibilitypartition and ranked by verdict (eligible,unknown,ineligible), then score descending with an absent score last, then patient ID.
The groups are exclusive, both defined by the verdicts a review can
impose (ELIGIBILITY_STATUS_FOR_REVIEW), so no patient is listed twice.
Each patient carries their criteria counts for the trial, read from the
page's evidence rows (None for one with no analysis).
Arguments
project_id: The trial (project) ID.statuses: The verdicts to admit: the analysis verdict for an unreviewed patient, what the review stands in for for a reviewed one.Noneadmits all three.review_state: Which patients to list: the unreviewed queue (the default), the reviewed ones, orall, reviewed first.review_statuses: When given, restrict the reviewed group to these review statuses, so one funnel stage (orrejected) lists on its own. Takes precedence overstatusesfor that group.page: 1-indexed page number.page_size: Items per page.
Returns
A TrialPatientList of the requested page and the count of all
admitted patients, across the listed groups, before pagination.