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

@@ -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()