Cache for reading annotation

This commit is contained in:
sebastien committed 2026-10-24 10:22:41 +02:00
1 parent 55d5cf16c9
commit ff7496b113
12 files changed
+466 -13

No files matched your search

+36
View File
@@ -2,11 +2,13 @@ import argparse
import math
import shutil
import sys
from decimal import Decimal, InvalidOperation, ROUND_CEILING
from pathlib import Path
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
from copienator import atomic_write_json, read_json
from copienator.configuration import (
FINAL_SCORE_FONT_PATH,
FINAL_SCORE_HISTOGRAM_PATH,
@@ -72,6 +74,39 @@ def copy_histogram(output_dir: Path, *, copy_merged_histogramme: bool = False) -
shutil.copy2(source, destination)
print(f"Copied histogram: {destination}")
def write_score_statistics(search_path: Path, output_dir: Path) -> None:
"""Average available per-question scores, rounding upward to one decimal."""
scores_by_label = {}
for path in sorted(search_path.glob("*/score.json")):
try:
scores = read_json(path)
if not isinstance(scores, dict):
raise ValueError("expected an object of per-label scores")
except (OSError, ValueError) as exc:
print(f"Warning: Could not read scores from '{path}': {exc}")
continue
for label, value in scores.items():
values = scores_by_label.setdefault(label, [])
if value is None or (isinstance(value, str) and not value.strip()):
continue
try:
number = Decimal(str(value))
if not number.is_finite():
raise InvalidOperation
except InvalidOperation:
print(f"Warning: Invalid score '{value}' for '{label}' in '{path}'.")
continue
values.append(number)
statistics = {
label: str((sum(values) / len(values)).quantize(Decimal("0.1"), rounding=ROUND_CEILING))
if values else ""
for label, values in scores_by_label.items()
}
atomic_write_json(output_dir / "stat.json", statistics)
print(f"Wrote per-label averages: {output_dir / 'stat.json'}")
def process_images(base_dir, output_dir, *, copy_merged_histogramme=False):
# 1. Load Data
try:
@@ -152,6 +187,7 @@ def process_images(base_dir, output_dir, *, copy_merged_histogramme=False):
for pdf_path in pdf_paths:
shutil.copy2(pdf_path, student_output / pdf_path.name)
write_score_statistics(search_path, output_dir)
copy_histogram(output_dir, copy_merged_histogramme=copy_merged_histogramme)