eligibility
The trial-eligibility read model.
A patient's status for a trial is the site's own verdict where one is recorded, and the analysis verdict otherwise. Precisely:
- A review in force for
(project_id, bitfount_patient_id)decides: itsreview_statusmaps tosuitable_for_screening/screening/enrolled→eligibleandrejected→ineligible(ELIGIBILITY_STATUS_FOR_REVIEW), and a value the map does not cover (orwithdrawn, which never reachespatient_reviews) falls back to the analysis verdict. - Otherwise the trial's published
patient_eligibilityrow decides. - A review for a trial the patient has no published analysis row for still counts, on the review's own verdict — a review stands on its own trial.
- A review never introduces a patient: the caller must already hold an identity
row for them (the patient listing's
scoped_toidentity scope), so this model only ever narrows a set of known patients.
The analysis side is partitioned by task_hash and only the published partition
is authoritative, so callers pass the project_id → task_hash map they resolved
from the pointer; the review side is unpartitioned.
Two shapes express the rules above, chosen per consumer:
cohort_condition, aWHEREpredicate for selecting which patients are in a trial's cohort (used by the patient listing, where the outer query scanspatients).trial_cohort, aSELECTof one row per(project_id, bitfount_patient_id)with its effective status (used where a cohort is scanned or grouped, e.g. hydrating a page'strial_statuses).
Both express all four rules, including the unmapped-verdict fallback, so a
status-filtered listing and the served trial_statuses cannot disagree.
Module
Functions
cohort_condition
def cohort_condition( project_to_hash: Mapping[str, str] | None = None, statuses: Collection[EligibilityStatus] | None = None, review_projects: Collection[str] | None = None,) ‑> sqlalchemy.sql.elements.ColumnElement[bool]:A predicate: this patient is in a requested trial's cohort.
True where a requested trial's review verdict is admitted, or — only where
that trial carries no review — its analysis verdict is admitted. The two
sources are the same effective status trial_cohort selects.
Arguments
project_to_hash:project_id → task_hashfor the requested projects with a published analysis partition.None/empty drops the analysis half.statuses: The verdicts to admit.Noneadmits every verdict.review_projects: The requested trial (project) ids whose reviews count.Nonedrops the review half (analysis verdicts alone).
Returns
A WHERE predicate over Patient.bitfount_patient_id.
counts_by_status
def counts_by_status( cache: CacheProtocol, project_id: str, task_hash: str | None,) ‑> dict[str, int]:Count a trial's effective cohort by status.
A GROUP BY over trial_cohort, so the counts match what
cohort_condition selects: an analysed patient counts under their review's
verdict where one is in force, and a review for a trial with no analysis row
counts on its own.
Arguments
cache: The cache backend.project_id: The trial (project) ID.task_hash: The trial's authoritativepatient_eligibilitytask hash, orNonewhen it publishes no analysis partition — then only its reviews count.
Returns
A dict with keys eligible/ineligible/unknown (missing statuses 0).
has_review_verdict
def has_review_verdict( patient_id_column: InstrumentedAttribute[str], project_id_column: InstrumentedAttribute[str],) ‑> sqlalchemy.sql.elements.ColumnElement[bool]:A predicate: a review in force imposes a verdict for (project, patient).
Served by the (project_id, bitfount_patient_id) primary key.
Arguments
patient_id_column: The column holding the patient id to test.project_id_column: The column holding the trial (project) id to test.
Returns
A correlated EXISTS predicate.
review_pending
def review_pending( patient_id_column: InstrumentedAttribute[str], project_id: str,) ‑> sqlalchemy.sql.elements.ColumnElement[bool]:A predicate: no review imposing a verdict is in force for one trial.
True for a patient the named trial still has to review: either no review at all, or one whose status imposes no verdict (so their analysis verdict is what shows meanwhile). A deciding review moves the patient out of the review queue and into the trial's reviews.
Arguments
patient_id_column: The column holding the patient id to test.project_id: The trial (project) id whose reviews decide.
Returns
A correlated EXISTS predicate, negated.
reviewed_cohort_exists
def reviewed_cohort_exists( project_ids: Collection[str], statuses: Collection[EligibilityStatus] | None = None,) ‑> sqlalchemy.sql.elements.ColumnElement[bool]:A predicate: a review in force puts this patient in one of these trials.
True for a patient with a current review for any of project_ids whose
verdict is in statuses (None admits every review that imposes one). A
review stands on its own trial, so unlike the analysis cohort this needs no
published partition.
Arguments
project_ids: The trial (project) ids whose reviews count. Empty matches nobody.statuses: The verdicts to admit, in the analysis vocabulary.
Returns
A WHERE predicate over Patient.bitfount_patient_id.
trial_cohort
def trial_cohort( project_to_hash: Mapping[str, str] | None = None, review_projects: Collection[str] | None = None, bitfount_patient_ids: Collection[str] | None = None,) ‑> sqlalchemy.sql.selectable.Select | sqlalchemy.sql.selectable.CompoundSelect:A relation: one row per (project_id, bitfount_patient_id) in a cohort.
The same effective status cohort_condition selects, as rows rather than a
predicate: an analysed patient carries the review's verdict where a review is
in force and their own otherwise, and a review for a trial that has no
published analysis row for the patient carries the review's verdict alone.
Arguments
project_to_hash:project_id → task_hashfor the projects with a published analysis partition.None/empty drops the analysis half.review_projects: The trial (project) ids whose reviews count.Nonecounts every review, which is what a whole-patient read (e.g.verdicts_for_patients) wants; an empty collection counts none, so the cohort is the analysis half alone.bitfount_patient_ids: When given, restrict to these patients, applied inside each half so a page's read does not build the whole cohort to discard it. An empty collection yields no rows.
Returns
A SELECT of (project_id, bitfount_patient_id, effective_status, review_status, eligibility_score).
verdict_for_patient
def verdict_for_patient( cache: CacheProtocol, bitfount_patient_id: str, project_to_hash: Mapping[str, str],) ‑> dict[str, TrialVerdict]:Return one patient's effective verdict per trial.
Arguments
cache: The cache backend.bitfount_patient_id: The patient to resolve.project_to_hash: Map ofproject_id → authoritative task_hashfor the published analysis partitions.
Returns
A map of project_id → TrialVerdict, empty for an unknown patient.
verdicts_for_patients
def verdicts_for_patients( cache: CacheProtocol, bitfount_patient_ids: Collection[str], project_to_hash: Mapping[str, str],) ‑> dict[str, dict[str, TrialVerdict]]:Return each patient's effective verdict per trial, over every trial.
Reads trial_cohort for the given patients, so a patient's set spans their
published analysis rows and every review they hold — including a review for a
trial that publishes no partition.
Arguments
cache: The cache backend.bitfount_patient_ids: The patients to resolve (e.g. the current page).project_to_hash: Map ofproject_id → authoritative task_hashfor the published analysis partitions.
Returns
A map of bitfount_patient_id → {project_id → TrialVerdict}. A patient
with nothing recorded is absent, as is a trial with an unmapped review
verdict and no analysis row.
Classes
TrialVerdict
class TrialVerdict( status: ForwardRef('EligibilityStatus'), eligibility_score: ForwardRef('float | None'),):A patient's effective verdict for one trial.
Attributes
status: The verdict in force (never a review value; reviews are mapped).eligibility_score: The analysis ranking score, orNonefor a review-only verdict (no analysis row to carry one).
Variables
eligibility_score : float | None- Alias for field number 1
status : bitfount.cache.types._eligibility_shared.EligibilityStatus- Alias for field number 0