feat(fhir): publish the Wilson interval of a proportion's score

A proportion's measureScore now carries its 95 % Wilson interval in the ic-bajo and ic-alto extensions,
as proportions, the way durations carry their quartiles. Nothing is published without a denominator.
This commit is contained in:
2026-11-21 17:42:02 -06:00
parent c2b18d9969
commit 0c102d89f7
3 changed files with 34 additions and 8 deletions
+2 -2
View File
@@ -122,8 +122,8 @@ def _proportion_cells(definition, counts: Counts, excluded: int | None = None) -
*(_count(c, counts.numerator) for c in definition.numerator.codes)]}
if excluded is not None and getattr(definition, "exclusion", None):
cells["population"] += [_count(c, excluded) for c in definition.exclusion.codes]
if counts.proportion is not None:
cells["measureScore"] = {"value": counts.proportion}
if counts.score:
cells["measureScore"] = counts.score
return cells
+15 -2
View File
@@ -41,6 +41,8 @@ from sqlalchemy import (
true,
)
from webapi.cama_analysis.stats import wilson
AGGREGATION_VERSION = "md19.1" # the unit-row contract the publication views read
TOTAL = "total"
NO_DATA = "sin_dato"
@@ -545,6 +547,15 @@ class Counts:
def proportion(self) -> float | None:
return self.numerator / self.denominator if self.denominator else None
@property
def score(self) -> dict | None:
"""The FHIR `measureScore`: the proportion and its 95 % Wilson interval, all as proportions in [0, 1]."""
if not self.denominator:
return None
low, high = wilson(self.numerator, self.denominator)
return {"value": self.proportion, "extension": [
{"url": INTERVAL_LOW, "valueDecimal": low}, {"url": INTERVAL_HIGH, "valueDecimal": high}]}
@dataclasses.dataclass(frozen=True)
class Answer:
@@ -657,6 +668,8 @@ POPULATION_SYSTEM = "http://terminology.hl7.org/CodeSystem/measure-population"
EVENT_CUTOFF = "https://isssteleon.org/fhir/StructureDefinition/corte-evento"
KNOWLEDGE_CUTOFF = "https://isssteleon.org/fhir/StructureDefinition/corte-conocimiento"
HISTORY_ENTRY = "https://isssteleon.org/fhir/StructureDefinition/reporte-id"
INTERVAL_LOW = "https://isssteleon.org/fhir/StructureDefinition/ic-bajo"
INTERVAL_HIGH = "https://isssteleon.org/fhir/StructureDefinition/ic-alto"
def _populations(definition: MeasureDefinition, counts: Counts) -> list[dict]:
@@ -667,8 +680,8 @@ def _populations(definition: MeasureDefinition, counts: Counts) -> list[dict]:
def _scored(entry: dict, counts: Counts) -> dict:
if counts.proportion is not None:
entry["measureScore"] = {"value": counts.proportion}
if counts.score:
entry["measureScore"] = counts.score
return entry