1

Ajoute l’étape 25 de répartition des minifigs par genre

This commit is contained in:
2025-12-02 11:42:50 +01:00
parent f5c1fa6333
commit 1f18195df2
9 changed files with 190 additions and 26 deletions

View File

@@ -0,0 +1,14 @@
"""Palette de couleurs et libellés pour les genres des personnages."""
GENDER_COLORS = {
"male": "#4c72b0",
"female": "#c44e52",
"unknown": "#7f7f7f",
}
GENDER_LABELS = {
"male": "Homme",
"female": "Femme",
"unknown": "Inconnu",
"": "Inconnu",
}

View File

@@ -8,22 +8,10 @@ from matplotlib.patches import Patch
from lib.filesystem import ensure_parent_dir
from lib.milestones import load_milestones
from lib.plots.gender_palette import GENDER_COLORS, GENDER_LABELS
from lib.rebrickable.stats import read_rows
GENDER_COLORS = {
"male": "#4c72b0",
"female": "#c44e52",
"unknown": "#7f7f7f",
}
GENDER_LABELS = {
"male": "Homme",
"female": "Femme",
"unknown": "Inconnu",
"": "Inconnu",
}
def load_spans(path: Path) -> List[dict]:
"""Charge le CSV des bornes min/max par personnage."""
return read_rows(path)

View File

@@ -7,22 +7,10 @@ import matplotlib.pyplot as plt
from matplotlib.patches import Patch
from lib.filesystem import ensure_parent_dir
from lib.plots.gender_palette import GENDER_COLORS, GENDER_LABELS
from lib.rebrickable.stats import read_rows
GENDER_COLORS = {
"male": "#4c72b0",
"female": "#c44e52",
"unknown": "#7f7f7f",
}
GENDER_LABELS = {
"male": "Homme",
"female": "Femme",
"unknown": "Inconnu",
"": "Inconnu",
}
def load_counts(path: Path) -> List[dict]:
"""Charge le CSV des comptes par personnage."""
return read_rows(path)

View File

@@ -0,0 +1,48 @@
"""Diagramme de répartition des minifigs par genre."""
from pathlib import Path
from typing import List
import matplotlib.pyplot as plt
from lib.filesystem import ensure_parent_dir
from lib.plots.gender_palette import GENDER_COLORS, GENDER_LABELS
from lib.rebrickable.stats import read_rows
def load_gender_counts(path: Path) -> List[dict]:
"""Charge le CSV des comptes par genre."""
return read_rows(path)
def plot_minifig_gender_share(counts_path: Path, destination_path: Path) -> None:
"""Trace un diagramme circulaire de la répartition des minifigs par genre."""
rows = load_gender_counts(counts_path)
if not rows:
return
genders = [row["gender"] for row in rows]
counts = [int(row["minifig_count"]) for row in rows]
colors = [GENDER_COLORS.get(gender.strip().lower(), GENDER_COLORS["unknown"]) for gender in genders]
total = sum(counts)
labels = []
for gender, count in zip(genders, counts):
percent = (count / total) * 100 if total else 0
label = f"{GENDER_LABELS.get(gender.strip().lower(), 'Inconnu')} ({percent:.1f} %)"
labels.append(label)
fig, ax = plt.subplots(figsize=(6, 6))
ax.pie(
counts,
labels=labels,
colors=colors,
startangle=90,
wedgeprops={"linewidth": 0.6, "edgecolor": "#0d0d0d"},
)
centre_circle = plt.Circle((0, 0), 0.5, fc="white")
ax.add_artist(centre_circle)
ax.set_title("Répartition des minifigs par genre (thèmes filtrés)")
ensure_parent_dir(destination_path)
fig.tight_layout()
fig.savefig(destination_path, dpi=160)
plt.close(fig)

View File

@@ -34,6 +34,28 @@ def aggregate_by_character(rows: Iterable[dict]) -> List[dict]:
return aggregates
def aggregate_by_gender(rows: Iterable[dict]) -> List[dict]:
"""Compte les minifigs distinctes par genre (fig_num unique)."""
genders_by_fig: Dict[str, str] = {}
counts: Dict[str, int] = defaultdict(int)
for row in rows:
fig_num = row["fig_num"].strip()
gender = row.get("gender", "").strip().lower()
normalized = gender if gender in ("male", "female") else "unknown"
if fig_num == "":
continue
if fig_num in genders_by_fig:
continue
genders_by_fig[fig_num] = normalized
counts[normalized] += 1
aggregates: List[dict] = []
ordered = ["female", "male", "unknown"]
for gender in ordered:
if gender in counts:
aggregates.append({"gender": gender, "minifig_count": str(counts[gender])})
return aggregates
def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes par personnage."""
ensure_parent_dir(path)
@@ -45,6 +67,17 @@ def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
writer.writerow(row)
def write_gender_counts(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des comptes par genre."""
ensure_parent_dir(path)
fieldnames = ["gender", "minifig_count"]
with path.open("w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for row in rows:
writer.writerow(row)
def load_sets_enriched(path: Path) -> Dict[str, str]:
"""Indexe les années par set_num."""
lookup: Dict[str, str] = {}