Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 13c82be780 |
@@ -1,9 +1,10 @@
|
||||
"""Numeric distributions chapter (NUM DISTR) for AutomaticEDA.
|
||||
|
||||
For every numeric column the chapter draws, as a single indivisible figure, a
|
||||
histogram with the **mean, median and ±1σ band drawn as reference lines** and a
|
||||
**Tukey boxplot right below it** sharing the same X axis — exactly the user
|
||||
requirement for this chapter. Each figure is emitted as a lazy ``Figure`` block
|
||||
histogram with the **mean, median and ±1σ band drawn as reference lines** (the
|
||||
legend reports the numeric value of the mean, the median **and the standard
|
||||
deviation σ**) and a **Tukey boxplot right below it** sharing the same X axis —
|
||||
exactly the user requirement for this chapter. Each figure is emitted as a lazy ``Figure`` block
|
||||
so the renderers rasterize and scale it to fit a whole page/slide and nothing is
|
||||
ever cut; columns with many numerics simply flow across pages as small
|
||||
multiples.
|
||||
@@ -34,7 +35,7 @@ try:
|
||||
except Exception: # noqa: BLE001 — keep the chapter importable no matter what.
|
||||
build_boxplot_stats = None # type: ignore[assignment]
|
||||
|
||||
CHAPTER_VERSION = "1.1.0"
|
||||
CHAPTER_VERSION = "1.2.0"
|
||||
CHAPTER_ID = "num_distr"
|
||||
CHAPTER_TITLE = "Distribuciones numéricas"
|
||||
|
||||
@@ -140,9 +141,11 @@ def _make_hist_box(name: str, numeric: dict, box: dict):
|
||||
std = numeric.get("std")
|
||||
|
||||
# ±1σ band first (behind the lines), then median (solid) and mean (dashed).
|
||||
# The band's legend entry also reports the numeric value of the standard
|
||||
# deviation, so the reader sees mean, median AND σ at a glance.
|
||||
if mean is not None and std is not None and std > 0:
|
||||
ax_h.axvspan(mean - std, mean + std, color="#f0c27b", alpha=0.22,
|
||||
zorder=1, label="±1σ")
|
||||
zorder=1, label=f"±1σ (σ = {_fmt_num(std)})")
|
||||
if median is not None:
|
||||
ax_h.axvline(median, color="#2e8b57", linestyle="-", linewidth=1.6,
|
||||
zorder=4, label=f"mediana = {_fmt_num(median)}")
|
||||
@@ -152,7 +155,19 @@ def _make_hist_box(name: str, numeric: dict, box: dict):
|
||||
|
||||
ax_h.set_ylabel("frecuencia", fontsize=8)
|
||||
ax_h.tick_params(labelsize=7)
|
||||
ax_h.legend(fontsize=6.5, loc="upper right", framealpha=0.85)
|
||||
# Always surface σ in the legend: if the ±1σ band could not be drawn (no mean
|
||||
# or std<=0) but σ is still known, add a label-only proxy handle so the value
|
||||
# of the standard deviation is reported regardless of the band.
|
||||
handles, labels = ax_h.get_legend_handles_labels()
|
||||
if std is not None and not any("σ =" in lbl for lbl in labels):
|
||||
from matplotlib.lines import Line2D
|
||||
proxy = Line2D([], [], linestyle="none", marker="",
|
||||
label=f"σ = {_fmt_num(std)}")
|
||||
handles.append(proxy)
|
||||
labels.append(f"σ = {_fmt_num(std)}")
|
||||
if handles:
|
||||
ax_h.legend(handles, labels, fontsize=6.5, loc="upper right",
|
||||
framealpha=0.85)
|
||||
for spine in ("top", "right"):
|
||||
ax_h.spines[spine].set_visible(False)
|
||||
|
||||
|
||||
@@ -159,6 +159,50 @@ def test_anti_corte_muchas_columnas_pdf_y_pptx():
|
||||
assert res_pptx["n_slides"] >= 8 # at least one slide per column figure.
|
||||
|
||||
|
||||
def _hist_legend_texts(numeric, box=None):
|
||||
"""Build the per-column figure and return its histogram-legend label texts."""
|
||||
from datascience.automatic_eda.chapters.num_distr import _make_hist_box
|
||||
import matplotlib.pyplot as plt
|
||||
fig = _make_hist_box("col", numeric, box or {})
|
||||
ax_h = fig.axes[0] # the histogram is the top axis.
|
||||
leg = ax_h.get_legend()
|
||||
texts = [t.get_text() for t in leg.get_texts()] if leg else []
|
||||
plt.close(fig)
|
||||
return texts
|
||||
|
||||
|
||||
def test_golden_leyenda_histograma_reporta_valor_std():
|
||||
# The histogram legend must report the numeric value of the standard
|
||||
# deviation σ next to mean and median.
|
||||
numeric = _numeric_block(42.5, 40.0, 12.3, 1.0, 100.0, "right-skewed", 5)
|
||||
texts = _hist_legend_texts(numeric)
|
||||
joined = " ".join(texts)
|
||||
assert any("σ =" in t for t in texts), f"σ value missing in legend: {texts}"
|
||||
assert "12.3" in joined, f"std value 12.3 not in legend: {texts}"
|
||||
assert any("media =" in t for t in texts)
|
||||
assert any("mediana =" in t for t in texts)
|
||||
|
||||
|
||||
def test_edge_std_en_leyenda_aunque_no_haya_banda():
|
||||
# When the ±1σ band cannot be drawn (no mean) but σ is known, the legend
|
||||
# still surfaces the σ value via a label-only proxy handle.
|
||||
numeric = _numeric_block(42.5, 40.0, 7.5, 1.0, 100.0, "right-skewed", 0)
|
||||
numeric["mean"] = None # forces the band off; σ must still appear.
|
||||
texts = _hist_legend_texts(numeric)
|
||||
assert any("σ = 7.5" in t for t in texts), f"σ proxy missing: {texts}"
|
||||
|
||||
|
||||
def test_edge_sin_std_no_revienta_la_figura():
|
||||
# A numeric block without σ must not raise and simply omits the σ entry.
|
||||
import matplotlib.pyplot as plt
|
||||
numeric = _numeric_block(42.5, 40.0, 0.0, 1.0, 100.0, "discrete", 0)
|
||||
numeric["std"] = None
|
||||
texts = _hist_legend_texts(numeric)
|
||||
assert not any("σ =" in t for t in texts)
|
||||
# mean/median lines still produce their own legend entries.
|
||||
assert any("media =" in t for t in texts)
|
||||
|
||||
|
||||
def test_distribution_gloss_cubre_todas_las_etiquetas():
|
||||
# Every label detect_distribution_type can emit has a Spanish gloss.
|
||||
for label in ("normal-ish", "right-skewed", "left-skewed", "heavy-tail",
|
||||
|
||||
@@ -20,7 +20,7 @@ from __future__ import annotations
|
||||
|
||||
from .. import model
|
||||
|
||||
CHAPTER_VERSION = "1.1.0"
|
||||
CHAPTER_VERSION = "1.0.0"
|
||||
CHAPTER_ID = "overview"
|
||||
CHAPTER_TITLE = "Overview"
|
||||
|
||||
@@ -90,14 +90,8 @@ def _head_block(profile: dict, ctx: dict):
|
||||
if not cols:
|
||||
cols = list(head[0].keys())
|
||||
rows = [[model._safe_str(r.get(c)) for c in cols] for r in head[:10]]
|
||||
# Honest note: how many rows are shown and, when known, out of how many
|
||||
# rows the dataset has (so "primeras 10 filas de 891" gives context).
|
||||
note = f"primeras {len(rows)} filas"
|
||||
n_rows = profile.get("n_rows")
|
||||
if isinstance(n_rows, int) and not isinstance(n_rows, bool) \
|
||||
and n_rows > len(rows):
|
||||
note += f" de {n_rows:,}".replace(",", ".")
|
||||
return model.DataTable(header=cols, rows=rows, note=note)
|
||||
return model.DataTable(header=cols, rows=rows,
|
||||
note=f"primeras {len(rows)} filas")
|
||||
return model.Note(
|
||||
"df.head no disponible: el TableProfile no incluye 'head_rows'. La fase "
|
||||
"de cálculo debe añadir profile['head_rows'] (lista de dicts fila) o "
|
||||
|
||||
@@ -1,187 +0,0 @@
|
||||
"""Tests for the OVERVIEW chapter — DoD: golden + edges + degradation.
|
||||
|
||||
Self-contained: builds synthetic TableProfiles (no DuckDB) so the suite is fast
|
||||
and deterministic. Verifies that ``build_overview`` renders the raw first rows
|
||||
(``df.head``) as a DataTable when ``head_rows`` is present — both when it arrives
|
||||
via ``profile['head_rows']`` (populated by ``profile_table``) and via
|
||||
``ctx['head_rows']`` (populated by ``build_eda_render_ctx``) — that the chapter
|
||||
also renders the column dictionary and the numeric describe, that the full
|
||||
document renders to PDF and PPTX showing the head values, and that a profile with
|
||||
NO head data degrades to an honest note instead of raising or inventing rows.
|
||||
"""
|
||||
|
||||
import os
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
from pypdf import PdfReader
|
||||
from pptx import Presentation
|
||||
|
||||
from datascience.automatic_eda.model import DataTable, Note
|
||||
from datascience.automatic_eda.chapters.overview import (
|
||||
CHAPTER_ID, CHAPTER_VERSION, build_overview,
|
||||
)
|
||||
from datascience.render_automatic_eda_pdf import render_automatic_eda_pdf
|
||||
from datascience.render_automatic_eda_pptx import render_automatic_eda_pptx
|
||||
|
||||
|
||||
def _columns() -> list:
|
||||
return [
|
||||
{"name": "PassengerId", "inferred_type": "numeric", "null_pct": 0.0,
|
||||
"null_count": 0, "numeric": {"mean": 2.0, "median": 2.0, "min": 1.0,
|
||||
"max": 3.0, "std": 1.0}},
|
||||
{"name": "Survived", "inferred_type": "numeric", "null_pct": 0.0,
|
||||
"null_count": 0, "numeric": {"mean": 0.33, "median": 0.0, "min": 0.0,
|
||||
"max": 1.0, "std": 0.58}},
|
||||
{"name": "Pclass", "inferred_type": "numeric", "null_pct": 0.0,
|
||||
"null_count": 0, "numeric": {"mean": 2.33, "median": 3.0, "min": 1.0,
|
||||
"max": 3.0, "std": 1.15}},
|
||||
{"name": "Name", "inferred_type": "categorical", "null_pct": 0.0,
|
||||
"null_count": 0, "distinct_count": 3},
|
||||
{"name": "Sex", "inferred_type": "categorical", "null_pct": 0.0,
|
||||
"null_count": 0, "distinct_count": 2,
|
||||
"categorical": {"top": [{"value": "male", "count": 2},
|
||||
{"value": "female", "count": 1}]}},
|
||||
]
|
||||
|
||||
|
||||
def _head_rows() -> list:
|
||||
return [
|
||||
{"PassengerId": 1, "Survived": 0, "Pclass": 3,
|
||||
"Name": "Braund Owen", "Sex": "male"},
|
||||
{"PassengerId": 2, "Survived": 1, "Pclass": 1,
|
||||
"Name": "Cumings Florence", "Sex": "female"},
|
||||
{"PassengerId": 3, "Survived": 1, "Pclass": 3,
|
||||
"Name": "Heikkinen Laina", "Sex": "female"},
|
||||
]
|
||||
|
||||
|
||||
def _profile(with_head: bool = True) -> dict:
|
||||
prof = {
|
||||
"table": "titanic",
|
||||
"source": "/data/titanic.csv",
|
||||
"profiled_at": "2026-06-30T10:00:00+00:00",
|
||||
"n_rows": 891,
|
||||
"n_cols": 5,
|
||||
"quality_score": 88.0,
|
||||
"columns": _columns(),
|
||||
}
|
||||
if with_head:
|
||||
prof["head_rows"] = _head_rows()
|
||||
return prof
|
||||
|
||||
|
||||
def _pdf_text(path: str) -> str:
|
||||
txt = "".join((pg.extract_text() or "") for pg in PdfReader(path).pages)
|
||||
return re.sub(r"\s+", " ", txt)
|
||||
|
||||
|
||||
def _pptx_text(path: str) -> str:
|
||||
prs = Presentation(path)
|
||||
parts = []
|
||||
for sl in prs.slides:
|
||||
for sh in sl.shapes:
|
||||
if sh.has_text_frame:
|
||||
parts.append(sh.text_frame.text)
|
||||
if sh.has_table:
|
||||
tb = sh.table
|
||||
for r in range(len(tb.rows)):
|
||||
for c in range(len(tb.columns)):
|
||||
parts.append(tb.cell(r, c).text)
|
||||
return re.sub(r"\s+", " ", " ".join(parts))
|
||||
|
||||
|
||||
def _flatten(blocks):
|
||||
"""Recursively flatten Group blocks into a flat list (none here today)."""
|
||||
out = []
|
||||
for b in blocks:
|
||||
inner = getattr(b, "blocks", None)
|
||||
if inner is not None and getattr(b, "kind", None) == "group":
|
||||
out.extend(_flatten(inner))
|
||||
else:
|
||||
out.append(b)
|
||||
return out
|
||||
|
||||
|
||||
def test_golden_build_overview_muestra_head_desde_profile():
|
||||
ch = build_overview(_profile(), {})
|
||||
assert ch is not None
|
||||
assert ch.id == CHAPTER_ID
|
||||
assert ch.version == CHAPTER_VERSION
|
||||
blocks = _flatten(ch.blocks)
|
||||
# The first DataTable is df.head: its header is the column names and the
|
||||
# real first rows are present (not a placeholder note).
|
||||
tables = [b for b in blocks if isinstance(b, DataTable)]
|
||||
assert tables, "overview must emit at least the df.head DataTable"
|
||||
head_tbl = tables[0]
|
||||
assert head_tbl.header == ["PassengerId", "Survived", "Pclass",
|
||||
"Name", "Sex"]
|
||||
assert len(head_tbl.rows) == 3
|
||||
flat = [str(c) for row in head_tbl.rows for c in row]
|
||||
assert "Braund Owen" in flat and "Cumings Florence" in flat
|
||||
# Honest note carries how many rows shown out of the dataset total.
|
||||
assert head_tbl.note is not None
|
||||
assert "primeras 3 filas" in head_tbl.note and "891" in head_tbl.note
|
||||
# No "df.head no disponible" placeholder when head_rows is present.
|
||||
assert not any(isinstance(b, Note) and "no disponible" in b.text
|
||||
for b in blocks)
|
||||
|
||||
|
||||
def test_golden_head_desde_ctx_tambien_funciona():
|
||||
# head_rows absent in profile but present in ctx (build_eda_render_ctx path).
|
||||
prof = _profile(with_head=False)
|
||||
ch = build_overview(prof, {"head_rows": _head_rows()})
|
||||
assert ch is not None
|
||||
tables = [b for b in _flatten(ch.blocks) if isinstance(b, DataTable)]
|
||||
flat = [str(c) for row in tables[0].rows for c in row]
|
||||
assert "Braund Owen" in flat
|
||||
|
||||
|
||||
def test_golden_render_pdf_muestra_head():
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
out = os.path.join(d, "eda.pdf")
|
||||
res = render_automatic_eda_pdf(_profile(), out, {"title": "EDA"})
|
||||
assert res["path"] == out and os.path.exists(out)
|
||||
assert CHAPTER_ID in [c["id"] for c in res["chapters"]]
|
||||
txt = _pdf_text(out)
|
||||
assert "Braund" in txt and "male" in txt
|
||||
assert "primeras" in txt # head note rendered.
|
||||
assert "df.head" in txt # chapter heading rendered.
|
||||
assert "no disponible" not in txt # placeholder NOT shown.
|
||||
|
||||
|
||||
def test_golden_render_pptx_muestra_head():
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
out = os.path.join(d, "eda.pptx")
|
||||
res = render_automatic_eda_pptx(_profile(), out, {"title": "EDA"})
|
||||
assert res["path"] == out and os.path.exists(out)
|
||||
assert CHAPTER_ID in [c["id"] for c in res["chapters"]]
|
||||
txt = _pptx_text(out)
|
||||
assert "Braund" in txt and "Cumings" in txt
|
||||
|
||||
|
||||
def test_edge_sin_head_rows_degrada_a_nota_honesta():
|
||||
# No head data anywhere: chapter still builds (columns exist), shows the
|
||||
# honest placeholder note, and never invents rows nor raises.
|
||||
prof = _profile(with_head=False)
|
||||
ch = build_overview(prof, {})
|
||||
assert ch is not None
|
||||
blocks = _flatten(ch.blocks)
|
||||
assert any(isinstance(b, Note) and "no disponible" in b.text
|
||||
for b in blocks)
|
||||
# The first DataTable now is the column dictionary, not df.head rows.
|
||||
tables = [b for b in blocks if isinstance(b, DataTable)]
|
||||
assert all("Braund" not in str(c)
|
||||
for tbl in tables for row in tbl.rows for c in row)
|
||||
|
||||
|
||||
def test_edge_none_y_vacio_no_rompen():
|
||||
# Nothing to render at all -> None, no raise.
|
||||
assert build_overview(None, None) is None
|
||||
assert build_overview({}, {}) is None
|
||||
assert build_overview({"columns": []}, {}) is None
|
||||
# Only head_rows (no columns) still yields a chapter with the head table.
|
||||
ch = build_overview({"columns": []}, {"head_rows": _head_rows()})
|
||||
assert ch is not None
|
||||
tables = [b for b in _flatten(ch.blocks) if isinstance(b, DataTable)]
|
||||
assert tables and len(tables[0].rows) == 3
|
||||
@@ -20,10 +20,6 @@ vacia y el resto del ctx se construye igual. Ante un fallo global devuelve al
|
||||
menos ``{**base_ctx, "db_path": db_path, "table": table}``.
|
||||
|
||||
Claves de DATOS que produce (las consumen los capitulos):
|
||||
- ``head_rows`` : [ {col: valor, ...}, ... ] primeras filas CRUDAS de la
|
||||
tabla (``SELECT * LIMIT head_n``), una entrada por fila.
|
||||
La lee el capitulo OVERVIEW para mostrar df.head real en
|
||||
lugar del placeholder "df.head no disponible".
|
||||
- ``raw_numeric`` : {col: [float|None, ...]} muestra cruda de las columnas
|
||||
numericas, ALINEADA POR FILA (una entrada por fila aunque
|
||||
sea None). La leen modelos (clustering 2D en vivo) y
|
||||
@@ -60,7 +56,7 @@ def _to_float(value):
|
||||
return None
|
||||
|
||||
|
||||
def build_eda_render_ctx(db_path, table, profile, backend="duckdb", sample=5000, base_ctx=None, head_n=10):
|
||||
def build_eda_render_ctx(db_path, table, profile, backend="duckdb", sample=5000, base_ctx=None):
|
||||
"""Construye el ctx de datos crudos para los renderers de AutomaticEDA.
|
||||
|
||||
Args:
|
||||
@@ -81,15 +77,13 @@ def build_eda_render_ctx(db_path, table, profile, backend="duckdb", sample=5000,
|
||||
base_ctx: dict opcional con claves de presentacion ya preparadas
|
||||
(dataset_name, source_origin, ...). Se parte de una copia y NO se
|
||||
pisan sus claves; solo se añaden las de datos. Default None -> {}.
|
||||
head_n: numero de filas crudas a muestrear para ``ctx["head_rows"]``
|
||||
(df.head del capitulo OVERVIEW). Default 10. <=0 omite la clave.
|
||||
|
||||
Returns:
|
||||
El dict ``ctx`` directamente (NO un wrapper {status,...}): se pasa tal
|
||||
cual como ``meta={"ctx": <ese dict>}`` a render_automatic_eda_pdf/pptx.
|
||||
Nunca lanza. Claves que puede contener: head_rows, raw_numeric,
|
||||
timeseries_raw, geo_points (omitidas si no aplican o fallan), y siempre
|
||||
db_path + table para backends validos.
|
||||
Nunca lanza. Claves que puede contener: raw_numeric, timeseries_raw,
|
||||
geo_points (omitidas si no aplican o fallan), y siempre db_path + table
|
||||
para backends validos.
|
||||
"""
|
||||
# Copia de base_ctx: nunca mutamos el dict del caller. Las claves de
|
||||
# presentacion que ya traiga se conservan; las de datos se añaden encima.
|
||||
@@ -123,24 +117,6 @@ def build_eda_render_ctx(db_path, table, profile, backend="duckdb", sample=5000,
|
||||
ctx["db_path"] = db_path
|
||||
ctx["table"] = table
|
||||
|
||||
# 1.5) head_rows: primeras filas CRUDAS de la tabla (SELECT * LIMIT n)
|
||||
# para que el capitulo OVERVIEW muestre df.head real en vez del
|
||||
# placeholder. Una sola query, dict-no-throw: si falla, se omite la
|
||||
# clave (el capitulo degrada a su nota honesta). No se pisa una clave
|
||||
# head_rows que ya viniera en base_ctx (presentacion).
|
||||
if head_n and int(head_n) > 0 and "head_rows" not in ctx:
|
||||
try:
|
||||
hq = query_fn(f'SELECT * FROM "{table}" LIMIT {int(head_n)}')
|
||||
if isinstance(hq, dict) and hq.get("status") == "ok":
|
||||
hrows = [
|
||||
dict(r) for r in (hq.get("rows") or [])
|
||||
if isinstance(r, dict)
|
||||
]
|
||||
if hrows:
|
||||
ctx["head_rows"] = hrows
|
||||
except Exception: # noqa: BLE001 - dict-no-throw: omitir la clave
|
||||
pass
|
||||
|
||||
# 2) Columnas del perfil agregado (lectura defensiva).
|
||||
cols = profile.get("columns") if isinstance(profile, dict) else None
|
||||
cols = cols or []
|
||||
|
||||
@@ -536,21 +536,6 @@ def profile_table(
|
||||
type_breakdown[it] += 1
|
||||
prof["type_breakdown"] = type_breakdown
|
||||
|
||||
# 8.1) Primeras filas crudas (df.head) para el capitulo OVERVIEW del motor
|
||||
# AutomaticEDA: una muestra SELECT col1,col2,... LIMIT 10 alineada por fila.
|
||||
# Se reusa _sample_rows (mismo lector read-only). Estilo dict-no-throw: si
|
||||
# falla, head_rows queda None y el capitulo degrada a su nota honesta. El
|
||||
# capitulo lo recoge via profile["head_rows"]; build_eda_render_ctx ademas
|
||||
# lo replica en ctx["head_rows"] cuando se construye el contexto de render.
|
||||
try:
|
||||
head_names = [c.get("name") for c in cols if c.get("name")]
|
||||
head_rows = _sample_rows(_q, table, head_names, 10)
|
||||
prof["head_rows"] = [
|
||||
dict(r) for r in head_rows if isinstance(r, dict)
|
||||
] or None
|
||||
except Exception: # noqa: BLE001
|
||||
prof["head_rows"] = None
|
||||
|
||||
# 8.5) Matriz de correlacion/asociacion sobre una muestra de filas
|
||||
# alineadas. Elige la metrica por par de tipos (Pearson/Spearman,
|
||||
# Cramer's V/Theil's U, correlation ratio, MI) via association_matrix.
|
||||
|
||||
Reference in New Issue
Block a user