|
| 1 | +from concepts.models import CategorizerResult, Item |
| 2 | +from django.core.management.base import BaseCommand |
| 3 | + |
| 4 | + |
| 5 | +class Command(BaseCommand): |
| 6 | + help = ( |
| 7 | + "Summarize the physics-concepts control experiment: distribution of " |
| 8 | + "'math' votes per item, mean confidence per group, and the full list " |
| 9 | + "of items that received a unanimous 'math' vote." |
| 10 | + ) |
| 11 | + |
| 12 | + def add_arguments(self, parser): |
| 13 | + parser.add_argument( |
| 14 | + "--session", |
| 15 | + type=str, |
| 16 | + required=True, |
| 17 | + help="Session name of the physics-concepts categorization run.", |
| 18 | + ) |
| 19 | + |
| 20 | + def handle(self, *args, **options): |
| 21 | + session = options["session"] |
| 22 | + |
| 23 | + by_item = {} |
| 24 | + for item_id, answer, confidence in CategorizerResult.objects.filter( |
| 25 | + session_name=session |
| 26 | + ).values_list("item_id", "result_answer", "result_confidence"): |
| 27 | + by_item.setdefault(item_id, []).append((bool(answer), int(confidence))) |
| 28 | + |
| 29 | + if not by_item: |
| 30 | + self.stdout.write( |
| 31 | + self.style.ERROR(f"No CategorizerResult rows for session '{session}'") |
| 32 | + ) |
| 33 | + return |
| 34 | + |
| 35 | + # Per-item aggregation |
| 36 | + groups = {0: [], 1: [], 2: [], 3: []} # yes_votes -> list[(item_id, [conf])] |
| 37 | + for item_id, judgments in by_item.items(): |
| 38 | + yes_votes = sum(1 for ans, _ in judgments if ans) |
| 39 | + confidences = [c for _, c in judgments] |
| 40 | + groups.setdefault(yes_votes, []).append((item_id, confidences)) |
| 41 | + |
| 42 | + total_items = sum(len(v) for v in groups.values()) |
| 43 | + |
| 44 | + self.stdout.write( |
| 45 | + f"\nPhysics control — session '{session}' — {total_items} items\n" |
| 46 | + ) |
| 47 | + |
| 48 | + # ----- Distribution + mean confidence per group ----- |
| 49 | + rows = [] |
| 50 | + for k in sorted(groups): |
| 51 | + items = groups[k] |
| 52 | + n = len(items) |
| 53 | + if n == 0: |
| 54 | + mean_conf = 0.0 |
| 55 | + else: |
| 56 | + all_confs = [c for _, confs in items for c in confs] |
| 57 | + mean_conf = sum(all_confs) / len(all_confs) |
| 58 | + rows.append((k, n, mean_conf)) |
| 59 | + |
| 60 | + header = f"{'Judges voting math':>20} {'Items':>8} {'Mean confidence':>18}" |
| 61 | + self.stdout.write(header) |
| 62 | + self.stdout.write("-" * len(header)) |
| 63 | + for k, n, mean_conf in rows: |
| 64 | + self.stdout.write(f"{k:>20} {n:>8} {mean_conf:>17.1f}") |
| 65 | + |
| 66 | + # LaTeX tabular for the augmented distribution table |
| 67 | + self.stdout.write("\nLaTeX tabular (vote distribution + mean confidence):\n") |
| 68 | + self.stdout.write( |
| 69 | + "\\begin{tabular}" |
| 70 | + "{>{\\raggedleft\\arraybackslash}p{0.28\\textwidth}" |
| 71 | + ">{\\raggedleft\\arraybackslash}p{0.20\\textwidth}" |
| 72 | + ">{\\raggedleft\\arraybackslash}p{0.20\\textwidth}}" |
| 73 | + ) |
| 74 | + self.stdout.write(" \\toprule") |
| 75 | + self.stdout.write( |
| 76 | + " Judges voting ``math'' & Number of items & " "Mean confidence \\\\" |
| 77 | + ) |
| 78 | + self.stdout.write(" \\midrule") |
| 79 | + for k, n, mean_conf in rows: |
| 80 | + self.stdout.write(f" {k} & {n:>3} & {mean_conf:5.1f} \\\\") |
| 81 | + self.stdout.write(" \\bottomrule") |
| 82 | + self.stdout.write("\\end{tabular}\n") |
| 83 | + |
| 84 | + # ----- Exhaustive list of items with unanimous 'math' votes ----- |
| 85 | + unanimous = groups.get(3, []) |
| 86 | + if not unanimous: |
| 87 | + self.stdout.write("\nNo items received a unanimous 'math' vote.\n") |
| 88 | + return |
| 89 | + |
| 90 | + unanimous_ids = [item_id for item_id, _ in unanimous] |
| 91 | + items_by_id = {i.id: i for i in Item.objects.filter(id__in=unanimous_ids)} |
| 92 | + |
| 93 | + # Attach name + mean confidence per item, ordered by name |
| 94 | + enriched = [] |
| 95 | + for item_id, confs in unanimous: |
| 96 | + item = items_by_id.get(item_id) |
| 97 | + name = item.name if item and item.name else f"(item #{item_id})" |
| 98 | + mean_conf = sum(confs) / len(confs) if confs else 0.0 |
| 99 | + enriched.append((name, mean_conf)) |
| 100 | + enriched.sort(key=lambda x: (x[0] or "").lower()) |
| 101 | + |
| 102 | + self.stdout.write(f"\nItems with unanimous 'math' vote ({len(enriched)}):\n") |
| 103 | + for name, mean_conf in enriched: |
| 104 | + self.stdout.write(f" - {name} (mean confidence {mean_conf:.1f})") |
| 105 | + |
| 106 | + # LaTeX tabular for the unanimous-items list |
| 107 | + self.stdout.write("\nLaTeX tabular (unanimous 'math' items):\n") |
| 108 | + self.stdout.write( |
| 109 | + "\\begin{tabular}" |
| 110 | + "{>{\\raggedright\\arraybackslash}p{0.60\\textwidth}" |
| 111 | + ">{\\raggedleft\\arraybackslash}p{0.20\\textwidth}}" |
| 112 | + ) |
| 113 | + self.stdout.write(" \\toprule") |
| 114 | + self.stdout.write(" Concept & Mean confidence \\\\") |
| 115 | + self.stdout.write(" \\midrule") |
| 116 | + for name, mean_conf in enriched: |
| 117 | + safe = (name or "").replace("&", "\\&").replace("_", "\\_") |
| 118 | + self.stdout.write(f" {safe} & {mean_conf:5.1f} \\\\") |
| 119 | + self.stdout.write(" \\bottomrule") |
| 120 | + self.stdout.write("\\end{tabular}\n") |
0 commit comments