Rudimentary support for --refaire

This commit is contained in:
2026-04-01 22:20:11 +02:00
parent c1524a99c3
commit 95db769751
6 changed files with 155 additions and 46 deletions
+57 -27
View File
@@ -5,6 +5,7 @@ import collections
import concurrent.futures
from pathlib import Path
from PIL import Image
import threading
import annotating
@@ -179,7 +180,7 @@ import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Read grouped annotations and compile PDFs")
parser.add_argument("input_path", help="Directory path")
parser.add_argument("--with-refaire", action="store_true", help="Merge refaire annotations from Bnot")
parser.add_argument("--refaire", action="store_true", help="Merge refaire annotations from Bnot")
args = parser.parse_args()
root_dir = sys.argv[1]
@@ -194,26 +195,52 @@ if __name__ == "__main__":
except FileNotFoundError:
all_labels = []
refaire_dict = {}
if args.refaire:
refaire_path = os.path.join(root_dir, "refaire.json")
if os.path.exists(refaire_path):
with open(refaire_path, "r", encoding="utf-8") as f:
refaire_list = json.load(f)
for c_name, labels in refaire_list:
sid = c_name.replace("Copie", "")
refaire_dict[sid] = labels
else:
print(f"Warning: --refaire flag used, but {refaire_path} not found.")
# Load original data
original_data = annotating.make_dictionary(root_dir)
if args.refaire and refaire_list:
original_data = annotating.make_dictionary(root_dir,
refaire=True,
refaire_list=refaire_list)
else:
original_data = annotating.make_dictionary(root_dir)
lock = threading.Lock()
actions_by_student = collections.defaultdict(list)
notes_by_student = collections.defaultdict(dict)
def process_bgnot_entry(entry):
def process_bgnot_entry(entry, only_ids=None):
gdir = os.path.join(bgnot_dir, entry)
if not os.path.isdir(gdir) or entry.startswith("Copie"):
return
bnote_path = os.path.join(gdir, "bnote.json")
with open(bnote_path, "r") as f:
bnote_data = json.load(f)
if only_ids:
id_found = False
for d in bnote_data["images"]:
if d["id"] in only_ids:
id_found = True
if not id_found:
return
actions, notes_img = detect_checks_and_notes(gdir)
bnote_path = os.path.join(gdir, "bnote.json")
if not os.path.exists(bnote_path) or notes_img is None:
return
with open(bnote_path, "r") as f:
bnote_data = json.load(f)
with lock:
for act in actions:
@@ -230,13 +257,16 @@ if __name__ == "__main__":
def process_refaire_entry(sid, r_labels):
s_bnot_dir = os.path.join(root_dir, "Bnot", f"Copie{sid}")
s_bnot_dir = os.path.join(root_dir, "BRnot", f"Copie{sid}")
if not os.path.exists(s_bnot_dir): return
if not r_labels: r_labels = list(original_data.get(sid, {}).keys())
if not r_labels:
r_labels = list(original_data.get(sid, {}).keys())
with lock:
actions_by_student[sid] = [a for a in actions_by_student[sid] if a.get('label') not in r_labels]
for lbl in r_labels: notes_by_student[sid].pop(lbl, None)
actions_by_student[sid] = [a for a in actions_by_student[sid]
if a.get('label') not in r_labels]
for lbl in r_labels:
notes_by_student[sid].pop(lbl, None)
b_actions, b_notes_img = detect_checks_and_notes(s_bnot_dir)
b_bnote_path = os.path.join(s_bnot_dir, "bnote.json")
@@ -259,24 +289,20 @@ if __name__ == "__main__":
# --- 0. Read refaire.json if requested ---
refaire_dict = {}
if args.with_refaire:
refaire_path = os.path.join(root_dir, "refaire.json")
if os.path.exists(refaire_path):
with open(refaire_path, "r", encoding="utf-8") as f:
refaire_list = json.load(f)
for c_name, labels in refaire_list:
sid = c_name.replace("Copie", "")
refaire_dict[sid] = labels
else:
print(f"Warning: --with-refaire flag used, but {refaire_path} not found.")
# Part 1 : lecture des bgnot
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
executor.map(process_bgnot_entry, os.listdir(bgnot_dir))
if refaire_dict:
only_ids = [ids for ids in refaire_dict]
else:
only_ids = None
# Part 1.5: Refaire
if args.with_refaire and refaire_dict:
# Lecture des bgnot
with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
executor.map(lambda x: process_bgnot_entry(x, only_ids=only_ids),
os.listdir(bgnot_dir))
# Refaire
if args.refaire and refaire_dict:
for sid, labels in refaire_dict.items():
process_refaire_entry(sid, labels)
@@ -296,7 +322,11 @@ if __name__ == "__main__":
# --- 2. Process each student concurrently using 4 threads ---
sids = sorted(original_data.keys(), key=natural_key)
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
futures = {executor.submit(process_student, sid): sid for sid in sids}
if refaire_dict:
futures = {executor.submit(process_student, sid): sid for sid in refaire_dict}
else:
futures = {executor.submit(process_student, sid): sid for sid in sids}
for future in concurrent.futures.as_completed(futures):
output = future.result()
if output: