Files
rodrigo 5a2afe7ebd Fase 1 de dev-data: modo de trabajo, historias de TEA y notebooks de punta a punta
- AGENTS.md y _docs/: proceso, tareas (D-1 a D-12, W-1, W-2, M-1, M-2),
  hallazgos I-D1 a I-D7, supuestos S-D1 a S-D7 y grooming G-D1.
- D-1: paquete dev_data con Core, Persistance e indicadores_kit por ruta.
- D-2: Azar, con semilla y fecha de referencia.
- D-3 y D-4: Historia (niño, EEG, evaluación y ADOS-2) e historias().
- D-5: esperado.py, la cohorte calculada de la historia.
- W-2: _docs/webapp_rutas.md.
- notebooks/: de las historias a K04 y M02 comparados con lo esperado, y
  WebApi sirviendo lo sembrado.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:42:55 +00:00

87 lines
2.7 KiB
Python

"""Azar: misma semilla, misma secuencia; derivación estable entre procesos."""
import subprocess
import sys
from datetime import date
import pytest
from dev_data.azar import Azar
REFERENCIA = date(2026, 1, 1)
DESDE = date(2020, 1, 1)
HASTA = date(2020, 12, 31)
def secuencia(azar: Azar) -> list:
"""Saca una muestra de fechas, enteros, elecciones y ocurrencias."""
fechas = [azar.fecha_entre(DESDE, HASTA) for _ in range(20)]
enteros = [azar.entero(0, 1000) for _ in range(20)]
elecciones = [azar.elegir(["F", "M", "X"], [0.4, 0.5, 0.1]) for _ in range(20)]
ocurrencias = [azar.ocurre(0.3) for _ in range(20)]
return [fechas, enteros, elecciones, ocurrencias]
def test_la_misma_semilla_da_la_misma_secuencia():
assert secuencia(Azar(42, REFERENCIA)) == secuencia(Azar(42, REFERENCIA))
def test_semillas_distintas_dan_secuencias_distintas():
assert secuencia(Azar(1, REFERENCIA)) != secuencia(Azar(2, REFERENCIA))
def test_derivar_con_nombres_distintos_da_semillas_distintas():
base = Azar(7, REFERENCIA)
assert base.derivar("a").semilla != base.derivar("b").semilla
assert base.derivar("a").entero(0, 10**9) != base.derivar("b").entero(0, 10**9)
def test_derivar_conserva_la_referencia():
assert Azar(7, REFERENCIA).derivar("a").referencia == REFERENCIA
def test_derivar_da_lo_mismo_en_dos_procesos():
codigo = (
"from datetime import date\n"
"from dev_data.azar import Azar\n"
"print(Azar(7, date(2026, 1, 1)).derivar('a').entero(0, 10**9))\n"
)
otro = subprocess.run(
[sys.executable, "-c", codigo],
capture_output=True,
text=True,
check=True,
).stdout.strip()
assert otro == "689836365"
assert Azar(7, REFERENCIA).derivar("a").entero(0, 10**9) == int(otro)
def test_fecha_entre_incluye_los_extremos():
azar = Azar(3, REFERENCIA)
fechas = {azar.fecha_entre(DESDE, date(2020, 1, 3)) for _ in range(1000)}
assert fechas <= {date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3)}
assert fechas == {date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3)}
def test_fecha_entre_nunca_sale_del_intervalo():
azar = Azar(11, REFERENCIA)
for _ in range(1000):
assert DESDE <= azar.fecha_entre(DESDE, HASTA) <= HASTA
def test_fecha_entre_con_desde_posterior_a_hasta_falla():
with pytest.raises(ValueError):
Azar(1, REFERENCIA).fecha_entre(HASTA, DESDE)
def test_entero_incluye_los_extremos():
azar = Azar(5, REFERENCIA)
valores = {azar.entero(2, 4) for _ in range(500)}
assert valores == {2, 3, 4}
def test_ocurre_con_probabilidad_cero_y_uno():
azar = Azar(9, REFERENCIA)
assert not any(azar.ocurre(0.0) for _ in range(100))
assert all(azar.ocurre(1.0) for _ in range(100))