You've already forked etude_lego_jurassic_world
Ajoute le genre des personnages et colore les graphiques
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
"""Agrégation des minifigs par personnage représenté."""
|
||||
|
||||
import csv
|
||||
from collections import defaultdict
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, List, Sequence, Set
|
||||
|
||||
from lib.rebrickable.stats import read_rows
|
||||
from lib.filesystem import ensure_parent_dir
|
||||
import csv
|
||||
from lib.rebrickable.stats import read_rows
|
||||
|
||||
|
||||
def load_minifigs_by_set(path: Path) -> List[dict]:
|
||||
@@ -15,17 +15,21 @@ def load_minifigs_by_set(path: Path) -> List[dict]:
|
||||
|
||||
|
||||
def aggregate_by_character(rows: Iterable[dict]) -> List[dict]:
|
||||
"""Compte les minifigs distinctes par personnage (fig_num unique)."""
|
||||
"""Compte les minifigs distinctes par personnage (fig_num unique) avec genre."""
|
||||
fig_nums_by_character: Dict[str, set] = defaultdict(set)
|
||||
genders: Dict[str, str] = {}
|
||||
for row in rows:
|
||||
character = row["known_character"].strip()
|
||||
fig_num = row["fig_num"].strip()
|
||||
gender = row.get("gender", "").strip()
|
||||
if character == "" or fig_num == "":
|
||||
continue
|
||||
fig_nums_by_character[character].add(fig_num)
|
||||
if character not in genders:
|
||||
genders[character] = gender
|
||||
aggregates: List[dict] = []
|
||||
for character, fig_nums in fig_nums_by_character.items():
|
||||
aggregates.append({"known_character": character, "minifig_count": len(fig_nums)})
|
||||
aggregates.append({"known_character": character, "gender": genders.get(character, ""), "minifig_count": len(fig_nums)})
|
||||
aggregates.sort(key=lambda r: (-r["minifig_count"], r["known_character"]))
|
||||
return aggregates
|
||||
|
||||
@@ -33,7 +37,7 @@ def aggregate_by_character(rows: Iterable[dict]) -> List[dict]:
|
||||
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"]
|
||||
fieldnames = ["known_character", "gender", "minifig_count"]
|
||||
with path.open("w", newline="") as csv_file:
|
||||
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
@@ -110,9 +114,11 @@ def aggregate_character_spans(
|
||||
excluded = set(excluded_characters or [])
|
||||
spans: Dict[str, Dict[str, int]] = {}
|
||||
total_counts: Dict[str, int] = defaultdict(int)
|
||||
genders: Dict[str, str] = {}
|
||||
for row in minifigs_rows:
|
||||
character = row["known_character"].strip()
|
||||
fig_num = row["fig_num"].strip()
|
||||
gender = row.get("gender", "").strip()
|
||||
if character == "" or fig_num == "":
|
||||
continue
|
||||
if character in excluded:
|
||||
@@ -122,6 +128,8 @@ def aggregate_character_spans(
|
||||
continue
|
||||
year_int = int(year)
|
||||
total_counts[character] += 1
|
||||
if character not in genders:
|
||||
genders[character] = gender
|
||||
current = spans.get(character)
|
||||
if current is None:
|
||||
spans[character] = {"start": year_int, "end": year_int}
|
||||
@@ -136,6 +144,7 @@ def aggregate_character_spans(
|
||||
"start_year": str(bounds["start"]),
|
||||
"end_year": str(bounds["end"]),
|
||||
"total_minifigs": str(total_counts[character]),
|
||||
"gender": genders.get(character, ""),
|
||||
}
|
||||
)
|
||||
results.sort(key=lambda r: (int(r["start_year"]), int(r["end_year"]), r["known_character"]))
|
||||
@@ -145,7 +154,7 @@ def aggregate_character_spans(
|
||||
def write_character_spans(path: Path, rows: Sequence[dict]) -> None:
|
||||
"""Écrit le CSV des bornes min/max par personnage."""
|
||||
ensure_parent_dir(path)
|
||||
fieldnames = ["known_character", "start_year", "end_year", "total_minifigs"]
|
||||
fieldnames = ["known_character", "start_year", "end_year", "total_minifigs", "gender"]
|
||||
with path.open("w", newline="") as csv_file:
|
||||
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
|
||||
Reference in New Issue
Block a user