Repository navigation
Expand file tree
/
Copy pathcompute_task_statistics.py
More file actions
1235 lines (1013 loc) · 44.8 KB
/
Copy pathcompute_task_statistics.py
File metadata and controls
1235 lines (1013 loc) · 44.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Compute Task Statistics
This script analyzes task metadata and computes statistics on task attributes,
showing the number of tasks per attribute and their distribution.
It can also group tasks by scene to show task distribution across scenes.
Usage:
# Use default paths and subfolders
python compute_task_statistics.py
# Specify custom metadata file
python compute_task_statistics.py --metadata-file /path/to/task_metadata.json
# Filter by specific subfolders
python compute_task_statistics.py --subfolders examples2
# Show detailed breakdown with all tasks per attribute
python compute_task_statistics.py --verbose
# Show tasks grouped by scene instead of attributes
python compute_task_statistics.py --by-scene
# Show tasks by scene with additional task details (subfolder, attributes)
python compute_task_statistics.py --by-scene --verbose
# Show full comprehensive report (attributes, objects, subtasks, episodes, scenes)
python compute_task_statistics.py --verbose
# Show individual analysis sections
python compute_task_statistics.py --objects
python compute_task_statistics.py --subtasks
python compute_task_statistics.py --episodes
Output:
A formatted table showing:
- (Default) Total unique attributes and tasks scanned
- (Default) Number of tasks per attribute
- (With --verbose) Comprehensive report: attributes, categories, objects, subtasks, episodes, scenes
- (With --by-scene) Total scenes and tasks, with task count per scene and list of tasks
- (With --by-scene --verbose) Same as above, but includes subfolder and attributes for each task
"""
import argparse
import json
import os
import re
import sys
from collections import Counter, defaultdict
from typing import Any, Dict, List, Tuple
from robolab.constants import BENCHMARK_TASK_CATEGORIES, DIFFICULTY_THRESHOLDS, SKILL_WEIGHTS
########################################################
# Loading and filtering
########################################################
def load_metadata(metadata_file: str) -> List[Dict[str, Any]]:
"""
Load metadata from JSON file.
"""
with open(metadata_file, 'r') as f:
tasks_data = json.load(f)
print(f"Analyzing tasks in {metadata_file}")
for task in tasks_data:
# Add subfolders if exists
filename = task.get('filename', '')
if '/' in filename:
task['subfolder'] = filename.split('/')[0]
else:
task['subfolder'] = ''
return tasks_data
def filter_by_subfolders(tasks_data: List[Dict[str, Any]], subfolders: List[str] = None) -> List[Dict[str, Any]]:
"""Filter tasks to only include those in the specified subfolders."""
if subfolders is None:
return tasks_data
subfolder_to_tasks = sort_tasks_by_subfolder(tasks_data)
filtered = []
for folder in subfolders:
filtered.extend(subfolder_to_tasks.get(folder, []))
return filtered
########################################################
# Grouping helpers
########################################################
def sort_tasks_by_scene(tasks_data: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
"""Generate a dictionary of scene names to tasks."""
scene_to_tasks = defaultdict(list)
for task in tasks_data:
scene = task.get('scene', '')
scene_to_tasks[scene].append(task)
return dict(scene_to_tasks)
def sort_tasks_by_subfolder(tasks_data: List[Dict[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
"""Generate a dictionary of subfolder names to tasks."""
subfolder_to_tasks = defaultdict(list)
for task in tasks_data:
subfolder = task.get('subfolder', '')
subfolder_to_tasks[subfolder].append(task)
return dict(subfolder_to_tasks)
########################################################
# Parsing helpers
########################################################
def parse_comma_separated_string_into_list(string: str) -> List[str]:
"""
Parse comma-separated string into a list, filtering empty entries.
Args:
string: Comma-separated string of attributes
Returns:
List of stripped, non-empty strings
"""
if not string or string.strip() == "":
return []
return [item.strip() for item in string.split(",") if item.strip()]
def normalize_object_name(obj: str) -> str:
"""
Normalize object name by removing numeric suffixes.
E.g., 'banana_01' -> 'banana', 'mug_01' -> 'mug', 'lime01_01' -> 'lime01'
"""
return re.sub(r'_\d+$', '', obj)
def colorize_attributes(attributes_str: str) -> str:
"""
Colorize specific attributes with ANSI color codes.
Args:
attributes_str: Comma-separated string of attributes
Returns:
String with colored attributes
"""
if not attributes_str:
return ""
color_map = {
'complex': '\033[1;31m', # Bold red
'moderate': '\033[1;33m', # Bold yellow/orange
'simple': '\033[1;32m' # Bold green
}
reset = '\033[0m'
attrs = [attr.strip() for attr in attributes_str.split(',')]
colored_attrs = []
for attr in attrs:
if attr in color_map:
colored_attrs.append(f"{color_map[attr]}{attr}{reset}")
else:
colored_attrs.append(attr)
return ', '.join(colored_attrs)
########################################################
# Attribute analysis
########################################################
def analyze_task_attributes(tasks_data: List[Dict[str, Any]]) -> Tuple[Dict[str, List[Dict[str, str]]], int, List[Dict[str, str]]]:
"""
Analyze task attributes from a list of task metadata dicts.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Tuple of (attribute_to_tasks dict, total_tasks_count, untagged_tasks list)
where attribute_to_tasks maps attribute names to lists of task info dicts
containing 'task_name', 'path', and 'filename' keys.
"""
attribute_to_tasks = defaultdict(list)
untagged_tasks = []
for task in tasks_data:
task_name = task.get('task_name', 'Unknown')
filename = task.get('filename', '')
path = task.get('path', '')
attributes = parse_comma_separated_string_into_list(task.get('attributes', ''))
task_info = {
'task_name': task_name,
'path': path,
'filename': filename
}
if not attributes:
untagged_tasks.append(task_info)
else:
for attr in attributes:
attribute_to_tasks[attr].append(task_info)
return dict(attribute_to_tasks), len(tasks_data), untagged_tasks
def print_attribute_summary(attribute_to_tasks: Dict[str, List[Dict[str, str]]],
total_tasks: int,
untagged_tasks: List[Dict[str, str]],
verbose: bool = False,
print_header: bool = True):
"""
Print formatted attribute summary table.
Args:
attribute_to_tasks: Dictionary mapping attributes to list of task info dicts
(each dict has 'task_name', 'path', 'filename' keys)
total_tasks: Total number of tasks scanned
untagged_tasks: List of task info dicts with no attributes
verbose: If True, show detailed breakdown with all tasks per attribute
print_header: If True, print the header section
"""
# Sort attributes by category
# Define priority categories (shown first)
priority_attrs = ['simple', 'moderate', 'complex']
# Separate into priority and other attributes
priority_items = []
other_items = []
for attr, tasks in attribute_to_tasks.items():
if attr in priority_attrs:
priority_items.append((attr, tasks))
else:
other_items.append((attr, tasks))
# Sort priority by the defined order
priority_items.sort(key=lambda x: priority_attrs.index(x[0]))
# Sort other attributes alphabetically
other_items.sort(key=lambda x: x[0])
# Calculate statistics
total_unique_attributes = len(attribute_to_tasks)
# Print header
if print_header:
print("\n" + "=" * 100)
print("TASK ATTRIBUTES SUMMARY")
print(f"Total unique attributes: {total_unique_attributes}")
print(f"Total tasks scanned: {total_tasks}")
if untagged_tasks:
print(f"Untagged tasks: {len(untagged_tasks)}")
print("=" * 100)
# Calculate column widths
max_attr_len = max(len(attr) for attr in attribute_to_tasks.keys()) if attribute_to_tasks else 10
attr_col_width = max(max_attr_len + 2, len("Attribute")) # +2 for indented sub-attributes
# Print compact table header with just counts
print(f"{'Attribute':<{attr_col_width}} | Count")
print("-" * (attr_col_width + 10))
# Print priority attributes (simple, moderate, complex) - just counts
if priority_items:
for attr, tasks in priority_items:
count = len(tasks)
print(f"{attr:<{attr_col_width}} | {count:5d}")
# Print separator after priority items
if other_items:
print("-" * (attr_col_width + 10))
# Group other attributes by category (visual, relational, procedural)
category_order = ['visual', 'relational', 'procedural']
category_groups = {cat: [] for cat in category_order}
uncategorized = []
for attr, tasks in other_items:
category = BENCHMARK_TASK_CATEGORIES.get(attr)
if category in category_groups:
category_groups[category].append((attr, tasks))
else:
uncategorized.append((attr, tasks))
for category in category_order:
items = category_groups[category]
if not items:
continue
# Collect unique tasks across all attributes in this category
seen = set()
category_total = 0
for _, tasks in items:
for t in tasks:
if t['task_name'] not in seen:
seen.add(t['task_name'])
category_total += 1
print(f"{category:<{attr_col_width}} | {category_total:5d}")
for attr, tasks in items:
print(f" {attr:<{attr_col_width - 2}} | {len(tasks):5d}")
# Print any uncategorized attributes
if uncategorized:
print("-" * (attr_col_width + 10))
for attr, tasks in uncategorized:
print(f"{attr:<{attr_col_width}} | {len(tasks):5d}")
print("-" * (attr_col_width + 10))
# Print detailed breakdown by attribute only if verbose
if verbose:
print()
print("=" * 100)
print("DETAILED BREAKDOWN BY ATTRIBUTE")
print("=" * 100)
# Print priority attributes with task lists
if priority_items:
for attr, tasks in priority_items:
count = len(tasks)
print(f"{attr.upper()} ({count} tasks):")
sorted_tasks = sorted(tasks, key=lambda x: x['task_name'])
for i, task_info in enumerate(sorted_tasks, 1):
print(f" {i:2d}. {task_info['task_name']} ({task_info['filename']})")
# Print other attributes with task lists
for attr, tasks in other_items:
count = len(tasks)
print(f"{attr.upper()} ({count} tasks):")
sorted_tasks = sorted(tasks, key=lambda x: x['task_name'])
for i, task_info in enumerate(sorted_tasks, 1):
print(f" {i:2d}. {task_info['task_name']} ({task_info['filename']})")
# Print untagged tasks if any
if untagged_tasks:
count = len(untagged_tasks)
print(f"UNTAGGED ({count} tasks):")
sorted_tasks = sorted(untagged_tasks, key=lambda x: x['task_name'])
for i, task_info in enumerate(sorted_tasks, 1):
print(f" {i:2d}. {task_info['task_name']} ({task_info['filename']})")
########################################################
# Attribute category breakdown
########################################################
def analyze_attribute_categories(tasks_data: List[Dict[str, Any]],
category_remap: Dict[str, str] = None) -> Dict[str, Counter]:
"""
Break down attributes into higher-level categories
(e.g., difficulty, visual, relational, procedural).
Args:
tasks_data: List of task metadata dicts
category_remap: Mapping from attribute name to category name.
Defaults to BENCHMARK_TASK_CATEGORIES.
Returns:
Dict mapping category names to Counter of attribute counts
"""
if category_remap is None:
category_remap = BENCHMARK_TASK_CATEGORIES
categories = defaultdict(Counter)
for task in tasks_data:
attributes = parse_comma_separated_string_into_list(task.get('attributes', ''))
for attr in attributes:
if attr in category_remap:
category = category_remap[attr]
categories[category][attr] += 1
return dict(categories)
def print_attribute_category_summary(categories: Dict[str, Counter]):
"""Print attribute breakdown by higher-level category."""
category_labels = {
'difficulty': 'Difficulty',
'visual': 'Visual',
'relational': 'Relational',
'procedural': 'Procedural',
}
print("\n" + "=" * 80)
print("ATTRIBUTE BREAKDOWN BY CATEGORY")
print("=" * 80)
for category_key in ['difficulty', 'visual', 'relational', 'procedural']:
counter = categories.get(category_key, Counter())
if not counter:
continue
label = category_labels.get(category_key, category_key)
total_in_category = sum(counter.values())
print(f"\n {label}:")
for attr, count in counter.most_common():
pct = (count / total_in_category * 100) if total_in_category > 0 else 0
print(f" {attr:15s}: {count:3d} ({pct:5.1f}%)")
########################################################
# Attribute reorganization
########################################################
def print_attribute_reorganization(attribute_to_tasks: Dict[str, List[Dict[str, str]]],
remap: Dict[str, str],
verbose: bool = False):
"""
Print a reorganized view of attributes based on a remapping dictionary.
Args:
attribute_to_tasks: Dictionary mapping attributes to list of task info dicts
remap: Dictionary mapping old attribute names to new category names
verbose: If True, show detailed breakdown with all tasks per category
"""
# Build new category to tasks mapping
category_to_tasks = defaultdict(list)
unmapped_attrs = {}
for attr, tasks in attribute_to_tasks.items():
if attr in remap:
new_category = remap[attr]
# Add tasks to the new category, avoiding duplicates
for task_info in tasks:
existing_task_names = [t['task_name'] for t in category_to_tasks[new_category]]
if task_info['task_name'] not in existing_task_names:
category_to_tasks[new_category].append(task_info)
else:
# Keep unmapped attributes separate
unmapped_attrs[attr] = tasks
# Print header
print("\n" + "=" * 100)
print("REORGANIZED ATTRIBUTES")
print(f"Mapping: {remap}")
print("=" * 100)
# Calculate column widths
all_categories = list(category_to_tasks.keys()) + list(unmapped_attrs.keys())
max_cat_len = max(len(cat) for cat in all_categories) if all_categories else 10
cat_col_width = max(max_cat_len, len("Category"))
# Print table header
print(f"{'Category':<{cat_col_width}} | Count")
print("-" * (cat_col_width + 10))
# Print new categories first (sorted alphabetically)
for category in sorted(category_to_tasks.keys()):
tasks = category_to_tasks[category]
count = len(tasks)
print(f"{category:<{cat_col_width}} | {count:5d}")
# Print separator if there are unmapped attributes
if unmapped_attrs:
print("-" * (cat_col_width + 10))
print("(Unmapped attributes)")
print("-" * (cat_col_width + 10))
for attr in sorted(unmapped_attrs.keys()):
tasks = unmapped_attrs[attr]
count = len(tasks)
print(f"{attr:<{cat_col_width}} | {count:5d}")
print("-" * (cat_col_width + 10))
# Print detailed breakdown if verbose
if verbose:
print()
print("=" * 100)
print("DETAILED BREAKDOWN BY NEW CATEGORY")
print("=" * 100)
for category in sorted(category_to_tasks.keys()):
tasks = category_to_tasks[category]
# Find which original attributes map to this category
original_attrs = [attr for attr, cat in remap.items() if cat == category]
print(f"\n{category.upper()} ({len(tasks)} tasks) - from: {', '.join(original_attrs)}")
sorted_tasks = sorted(tasks, key=lambda x: x['task_name'])
for i, task_info in enumerate(sorted_tasks, 1):
print(f" {i:2d}. {task_info['task_name']} ({task_info['filename']})")
if unmapped_attrs:
print("\n--- UNMAPPED ATTRIBUTES ---")
for attr in sorted(unmapped_attrs.keys()):
tasks = unmapped_attrs[attr]
print(f"\n{attr.upper()} ({len(tasks)} tasks):")
sorted_tasks = sorted(tasks, key=lambda x: x['task_name'])
for i, task_info in enumerate(sorted_tasks, 1):
print(f" {i:2d}. {task_info['task_name']} ({task_info['filename']})")
########################################################
# Instruction variant analysis
########################################################
def analyze_instruction_variants(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""Analyze instruction variant coverage across tasks.
Returns:
Dict with keys:
- 'tasks_with_variants': list of task metadata dicts that have instruction_variants
- 'tasks_single': list of task metadata dicts with a single instruction string
- 'variant_key_to_tasks': dict mapping each variant key to the list of task dicts that define it
"""
tasks_with_variants = []
tasks_single = []
variant_key_to_tasks: Dict[str, List[Dict[str, Any]]] = {}
for task in tasks_data:
variants = task.get('instruction_variants')
if variants and isinstance(variants, dict):
tasks_with_variants.append(task)
for key in variants:
variant_key_to_tasks.setdefault(key, []).append(task)
else:
tasks_single.append(task)
return {
'tasks_with_variants': tasks_with_variants,
'tasks_single': tasks_single,
'variant_key_to_tasks': variant_key_to_tasks,
}
def print_instruction_variant_summary(tasks_data: List[Dict[str, Any]], verbose: bool = False):
"""Print a summary of instruction variant coverage.
Shows how many tasks define multiple instruction variants, which variant
keys exist, and lists the tasks with their variant texts.
"""
stats = analyze_instruction_variants(tasks_data)
tasks_with = stats['tasks_with_variants']
tasks_single = stats['tasks_single']
key_to_tasks = stats['variant_key_to_tasks']
total = len(tasks_data)
n_with = len(tasks_with)
n_single = len(tasks_single)
print("\n" + "=" * 100)
print("INSTRUCTION VARIANT SUMMARY")
print(f"Total tasks: {total}")
print(f" Tasks with instruction variants: {n_with}")
print(f" Tasks with single instruction: {n_single}")
print("=" * 100)
if not key_to_tasks:
print("\nNo tasks define instruction variants.")
return
# Table of variant keys
sorted_keys = sorted(key_to_tasks.keys())
max_key_len = max(len(k) for k in sorted_keys)
key_col_w = max(max_key_len, len("Variant Key"))
print(f"\n{'Variant Key':<{key_col_w}} | Count")
print("-" * (key_col_w + 10))
for key in sorted_keys:
print(f"{key:<{key_col_w}} | {len(key_to_tasks[key]):5d}")
print("-" * (key_col_w + 10))
# Detailed listing of tasks with variants
print("\n" + "=" * 100)
print("TASKS WITH INSTRUCTION VARIANTS")
print("=" * 100)
sorted_tasks = sorted(tasks_with, key=lambda t: t.get('task_name', ''))
for i, task in enumerate(sorted_tasks, 1):
task_name = task.get('task_name', 'Unknown')
variants = task.get('instruction_variants', {})
keys_str = ", ".join(sorted(variants.keys()))
print(f"\n {i:2d}. {task_name} [{keys_str}]")
for key in sorted(variants.keys()):
print(f" {key}: \"{variants[key]}\"")
if verbose and tasks_single:
print("\n" + "=" * 100)
print(f"TASKS WITH SINGLE INSTRUCTION ({n_single})")
print("=" * 100)
sorted_single = sorted(tasks_single, key=lambda t: t.get('task_name', ''))
for i, task in enumerate(sorted_single, 1):
task_name = task.get('task_name', 'Unknown')
instruction = task.get('instruction', '')
print(f" {i:2d}. {task_name}: \"{instruction}\"")
########################################################
# Scene analysis
########################################################
def print_task_summary_by_scene(tasks_data: List[Dict[str, Any]], verbose: bool = False):
"""
Print a table showing the number of tasks per scene and list of tasks for each scene.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
verbose: If True, also show additional details (subfolder, attributes) for each task
"""
scene_to_tasks = sort_tasks_by_scene(tasks_data)
total_scenes = len(scene_to_tasks)
total_tasks = len(tasks_data)
# Print header
print("\n" + "=" * 100)
print("TASKS BY SCENE SUMMARY")
print(f"Total scenes: {total_scenes}")
print(f"Total tasks: {total_tasks}")
print("=" * 100)
# Sort scenes alphabetically
sorted_scenes = sorted(scene_to_tasks.items(), key=lambda x: x[0])
# Calculate column width for scene names
max_scene_len = max(len(scene) for scene in scene_to_tasks.keys()) if scene_to_tasks else 10
scene_col_width = max(max_scene_len, len("Scene"))
# Print table header
print(f"{'Scene':<{scene_col_width}} | Count")
print("-" * (scene_col_width + 10))
# Print each scene with task count
for scene, tasks in sorted_scenes:
count = len(tasks)
scene_name = scene if scene else "(no scene)"
print(f"{scene_name:<{scene_col_width}} | {count:5d}")
print("-" * (scene_col_width + 10))
# Always print detailed breakdown by scene with task lists
print()
print("=" * 100)
print("TASKS BY SCENE")
print("=" * 100)
for scene, tasks in sorted_scenes:
scene_name = scene if scene else "(no scene)"
count = len(tasks)
print(f"\n{scene_name} ({count} tasks):")
# Sort tasks by task name
sorted_tasks = sorted(tasks, key=lambda x: x.get('task_name', ''))
for i, task in enumerate(sorted_tasks, 1):
task_name = task.get('task_name', 'Unknown')
language_instruction = task.get('instruction', '')
variants = task.get('instruction_variants')
variant_info = f" [variants: {', '.join(variants.keys())}]" if variants else ""
# In verbose mode, also show additional task details
if verbose:
attributes = task.get('attributes', '')
colored_attributes = colorize_attributes(attributes)
extra_info = f" ({colored_attributes})" if attributes else ""
print(f" {i:2d}. {task_name}: \"{language_instruction}\"{variant_info} {extra_info}")
else:
print(f" {i:2d}. {task_name}: \"{language_instruction}\"{variant_info}")
########################################################
# Object analysis
########################################################
def analyze_objects(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze objects across all tasks.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Dict with keys: total_instances, unique_objects, normalized_unique,
object_frequency, normalized_frequency, objects_per_task, repeat_objects
"""
all_objects = []
unique_objects = set()
normalized_unique = set()
object_frequency = Counter()
normalized_frequency = Counter()
objects_per_task = []
for task in tasks_data:
objects = parse_comma_separated_string_into_list(task.get('contact_objects', ''))
# Filter out 'table' as it's a fixture, not a manipulable object
objects = [obj for obj in objects if obj.lower() != 'table']
all_objects.extend(objects)
unique_objects.update(objects)
objects_per_task.append(len(objects))
for obj in objects:
object_frequency[obj] += 1
normalized = normalize_object_name(obj)
normalized_unique.add(normalized)
normalized_frequency[normalized] += 1
repeat_objects = sorted(
[(obj, count) for obj, count in object_frequency.items() if count > 1],
key=lambda x: -x[1]
)
return {
'total_instances': len(all_objects),
'unique_objects': unique_objects,
'normalized_unique': normalized_unique,
'object_frequency': object_frequency,
'normalized_frequency': normalized_frequency,
'objects_per_task': objects_per_task,
'repeat_objects': repeat_objects,
}
def print_object_summary(object_stats: Dict[str, Any]):
"""Print formatted object analysis report."""
print("\n" + "=" * 80)
print("OBJECT STATISTICS")
print("=" * 80)
objects_per_task = object_stats['objects_per_task']
print(f"Unique objects (exact): {len(object_stats['unique_objects'])}")
print(f"Unique objects (normalized, excl. duplicates like mug_01/mug_02): {len(object_stats['normalized_unique'])}")
print(f"Total object instances across all tasks: {object_stats['total_instances']}")
if objects_per_task:
avg = sum(objects_per_task) / len(objects_per_task)
print(f"Average objects per task: {avg:.1f}")
print(f"Min objects in a task: {min(objects_per_task)}")
print(f"Max objects in a task: {max(objects_per_task)}")
print("\n Top 20 Most Used Objects (normalized names):")
for obj, count in object_stats['normalized_frequency'].most_common(20):
print(f" {obj:25s}: {count:3d} tasks")
if object_stats['repeat_objects']:
print("\n Objects Used in Multiple Tasks (top 30):")
for obj, count in object_stats['repeat_objects'][:30]:
print(f" {obj:30s}: appears in {count:3d} tasks")
########################################################
# Subtask complexity analysis
########################################################
def analyze_subtask_complexity(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze subtask complexity across all tasks.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Dict with keys: stage_counts, atomic_condition_counts,
avg_stages, avg_atomic_conditions
"""
stage_counts = Counter()
atomic_condition_counts = []
for task in tasks_data:
num_stages = task.get('num_sequential_stages', 0)
atomic_count = task.get('num_atomic_conditions', 0)
stage_counts[num_stages] += 1
atomic_condition_counts.append(atomic_count)
avg_stages = 0.0
if tasks_data:
total_stages = sum(k * v for k, v in stage_counts.items())
avg_stages = total_stages / len(tasks_data)
avg_atomic = 0.0
if atomic_condition_counts:
avg_atomic = sum(atomic_condition_counts) / len(atomic_condition_counts)
return {
'stage_counts': stage_counts,
'atomic_condition_counts': atomic_condition_counts,
'avg_stages': avg_stages,
'avg_atomic_conditions': avg_atomic,
}
def print_subtask_summary(subtask_stats: Dict[str, Any]):
"""Print formatted subtask complexity report."""
print("\n" + "=" * 80)
print("SUBTASK COMPLEXITY")
print("=" * 80)
print(f"Average sequential stages per task: {subtask_stats['avg_stages']:.2f}")
print(f"Average atomic conditions per task: {subtask_stats['avg_atomic_conditions']:.2f}")
print("\nSequential stage count distribution:")
for count, num_tasks in sorted(subtask_stats['stage_counts'].items()):
print(f" {count} stage(s): {num_tasks} tasks")
########################################################
# Subtask count analysis
########################################################
def analyze_subtask_counts(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze subtask counts across all tasks.
Subtask count represents the number of distinct manipulation actions
(e.g., pick-and-place) required, accounting for logical mode:
- "all": every object group must complete
- "any": only one object group must complete
- "choose": exactly K object groups must complete
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Dict with keys: distribution (Counter), per_task (list of dicts),
avg, min_val, max_val
"""
distribution = Counter()
per_task = []
for task in tasks_data:
num_subtasks = task.get('num_subtasks', 0)
distribution[num_subtasks] += 1
per_task.append({
'task_name': task.get('task_name', 'Unknown'),
'num_subtasks': num_subtasks,
'filename': task.get('filename', ''),
})
values = [t['num_subtasks'] for t in per_task]
avg = sum(values) / len(values) if values else 0.0
return {
'distribution': distribution,
'per_task': per_task,
'avg': avg,
'min_val': min(values) if values else 0,
'max_val': max(values) if values else 0,
}
def print_subtask_count_summary(subtask_count_stats: Dict[str, Any], verbose: bool = False):
"""Print formatted subtask count report."""
print("\n" + "=" * 80)
print("SUBTASK COUNTS")
print("=" * 80)
print("num_subtasks = number of distinct manipulation actions required,")
print("accounting for logical mode (all/any/choose).")
print(f"\nAverage subtasks per task: {subtask_count_stats['avg']:.2f}")
print(f"Min: {subtask_count_stats['min_val']}")
print(f"Max: {subtask_count_stats['max_val']}")
print("\nDistribution:")
for count, num_tasks in sorted(subtask_count_stats['distribution'].items()):
print(f" {count} subtask(s): {num_tasks:3d} tasks")
if verbose:
print("\nTasks with most subtasks:")
sorted_tasks = sorted(subtask_count_stats['per_task'],
key=lambda x: -x['num_subtasks'])
for i, t in enumerate(sorted_tasks[:20], 1):
print(f" {i:2d}. {t['task_name']:<45s} {t['num_subtasks']} subtask(s) ({t['filename']})")
########################################################
# Difficulty score analysis
########################################################
def analyze_difficulty_scores(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze difficulty scores and labels across all tasks.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Dict with keys: label_distribution, score_distribution, per_task,
avg_score, min_score, max_score
"""
label_distribution = Counter()
score_distribution = Counter()
per_task = []
for task in tasks_data:
score = task.get('difficulty_score', 0)
label = task.get('difficulty_label', 'simple')
num_subtasks = task.get('num_subtasks', 0)
attributes = task.get('attributes', '')
label_distribution[label] += 1
score_distribution[score] += 1
per_task.append({
'task_name': task.get('task_name', 'Unknown'),
'filename': task.get('filename', ''),
'difficulty_score': score,
'difficulty_label': label,
'num_subtasks': num_subtasks,
'attributes': attributes,
})
scores = [t['difficulty_score'] for t in per_task]
avg_score = sum(scores) / len(scores) if scores else 0.0
return {
'label_distribution': label_distribution,
'score_distribution': score_distribution,
'per_task': per_task,
'avg_score': avg_score,
'min_score': min(scores) if scores else 0,
'max_score': max(scores) if scores else 0,
}
def print_difficulty_score_summary(difficulty_stats: Dict[str, Any], verbose: bool = False):
"""Print formatted difficulty score analysis report."""
print("\n" + "=" * 80)
print("DIFFICULTY SCORING")
print("=" * 80)
print("score = num_subtasks + max(skill_weight)")
print(f"Thresholds: simple <= {DIFFICULTY_THRESHOLDS[0]}, "
f"moderate <= {DIFFICULTY_THRESHOLDS[1]}, "
f"complex > {DIFFICULTY_THRESHOLDS[1]}")
print(f"Skill weights: {dict(sorted(SKILL_WEIGHTS.items(), key=lambda x: x[1]))}")
print(f"\nAverage difficulty score: {difficulty_stats['avg_score']:.2f}")
print(f"Score range: {difficulty_stats['min_score']} – {difficulty_stats['max_score']}")
print("\nDistribution by label:")
total = sum(difficulty_stats['label_distribution'].values())
for label in ['simple', 'moderate', 'complex']:
count = difficulty_stats['label_distribution'].get(label, 0)
pct = (count / total * 100) if total > 0 else 0
print(f" {label:10s}: {count:3d} tasks ({pct:5.1f}%)")
print("\nScore histogram:")
for score, count in sorted(difficulty_stats['score_distribution'].items()):
bar = "#" * count
print(f" score {score:2d}: {count:3d} {bar}")
if verbose:
print("\nAll tasks by difficulty (highest first):")
sorted_tasks = sorted(difficulty_stats['per_task'],
key=lambda x: (-x['difficulty_score'], x['task_name']))
for i, t in enumerate(sorted_tasks, 1):
attrs = t['attributes']
print(f" {i:3d}. {t['task_name']:<45s} "
f"score={t['difficulty_score']:2d} "
f"num_subtasks={t['num_subtasks']} "
f"[{t['difficulty_label']}] "
f"({attrs})")
########################################################
# Episode length analysis
########################################################
def analyze_episode_lengths(tasks_data: List[Dict[str, Any]]) -> Dict[str, Any]:
"""
Analyze episode lengths across all tasks.
Args:
tasks_data: List of task metadata dicts (already loaded and filtered)
Returns:
Dict with keys: lengths, by_difficulty
"""
lengths = []
by_difficulty = defaultdict(list)
for task in tasks_data:
try:
ep_length = int(task.get('episode_s', 0))
if ep_length > 0:
lengths.append(ep_length)
diff_label = task.get('difficulty_label', '')
if diff_label in ('simple', 'moderate', 'complex'):
by_difficulty[diff_label].append(ep_length)
except (ValueError, TypeError):
pass
return {
'lengths': lengths,
'by_difficulty': dict(by_difficulty),
}
def print_episode_length_summary(episode_stats: Dict[str, Any]):
"""Print formatted episode length analysis report."""
lengths = episode_stats['lengths']
if not lengths:
return
print("\n" + "=" * 80)
print("EPISODE LENGTH ANALYSIS")
print("=" * 80)
print(f"Average episode length: {sum(lengths) / len(lengths):.1f} seconds")
print(f"Min episode length: {min(lengths)} seconds")
print(f"Max episode length: {max(lengths)} seconds")
print("\nAverage episode length by difficulty:")