cropping summaries
This commit is contained in:
@@ -134,6 +134,27 @@ def _publish(
|
||||
return backup_root
|
||||
|
||||
|
||||
def crop_statistics(records: list[dict]) -> tuple[int, float]:
|
||||
"""Return cropped exercise count and mean removed percentage per exercise."""
|
||||
totals: dict[str, list[float]] = {}
|
||||
cropped_files: set[str] = set()
|
||||
for record in records:
|
||||
total_height, removed_height = totals.setdefault(record["file"], [0.0, 0.0])
|
||||
totals[record["file"]] = [
|
||||
total_height + float(record["height_lines"]),
|
||||
removed_height + float(record["bottom_removed_lines"]),
|
||||
]
|
||||
if record["status"] == "cropped":
|
||||
cropped_files.add(record["file"])
|
||||
percentages = [
|
||||
min(100.0, totals[file_name][1] / totals[file_name][0] * 100)
|
||||
for file_name in cropped_files
|
||||
if totals[file_name][0] > 0
|
||||
]
|
||||
mean_percentage = sum(percentages) / len(percentages) if percentages else 0.0
|
||||
return len(cropped_files), mean_percentage
|
||||
|
||||
|
||||
def run(workspace: EvaluationWorkspace, target: Path, *, workers: int = 5) -> ExitCode:
|
||||
if workers < 1:
|
||||
raise CliError(
|
||||
@@ -161,15 +182,23 @@ def run(workspace: EvaluationWorkspace, target: Path, *, workers: int = 5) -> Ex
|
||||
raise CliError(
|
||||
f"{source} a changé pendant l’analyse. Aucun PDF remplacé."
|
||||
)
|
||||
cropped_exercises, mean_percentage = crop_statistics(records)
|
||||
if not changed_files:
|
||||
print("Terminé : aucun PDF ne remplit les critères de rognage.", flush=True)
|
||||
print("Terminé : aucun exercice ne remplit les critères de rognage.", flush=True)
|
||||
print(
|
||||
"Rognage moyen des exercices modifiés : 0.0 %.", flush=True
|
||||
)
|
||||
return ExitCode.SUCCESS
|
||||
backup = _publish(workspace, changed_files, staging, records)
|
||||
cropped_pages = sum(record["status"] == "cropped" for record in records)
|
||||
print(f"Sauvegarde des PDF non rognés : {backup}", flush=True)
|
||||
print(
|
||||
f"Terminé : {cropped_pages} page(s) rognée(s) dans "
|
||||
f"{len(changed_files)} PDF remplacé(s).",
|
||||
f"Terminé : {cropped_exercises} exercice(s) rogné(s), soit "
|
||||
f"{cropped_pages} page(s) dans {len(changed_files)} PDF remplacé(s).",
|
||||
flush=True,
|
||||
)
|
||||
print(
|
||||
f"Rognage moyen des exercices modifiés : {mean_percentage:.1f} %.",
|
||||
flush=True,
|
||||
)
|
||||
return ExitCode.SUCCESS
|
||||
|
||||
@@ -83,6 +83,26 @@ def process_copies(files: list[Path], staging: Path, workers: int) -> list[dict]
|
||||
key=lambda row: (order[row["file"]], row["page"]))
|
||||
|
||||
|
||||
def crop_statistics(records: list[dict]) -> tuple[int, float, int]:
|
||||
"""Return cropped page count, their mean removed percentage, and >30% count."""
|
||||
percentages: list[float] = []
|
||||
for record in records:
|
||||
removed_mm = record["top_removed_mm"] + record["bottom_removed_mm"]
|
||||
if removed_mm <= 0:
|
||||
continue
|
||||
x0, y0, x1, y1 = record["original_cropbox"]
|
||||
original_height_points = (
|
||||
x1 - x0 if record.get("rotation", 0) % 180 else y1 - y0
|
||||
)
|
||||
if original_height_points <= 0:
|
||||
continue
|
||||
original_height_mm = original_height_points * 25.4 / 72
|
||||
percentages.append(min(100.0, removed_mm / original_height_mm * 100))
|
||||
mean_percentage = sum(percentages) / len(percentages) if percentages else 0.0
|
||||
over_thirty = sum(percentage > 30 for percentage in percentages)
|
||||
return len(percentages), mean_percentage, over_thirty
|
||||
|
||||
|
||||
def run(workspace: EvaluationWorkspace, target: Path, *, workers: int = 5) -> ExitCode:
|
||||
if workers < 1:
|
||||
raise CliError("Le nombre de traitements parallèles doit être positif.",
|
||||
@@ -109,9 +129,14 @@ def run(workspace: EvaluationWorkspace, target: Path, *, workers: int = 5) -> Ex
|
||||
(backup/"report.json").write_text(json.dumps(records, ensure_ascii=False, indent=2),
|
||||
encoding="utf-8")
|
||||
print(f"Sauvegarde des PDF non rognés : {originals}", flush=True)
|
||||
cropped = sum(row["top_removed_mm"]+row["bottom_removed_mm"] > 0 for row in records)
|
||||
cropped, mean_percentage, over_thirty = crop_statistics(records)
|
||||
print(f"Terminé : {cropped}/{len(records)} pages rognées ; "
|
||||
f"{len(files)} PDF remplacés dans Copies.", flush=True)
|
||||
print(
|
||||
f"Rognage moyen des pages modifiées : {mean_percentage:.1f} % ; "
|
||||
f"{over_thirty} page(s) rognée(s) de plus de 30 %.",
|
||||
flush=True,
|
||||
)
|
||||
return ExitCode.SUCCESS
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user