You've already forked etude_lego_jurassic_world
Complète l’étape 26 avec l’évolution minifigs/set
This commit is contained in:
45
lib/plots/minifig_parts_timeline.py
Normal file
45
lib/plots/minifig_parts_timeline.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""Évolution annuelle du nombre moyen de minifigs par set."""
|
||||
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
from lib.rebrickable.stats import read_rows
|
||||
|
||||
|
||||
def load_minifigs_per_year(path: Path, scope: str) -> List[tuple[int, float]]:
|
||||
"""Charge les moyennes annuelles pour un scope donné."""
|
||||
rows = read_rows(path)
|
||||
values: List[tuple[int, float]] = []
|
||||
for row in rows:
|
||||
if row["scope"] != scope:
|
||||
continue
|
||||
values.append((int(row["year"]), float(row["average_minifigs_per_set"])))
|
||||
values.sort(key=lambda item: item[0])
|
||||
return values
|
||||
|
||||
|
||||
def plot_minifigs_per_set_timeline(path: Path, destination_path: Path) -> None:
|
||||
"""Trace l'évolution annuelle des minifigs par set (global vs filtré)."""
|
||||
filtered = load_minifigs_per_year(path, "filtered")
|
||||
catalog = load_minifigs_per_year(path, "catalog")
|
||||
if not filtered or not catalog:
|
||||
return
|
||||
filtered_years, filtered_values = zip(*filtered)
|
||||
catalog_years, catalog_values = zip(*catalog)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, 6))
|
||||
ax.plot(catalog_years, catalog_values, color="#888888", linestyle="--", linewidth=1.6, label="Catalogue global")
|
||||
ax.plot(filtered_years, filtered_values, color="#1f77b4", linewidth=2.2, marker="o", label="Thèmes filtrés")
|
||||
ax.set_xlabel("Année")
|
||||
ax.set_ylabel("Nombre moyen de minifigs par set")
|
||||
ax.set_title("Évolution des minifigs par set")
|
||||
ax.grid(True, linestyle="--", alpha=0.3)
|
||||
ax.legend(loc="upper left")
|
||||
|
||||
ensure_parent_dir(destination_path)
|
||||
fig.tight_layout()
|
||||
fig.savefig(destination_path, dpi=160)
|
||||
plt.close(fig)
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user