Ajoute le graphique des personnages représentés
This commit is contained in:
parent
6186a5be4f
commit
52dc77878c
@ -233,3 +233,10 @@ Le script relit `data/intermediate/sets_enriched.csv`, `data/intermediate/parts_
|
||||
|
||||
- `data/intermediate/minifig_counts_by_set.csv` : `set_num`, `set_id`, `name`, `year`, `minifig_count`
|
||||
- `figures/step21/minifigs_per_set.png` : diagramme en barres horizontales (ordre décroissant) du nombre de minifigs par set filtré
|
||||
|
||||
### Étape 22 : personnages représentés par les minifigs
|
||||
|
||||
1. `source .venv/bin/activate`
|
||||
2. `python -m scripts.plot_minifig_characters`
|
||||
|
||||
Le script lit `data/intermediate/minifigs_by_set.csv`, compte le nombre de minifigs distinctes par personnage (`known_character` + `fig_num`), écrit `data/intermediate/minifig_characters_counts.csv`, puis trace `figures/step22/minifig_characters.png` (barres horizontales triées).
|
||||
|
||||
42
lib/plots/minifig_characters.py
Normal file
42
lib/plots/minifig_characters.py
Normal file
@ -0,0 +1,42 @@
|
||||
"""Graphique du nombre de minifigs par personnage."""
|
||||
|
||||
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_counts(path: Path) -> List[dict]:
|
||||
"""Charge le CSV des comptes par personnage."""
|
||||
return read_rows(path)
|
||||
|
||||
|
||||
def plot_minifigs_per_character(counts_path: Path, destination_path: Path) -> None:
|
||||
"""Trace un diagramme en barres horizontales du nombre de minifigs par personnage."""
|
||||
rows = load_counts(counts_path)
|
||||
characters = [row["known_character"] for row in rows]
|
||||
counts = [int(row["minifig_count"]) for row in rows]
|
||||
positions = list(range(len(rows)))
|
||||
height = max(6, len(rows) * 0.22)
|
||||
|
||||
fig, ax = plt.subplots(figsize=(12, height))
|
||||
bars = ax.barh(positions, counts, color="#1f77b4", edgecolor="#0d0d0d", linewidth=0.6)
|
||||
ax.set_yticks(positions)
|
||||
ax.set_yticklabels(characters)
|
||||
ax.invert_yaxis()
|
||||
ax.set_xlabel("Nombre de minifigs distinctes")
|
||||
ax.set_title("Minifigs par personnage (thèmes filtrés)")
|
||||
ax.grid(True, axis="x", linestyle="--", alpha=0.25)
|
||||
max_value = max(counts) if counts else 0
|
||||
ax.set_xlim(0, max_value + 1)
|
||||
for index, bar in enumerate(bars):
|
||||
value = counts[index]
|
||||
ax.text(value + 0.1, bar.get_y() + bar.get_height() / 2, str(value), va="center", fontsize=8)
|
||||
|
||||
ensure_parent_dir(destination_path)
|
||||
fig.tight_layout()
|
||||
fig.savefig(destination_path, dpi=160)
|
||||
plt.close(fig)
|
||||
41
lib/rebrickable/minifig_characters.py
Normal file
41
lib/rebrickable/minifig_characters.py
Normal file
@ -0,0 +1,41 @@
|
||||
"""Agrégation des minifigs par personnage représenté."""
|
||||
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, List, Sequence
|
||||
|
||||
from lib.rebrickable.stats import read_rows
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
import csv
|
||||
|
||||
|
||||
def load_minifigs_by_set(path: Path) -> List[dict]:
|
||||
"""Charge le CSV minifigs_by_set."""
|
||||
return read_rows(path)
|
||||
|
||||
|
||||
def aggregate_by_character(rows: Iterable[dict]) -> List[dict]:
|
||||
"""Compte les minifigs distinctes par personnage (fig_num unique)."""
|
||||
fig_nums_by_character: Dict[str, set] = defaultdict(set)
|
||||
for row in rows:
|
||||
character = row["known_character"].strip()
|
||||
fig_num = row["fig_num"].strip()
|
||||
if character == "" or fig_num == "":
|
||||
continue
|
||||
fig_nums_by_character[character].add(fig_num)
|
||||
aggregates: List[dict] = []
|
||||
for character, fig_nums in fig_nums_by_character.items():
|
||||
aggregates.append({"known_character": character, "minifig_count": len(fig_nums)})
|
||||
aggregates.sort(key=lambda r: (-r["minifig_count"], r["known_character"]))
|
||||
return aggregates
|
||||
|
||||
|
||||
def write_character_counts(path: Path, rows: Sequence[dict]) -> None:
|
||||
"""Écrit le CSV des comptes par personnage."""
|
||||
ensure_parent_dir(path)
|
||||
fieldnames = ["known_character", "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)
|
||||
23
scripts/plot_minifig_characters.py
Normal file
23
scripts/plot_minifig_characters.py
Normal file
@ -0,0 +1,23 @@
|
||||
"""Trace le nombre de minifigs distinctes par personnage."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from lib.plots.minifig_characters import plot_minifigs_per_character
|
||||
from lib.rebrickable.minifig_characters import aggregate_by_character, load_minifigs_by_set, write_character_counts
|
||||
|
||||
|
||||
MINIFIGS_BY_SET_PATH = Path("data/intermediate/minifigs_by_set.csv")
|
||||
COUNTS_PATH = Path("data/intermediate/minifig_characters_counts.csv")
|
||||
DESTINATION_PATH = Path("figures/step22/minifig_characters.png")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Construit le CSV de comptage par personnage et trace le graphique."""
|
||||
rows = load_minifigs_by_set(MINIFIGS_BY_SET_PATH)
|
||||
aggregates = aggregate_by_character(rows)
|
||||
write_character_counts(COUNTS_PATH, aggregates)
|
||||
plot_minifigs_per_character(COUNTS_PATH, DESTINATION_PATH)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
37
tests/test_minifig_characters.py
Normal file
37
tests/test_minifig_characters.py
Normal file
@ -0,0 +1,37 @@
|
||||
"""Tests de l'agrégation des minifigs par personnage."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from lib.rebrickable.minifig_characters import aggregate_by_character, write_character_counts
|
||||
|
||||
|
||||
def test_aggregate_by_character_counts_unique_figs() -> None:
|
||||
"""Compter les minifigs distinctes par personnage en excluant les noms vides."""
|
||||
aggregates = aggregate_by_character(
|
||||
[
|
||||
{"set_num": "123-1", "part_num": "head-a", "known_character": "Owen Grady", "fig_num": "fig-owen-1"},
|
||||
{"set_num": "124-1", "part_num": "head-b", "known_character": "Owen Grady", "fig_num": "fig-owen-1"},
|
||||
{"set_num": "125-1", "part_num": "head-c", "known_character": "Owen Grady", "fig_num": "fig-owen-2"},
|
||||
{"set_num": "126-1", "part_num": "head-d", "known_character": "Figurant", "fig_num": "fig-guard-1"},
|
||||
{"set_num": "128-1", "part_num": "head-f", "known_character": "Figurant", "fig_num": "fig-guard-1"},
|
||||
{"set_num": "129-1", "part_num": "head-g", "known_character": "", "fig_num": "fig-guard-2"},
|
||||
]
|
||||
)
|
||||
|
||||
assert aggregates == [
|
||||
{"known_character": "Owen Grady", "minifig_count": 2},
|
||||
{"known_character": "Figurant", "minifig_count": 1},
|
||||
]
|
||||
|
||||
|
||||
def test_write_character_counts_outputs_csv(tmp_path: Path) -> None:
|
||||
"""Écrit le CSV des comptes par personnage."""
|
||||
destination = tmp_path / "counts.csv"
|
||||
rows = [
|
||||
{"known_character": "A", "minifig_count": 2},
|
||||
{"known_character": "B", "minifig_count": 1},
|
||||
]
|
||||
|
||||
write_character_counts(destination, rows)
|
||||
|
||||
assert destination.read_text() == "known_character,minifig_count\nA,2\nB,1\n"
|
||||
25
tests/test_minifig_characters_plot.py
Normal file
25
tests/test_minifig_characters_plot.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""Tests du graphique minifigs par personnage."""
|
||||
|
||||
import matplotlib
|
||||
from pathlib import Path
|
||||
|
||||
from lib.plots.minifig_characters import plot_minifigs_per_character
|
||||
|
||||
|
||||
matplotlib.use("Agg")
|
||||
|
||||
|
||||
def test_plot_minifigs_per_character(tmp_path: Path) -> None:
|
||||
"""Génère l'image de comptage par personnage."""
|
||||
counts_path = tmp_path / "counts.csv"
|
||||
destination = tmp_path / "figures" / "step22" / "minifig_characters.png"
|
||||
counts_path.write_text(
|
||||
"known_character,minifig_count\n"
|
||||
"Owen Grady,2\n"
|
||||
"Figurant,1\n"
|
||||
)
|
||||
|
||||
plot_minifigs_per_character(counts_path, destination)
|
||||
|
||||
assert destination.exists()
|
||||
assert destination.stat().st_size > 0
|
||||
Loading…
x
Reference in New Issue
Block a user