task
Prefect task for the patient_eligibility reduce step.
Re-aggregates every patient in the partition: reads their stored per-scan verdicts
(scan_eligibility) and their patient-level verdict (patient_level_eligibility)
— each under its own partition, located via parent_task_hashes — picks the
determining scan, combines the two verdicts, and merges a patient_eligibility
row. The trials_published_data_pointer is flipped last, with one entry per
table this run wrote — an atomic cutover so readers never see a half-written
partition.
The reads are deliberately of the tables, not of the rows this run happened to produce: rows accumulate across runs within a partition, so reading back is what keeps a partial run from publishing partial rollups.
read_trial_scans_for_recompute and build_patient_records rebuild a trial's
rollups from its stored rows outside a run, as the reduce would.
Module
Functions
build_patient_records
def build_patient_records( *, scans_by_patient: Mapping[str, list[ScanEligibilityRecord]], patient_level_by_patient: Mapping[str, PatientLevelEligibilityRecord], scan_driven: bool, task_hash: str, project_id: str, now: datetime, run_id: str | None,) ‑> list[PatientEligibilityRecord]:Roll each patient's stored verdicts up into their patient_eligibility row.
A scan-driven trial serves the patients in scans_by_patient: each one's
scans are rolled up (roll_up_patient) and combined with their
patient-level verdict. An EHR-only trial serves every patient in
patient_level_by_patient on that verdict alone. Nothing is read or
written here, so a trial's roll-up can be rebuilt from its stored rows
outside a run as well as in the reduce.
Arguments
scans_by_patient: The scans to read, by patient, already filtered by dataset liveness and scope. Unused for an EHR-only trial.patient_level_by_patient: Each patient's patient-level verdict.scan_driven: Whether the trial is scan-driven, i.e. its flow wiresscan_eligibility.task_hash: Thepatient_eligibilitypartition the rows are written to.project_id: The trial (project) ID.now: Each row'screated_dateandprocessed_at.run_id: The run the rows are written by, orNoneoutside a run.
Returns One row per patient served.
live_saved_scopes
def live_saved_scopes( cache: CacheProtocol, project_id: str, liveness: DatasetLiveness, *, exclude: Collection[str] = (),) ‑> dict[str, frozenset[str]]:Return the saved scope of each dataset live for the trial.
A dataset no longer live judges nothing more, so its saved scope is
ignored. The scopes of the datasets liveness names as not live, and of
those in exclude, are never loaded; a non-live dataset the snapshot has
never named is dropped once read. A reduce loads them once, and so does a
recompute outside a run, which hands them to
read_trial_scans_for_recompute and patients_held_by_unjudged_scope_files.
Arguments
cache: The cache backend, holding the saved scopes.project_id: The trial (project) ID.liveness: Which datasets are live for the trial.exclude: Datasources whose saved scopes are not wanted.
Returns
{datasource task hash: its saved scope} for each live dataset that
has one; an empty set for one whose last publish selected no file.
patient_eligibility_task
def patient_eligibility_task( *, scan_eligibility: CacheAccessor | None = None, patient_level_eligibility: CacheAccessor | None, cache: CacheProtocol, task_hash: str, project_id: str, parent_task_hashes: dict[str, str] | None = None, config: PatientEligibilityConfig | None = None, run_id: str | None = None, filenames: list[str] | None = None, scope_filenames: list[str] | None = None, datasource_task_hash: str | None = None, scan_coverage_probe: ScanCoverageProbeProtocol | None = None, ehr_patient_listing: EHRPatientListing | None = None, eligibility_metrics: EligibilityMetricsEventReporter | None = None,) ‑> PatientEligibilityResult:Combine per-scan + patient-level verdicts into per-patient rows; flip pointer.
The rollup rows are always written. The pointer flip is withheld for an
empty run, and for one run when this run would publish zero eligible
patients on EHR coverage worse than the currently-published partition's —
see _withhold_publish, which also documents the case this does not
cover. A flip that happens records that coverage and the partition's EHR
staleness span in the pointer's tags.
A flow that wires no scan_eligibility input is EHR-only: every
patient-level row is served on its own verdict, with no scan rollup and no
scan-coverage tags. A flow that wires it is scan-driven, and a patient with
no stored scan is not served: a cutover removes any row left with none
(_remove_patients_without_stored_scans). A stored scan of a dataset no
longer live for the trial (removed from the pod's config, or unlinked from
the trial on the Hub) is not read at all (_drop_non_live), and each live
dataset's scans are read through that dataset's scope where it has one
(_in_scope).
Arguments
scan_eligibility: The upstreamscan_eligibilityedge, orNonewhen the flow wires none (EHR-only). Its value is unused — this step reduces the table through direct store calls, which an accessor's grouped queries cannot express — but declaring it is this step's dependency contract, and the name is the key its partition appears under inparent_task_hashes.patient_level_eligibility: The upstreampatient_level_eligibilityedge, declared on the same basis.cache: The pod background cache to read verdicts from and write rollups to.task_hash: This step's own partition key — where the rollup rows are written, and what the published pointer names for this table.project_id: The trial (project) ID (a runtime param).parent_task_hashes: Partition key of each upstream step, keyed by this step's input parameter name (bitfount.flows.dag.hashing). Where the two source tables are read from — this step's owntask_hashwould find nothing in them.Nonebecause the executor injects it only for a stamped DAG; an unstamped one then reaches the explicit error below rather than aTypeErrorabout a missing argument.config: Optional step config (trial display name); nothing in it is persisted by this step.run_id: Optional provenance run ID.filenames: Every file the sweep has carried through the DAG so far ($file_metadata.covered); on an unwaved run, the whole scoped selection. Its length is the numerator of the published scan-coverage figure, and a patient with no scan in scope is not removed while a file inscope_filenamesthat the scan sweep identifies as theirs is missing from it — this step reduces stored rows and never reads a file.scope_filenames: Every file the run's scope step selected, across all waves; a stored scan row outside it is ignored unless the file inventory holds it under another datasource, whose own saved scope then decides (_in_scope).Nonewhen the run is unscoped, and then every stored row is read. A context ref, so it moves no task hash.datasource_task_hash: The run's own datasource's key (a runtime param),generate_prefect_task_hash(pod_name, datasource_name), which the file inventory'stask_hashcolumn records on each row that datasource's index wrote. Its scans are never left out as a non-live dataset's, and a stored scan whose row names another hash is that datasource's, read through that datasource's saved scope.Nonereads the run's datasource off the scope files' inventory rows instead, which can misattribute a file shared between datasources' roots; with no scope either, every dataset's scans are judged on its liveness. This step's owntask_hashis its partition key, not the datasource's, hence the separate param.scan_coverage_probe: Injected probe reporting how much of the datasource is indexed now, for the denominator of that figure.Noneleaves the coverage tags unmeasured rather than failing the publish.ehr_patient_listing: Injected description of the EHR listing snapshot an EHR-only run's cohort came from, recorded on the pointer.Noneleaves the listing tags unmeasured.eligibility_metrics: Injected reporter for this run's eligibility summary, reported once the cutover has been decided.Nonereports nothing.
Returns
PatientEligibilityResult with the number of patient_eligibility rows
written and a cache accessor scoped to this task_hash.
Raises
ValueError: Ifproject_idis not supplied, or parent_task_hashes is missing thepatient_level_eligibilitypartition — without which this would silently roll up zero patients and leave the pointer on the previous run's data.
patients_held_by_unjudged_scope_files
def patients_held_by_unjudged_scope_files( cache: CacheProtocol, candidates: Mapping[str, Collection[ScanEligibilityRecord]], *, scopes: Mapping[str, frozenset[str]], spellings: Mapping[str, str],) ‑> set[str]:Return the candidates who own a live scope's file with no stored scan row.
A run keeps a patient it has no stored scan left to read for while they
own a file in a live sibling's saved scope that has no stored row: the
sibling has selected the file and not judged it yet (_in_scope,
_remove_out_of_scope_patients). This is the same hold outside a run,
where every live dataset is judged by its saved scope, for patients a
recompute (read_trial_scans_for_recompute) read no scan for.
A scope file counts as having a stored row when it is one of the
candidates' stored scans, by scan_id or by the spelling already resolved
for it. No path is resolved here, a stored scan's or a saved scope's.
Every other scope file is looked up in the scan sweep
(_patients_with_uncovered_scope_files); one stored for a patient who is
not a candidate maps to that patient, so it holds no candidate back.
Arguments
cache: The cache backend, holding the scan sweep's rows.candidates: Each patient to judge, with all of their stored scans.scopes: The saved scope of each dataset live for the trial (live_saved_scopes), in the file inventory's spelling.spellings: The spellings resolved so far for the candidates' scans, by scan id.
Returns The candidates who own such a file.
read_trial_scans_for_recompute
def read_trial_scans_for_recompute( cache: CacheProtocol, project_id: str, scan_partition: str, *, stored: Mapping[str, list[ScanEligibilityRecord]] | None = None, liveness: DatasetLiveness | None = None, scopes: Mapping[str, frozenset[str]] | None = None, spellings: dict[str, str] | None = None,) ‑> dict[str, list[ScanEligibilityRecord]]:Read a trial's stored scans as a run reads them, with no dataset running.
A trial's roll-up is recomputed outside a run once a dataset is removed
from the pod or unlinked from the trial. A run reads its own dataset's
scans through the run's scope and every other dataset's through that
dataset's saved scope (_in_scope); with no run, every dataset is read
through its saved scope.
A stored scan is read when the dataset the file inventory holds it under
(_scan_owners) is live for the trial (load_dataset_liveness) and has
either no saved scope or one that names the scan. A scan of a dataset no
longer live is still read when a live dataset's saved scope names it: a
file under two datasets' roots has one inventory row, under whichever
indexed it last. A scan with no inventory row belongs to no known dataset,
and there is no run's scope to claim it, so it is read only when some live
dataset's saved scope names it, or when no live dataset has saved a scope
to judge it by.
A scope names a scan by its scan_id or, on a miss, by its resolved
spelling (_scope_holds), as in a run; a non-live dataset's scan is
matched only by a spelling already resolved for it. Each scan is resolved
at most once, and no saved scope's file is resolved.
Arguments
cache: The cache backend, holding the stored scans, the file inventory, the saved scopes and the trial's dataset liveness.project_id: The trial (project) ID.scan_partition: The trial'sscan_eligibilitypartition.stored: The stored scans to read, by patient, already loaded from scan_partition; every patient given is read with all of their stored scans, and no other patient is read.Nonereads the whole partition.liveness: The trial's dataset liveness, already loaded;Nonereads it.scopes: The saved scope of each dataset live for the trial, already loaded (live_saved_scopes);Noneloads them.spellings: The spellings resolved so far, by scan id. A scan found in it is not resolved again, and each one resolved here is added to it, so a caller can judge the same scans by them afterwards.
Returns The scans to read, by patient. A patient with none left is absent.