-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotting_utils.py
More file actions
1664 lines (1402 loc) · 60.6 KB
/
Copy pathplotting_utils.py
File metadata and controls
1664 lines (1402 loc) · 60.6 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
"""
Plotting utilities for routing visualization.
Provides reusable functions for aggregating routing data and creating heatmaps.
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import matplotlib.gridspec as gridspec
from collections import defaultdict
from functools import partial
def build_routing_data_tuples(cache: dict) -> list:
"""Convert a loaded routing_data.json cache dict into the list-of-dicts format
consumed by all plotting functions.
Supports v1 (per-timestep env_context), v2/v3 (deduplicated episodes list).
If action_logits are absent (old cache), emits a one-time warning and sets
'action_logits' to None per record.
"""
episodes = cache.get('episodes')
raw = cache['routing_data']
has_logits = 'action_logits' in raw[0] if raw else False
if not has_logits:
import warnings
warnings.warn(
"Cache has no 'action_logits' (old format). "
"Logit-based plots will not work — re-run evaluation to populate them.",
stacklevel=2,
)
return [
{
'position': tuple(r['position']),
'layer_routing': {k: np.array(v) for k, v in r['layer_routing'].items()},
'lpc': r['lpc'],
'env_context': episodes[r['episode']] if episodes is not None else r.get('env_context', {}),
'carrying': r.get('carrying', 0),
'door_unlocked': r.get('door_unlocked', 0),
'action_logits': np.array(r['action_logits'], dtype=np.float32) if 'action_logits' in r else None,
'action': (
int(r['action']) if 'action' in r
else int(np.argmax(r['action_logits'])) if r.get('action_logits') is not None
else None
),
'entropy': r['entropy'] if 'entropy' in r else None,
't_step': r.get('t_step'),
't_unlocked': r.get('t_unlocked'),
't_pick': r.get('t_pick'),
't_drop': r.get('t_drop'),
'dist_to_door': r.get('dist_to_door'),
'dist_to_key': r.get('dist_to_key'),
'dist_to_target': r.get('dist_to_target'),
}
for r in raw
]
def compute_empirical_entropy(
routing_data: list,
n_actions: int = 7,
alpha: float = 0.5,
min_visits: int = 5,
P_a: np.ndarray = None,
) -> dict:
"""
Compute empirical action entropy per grid position from actual actions taken.
Extracts (position, action) pairs, builds count tables, applies Dirichlet
smoothing (alpha=0.5, Jeffreys prior), then computes:
- pi_hat(a|s): smoothed empirical distribution
- H(A|S=s) in bits (log2)
- KL(pi_hat(.|s) || P_a_ref) in bits
- P(s): visitation frequency
- include_mask: positions with n_visits >= min_visits
Args:
routing_data: List of sample dicts with 'position' and 'action' keys.
n_actions: Number of discrete actions (7 for BabyAI).
alpha: Dirichlet smoothing concentration (Jeffreys prior = 0.5).
min_visits: Minimum visits to include a position in analysis.
P_a: Optional reference marginal action distribution (shape (n_actions,)).
If None, P_a is computed from routing_data (local/phase marginal).
Pass a pre-computed global P_a to get KL relative to the global marginal.
Returns:
dict with keys: 'pi_hat', 'H_s', 'KL_s', 'P_s', 'include_mask', 'P_a'
'P_a' in the return dict always reflects the distribution actually used for KL.
"""
valid = [s for s in routing_data if s.get('action') is not None]
if not valid:
return {
'pi_hat': {}, 'H_s': {}, 'KL_s': {},
'P_s': {}, 'include_mask': {}, 'P_a': np.ones(n_actions) / n_actions,
}
counts = defaultdict(lambda: np.zeros(n_actions, dtype=np.float64))
for s in valid:
counts[s['position']][s['action']] += 1
total_visits = sum(c.sum() for c in counts.values())
# Marginal action distribution (Dirichlet-smoothed) from input data
global_counts = np.zeros(n_actions, dtype=np.float64)
for c in counts.values():
global_counts += c
P_a_local = (global_counts + alpha) / (global_counts.sum() + n_actions * alpha)
# Use provided P_a as reference if given, otherwise use local marginal
P_a_ref = P_a if P_a is not None else P_a_local
pi_hat, H_s, KL_s, P_s, include_mask = {}, {}, {}, {}, {}
for pos, c in counts.items():
n = c.sum()
smoothed = (c + alpha) / (n + n_actions * alpha)
pi_hat[pos] = smoothed
H_s[pos] = float(-np.sum(smoothed * np.log2(smoothed + 1e-12)))
KL_s[pos] = float(np.sum(smoothed * np.log2((smoothed + 1e-12) / (P_a_ref + 1e-12))))
P_s[pos] = n / total_visits
include_mask[pos] = bool(n >= min_visits)
return {
'pi_hat': pi_hat,
'H_s': H_s,
'KL_s': KL_s,
'P_s': P_s,
'include_mask': include_mask,
'P_a': P_a_ref,
}
def compute_global_mutual_information(KL_s: dict, P_s: dict) -> float:
"""
Compute I(S; A) = sum_s P(s) * KL(pi_hat(.|s) || P(a)) in bits.
"""
shared = set(KL_s) & set(P_s)
return float(sum(P_s[s] * KL_s[s] for s in shared))
def _make_expert_cmap(light_color, dark_color, name):
"""Create a colormap from a light to dark shade for expert confidence gradients."""
return mcolors.LinearSegmentedColormap.from_list(name, [light_color, dark_color], N=256)
# Expert colormaps: intensity reflects expert complexity.
# Expert 0 (identity/skip) is mildest, higher experts are progressively more vivid.
# Each colormap provides a gradient from light (low confidence) to dark (high confidence).
EXPERT_CMAPS = [
_make_expert_cmap('#d4e6f1', '#7fb3d8', 'expert0_light_blue'), # Expert 0: soft pastel blue
_make_expert_cmap('#c3a6d6', '#8e44ad', 'expert1_purple'), # Expert 1: medium purple
_make_expert_cmap('#f1948a', '#c0392b', 'expert2_red'), # Expert 2: strong red
_make_expert_cmap('#f0b27a', '#e67e22', 'expert3_orange'), # Expert 3: orange
_make_expert_cmap('#82e0aa', '#27ae60', 'expert4_green'), # Expert 4: green
_make_expert_cmap('#aab7b8', '#515a5a', 'expert5_grey'), # Expert 5: grey
]
# Color for unvisited cells
UNVISITED_COLOR = (1.0, 1.0, 1.0) # White for unvisited/no-data cells
def compute_grid_bounds(positions: list[tuple]) -> dict:
"""
Compute grid bounds from a list of positions.
Args:
positions: List of (x, y) tuples
Returns:
dict with x_min, x_max, y_min, y_max, grid_width, grid_height
"""
x_coords = [p[0] for p in positions]
y_coords = [p[1] for p in positions]
x_min, x_max = min(x_coords), max(x_coords)
y_min, y_max = min(y_coords), max(y_coords)
return {
'x_min': x_min,
'x_max': x_max,
'y_min': y_min,
'y_max': y_max,
'grid_width': x_max - x_min + 1,
'grid_height': y_max - y_min + 1,
}
def pos_to_quadrant(x: int, y: int, room_bounds: tuple) -> str:
"""Map a grid position to a room quadrant label.
Args:
x: Grid x coordinate
y: Grid y coordinate
room_bounds: (x_min, y_min, x_max, y_max) inclusive interior floor bounds
Returns:
One of 'TL', 'TR', 'BL', 'BR'.
MiniGrid convention: y increases downward, so lower y = Top.
"""
x_min, y_min, x_max, y_max = room_bounds
mid_x = (x_min + x_max) / 2
mid_y = (y_min + y_max) / 2
top = y < mid_y
left = x < mid_x
if top and left:
return "TL"
elif top:
return "TR"
elif left:
return "BL"
else:
return "BR"
def aggregate_routing_by_position(
routing_data: list,
layer_expert_sizes: list = None,
) -> tuple[dict, dict, dict, list]:
"""
Aggregate routing weights, LPC, and per-layer LPC by position.
Args:
routing_data: List of dicts with keys position, layer_routing, lpc, env_context
layer_expert_sizes: Optional list of per-layer expert size lists, e.g.
[[0, 16, 32], [0, 16, 32], [0, 16, 32]]. When provided, computes
per-layer LPC = mean_t(sum_k(w_k * s_k^2)) per position per layer.
Returns:
avg_routing_by_pos: dict mapping position -> {layer_name: avg_weights}
avg_lpc_by_pos: dict mapping position -> avg_lpc
avg_layer_lpc_by_pos: dict mapping position -> {layer_name: avg_layer_lpc}
(empty dict if layer_expert_sizes is None)
layer_names: sorted list of layer names
"""
position_routing = defaultdict(list)
position_lpc = defaultdict(list)
position_layer_lpc = defaultdict(lambda: defaultdict(list))
for sample in routing_data:
pos = sample['position']
layer_routing = sample['layer_routing']
lpc = sample['lpc']
position_routing[pos].append(layer_routing)
position_lpc[pos].append(lpc)
if layer_expert_sizes is not None:
for layer_idx, expert_sizes in enumerate(layer_expert_sizes):
layer_key = f'layer_{layer_idx}'
weights = layer_routing.get(layer_key)
if weights is not None:
sizes_sq = np.array([s ** 2 for s in expert_sizes], dtype=np.float64)
position_layer_lpc[pos][layer_key].append(float(np.dot(weights, sizes_sq)))
if not position_routing:
return {}, {}, {}, []
# Compute averages
avg_routing_by_pos = {}
for pos, routings in position_routing.items():
avg_routing_by_pos[pos] = {
layer: np.mean([r[layer] for r in routings], axis=0)
for layer in routings[0].keys()
}
avg_lpc_by_pos = {pos: np.mean(lpcs) for pos, lpcs in position_lpc.items()}
avg_layer_lpc_by_pos = {
pos: {layer: np.mean(vals) for layer, vals in layers.items()}
for pos, layers in position_layer_lpc.items()
}
# Get layer names from first sample
layer_names = sorted(list(position_routing.values())[0][0].keys())
return avg_routing_by_pos, avg_lpc_by_pos, avg_layer_lpc_by_pos, layer_names
def render_routing_heatmap(
ax,
avg_routing_by_pos: dict,
grid_info: dict,
layer_name: str,
expert_cmaps: list = None
):
"""
Render a routing heatmap for a single layer on the given axes.
Shows dominant expert (by color) and confidence (by intensity).
Unvisited cells are rendered as dark gray.
Args:
ax: Matplotlib axes to render on
avg_routing_by_pos: dict mapping position -> {layer_name: avg_weights}
grid_info: dict from compute_grid_bounds()
layer_name: Name of the layer to visualize
expert_cmaps: List of colormaps for each expert (defaults to EXPERT_CMAPS)
"""
if expert_cmaps is None:
expert_cmaps = EXPERT_CMAPS
grid_width = grid_info['grid_width']
grid_height = grid_info['grid_height']
x_min = grid_info['x_min']
y_min = grid_info['y_min']
# Create grids for dominant expert and confidence
dominant_grid = np.full((grid_height, grid_width), -1, dtype=int)
confidence_grid = np.full((grid_height, grid_width), np.nan)
for pos, routing in avg_routing_by_pos.items():
x, y = pos
gx, gy = x - x_min, y - y_min
weights = routing[layer_name]
dominant_expert = np.argmax(weights)
confidence = weights[dominant_expert]
dominant_grid[gy, gx] = dominant_expert
confidence_grid[gy, gx] = confidence
# Create RGB image - unvisited cells are dark gray
rgb_image = np.ones((grid_height, grid_width, 3)) * np.array(UNVISITED_COLOR)
for gy in range(grid_height):
for gx in range(grid_width):
expert_idx = dominant_grid[gy, gx]
if expert_idx >= 0:
cmap = expert_cmaps[expert_idx % len(expert_cmaps)]
# Map confidence (0.33 to 1.0 typical range) to color intensity (0.3 to 1.0)
intensity = 0.3 + 0.7 * confidence_grid[gy, gx]
color = cmap(intensity)
rgb_image[gy, gx, :] = color[:3]
ax.imshow(rgb_image, origin='upper')
ax.set_title(f"{layer_name}\n(color=expert, intensity=confidence)")
ax.set_xlabel("X")
ax.set_ylabel("Y")
# Add grid lines
ax.set_xticks(np.arange(-0.5, grid_width, 1), minor=True)
ax.set_yticks(np.arange(-0.5, grid_height, 1), minor=True)
ax.grid(which='minor', color='white', linestyle='-', linewidth=0.5)
ax.tick_params(which='minor', size=0)
def render_lpc_heatmap(ax, avg_lpc_by_pos: dict, grid_info: dict, vmin=None, vmax=None):
"""
Render an LPC heatmap on the given axes.
Unvisited cells are rendered as dark gray (NaN in colormap).
Args:
ax: Matplotlib axes to render on
avg_lpc_by_pos: dict mapping position -> avg_lpc
grid_info: dict from compute_grid_bounds()
vmin, vmax: Optional color scale bounds (for shared scaling across columns)
"""
grid_width = grid_info['grid_width']
grid_height = grid_info['grid_height']
x_min = grid_info['x_min']
y_min = grid_info['y_min']
# Create LPC grid - NaN for unvisited
lpc_grid = np.full((grid_height, grid_width), np.nan)
for pos, lpc in avg_lpc_by_pos.items():
x, y = pos
gx, gy = x - x_min, y - y_min
lpc_grid[gy, gx] = lpc
# Create colormap that shows NaN as dark gray
cmap = plt.cm.viridis.copy()
cmap.set_bad(color=UNVISITED_COLOR)
im = ax.imshow(lpc_grid, origin='upper', cmap=cmap, vmin=vmin, vmax=vmax)
ax.set_title("Mean LPC")
ax.set_xlabel("X")
ax.set_ylabel("Y")
# Add grid lines
ax.set_xticks(np.arange(-0.5, grid_width, 1), minor=True)
ax.set_yticks(np.arange(-0.5, grid_height, 1), minor=True)
ax.grid(which='minor', color='white', linestyle='-', linewidth=0.5)
ax.tick_params(which='minor', size=0)
return im
def render_layer_lpc_heatmap(
ax,
avg_layer_lpc_by_pos: dict,
grid_info: dict,
layer_name: str,
vmin=None,
vmax=None,
):
"""
Render a per-layer LPC heatmap: mean_t(sum_k(w_k * s_k^2)) per cell for one layer.
Unvisited cells are rendered as dark gray (NaN in colormap).
Args:
ax: Matplotlib axes to render on
avg_layer_lpc_by_pos: dict mapping position -> {layer_name: avg_layer_lpc}
grid_info: dict from compute_grid_bounds()
layer_name: which layer to render (e.g. 'layer_0')
vmin, vmax: Optional color scale bounds (for shared scaling across columns)
"""
grid_width = grid_info['grid_width']
grid_height = grid_info['grid_height']
x_min = grid_info['x_min']
y_min = grid_info['y_min']
lpc_grid = np.full((grid_height, grid_width), np.nan)
for pos, layer_lpcs in avg_layer_lpc_by_pos.items():
val = layer_lpcs.get(layer_name)
if val is not None:
x, y = pos
gx, gy = x - x_min, y - y_min
lpc_grid[gy, gx] = val
cmap = plt.cm.viridis.copy()
cmap.set_bad(color=UNVISITED_COLOR)
im = ax.imshow(lpc_grid, origin='upper', cmap=cmap, vmin=vmin, vmax=vmax)
ax.set_title(f"{layer_name} LPC")
ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_xticks(np.arange(-0.5, grid_width, 1), minor=True)
ax.set_yticks(np.arange(-0.5, grid_height, 1), minor=True)
ax.grid(which='minor', color='white', linestyle='-', linewidth=0.5)
ax.tick_params(which='minor', size=0)
return im
def plot_overall_routing(
routing_data: list,
env_image: np.ndarray = None,
env_mission: str = "",
layer_expert_sizes: list = None,
) -> plt.Figure:
"""
Create a complete routing visualization figure.
Shows environment layout (if provided), mean LPC heatmap, and per-layer LPC
heatmaps. Columns go from general (mean LPC) to specific (per-layer LPC).
All LPC columns share a single viridis colorbar with global vmin/vmax.
Args:
routing_data: List of dicts with keys position, layer_routing, lpc, env_context
env_image: Optional environment render to show
env_mission: Optional language instruction to show under the environment image
layer_expert_sizes: Optional list of per-layer expert size lists, e.g.
[[0, 16, 32], [0, 16, 32], [0, 16, 32]]. Required for per-layer LPC columns.
Returns:
matplotlib Figure object
"""
# Aggregate data
avg_routing_by_pos, avg_lpc_by_pos, avg_layer_lpc_by_pos, layer_names = (
aggregate_routing_by_position(routing_data, layer_expert_sizes=layer_expert_sizes)
)
if not avg_routing_by_pos:
fig, ax = plt.subplots(1, 1, figsize=(6, 4))
ax.text(0.5, 0.5, "No data after filtering", ha='center', va='center', fontsize=14)
ax.axis('off')
return fig
# Compute grid bounds
positions = list(avg_routing_by_pos.keys())
grid_info = compute_grid_bounds(positions)
has_layer_lpc = bool(avg_layer_lpc_by_pos)
# Column layout: [env?] | [mean LPC] | [layer_0 LPC] | ... | [colorbar]
num_lpc_cols = 1 + (len(layer_names) if has_layer_lpc else 0) # mean + per-layer
num_main_cols = num_lpc_cols + (1 if env_image is not None else 0)
num_cols = num_main_cols + 1 # +1 for shared colorbar column
fig = plt.figure(figsize=(5 * num_main_cols, 7))
gs = gridspec.GridSpec(
1, num_cols,
width_ratios=[1] * num_main_cols + [0.05],
hspace=0.08, wspace=0.35,
)
plot_idx = 0
# Environment render (if provided)
if env_image is not None:
ax = fig.add_subplot(gs[0, plot_idx])
ax.imshow(env_image)
ax.set_title("Environment Layout")
if env_mission:
ax.set_xlabel(env_mission, fontsize=9)
ax.set_xticks([])
ax.set_yticks([])
else:
ax.axis('off')
plot_idx += 1
# Compute shared color scale across all LPC values
all_lpc_vals = list(avg_lpc_by_pos.values())
if has_layer_lpc:
for layer_lpcs in avg_layer_lpc_by_pos.values():
all_lpc_vals.extend(layer_lpcs.values())
global_vmin = float(np.nanmin(all_lpc_vals)) if all_lpc_vals else None
global_vmax = float(np.nanmax(all_lpc_vals)) if all_lpc_vals else None
# Mean LPC heatmap (general, leftmost data column)
ax = fig.add_subplot(gs[0, plot_idx])
lpc_im = render_lpc_heatmap(ax, avg_lpc_by_pos, grid_info, vmin=global_vmin, vmax=global_vmax)
plot_idx += 1
# Per-layer LPC heatmaps (specific, right of mean LPC)
if has_layer_lpc:
for layer_name in layer_names:
ax = fig.add_subplot(gs[0, plot_idx])
render_layer_lpc_heatmap(
ax, avg_layer_lpc_by_pos, grid_info, layer_name,
vmin=global_vmin, vmax=global_vmax,
)
plot_idx += 1
# Shared colorbar
cbar_ax = fig.add_subplot(gs[0, num_main_cols])
fig.colorbar(lpc_im, cax=cbar_ax).set_label('LPC')
return fig
def get_available_analyses(routing_data: list) -> list[str]:
"""
Determine which analysis types are available based on the routing data.
Args:
routing_data: List of (position, layer_routing, lpc, env_context) tuples
Returns:
List of available analysis names (e.g., ['overall', 'by_starting_room'])
"""
available = ['overall']
if not routing_data:
return available
# Check first sample's env_context for available fields
env_context = routing_data[0]['env_context']
if not env_context:
return available
# Check for room-based grouping
if env_context.get('agent_start_room') is not None:
available.append('by_starting_room')
# Door-location grouping: only offer if distinct door-position-sets <= 9.
# Sample up to 200 timesteps — env_context repeats within episodes so a
# small probe is sufficient to discover the distinct door configurations.
if env_context.get('doors') is not None:
distinct_door_sets = set()
for sample in routing_data[:200]:
ctx = sample['env_context']
doors = ctx.get('doors')
if doors:
door_pos_key = tuple(sorted((d[0], d[1]) for d in doors))
distinct_door_sets.add(door_pos_key)
if 1 <= len(distinct_door_sets) <= 9:
available.append('by_door_location')
# Combined door+box-row grouping: only offer if both doors and boxes exist
# and distinct combined keys <= 16.
if env_context.get('doors') is not None and env_context.get('boxes') is not None:
distinct_combined = set()
for sample in routing_data[:200]:
ctx = sample['env_context']
doors = ctx.get('doors')
boxes = ctx.get('boxes')
if doors and boxes:
door_key = tuple(sorted((d[0], d[1]) for d in doors))
box_row_key = tuple(sorted(b[1] for b in boxes))
distinct_combined.add((door_key, box_row_key))
if 1 <= len(distinct_combined) <= 16:
available.append('by_door_and_box_row')
# Carrying-phase split: only offer if both carrying=0 and carrying=1 timesteps exist.
if len(routing_data) > 0 and 'carrying' in routing_data[0]:
carrying_values = set(sample['carrying'] for sample in routing_data)
if carrying_values == {0, 1}:
available.append('by_carrying_phase')
# Agent+target quadrant grouping: requires agent_start_pos, target_pos, room_bounds.
# Only offered when there is meaningful variation in agent start quadrant (>= 2 distinct)
# and the total number of combinations is within reason (<= 16, i.e. 4x4).
if (env_context.get('agent_start_pos') is not None
and env_context.get('target_pos') is not None
and env_context.get('room_bounds') is not None):
distinct_quad_combos = set()
distinct_agent_quads = set()
for sample in routing_data[:200]:
ctx = sample['env_context']
agent_pos = ctx.get('agent_start_pos')
target_pos = ctx.get('target_pos')
room_bounds = ctx.get('room_bounds')
if agent_pos is None or target_pos is None or room_bounds is None:
continue
aq = pos_to_quadrant(agent_pos[0], agent_pos[1], room_bounds)
tq = pos_to_quadrant(target_pos[0], target_pos[1], room_bounds)
distinct_quad_combos.add((aq, tq))
distinct_agent_quads.add(aq)
if len(distinct_agent_quads) >= 2 and len(distinct_quad_combos) <= 16:
available.append('by_agent_and_target_quadrant')
return available
def group_routing_data(routing_data: list, group_by: str) -> dict[tuple, list]:
"""
Group routing data by a field in env_context.
Args:
routing_data: List of (position, layer_routing, lpc, env_context) tuples
group_by: Field name in env_context to group by (e.g., 'agent_start_room')
Returns:
Dict mapping group_key -> list of samples in that group.
Keys are tuples for hashability.
"""
groups = defaultdict(list)
for sample in routing_data:
env_context = sample['env_context']
if group_by == 'carrying_phase':
key = (sample.get('carrying', 0),)
elif group_by == 'door_unlocked_phase':
t_step = sample.get('t_step')
t_unlocked = sample.get('t_unlocked')
if t_step is None:
continue
if t_unlocked is None or t_step < t_unlocked:
key = (0,) # pre-unlock / pre-open
else:
key = (1,) # post-unlock / post-open
elif group_by == 'door_location':
doors = env_context.get('doors')
if not doors:
continue
# sorted tuple of (x, y) pairs — order-independent, ignores color/state
key = tuple(sorted((d[0], d[1]) for d in doors))
elif group_by == 'door_and_box_row':
doors = env_context.get('doors')
boxes = env_context.get('boxes')
if not doors or not boxes:
continue
door_key = tuple(sorted((d[0], d[1]) for d in doors))
box_row_key = tuple(sorted(b[1] for b in boxes))
key = (door_key, box_row_key)
elif group_by == 'agent_and_target_quadrant':
agent_pos = env_context.get('agent_start_pos')
target_pos = env_context.get('target_pos')
room_bounds = env_context.get('room_bounds')
if agent_pos is None or target_pos is None or room_bounds is None:
continue
aq = pos_to_quadrant(agent_pos[0], agent_pos[1], room_bounds)
tq = pos_to_quadrant(target_pos[0], target_pos[1], room_bounds)
key = (aq, tq)
elif group_by == 'unlock_phase':
t_step = sample.get('t_step')
t_unlocked = sample.get('t_unlocked')
if t_step is None:
continue
if t_unlocked is None or t_step < t_unlocked:
key = (0,) # pre-unlock
else:
key = (1,) # post-unlock
elif group_by == 'key_phase':
t_step = sample.get('t_step')
t_pick = sample.get('t_pick')
t_unlocked = sample.get('t_unlocked')
t_drop = sample.get('t_drop')
if t_step is None:
continue
if t_pick is None or t_step < t_pick:
key = (0,) # pre-key
elif t_unlocked is None or t_step < t_unlocked:
key = (1,) # with-key, pre-unlock
elif t_drop is None or t_step < t_drop:
key = (2,) # with-key, post-unlock
else:
key = (3,) # post-unlock, post-key (key dropped)
else:
key = env_context.get(group_by)
if key is None:
continue
if isinstance(key, list):
key = tuple(key)
elif not isinstance(key, tuple):
key = (key,)
groups[key].append(sample)
return dict(groups)
def _positional_names(n: int, axis: str) -> list[str]:
"""Generate positional names for a grid axis.
Args:
n: Number of positions along this axis
axis: 'row' for Top/Bottom, 'col' for Left/Right
"""
if axis == 'row':
ends = ('Top', 'Bottom')
else:
ends = ('Left', 'Right')
if n == 1:
return ['Center']
if n == 2:
return list(ends)
if n == 3:
return [ends[0], 'Center', ends[1]]
return [ends[0]] + [f'{axis.title()} {i}' for i in range(1, n - 1)] + [ends[1]]
def room_labels_for_groups(sorted_keys: list, room_grid_shape: tuple = None) -> dict:
"""Map room top-left keys to human-readable labels like 'Top-Left', 'Center'.
Args:
sorted_keys: List of room_top tuples, sorted
room_grid_shape: (num_cols, num_rows) if known
Returns:
Dict mapping room_key -> label string
"""
if room_grid_shape is None or not sorted_keys:
return {k: f"Room at {k}" for k in sorted_keys}
num_cols, num_rows = room_grid_shape
xs = sorted(set(k[0] for k in sorted_keys))
ys = sorted(set(k[1] for k in sorted_keys))
x_to_col = {x: i for i, x in enumerate(xs)}
y_to_row = {y: j for j, y in enumerate(ys)}
row_names = _positional_names(num_rows, 'row')
col_names = _positional_names(num_cols, 'col')
labels = {}
for key in sorted_keys:
col_idx = x_to_col[key[0]]
row_idx = y_to_row[key[1]]
r = row_names[row_idx] if row_idx < len(row_names) else f"Row {row_idx}"
c = col_names[col_idx] if col_idx < len(col_names) else f"Col {col_idx}"
if r == 'Center' and c == 'Center':
labels[key] = 'Center'
else:
labels[key] = f"{r}-{c}"
return labels
def door_location_labels_for_groups(sorted_keys: list) -> dict:
"""Map door-location group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a tuple of (x, y) tuples,
e.g. ((5, 3),) for a single door.
Returns:
Dict mapping group_key -> label string
"""
labels = {}
for key in sorted_keys:
if len(key) == 1:
labels[key] = f"Door at {key[0][0]},{key[0][1]}"
else:
coords = ",".join(f"({x},{y})" for x, y in key)
labels[key] = f"Doors at {coords}"
return labels
def door_and_box_row_labels_for_groups(sorted_keys: list) -> dict:
"""Map door+box-row group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a tuple of
(door_pos_tuple, box_row_tuple), e.g.
(((5, 3),), (2,)) meaning door at (5,3), box on row 2.
Returns:
Dict mapping group_key -> label string
"""
labels = {}
for key in sorted_keys:
door_part, box_row_part = key
if len(door_part) == 1:
door_str = f"Door {door_part[0][0]},{door_part[0][1]}"
else:
door_str = "Doors " + ",".join(f"({x},{y})" for x, y in door_part)
if len(box_row_part) == 1:
box_str = f"Box row {box_row_part[0]}"
else:
box_str = "Box rows " + ",".join(str(r) for r in box_row_part)
labels[key] = f"{door_str} / {box_str}"
return labels
def agent_and_target_quadrant_labels_for_groups(sorted_keys: list) -> dict:
"""Map (agent_quadrant, target_quadrant) group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a 2-tuple of quadrant strings
e.g. ('TL', 'BR')
Returns:
Dict mapping group_key -> label string like 'Agent: TL / Target: BR'
"""
return {key: f"Agent: {key[0]} / Target: {key[1]}" for key in sorted_keys}
ACTION_NAMES = ['left', 'right', 'forward', 'pickup', 'drop', 'toggle', 'done']
def carrying_phase_labels_for_groups(sorted_keys: list) -> dict:
"""Map carrying-phase group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a 1-tuple (carrying,)
where carrying is 0 (not carrying) or 1 (carrying object).
Returns:
Dict mapping group_key -> label string
"""
labels = {(0,): "Not carrying", (1,): "Carrying object"}
return {k: labels.get(k, f"Phase {k[0]}") for k in sorted_keys}
def door_unlocked_phase_labels_for_groups(sorted_keys: list) -> dict:
"""Map door-unlocked-phase group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a 1-tuple (door_unlocked,)
where door_unlocked is 0 (door still locked) or 1 (door unlocked).
Returns:
Dict mapping group_key -> label string
"""
labels = {(0,): "Door locked", (1,): "Door unlocked"}
return {k: labels.get(k, f"Phase {k[0]}") for k in sorted_keys}
def unlock_phase_labels_for_groups(sorted_keys: list) -> dict:
"""Map unlock-phase group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a 1-tuple (phase,)
where 0 = pre-unlock (t_step < t_unlocked or never unlocked)
and 1 = post-unlock (t_step >= t_unlocked).
Returns:
Dict mapping group_key -> label string
"""
labels = {(0,): "Pre-unlock", (1,): "Post-unlock"}
return {k: labels.get(k, f"Phase {k[0]}") for k in sorted_keys}
def key_phase_labels_for_groups(sorted_keys: list) -> dict:
"""Map key-phase group keys to human-readable labels.
Args:
sorted_keys: List of group keys, each a 1-tuple (phase,)
where 0 = pre-key, 1 = with-key/pre-unlock, 2 = with-key/post-unlock,
3 = post-unlock/post-key (key dropped).
Returns:
Dict mapping group_key -> label string
"""
labels = {
(0,): "Pre-key",
(1,): "With-key (pre-unlock)",
(2,): "With-key (post-unlock)",
(3,): "Post-unlock (post-key)",
}
return {k: labels.get(k, f"Phase {k[0]}") for k in sorted_keys}
def plot_grouped_routing(
routing_data: list,
group_by: str,
env_image: np.ndarray = None,
env_mission: str = "",
room_env_images: dict = None,
max_groups: int = 25,
layer_expert_sizes: list = None,
) -> plt.Figure:
"""
Create a multi-row figure with one row of LPC heatmaps per group.
Each row shows: [env_image?] | [mean LPC] | [layer_0 LPC] | [layer_1 LPC] | ...
Columns go from general (mean LPC) to specific (per-layer LPC).
All LPC columns share a single viridis colorbar with global vmin/vmax.
Args:
routing_data: List of (position, layer_routing, lpc, env_context) tuples
group_by: Field in env_context to group by (e.g., 'agent_start_room')
env_image: Optional fallback environment render
env_mission: Optional fallback mission string
room_env_images: Optional dict mapping group_key -> (image, mission) per group
max_groups: Maximum number of groups to display
layer_expert_sizes: Optional list of per-layer expert size lists for per-layer LPC
Returns:
matplotlib Figure object
"""
groups = group_routing_data(routing_data, group_by)
if not groups:
fig, ax = plt.subplots(1, 1, figsize=(6, 4))
ax.text(0.5, 0.5, f"No data for grouping by '{group_by}'",
ha='center', va='center', fontsize=14)
ax.axis('off')
return fig
# Sort groups by key for consistent ordering
sorted_keys = sorted(groups.keys())[:max_groups]
num_groups = len(sorted_keys)
# Get room_grid_shape from first sample (for room labels)
first_ctx = routing_data[0]['env_context']
room_grid_shape = first_ctx.get('room_grid_shape')
# Aggregate first group to get layer names
first_avg, _, _, layer_names = aggregate_routing_by_position(
groups[sorted_keys[0]], layer_expert_sizes=layer_expert_sizes
)
if not first_avg:
fig, ax = plt.subplots(1, 1, figsize=(6, 4))
ax.text(0.5, 0.5, "No visited positions in first group",
ha='center', va='center', fontsize=14)
ax.axis('off')
return fig
# Pre-aggregate all groups to compute global vmin/vmax across all LPC values
group_aggregates = {}
all_lpc_vals = []
for group_key in sorted_keys:
avg_routing, avg_lpc, avg_layer_lpc, _ = aggregate_routing_by_position(
groups[group_key], layer_expert_sizes=layer_expert_sizes
)
if not avg_routing:
continue
group_aggregates[group_key] = (avg_routing, avg_lpc, avg_layer_lpc)
all_lpc_vals.extend(avg_lpc.values())
for layer_lpcs in avg_layer_lpc.values():
all_lpc_vals.extend(layer_lpcs.values())
global_vmin = float(np.nanmin(all_lpc_vals)) if all_lpc_vals else None
global_vmax = float(np.nanmax(all_lpc_vals)) if all_lpc_vals else None
has_layer_lpc = any(agg[2] for agg in group_aggregates.values())
# Column layout: [env?] | [mean LPC] | [layer_0 LPC] | ... | [colorbar]
num_lpc_cols = 1 + (len(layer_names) if has_layer_lpc else 0)
has_env_image = env_image is not None
num_main_cols = num_lpc_cols + (1 if has_env_image else 0)
num_cols = num_main_cols + 1 # +1 for shared colorbar column
# Figure dimensions
col_width = 4.5
row_height = 4.5
fig = plt.figure(figsize=(col_width * num_main_cols, row_height * num_groups + 0.5))
width_ratios = [1] * num_main_cols + [0.05]
gs = gridspec.GridSpec(
num_groups, num_cols,
height_ratios=[1] * num_groups,
width_ratios=width_ratios,
hspace=0.35, wspace=0.35,
)
# Compute grid bounds from ALL data (so all rows share the same coordinate system)
all_positions = [sample['position'] for sample in routing_data]
global_grid_info = compute_grid_bounds(all_positions)
lpc_im = None # Track last rendered image for colorbar
# Compute human-readable labels for all groups
if group_by == 'agent_start_room':
group_labels = room_labels_for_groups(sorted_keys, room_grid_shape)
elif group_by == 'door_location':
group_labels = door_location_labels_for_groups(sorted_keys)
elif group_by == 'door_and_box_row':
group_labels = door_and_box_row_labels_for_groups(sorted_keys)
elif group_by == 'carrying_phase':
group_labels = carrying_phase_labels_for_groups(sorted_keys)
elif group_by == 'door_unlocked_phase':
group_labels = door_unlocked_phase_labels_for_groups(sorted_keys)