1

Trie le graphique de réutilisation des têtes par usage décroissant

This commit is contained in:
2025-12-02 22:51:08 +01:00
parent 14a7dc8561
commit 6dc1f1cac5
5 changed files with 73 additions and 12 deletions

View File

@@ -5,8 +5,11 @@ from pathlib import Path
from typing import List
import matplotlib.pyplot as plt
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from PIL import Image
from lib.filesystem import ensure_parent_dir
from lib.rebrickable.resources import sanitize_name
def load_head_reuse(path: Path) -> List[dict]:
@@ -27,25 +30,65 @@ def format_label(row: dict) -> str:
return row["part_num"]
def plot_head_reuse(path: Path, destination_path: Path, top: int = 30) -> None:
def load_head_image(row: dict, resources_dir: Path) -> Image.Image | None:
"""Charge l'image d'une tête si disponible localement."""
set_id = row.get("sample_set_id", "").strip()
character = row.get("known_character", "").strip()
if set_id == "" or character == "":
return None
path = resources_dir / set_id / sanitize_name(character) / "head.jpg"
if not path.exists():
return None
return Image.open(path)
def plot_head_reuse(
path: Path,
destination_path: Path,
top: int | None = None,
resources_dir: Path = Path("figures/rebrickable"),
show_images: bool = True,
) -> None:
"""Trace un bar chart horizontal mettant en avant les têtes exclusives ou rares."""
rows = load_head_reuse(path)
rows.sort(key=lambda r: (int(r["other_sets"]), -int(r["filtered_sets"]), r["part_num"]))
selected = rows[:top]
rows.sort(key=lambda r: (int(r["total_sets"]), int(r["other_sets"]), r["part_num"]))
selected = rows if top is None else rows[:top]
labels = [format_label(r) for r in selected]
filtered_counts = [int(r["filtered_sets"]) for r in selected]
other_counts = [int(r["other_sets"]) for r in selected]
positions = list(reversed(range(len(selected))))
positions = list(range(len(selected)))
fig, ax = plt.subplots(figsize=(12, 0.5 * len(selected) + 1.5))
fig, ax = plt.subplots(figsize=(13, 0.5 * len(selected) + 1.5))
ax.barh(positions, filtered_counts, color="#1f78b4", label="Sets filtrés")
ax.barh(positions, other_counts, left=filtered_counts, color="#b2df8a", label="Autres sets")
ax.set_yticks(positions)
ax.set_yticklabels(reversed(labels))
ax.set_yticklabels(labels)
ax.set_xlabel("Nombre de sets contenant la tête")
ax.invert_yaxis()
ax.grid(axis="x", linestyle="--", alpha=0.4)
ax.legend()
if show_images:
max_count = max((f + o) for f, o in zip(filtered_counts, other_counts))
pad = max_count * 0.15 if max_count > 0 else 1.0
ax.set_xlim(left=-pad, right=max_count + pad * 0.2)
for row, pos in zip(selected, positions):
image = load_head_image(row, resources_dir)
if image is None:
continue
target_height = 24
ratio = target_height / image.height
resized = image.resize((int(image.width * ratio), target_height))
imagebox = OffsetImage(resized)
ab = AnnotationBbox(
imagebox,
(-pad * 0.4, pos),
xycoords=("data", "data"),
box_alignment=(0.5, 0.5),
frameon=False,
)
ax.add_artist(ab)
fig.subplots_adjust(left=0.42)
fig.tight_layout()
ensure_parent_dir(destination_path)

View File

@@ -6,6 +6,7 @@ from pathlib import Path
from typing import Dict, Iterable, List, Sequence, Set
from lib.filesystem import ensure_parent_dir
from lib.rebrickable.minifig_character_sets import load_sets
from lib.rebrickable.minifigs_by_set import load_parts_catalog, select_head_parts
from lib.rebrickable.parts_inventory import (
index_inventory_parts_by_inventory,
@@ -76,19 +77,24 @@ def aggregate_head_reuse(
minifigs_rows: Iterable[dict],
parts_catalog: Dict[str, dict],
head_presence: Dict[str, Set[str]],
sets_lookup: Dict[str, dict],
) -> List[dict]:
"""Construit le tableau des têtes présentes dans les sets filtrés avec leur réutilisation globale."""
filtered_presence = build_filtered_presence(minifigs_rows)
labels = build_character_labels(minifigs_rows)
aggregates: List[dict] = []
for part_num, filtered_sets in filtered_presence.items():
all_sets = head_presence.get(part_num, set())
all_sets = set(head_presence.get(part_num, set()))
all_sets.update(filtered_sets)
other_sets = all_sets - filtered_sets
sample_set = sorted(filtered_sets)[0]
sample_set_id = sets_lookup.get(sample_set, {}).get("set_id", sample_set.split("-")[0])
aggregates.append(
{
"part_num": part_num,
"part_name": parts_catalog[part_num]["name"],
"known_character": labels.get(part_num, ""),
"sample_set_id": sample_set_id,
"filtered_sets": str(len(filtered_sets)),
"other_sets": str(len(other_sets)),
"total_sets": str(len(all_sets)),
@@ -101,7 +107,7 @@ def aggregate_head_reuse(
def write_head_reuse(destination_path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV des usages de têtes filtrées vs reste du catalogue."""
ensure_parent_dir(destination_path)
fieldnames = ["part_num", "part_name", "known_character", "filtered_sets", "other_sets", "total_sets"]
fieldnames = ["part_num", "part_name", "known_character", "sample_set_id", "filtered_sets", "other_sets", "total_sets"]
with destination_path.open("w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()