diff --git a/docs/seguimiento_instancias_cacu.md b/docs/seguimiento_instancias_cacu.md index 9bacd0d..b99028d 100644 --- a/docs/seguimiento_instancias_cacu.md +++ b/docs/seguimiento_instancias_cacu.md @@ -2,7 +2,7 @@ This proposal gives `seguimiento` and all six CaCu model scenarios a shared unit of work. A **scenario instance** represents one patient, one model scenario, and one candidate procedure. The follow-up list reads the instance and its eligibility. A separate episode endpoint returns the clinical studies. Hamilton uses the same procedure references to assemble the features and outcomes required by each model. -The existing `GET /cacu/seguimiento` delegates to `indicadores_kit.serving.seguimiento_de`, which builds one item per patient and slices the list for pagination. The new list changes the unit of pagination to the scenario instance. The proposed endpoints and schema below are for review. The names and IDs in the examples are illustrative. +The current WebApi `GET /cacu/seguimiento` delegates to `webapi.follow_up.listing.seguimiento_de`, which builds one item per patient and slices the list for pagination. The new list changes the unit of pagination to the scenario instance. The names and IDs in the examples are illustrative. See [Experiment and measurements](#experiment-and-measurements) for the implemented slice. ## Scenario instance schema @@ -167,3 +167,39 @@ Use the CaCu routes as application JSON. A FHIR R5 facade can publish the linked | Computed risk | `RiskAssessment` when a score exists. Use `basis` for the evidence and `prediction` for the outcome, probability, and time horizon. [RiskAssessment](https://hl7.org/fhir/R5/riskassessment.html) | For a single FHIR response containing all studies, a server could declare an instance operation such as `GET /fhir/EpisodeOfCare/{id}/$estudios` in its `OperationDefinition` and `CapabilityStatement` and return a `Bundle` of type `collection`. This would be a CaCu operation. Its published definition should specify the cutoff parameter, included resource types, and authorization behavior. The application JSON endpoint above is sufficient for the initial follow-up UI. [FHIR operations](https://hl7.org/fhir/R5/operations.html) ยท [FHIR Bundle](https://hl7.org/fhir/R5/bundle.html) + +## Experiment and measurements + +The first implementation adds [PIM instance nodes](../models/cacu/instance_pim.py) and a [SQLite publication reader](../models/cacu/instance_publication.py). The PIM experiment fits the existing mixture with explicit outcome intervals joined by instance ID. It reuses the fitted mixture for inference and writes probabilities by instance and horizon. A regression test scores two visits for the same patient independently, checks single-versus-batch parity, and checks the effect of a result posted after the cutoff. Unscored instances still have a decision. The feature input expects the owning sample ID for each result, while referral history can be supplied as a feature keyed by instance. + +The WebApi branch `feature/cacu-seguimiento-instances` adds the two application JSON routes when `CACU_SIMULATED_INSTANCE_PUBLICATION` points to a prepared SQLite publication. The publisher stores immutable snapshots in one transaction. SQL applies filters and pagination before the response is built. The episode route resolves the linked studies from the same snapshot. The existing `/cacu/seguimiento` route also filters the cytology date window in SQL before pulling study rows into Python, while retaining its original response. + +This experiment uses synthetic instance rows and preassembled episode studies. The live dbt eligibility producer, migration of registered models and risk storage, and incremental refresh of live data remain design work. The FHIR section specifies a possible mapping. The current WebApi validates its FHIR routes against R4B, so the project would need to choose and test that version's profiles for an implementation. + +### Measured request times + +Both comparisons used local SQLite and Flask's HTTP test client. The scripts checked response equality before timing. The two rows below have different workloads and baselines. + +| Request and workload | Requests per path | Baseline median / p95 | Prototype median / p95 | +|---|---:|---:|---:| +| Existing `/cacu/seguimiento` with 25,000 extra cytology samples outside its requested window. Compared to WebApi `4840bd3`. | 15 | 203.69 ms / 379.72 ms | 97.02 ms / 141.29 ms | +| New `/cacu/seguimiento/instancias`, one filtered 50-row page from 120,000 instances. Compared to reading the same full publication and filtering and paging it in Python. | 20 | 934.36 ms / 1,091.52 ms | 3.20 ms / 5.11 ms | + +The existing endpoint's median fell by about 52% on this fixture. The new endpoint handled the filtered page in about 3 ms after a 4.71-second full local publication. These measurements describe request latency and synthetic publication cost. Production latency, live publication throughput, and clinical model performance still need separate measurements. + +### Reproduce the prototype + +In the Models checkout, generate a synthetic publication. + +```bash +python -m models.cacu.experiments.instance_follow_up /tmp/cacu-follow-up.sqlite --patients 1000 +``` + +Run the WebApi branch with the Models branch importable and `CACU_SIMULATED_INSTANCE_PUBLICATION=/tmp/cacu-follow-up.sqlite`. A request to `/cacu/seguimiento/instancias?escenario=cin2_pim` returns a page. The sample episode is available at `/cacu/episodios/eoc-demo-1/estudios?publicacion_id=demo-v1`. + +From the WebApi checkout, run the experiment scripts. The existing endpoint benchmark also takes `DBT_MANIFEST` for the standard synthetic fixture and uses WebApi commit `4840bd3` as its baseline. + +```bash +python experiments/benchmark_follow_up_window.py +python experiments/benchmark_follow_up_instances.py --patients 20000 --repeats 20 +```