b1bbbc25b4
Preserve dev risk batches and governance, individual patient selection, optional access and explicit cohort reader overrides. Register one CaMa implementation and retain legacy Python blueprint consumers. Validated with 681 WebApi tests on the integrated parent and submodules.
50 lines
2.4 KiB
Python
50 lines
2.4 KiB
Python
"""Suppression of small groups in the descriptive analysis.
|
|
|
|
A count of 1 to `umbral - 1` patients is never published. Hiding only those would still let a difference give them away
|
|
(a published total minus the published cells), so a hidden cell drags the smallest published cell of its line with it
|
|
until the hidden patients are at least `umbral`. A proportion is published only when neither its group, its cases nor its
|
|
non-cases is such a small count. Zero is published: it describes nobody.
|
|
"""
|
|
|
|
PEQUENO = lambda n, umbral: 0 < n < umbral # noqa: E731
|
|
|
|
|
|
def proporcion_publicable(casos: int, total: int, umbral: int) -> bool:
|
|
"""Whether `casos` of `total` may be published: the group, the cases and the rest are each zero or at least `umbral`."""
|
|
return total >= umbral and not PEQUENO(casos, umbral) and not PEQUENO(total - casos, umbral)
|
|
|
|
|
|
def _completar(suprimidas: set, conteos: dict, linea: list, umbral: int) -> bool:
|
|
"""Adds to `suprimidas` the smallest published cells of `linea` until the hidden ones of it total `umbral` or more."""
|
|
cambio = False
|
|
while True:
|
|
ocultos = sum(conteos[c] for c in linea if c in suprimidas)
|
|
if ocultos == 0 or ocultos >= umbral:
|
|
return cambio
|
|
candidatas = [c for c in linea if c not in suprimidas and conteos[c] > 0]
|
|
if not candidatas:
|
|
return cambio
|
|
suprimidas.add(min(candidatas, key=lambda c: (conteos[c], str(c))))
|
|
cambio = True
|
|
|
|
|
|
def suprimir_categorias(conteos: dict[str, int], umbral: int) -> set[str]:
|
|
"""The categories of one distribution whose count is not published."""
|
|
suprimidas = {c for c, n in conteos.items() if PEQUENO(n, umbral)}
|
|
_completar(suprimidas, conteos, list(conteos), umbral)
|
|
return suprimidas
|
|
|
|
|
|
def suprimir_tabla(conteos: dict[tuple[str, str], int], umbral: int) -> set[tuple[str, str]]:
|
|
"""The cells of a two-way table whose count is not published, with the same rule applied along every row and column."""
|
|
suprimidas = {c for c, n in conteos.items() if PEQUENO(n, umbral)}
|
|
filas = {f for f, _ in conteos}
|
|
columnas = {c for _, c in conteos}
|
|
lineas = [[k for k in conteos if k[0] == f] for f in sorted(filas)] + [[k for k in conteos if k[1] == c] for c in sorted(columnas)]
|
|
cambio = True
|
|
while cambio:
|
|
cambio = False
|
|
for linea in lineas:
|
|
cambio |= _completar(suprimidas, conteos, linea, umbral)
|
|
return suprimidas
|