You've already forked etude_lego_jurassic_world
Exclure les pièces de minifigs et intégrer les visuels de rareté
This commit is contained in:
34
scripts/compute_part_rarity.py
Normal file
34
scripts/compute_part_rarity.py
Normal file
@@ -0,0 +1,34 @@
|
||||
"""Calcule les pièces rares en comparant les sets filtrés au reste du catalogue."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from lib.rebrickable.part_rarity import build_part_rarity, select_until_reused, write_part_rarity
|
||||
|
||||
|
||||
PARTS_FILTERED_PATH = Path("data/intermediate/parts_filtered.csv")
|
||||
INVENTORIES_PATH = Path("data/raw/inventories.csv")
|
||||
INVENTORY_PARTS_PATH = Path("data/raw/inventory_parts.csv")
|
||||
PARTS_CATALOG_PATH = Path("data/raw/parts.csv")
|
||||
PART_CATEGORIES_PATH = Path("data/raw/part_categories.csv")
|
||||
FILTERED_SETS_PATH = Path("data/intermediate/sets_enriched.csv")
|
||||
DESTINATION_PATH = Path("data/intermediate/part_rarity.csv")
|
||||
TOP_DESTINATION_PATH = Path("data/intermediate/part_rarity_exclusive.csv")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Construit les CSV complets et top 10 des pièces les plus rares."""
|
||||
rows = build_part_rarity(
|
||||
PARTS_FILTERED_PATH,
|
||||
INVENTORIES_PATH,
|
||||
INVENTORY_PARTS_PATH,
|
||||
PARTS_CATALOG_PATH,
|
||||
PART_CATEGORIES_PATH,
|
||||
FILTERED_SETS_PATH,
|
||||
)
|
||||
write_part_rarity(DESTINATION_PATH, rows)
|
||||
top_rows = select_until_reused(rows)
|
||||
write_part_rarity(TOP_DESTINATION_PATH, top_rows)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
63
scripts/download_part_rarity_resources.py
Normal file
63
scripts/download_part_rarity_resources.py
Normal file
@@ -0,0 +1,63 @@
|
||||
"""Télécharge les visuels des pièces les plus rares identifiées à l'étape 34."""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from lib.rebrickable.part_rarity import load_part_rarity
|
||||
from lib.rebrickable.resources import (
|
||||
build_part_img_lookup,
|
||||
download_binary,
|
||||
download_resources,
|
||||
fetch_part_img_url,
|
||||
load_part_img_cache,
|
||||
persist_part_img_cache,
|
||||
)
|
||||
|
||||
|
||||
PART_RARITY_TOP_PATH = Path("data/intermediate/part_rarity_exclusive.csv")
|
||||
RESOURCES_DIR = Path("figures/rebrickable")
|
||||
PART_IMG_CACHE_PATH = Path("data/intermediate/part_img_cache.csv")
|
||||
DOWNLOAD_LOG_PATH = Path("data/intermediate/part_rarity_download_log.csv")
|
||||
REQUEST_DELAY_SECONDS_IMAGES = 0.35
|
||||
REQUEST_DELAY_SECONDS_LOOKUP = 0.6
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Construit les URLs d'images des pièces rares et les télécharge."""
|
||||
load_dotenv()
|
||||
token = os.environ["REBRICKABLE_TOKEN"]
|
||||
session = requests.Session()
|
||||
|
||||
rows = load_part_rarity(PART_RARITY_TOP_PATH)
|
||||
cache = load_part_img_cache(PART_IMG_CACHE_PATH)
|
||||
part_img_lookup = build_part_img_lookup(
|
||||
{row["part_num"] for row in rows},
|
||||
fetcher=lambda part_num: fetch_part_img_url(part_num, token, session),
|
||||
cache_path=PART_IMG_CACHE_PATH,
|
||||
existing_cache=cache,
|
||||
delay_seconds=REQUEST_DELAY_SECONDS_LOOKUP,
|
||||
)
|
||||
if cache:
|
||||
part_img_lookup.update(cache)
|
||||
persist_part_img_cache(PART_IMG_CACHE_PATH, part_img_lookup)
|
||||
|
||||
plan = [
|
||||
{
|
||||
"url": part_img_lookup[row["part_num"]],
|
||||
"path": RESOURCES_DIR / row["sample_set_id"] / "rare_parts" / f"{row['part_num']}.jpg",
|
||||
}
|
||||
for row in rows
|
||||
]
|
||||
download_resources(
|
||||
plan,
|
||||
downloader=lambda url, path: download_binary(url, path, session),
|
||||
delay_seconds=REQUEST_DELAY_SECONDS_IMAGES,
|
||||
log_path=DOWNLOAD_LOG_PATH,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
19
scripts/plot_part_rarity.py
Normal file
19
scripts/plot_part_rarity.py
Normal file
@@ -0,0 +1,19 @@
|
||||
"""Trace le diagramme des pièces rares pour l'étape 34."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from lib.plots.part_rarity import plot_part_rarity
|
||||
|
||||
|
||||
PART_RARITY_TOP_PATH = Path("data/intermediate/part_rarity_exclusive.csv")
|
||||
DESTINATION_PATH = Path("figures/step34/part_rarity.png")
|
||||
RESOURCES_DIR = Path("figures/rebrickable")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Charge le top des pièces rares et produit le graphique illustré."""
|
||||
plot_part_rarity(PART_RARITY_TOP_PATH, DESTINATION_PATH, resources_dir=RESOURCES_DIR)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user