Precompute (offline)¶
Pure functions for the offline workflows: patient-protocol fit (PPF)
and the pairwise protocol-similarity matrix. Run on enrollment / protocol
changes; persisted to ~/.ai_cdss/output/ and read by the repository at
recommend time. Never on the hot path.
precompute
¶
Offline computations: PPF and protocol similarity.
Pure functions. No classes. No state. These run during the offline
patient-registration and protocol-addition workflows, NOT during a
recommendation call (the recommender reads precomputed PPF / similarity
from disk via RGSCohortRepository).
Sections:
SECTION 1 PPF: compute and persist patient-protocol fit.
SECTION 2 Protocol similarity: compute and persist pairwise
Gower-distance similarity over protocol attributes.
compute_ppf_for_patients
¶
compute_ppf_for_patients(
patient_subscales: DataFrame,
protocol_attributes: DataFrame,
scales_yaml: Optional[Path | str] = None,
mapping_yaml: Optional[Path | str] = None,
) -> DataFrame
Compute PPF for the patients in patient_subscales.
Pure: takes raw frames in, returns the joined (PPF, CONTRIB)
long-form DataFrame. Does NOT persist, call persist_ppf for
that. The SUBSCALES list used in the computation is attached
via result.attrs["SUBSCALES"].
Raises if the patient frame is missing any subscale required by the protocol mapping.
Source code in src\ai_cdss\precompute.py
persist_ppf
¶
Persist a PPF DataFrame to Parquet.
First call creates the file; subsequent calls upsert by BY_PP
keys (existing rows for those (patient, protocol) pairs are
replaced).
Source code in src\ai_cdss\precompute.py
compute_protocol_similarity_matrix
¶
compute_protocol_similarity_matrix(
protocol_attributes: DataFrame,
mapping_yaml: Optional[Path | str] = None,
) -> DataFrame
Compute pairwise protocol similarity.
Pure: protocol_attributes → long-form (PROTOCOL_A, PROTOCOL_B,
SIMILARITY) DataFrame. Does NOT persist, call persist_similarity.
Source code in src\ai_cdss\precompute.py
persist_similarity
¶
Persist protocol similarity to CSV.