1

Ajoute l’étape 27 de palettes dominantes par set

This commit is contained in:
2025-12-02 14:36:24 +01:00
parent fbf20e2592
commit a649283cf2
7 changed files with 390 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
"""Tests de la préparation des palettes par set."""
from pathlib import Path
from lib.rebrickable.set_color_swatches import build_top_colors_by_set
def write_csv(path: Path, content: str) -> None:
"""Écrit un CSV brut."""
path.write_text(content)
def test_build_top_colors_by_set_selects_top5_non_minifig(tmp_path: Path) -> None:
"""Sélectionne les 5 couleurs dominantes en excluant les minifigs."""
colors_path = tmp_path / "colors_by_set.csv"
write_csv(
colors_path,
"set_num,set_id,year,color_rgb,is_translucent,color_name,quantity_total,quantity_non_spare,quantity_minifig,quantity_non_minifig\n"
"123-1,123,2020,111111,false,Black,10,10,0,10\n"
"123-1,123,2020,222222,false,Red,5,5,0,5\n"
"123-1,123,2020,333333,false,Blue,3,3,0,3\n"
"123-1,123,2020,444444,false,Green,2,2,0,2\n"
"123-1,123,2020,555555,false,Yellow,1,1,0,1\n"
"123-1,123,2020,666666,false,Pink,1,1,0,1\n"
"124-1,124,2021,aaaaaa,false,Gray,4,4,4,0\n",
)
sets_path = tmp_path / "sets_enriched.csv"
write_csv(
sets_path,
"set_num,name,year,theme_id,num_parts,img_url,set_id,rebrickable_url,in_collection\n"
"123-1,Set A,2020,1,100,,123,,false\n"
"124-1,Set B,2021,1,50,,124,,false\n",
)
rows = build_top_colors_by_set(
[
row
for row in [
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "111111",
"color_name": "Black",
"quantity_non_minifig": "10",
},
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "222222",
"color_name": "Red",
"quantity_non_minifig": "5",
},
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "333333",
"color_name": "Blue",
"quantity_non_minifig": "3",
},
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "444444",
"color_name": "Green",
"quantity_non_minifig": "2",
},
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "555555",
"color_name": "Yellow",
"quantity_non_minifig": "1",
},
{
"set_num": "123-1",
"set_id": "123",
"year": "2020",
"color_rgb": "666666",
"color_name": "Pink",
"quantity_non_minifig": "1",
},
{
"set_num": "124-1",
"set_id": "124",
"year": "2021",
"color_rgb": "aaaaaa",
"color_name": "Gray",
"quantity_non_minifig": "0",
},
]
],
{
"123-1": {"name": "Set A", "year": 2020, "set_id": "123"},
"124-1": {"name": "Set B", "year": 2021, "set_id": "124"},
},
top_n=5,
)
assert rows == [
{
"set_num": "123-1",
"set_id": "123",
"name": "Set A",
"year": "2020",
"rank": "1",
"color_rgb": "111111",
"color_name": "Black",
"quantity_non_minifig": "10",
},
{
"set_num": "123-1",
"set_id": "123",
"name": "Set A",
"year": "2020",
"rank": "2",
"color_rgb": "222222",
"color_name": "Red",
"quantity_non_minifig": "5",
},
{
"set_num": "123-1",
"set_id": "123",
"name": "Set A",
"year": "2020",
"rank": "3",
"color_rgb": "333333",
"color_name": "Blue",
"quantity_non_minifig": "3",
},
{
"set_num": "123-1",
"set_id": "123",
"name": "Set A",
"year": "2020",
"rank": "4",
"color_rgb": "444444",
"color_name": "Green",
"quantity_non_minifig": "2",
},
{
"set_num": "123-1",
"set_id": "123",
"name": "Set A",
"year": "2020",
"rank": "5",
"color_rgb": "666666",
"color_name": "Pink",
"quantity_non_minifig": "1",
},
]

View File

@@ -0,0 +1,29 @@
"""Tests du graphique de palettes dominantes par set."""
import matplotlib
from pathlib import Path
from lib.plots.set_color_swatches import plot_set_color_swatches
matplotlib.use("Agg")
def test_plot_set_color_swatches(tmp_path: Path) -> None:
"""Génère le nuancier top 5 par set."""
swatches_path = tmp_path / "set_color_swatches.csv"
destination = tmp_path / "figures" / "step27" / "set_color_swatches.png"
swatches_path.write_text(
"set_num,set_id,name,year,rank,color_rgb,color_name,quantity_non_minifig\n"
"123-1,123,Set A,2020,1,111111,Black,10\n"
"123-1,123,Set A,2020,2,222222,Red,5\n"
"123-1,123,Set A,2020,3,333333,Blue,3\n"
"123-1,123,Set A,2020,4,444444,Green,2\n"
"123-1,123,Set A,2020,5,555555,Yellow,1\n"
"124-1,124,Set B,2021,1,aaaaaa,Gray,4\n"
)
plot_set_color_swatches(swatches_path, destination)
assert destination.exists()
assert destination.stat().st_size > 0