served_tree
The served criteria_tree: the evaluated tree as the evidence carries it.
attach_criteria_tree_verdict evaluates the tree for each evaluation and
stores the result on it. annotate_criteria_tree gives every node its state
and source, and every leaf its findings, with the findings builders of the
domain modules.
Module
Functions
annotate_criteria_tree
def annotate_criteria_tree( criteria_tree: CriterionTreeNode, node_outcomes: Sequence[CriterionOutcome], authored: Mapping[str, Any] | None = None, identity: SubjectIdentity | None = None,) ‑> AnnotatedTreeNode:Add the results of one row to the configured tree.
Each node keeps its configured keys, and gets these keys:
state:pass,failorunknown, the result of the condition of the node itself. Anotnode reverses the result of its child, so below anot,passmeans that the condition matched. Every node of a not-analysed criterion (seeis_not_analysed) isnot_assessed, and gets none of the keys below.source: theCriterionProvenancevalues of the node, or of the leaves below it. Not included if no leaf has a known source.findings: only on a leaf. The records that the leaf read and decided on, asTreeFindings, whatever its state. An empty list means that the leaf read no record. Not included if the leaf has no records to show.checked_window: only on a leaf. The dates within which the leaf searched for records. Not included if the leaf searched all records, or could not search.unknown_reason: only on a node whose state isunknown, and only when the node has a reason of its own. Why it could not decide. A leaf usually has one; a combinator usually does not, since its children's reasons explain it. Amulti_scannode that selected no scan has one, and so does anotwithon_match: unknownwhose child matched (flagged_match).
Arguments
criteria_tree: The configured tree. The node ids must be set.node_outcomes: The outcome of each node for the row, fromevaluate_criterion_tree.authored: The result ofauthored_criteria_tree(criteria_tree). Give it to use one dump for many rows.identity: The subject of the row. A scan finding gets the date and the eye of its scan.
Returns
A new JSON-safe dict. This function does not change authored.
attach_criteria_tree_verdict
def attach_criteria_tree_verdict( evaluations: list[CriteriaEvaluation], criteria_tree: CriterionTreeNode | None,) ‑> None:Fold a criteria_tree's verdict into each evaluation, in place.
For each evaluation:
- Moves every outcome whose
tree_node_idis set out ofoutcomesinto a leaf-id-to-outcome map. - Evaluates
criteria_treeagainst that map viaevaluate_criterion_tree, which builds one outcome per node (leaf and combinator alike, stamping each one's resolvedtree_groupalong the way) and collectsreview_flagsin the same pass. - Stores every node's outcome on
tree_node_outcomes. A copy of the root's outcome — withconfig_field/output_field/column_nameset to the fixedCRITERIA_TREE_FIELDandtree_node_idcleared, same as every prior phase's hand-built synthetic outcome — is appended back intooutcomes. - Puts the annotated tree in
criteria_tree.
The copy, not the same object, is what goes back into outcomes: the
root's real tree_node_id/output_field/column_name (its own node
address, f"criteria_tree.{id}" for a combinator, or a real leaf's own
identity when the whole tree is a single leaf) must survive unchanged in
tree_node_outcomes for per-node introspection, while outcomes still
needs exactly the fixed CRITERIA_TREE_FIELD tag: tabulate_criteria_outcomes
keys off that constant (via column_name) to recognise the tree's
synthetic combined outcome and exclude it from FILTER_CONTEXT_COLUMN,
and attach_criteria_tree_verdict itself keys off tree_node_id is not None to recognise a tree outcome and move it out of outcomes — the
copy must clear it, or a second pass over the same evaluations would
mistake this already-attached root for a leaf to move again.
The root's outcome — and every combinator's — has grain/provenance
derived from its children by _combinator_outcome, not fixed: a tree of
only EHR-provenance, patient-grain leaves derives
grain=PATIENT, provenance=EHR and routes through build_patient_level_evidence/
patient_grain_status accordingly. A tree with an eye-scoped EHR leaf
derives grain=ORGAN and routes through build_scan_evidence, because its
verdict depends on the study eye.
Leaf outcomes are split for every evaluation first, so a multi_scan
node can read the outcomes of the patient's other rows.
A no-op when criteria_tree is None.
Arguments
evaluations: The evaluations to update in place.criteria_tree: The configured tree, orNone.