1

Complète l’étape 26 avec l’évolution minifigs/set

This commit is contained in:
2025-12-02 14:28:11 +01:00
parent f23f54d040
commit c9f1acee4b
6 changed files with 211 additions and 2 deletions

View File

@@ -80,6 +80,62 @@ def build_correlation_rows(
return rows
def build_minifigs_per_year(
filtered_counts_path: Path,
all_sets_path: Path,
inventories_path: Path,
inventory_minifigs_path: Path,
) -> List[dict]:
"""Calcule le nombre moyen de minifigs par set et par année (filtré vs catalogue)."""
filtered_totals: Dict[int, Dict[str, int]] = {}
with filtered_counts_path.open() as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
year = int(row["year"])
current = filtered_totals.get(year)
if current is None:
filtered_totals[year] = {"minifigs": int(row["minifig_count"]), "sets": 1}
else:
current["minifigs"] += int(row["minifig_count"])
current["sets"] += 1
global_minifigs = build_global_minifig_counts(inventories_path, inventory_minifigs_path)
catalog_totals: Dict[int, Dict[str, int]] = {}
with all_sets_path.open() as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
year = int(row["year"])
current = catalog_totals.get(year)
if current is None:
catalog_totals[year] = {"minifigs": global_minifigs.get(row["set_num"], 0), "sets": 1}
else:
current["minifigs"] += global_minifigs.get(row["set_num"], 0)
current["sets"] += 1
rows: List[dict] = []
for year in sorted(filtered_totals.keys()):
totals = filtered_totals[year]
average = totals["minifigs"] / totals["sets"]
rows.append(
{
"scope": "filtered",
"year": str(year),
"average_minifigs_per_set": f"{average:.3f}",
"set_count": str(totals["sets"]),
}
)
for year in sorted(catalog_totals.keys()):
totals = catalog_totals[year]
average = totals["minifigs"] / totals["sets"]
rows.append(
{
"scope": "catalog",
"year": str(year),
"average_minifigs_per_set": f"{average:.3f}",
"set_count": str(totals["sets"]),
}
)
return rows
def write_correlation_rows(path: Path, rows: Sequence[dict]) -> None:
"""Écrit les lignes de corrélation pièces/minifigs."""
ensure_parent_dir(path)
@@ -91,6 +147,17 @@ def write_correlation_rows(path: Path, rows: Sequence[dict]) -> None:
writer.writerow(row)
def write_minifigs_per_year(path: Path, rows: Sequence[dict]) -> None:
"""Écrit le CSV annuel minifigs / set."""
ensure_parent_dir(path)
fieldnames = ["scope", "year", "average_minifigs_per_set", "set_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_correlation_rows(path: Path) -> List[dict]:
"""Charge le CSV de corrélation pièces/minifigs."""
return read_rows(path)