English
File size: 54,206 Bytes
26225c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
import copy
import h5py
import torch
import warnings
import numpy as np
from time import time
from typing import List, Tuple, Optional, Union, Any
from torch_geometric.data import Data as PyGData
from torch_geometric.data import Batch as PyGBatch
from torch_geometric.nn.pool.consecutive import consecutive_cluster

import src
from src.data.cluster import CSRData
from src.data.cluster import Cluster
from src.data.instance import InstanceData
from src.metrics import SemanticMetricResults, PanopticMetricResults, \
    InstanceMetricResults
from src.utils import tensor_idx, is_dense, has_duplicates, \
    isolated_nodes, knn_2, save_tensor, load_tensor, save_tensor_dict, \
    load_tensor_dict, save_dense_to_csr, load_csr_to_dense, to_trimmed, \
    to_float_rgb, to_byte_rgb


__all__ = ['Data', 'Batch']


class Data(PyGData):
    """Inherit from torch_geometric.Data with extensions tailored to our
    specific needs.
    """

    _NOT_INDEXABLE = [
        '_csr_', '_cluster_', '_instance_data_', 'edge_index', 'edge_attr',
        '_slice_dict', '_inc_dict', '_num_graphs']


    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        if src.is_debug_enabled():
            self.debug()

    @property
    def pos(self):
        return self['pos'] if 'pos' in self._store else None

    @property
    def rgb(self):
        return self['rgb'] if 'rgb' in self._store else None

    @property
    def obj(self) -> InstanceData:
        """InstanceData object indicating the instance indices for each
        node/point/superpoint in the Data.
        """
        return self['obj'] if 'obj' in self._store else None

    @property
    def semantic_pred(self):
        return self['semantic_pred'] if 'semantic_pred' in self._store else None

    @property
    def neighbor_index(self):
        return self['neighbor_index'] if 'neighbor_index' in self._store \
            else None

    @property
    def sub(self) -> Cluster:
        """Cluster object indicating subpoint indices for each point."""
        return self['sub'] if 'sub' in self._store else None

    @property
    def super_index(self):
        """Index of the superpoint each point belongs to."""
        return self['super_index'] if 'super_index' in self._store else None

    @property
    def v_edge_attr(self):
        """Vertical edge features."""
        return self['v_edge_attr'] if 'v_edge_attr' in self._store else None

    def norm_index(self, mode: str = 'graph') -> torch.Tensor:
        """Index to be used for LayerNorm.

        :param mode: str
            Normalization mode. 'graph' will normalize per graph (i.e.
            per cloud, i.e. per batch). 'node' will normalize per node
            (i.e. per point). 'segment' will normalize per segment
            (i.e.  per cluster)
        """
        if getattr(self, 'batch', None) is not None:
            batch = self.batch
        else:
            batch = torch.zeros(
                self.num_nodes, device=self.device, dtype=torch.long)
        if self.super_index is not None:
            super_index = self.super_index
        else:
            super_index = torch.zeros(
                self.num_nodes, device=self.device, dtype=torch.long)
        if mode == 'graph':
            return batch
        elif mode == 'node':
            return torch.arange(self.num_nodes, device=self.device)
        elif mode == 'segment':
            num_batches = batch.max() + 1
            return super_index * num_batches + batch
        else:
            raise NotImplementedError(f"Unknown mode='{mode}'")

    @property
    def is_super(self):
        """Whether the points are superpoints for a denser sub-graph."""
        return self.sub is not None

    @property
    def is_sub(self):
        """Whether the points belong to a coarser super-graph."""
        return self.super_index is not None

    @property
    def has_neighbors(self):
        """Whether the points have neighbors."""
        return self.neighbor_index is not None and self.neighbor_index.shape[1] > 0

    @property
    def has_edges(self):
        """Whether the points have edges."""
        return self.edge_index is not None and self.edge_index.shape[1] > 0

    @property
    def has_edge_attr(self):
        """Whether the edges have features in `edge_attr`."""
        return self.edge_attr is not None and self.edge_attr.shape[0] > 0

    @property
    def edge_keys(self) -> List[str]:
        """All keys starting with `edge_`, apart from `edge_index` and
        `edge_attr`.
        """
        return [
            k for k in self.keys
            if k.startswith('edge_') and k not in ['edge_index', 'edge_attr']]

    def raise_if_edge_keys(self):
        """This is a TEMPORARY, HACKY method to be called wherever
        edge_keys may cause an issue.
        """
        if len(self.edge_keys) > 0:
            raise NotImplementedError(
                "Edge keys are not fully supported yet, please consider "
                "stacking all your `edge_` attributes in `edge_attr` for the "
                "time being. This error was triggered by the presence of the "
                f"following attributes: {self.edge_keys}")

    @property
    def v_edge_keys(self) -> List[str]:
        """All keys starting with `v_edge_`."""
        return [k for k in self.keys if k.startswith('v_edge_')]

    @property
    def num_edges(self):
        """Overwrite the torch_geometric initial definition, which
        somehow returns incorrect results, like:
            data.num_edges != data.edge_index.shape[1]
        """
        return self.edge_index.shape[1] if self.has_edges else 0

    @property
    def num_points(self):
        return self.num_nodes

    @property
    def num_super(self):
        return self.super_index.max().item() + 1 if self.is_sub else 0

    @property
    def num_sub(self):
        return self.sub.points.max().item() + 1 if self.is_super else 0

    def detach(self) -> 'Data':
        """Extend `torch_geometric.Data.detach` to handle Cluster and
        InstanceData attributes.
        """
        self = super().detach()
        for k in self.keys:
            if isinstance(self[k], CSRData):
                self[k] = self[k].detach()
        return self

    def to(self, device, **kwargs) -> 'Data':
        """Extend `torch_geometric.Data.to` to handle Cluster and
        InstanceData attributes.
        """
        self = super().to(device, **kwargs)
        for k in self.keys:
            if isinstance(self[k], CSRData):
                self[k] = self[k].to(device, **kwargs)
        return self

    def cpu(self, **kwargs) -> 'Data':
        """Move the NAG with all Data in it to CPU."""
        return self.to('cpu', **kwargs)

    def cuda(self, **kwargs) -> 'Data':
        """Move the NAG with all Data in it to CUDA."""
        return self.to('cuda', **kwargs)

    @property
    def device(self) -> torch.device:
        """Device of the first-encountered tensor in 'self'."""
        for key, item in self:
            if torch.is_tensor(item):
                return item.device
        return torch.tensor([]).device

    def debug(self):
        """Sanity checks."""
        self.validate()

        if self.is_super:
            assert isinstance(self.sub, Cluster), \
                "Clusters in 'sub' must be expressed using a Cluster object"
            assert self.y is None or self.y.dim() == 2, \
                "Clusters in 'sub' must hold label histograms"

        if self.obj is not None:
            assert isinstance(self.obj, InstanceData), \
                "Instance labels in 'obj' must be expressed using an " \
                "InstanceData object"

        if self.is_sub:
            if not is_dense(self.super_index):
                print(
                    "WARNING: super_index indices are generally expected to be "
                    "dense (i.e. all indices in [0, super_index.max()] are used),"
                    " which is not the case here. This may be because you are "
                    "creating a Data object after applying a selection of "
                    "points without updating the cluster indices.")

        if self.has_edges:
            assert self.edge_index.max() < self.num_points
            assert 0 <= self.edge_index.min()

    def __inc__(self, key: str, value: Any, *args, **kwargs) -> Any:
        """Extend the PyG.Data.__inc__ behavior on '*index*' and
        'face' attributes to our 'super_index'. This is needed for
        maintaining clusters when batching Data objects together.
        """
        if 'super_index' in key:
            return self.num_super
        return super().__inc__(key, value, *args, **kwargs)

    def __cat_dim__(self, key: str, value: Any, *args, **kwargs) -> Any:
        """Extend the PyG.Data.__cat_dim__ behavior on '*index*' and
        'face' attributes to our 'neighbor_index'. This is needed for
        maintaining neighbors when batching Data objects together.
        """
        return 0 if key == 'neighbor_index' \
            else super().__cat_dim__(key, value, *args, **kwargs)

    # TODO: this closely aligns with PyG.Data.subgraph(). Can't we
    #  refactor this to better depend on existing PyG code ?
    def select(
            self,
            idx: Union[int, List[int], torch.Tensor, np.ndarray],
            update_sub: bool = True,
            update_super: bool = True
    ) -> Tuple['Data', Tuple[torch.Tensor, torch.Tensor], Tuple[torch.Tensor, 'Cluster']]:
        """Returns a new Data with updated clusters, which indexes
        `self` using entries in `idx`. Supports torch and numpy fancy
        indexing. `idx` must not contain duplicate entries, as this
        would cause ambiguities in edges and super- and sub- indices.

        This operations breaks neighborhoods, so if 'self.has_neighbors'
        the output Data will not.

        NB: if `self` belongs to a NAG, calling this function in
        isolation may break compatibility with point and cluster indices
        in the other hierarchy levels. If consistency matters, prefer
        using NAG indexing instead.

        :parameter
        idx: int or 1D torch.LongTensor or numpy.NDArray
            Data indices to select from 'self'. Must NOT contain
            duplicates
        update_sub: bool
            If True, the point (i.e. subpoint) indices will also be
            updated to maintain dense indices. The output will then
            contain '(idx_sub, sub_super)' which can help apply these
            changes to maintain consistency with lower hierarchy levels
            of a NAG.
        update_super: bool
            If True, the cluster (i.e. superpoint) indices will also be
            updated to maintain dense indices. The output will then
            contain '(idx_super, super_sub)' which can help apply these
            changes to maintain consistency with higher hierarchy levels
            of a NAG.

        :return: data, (idx_sub, sub_super), (idx_super, super_sub)
            data: Data
                indexed data
            idx_sub: torch.LongTensor
                to be used with 'Data.select()' on the sub-level
            sub_super: torch.LongTensor
                to replace 'Data.super_index' on the sub-level
            idx_super: torch.LongTensor
                to be used with 'Data.select()' on the super-level
            super_sub: Cluster
                to replace 'Data.sub' on the super-level
        """
        device = self.device

        # Convert idx to a torch.LongTensor
        idx = tensor_idx(idx).to(device)

        # Make sure idx contains no duplicate entries
        if src.is_debug_enabled():
            assert not has_duplicates(idx), \
                "Duplicate indices are not supported. This would cause " \
                "ambiguities in edges and super- and sub- indices."

        # Output Data will not share memory with input Data.
        # NB: it is generally not recommended to instantiate en empty
        # Data like this, as it might cause issues when calling
        # 'data.num_nodes' later on. Need to be careful when calling
        # 'data.num_nodes' before having set any of the pointwise
        # attributes (e.g. 'x', 'pos', 'rgb', 'y', etc.)
        data = Data()

        # If Data contains edges, we will want to update edge indices
        # and attributes with respect to the new point order. Edge
        # indices are updated here, to compute 'idx_edge', which
        # will be used to select edge attributes
        if self.has_edges:
            # To update edge indices, create a 'reindex' tensor so that
            # the desired output can be computed with simple indexation
            # 'reindex[edge_index]'. This avoids using map() or
            # numpy.vectorize alternatives.
            reindex = torch.full(
                (self.num_nodes,), -1, dtype=torch.int64, device=device)
            reindex = reindex.scatter_(
                0, idx, torch.arange(idx.shape[0], device=device))
            edge_index = reindex[self.edge_index]

            # Remove obsolete edges (i.e. those involving a '-1' index)
            idx_edge = torch.where((edge_index != -1).all(dim=0))[0]
            data.edge_index = edge_index[:, idx_edge]

        # Selecting points may affect their order, if we need to
        # preserve subpoint consistency, we need to update the
        # 'Data.sub' of the current level and the 'Data.super_index'
        # of the level below
        out_sub = (None, None)
        if self.is_super:
            data.sub, out_sub = self.sub.select(idx, update_sub=update_sub)

        # Selecting points may affect their order, if we need to
        # preserve superpoint consistency, we need to update the
        # 'Data.super_index' of the current level along with the
        # 'Data.sub' of the level above
        out_super = (None, None)
        if self.is_sub:
            data.super_index = self.super_index[idx]

        if self.is_sub and update_super:
            # Convert superpoint indices, in case some superpoints have
            # disappeared. 'idx_super' is intended to be used with
            # Data.select() on the level above
            new_super_index, perm = consecutive_cluster(data.super_index)
            idx_super = data.super_index[perm]
            data.super_index = new_super_index

            # Selecting the superpoints with 'idx_super' will not be
            # enough to maintain consistency with the current points. We
            # also need to update the super-level's 'Data.sub', which
            # can be computed from 'super_index'
            super_sub = Cluster(
                data.super_index, torch.arange(idx.shape[0], device=device),
                dense=True)

            out_super = (idx_super, super_sub)

        # Index data items depending on their type
        warn_keys = ['neighbor_index', 'neighbor_distance']
        skip_keys = ['edge_index', 'sub', 'super_index'] + warn_keys
        for key, item in self:

            # 'skip_keys' have already been dealt with earlier on, so we
            # can skip them here
            if key in warn_keys and src.is_debug_enabled():
                print(
                    f"WARNING: Data.select does not support '{key}', this "
                    f"attribute will be absent from the output")
            if key in skip_keys:
                continue

            # Slice CSRData elements, unless specified otherwise
            if isinstance(item, CSRData):
                data[key] = item[idx]
                continue

            is_tensor = torch.is_tensor(item)
            is_node_size = item.shape[0] == self.num_nodes
            is_edge_size = item.shape[0] == self.num_edges

            # Slice tensor elements containing num_edges elements. Note
            # we deal with edges first, to rule out the case where
            # num_edges = num_nodes. This will deal with `edge_attr` but
            # also any other attribute whose key starts with 'edge_' and
            # whose first dimension size matches the number of edges in
            # `edge_index`. An exception is made for attributes
            # starting with 'v_edge': those are expected to be node
            # attributes and must be treated as such
            if is_tensor and is_node_size and key in self.v_edge_keys:
                data[key] = item[idx]

            elif self.has_edges and is_tensor and is_edge_size and \
                    key in ['edge_attr'] + self.edge_keys:
                data[key] = item[idx_edge]

            # Slice other tensor elements containing num_nodes elements
            elif is_tensor and is_node_size:
                data[key] = item[idx]

            # Other Data attributes are simply copied
            else:
                data[key] = copy.deepcopy(item)

        # Security just in case no node-level attribute was passed, Data
        # will not be able to properly infer its number of nodes
        if data.num_nodes != idx.shape[0]:
            data.num_nodes = idx.shape[0]

        return data, out_sub, out_super

    def is_isolated(self):
        """If self.has_edges, returns a boolean tensor of size
        self.num_nodes indicating which are absent from self.edge_index.
        Will raise an error if self.has_edges is False.
        """
        edge_index = self.edge_index if self.has_edges \
            else torch.zeros(2, 0, dtype=torch.long, device=self.device)
        return isolated_nodes(edge_index, num_nodes=self.num_nodes)

    def connect_isolated(self, k: int = 1) -> 'Data':
        """Search for nodes with no edges in the graph and connect them
        to their k nearest neighbors. Update self.edge_index and
        self.edge_attr accordingly.

        Will raise an error if self has no edges or no pos.

        Returns self updated with the newly-created edges.
        """
        assert self.pos is not None

        # Make sure there is no edge_attr if there is no edge_index
        if not self.has_edges:
            self.edge_attr = None

        self.raise_if_edge_keys()

        # Search for isolated nodes and exit if no node is isolated
        is_isolated = self.is_isolated()
        is_out = torch.where(is_isolated)[0]
        if not is_isolated.any():
            return self

        # Search the nearest nodes for isolated nodes, among all nodes
        # NB: we remove the nodes themselves from their own neighborhood
        high = self.pos.max(dim=0).values
        low = self.pos.min(dim=0).values
        r_max = (high - low).norm()
        neighbors, distances = knn_2(
            self.pos,
            self.pos[is_out],
            k + 1,
            r_max=r_max,
            batch_search=self.batch,
            batch_query=self.batch[is_out] if self.batch is not None else None)
        distances = distances[:, 1:]
        neighbors = neighbors[:, 1:]

        # Add new edges between the nodes
        source = is_out.repeat_interleave(k)
        target = neighbors.flatten()
        edge_index_new = torch.vstack((source, target))
        edge_index_old = self.edge_index
        self.edge_index = torch.cat((edge_index_old, edge_index_new), dim=1)

        # Exit here if there are no edge attributes
        if self.edge_attr is None:
            return self

        # If the edges have attributes, we also create attributes for
        # the new edges. There is no trivial way of doing so, the
        # heuristic here simply attempts to linearly regress the edge
        # weights based on the corresponding node distances.
        # First, get existing edges attributes and associated distance
        w = self.edge_attr
        s = edge_index_old[0]
        t = edge_index_old[1]
        d = (self.pos[s] - self.pos[t]).norm(dim=1)
        d_1 = torch.vstack((d, torch.ones_like(d))).T

        # Least square on d_1.x = w  (i.e. d.a + b = w)
        # NB: CUDA may crash trying to solve this simple system, in
        # which case we will fall back to CPU. Not ideal though
        try:
            a, b = torch.linalg.lstsq(d_1, w).solution
        except:
            if src.is_debug_enabled():
                print(
                    '\nWarning: torch.linalg.lstsq failed, trying again '
                    'on CPU')
            a, b = torch.linalg.lstsq(d_1.cpu(), w.cpu()).solution
            a = a.to(self.device)
            b = b.to(self.device)

        # Heuristic: linear approximation of w by d
        edge_attr_new = distances.flatten() * a + b

        # Append to existing self.edge_attr
        self.edge_attr = torch.cat((self.edge_attr, edge_attr_new))

        return self

    def to_trimmed(self, reduce: str = 'mean') -> 'Data':
        """Convert to 'trimmed' graph: same as coalescing with the
        additional constraint that (i, j) and (j, i) edges are duplicates.

        If edge attributes are passed, 'reduce' will indicate how to fuse
        duplicate edges' attributes.

        NB: returned edges are expressed with i<j by default.
        """
        assert self.has_edges

        self.raise_if_edge_keys()

        if self.edge_attr is not None:
            edge_index, edge_attr = to_trimmed(
                self.edge_index, edge_attr=self.edge_attr, reduce=reduce)
        else:
            edge_index = to_trimmed(self.edge_index)
            edge_attr = None

        self.edge_index = edge_index
        self.edge_attr = edge_attr

        return self

    def __eq__(self, other: Any) -> bool:
        if not isinstance(other, self.__class__):
            if src.is_debug_enabled():
                print(f'{self.__class__.__name__}.__eq__: classes differ')
            return False
        if sorted(self.keys) != sorted(other.keys):
            if src.is_debug_enabled():
                print(f'{self.__class__.__name__}.__eq__: keys differ')
            return False
        for k, v in self.items():
            if isinstance(v, torch.Tensor):
                if not torch.equal(v, other[k]):
                    if src.is_debug_enabled():
                        print(f'{self.__class__.__name__}.__eq__: {k} differ')
                    return False
                continue
            if isinstance(v, np.ndarray):
                if not np.array_equal(v, other[k]):
                    if src.is_debug_enabled():
                        print(f'{self.__class__.__name__}.__eq__: {k} differ')
                    return False
                continue
            if v != other[k]:
                if src.is_debug_enabled():
                    print(f'{self.__class__.__name__}.__eq__: {k} differ')
                return False
        return True

    def save(
            self,
            f: Union[str, h5py.File, h5py.Group],
            y_to_csr: bool = True,
            pos_dtype: torch.dtype = torch.float,
            fp_dtype: torch.dtype = torch.float):
        """Save Data to HDF5 file.

        :param f: h5 file path of h5py.File or h5py.Group
        :param y_to_csr: bool
            Convert 'y' to CSR format before saving. Only applies if
            'y' is a 2D histogram
        :param pos_dtype: torch dtype
            Data type to which 'pos' should be cast before saving. The
            reason for this separate treatment of 'pos' is that global
            coordinates may be too large and casting to 'fp_dtype' may
            result in hurtful precision loss
        :param fp_dtype: torch dtype
            Data type to which floating point tensors should be cast
            before saving
        :return:
        """
        if not isinstance(f, (h5py.File, h5py.Group)):
            with h5py.File(f, 'w') as file:
                self.save(
                    file,
                    y_to_csr=y_to_csr,
                    pos_dtype=pos_dtype,
                    fp_dtype=fp_dtype)
            return

        assert isinstance(f, (h5py.File, h5py.Group))

        for k, val in self.items():
            if k == 'pos_offset':
                save_tensor(val, f, k, fp_dtype=torch.double)
            elif k == 'pos':
                save_tensor(val, f, k, fp_dtype=pos_dtype)
            elif k == 'y' and val.dim() > 1 and y_to_csr:
                sg = f.create_group(f"{f.name}/_csr_/{k}")
                save_dense_to_csr(val, sg, fp_dtype=fp_dtype)
            elif k in ['rgb', 'mean_rgb']:
                if val.is_floating_point():
                    save_tensor((val * 255).byte(), f, k, fp_dtype=fp_dtype)
                else:
                    save_tensor(val.byte(), f, k, fp_dtype=fp_dtype)
            elif isinstance(val, Cluster):
                sg = f.create_group(f"{f.name}/_cluster_/{k}")
                val.save(sg, fp_dtype=fp_dtype)
            elif isinstance(val, InstanceData):
                sg = f.create_group(f"{f.name}/_instance_data_/{k}")
                val.save(sg, fp_dtype=fp_dtype)
            elif isinstance(val, CSRData):
                sg = f.create_group(f"{f.name}/_csr_/{k}")
                val.save(sg, fp_dtype=fp_dtype)
            elif isinstance(val, torch.Tensor):
                save_tensor(val, f, k, fp_dtype=fp_dtype)
            else:
                raise NotImplementedError(
                    f"Cannot save attribute {k} with unsupported type "
                    f"{type(val)}")

        # Save the information of which attributes are indexable and
        # which are not
        f['_not_indexable_'] = list(set(self.keys) - set(self.node_attrs()))

    @classmethod
    def load(
            cls,
            f: Union[h5py.File, h5py.Group],
            idx: Union[int, List, np.ndarray, torch.Tensor] = None,
            keys_idx: List[str] = None,
            keys: List[str] = None,
            update_sub: bool = True,
            verbose: bool = False,
            rgb_to_float: bool = False
    ) -> 'Data':
        """Read an HDF5 file and return its content as a Data object.

        NB: if relevant, a Batch object will be returned.

        :param f: h5 file path of h5py.File or h5py.Group
        :param idx: int, list, numpy.ndarray, torch.Tensor
            Used to select the elements in `keys_idx`. Supports fancy
            indexing
        :param keys_idx: List(str)
            Keys on which the indexing should be applied
        :param keys: List(str)
            Keys should be loaded from the file, ignoring the rest
        :param update_sub: bool
            If True, the point (i.e. subpoint) indices will also be
            updated to maintain dense indices. The output will then
            contain '(idx_sub, sub_super)' which can help apply these
            changes to maintain consistency with lower hierarchy levels
            of a NAG.
        :param verbose: bool
        :param rgb_to_float: bool
            If True and an integer 'rgb' or 'mean_rgb' attribute is
            loaded, it will be cast to float
        :return:
        """
        if not isinstance(f, (h5py.File, h5py.Group)):
            with h5py.File(f, 'r') as file:
                out = cls.load(
                    file, idx=idx, keys_idx=keys_idx, keys=keys,
                    update_sub=update_sub, verbose=verbose,
                    rgb_to_float=rgb_to_float)
            return out

        # Recover the keys that do not support node indexing
        _not_indexable = cls._NOT_INDEXABLE
        if '_not_indexable_' in f.keys():
            _not_indexable += [s.decode("utf-8") for s in f['_not_indexable_']]

        idx = tensor_idx(idx)
        if idx.shape[0] == 0:
            keys_idx = []
        elif keys_idx is None:
            keys_idx = list(set(f.keys()) - set(_not_indexable))

        if keys is None:
            all_keys = list(f.keys())
            for k in ['_csr_', '_cluster_', '_instance_data_']:
                if k in all_keys:
                    all_keys.remove(k)
                    all_keys += list(f[k].keys())
            keys = all_keys

        d_dict = {}
        csr_keys = []
        cluster_keys = []
        instance_data_keys = []

        # Check if the file actually corresponds to a Batch object
        # rather than a simple Data object
        has_slice_and_inc = '_slice_dict' in f.keys() and '_inc_dict' in f.keys()
        has_batch = 'batch' in f.keys()
        has_no_indexing = idx is None or idx.shape[0] == 0
        if (has_slice_and_inc or has_batch) and has_no_indexing:
            cls = Batch
        else:
            cls = Data

        # Deal with special keys first, then read other keys if required
        for k in f.keys():
            start = time()
            if k == '_not_indexable_':
                continue
            if k == '_csr_':
                csr_keys = list(f[k].keys())
                continue
            if k == '_cluster_':
                cluster_keys = list(f[k].keys())
                continue
            if k == '_instance_data_':
                instance_data_keys = list(f[k].keys())
                continue
            if k in ['_slice_dict', '_inc_dict']:
                if cls == Batch:
                    d_dict[k] = load_tensor_dict(f[k])
                continue
            if k == '_num_graphs':
                if cls == Batch:
                    d_dict[k] = f['_num_graphs'][0]
                continue
            if k in keys_idx:
                d_dict[k] = load_tensor(f[k], idx=idx)
            elif k in keys:
                d_dict[k] = load_tensor(f[k])
            if verbose and k in d_dict.keys():
                print(f'{cls.__name__}.load {k:<22}: {time() - start:0.5f}s')

        # Small sanity check on '_slice_dict' and '_inc_dict'. It is
        # possible, for attributes of type 'other' that the _inc_dict
        # contains a 'None' value when the _slice_dict holds a Tensor.
        # As a result of calling 'save_tensor_dict()' and
        # 'load_tensor_dict()', these values will be lost. So we need to
        # restore them here
        if '_slice_dict' in d_dict.keys():
            for k in set(d_dict['_slice_dict']) - set(d_dict['_inc_dict']):
                d_dict['_inc_dict'][k] = None

        # Update the 'keys_idx' with newly-found 'csr_keys',
        # 'cluster_keys', and 'instance_data_keys'
        if idx.shape[0] != 0:
            keys_idx = list(set(keys_idx).union(set(csr_keys)))
            keys_idx = list(set(keys_idx).union(set(cluster_keys)))
            keys_idx = list(set(keys_idx).union(set(instance_data_keys)))

        # Special key '_csr_' holds data saved in CSR format
        for k in csr_keys:
            start = time()
            if k in keys_idx:
                d_dict[k] = load_csr_to_dense(
                    f['_csr_'][k], idx=idx, verbose=verbose)
            elif k in keys:
                d_dict[k] = load_csr_to_dense(f['_csr_'][k], verbose=verbose)
            if verbose and k in d_dict.keys():
                print(f'{cls.__name__}.load {k:<22}: {time() - start:0.5f}s')

        # Special key '_cluster_' holds Cluster data
        for k in cluster_keys:
            start = time()
            if k in keys_idx:
                d_dict[k] = Cluster.load(
                    f['_cluster_'][k], idx=idx, update_sub=update_sub,
                    verbose=verbose)[0]
            elif k in keys:
                d_dict[k] = Cluster.load(
                    f['_cluster_'][k], update_sub=update_sub,
                    verbose=verbose)[0]
            if verbose and k in d_dict.keys():
                print(f'{cls.__name__}.load {k:<22}: {time() - start:0.5f}s')

        # Special key '_instance_data_' holds InstanceData data
        for k in instance_data_keys:
            start = time()
            if k in keys_idx:
                d_dict[k] = InstanceData.load(
                    f['_instance_data_'][k], idx=idx, verbose=verbose)
            elif k in keys:
                d_dict[k] = InstanceData.load(
                    f['_instance_data_'][k], verbose=verbose)
            if verbose and k in d_dict.keys():
                print(f'{cls.__name__}.load {k:<22}: {time() - start:0.5f}s')

        # In case RGB is among the keys and is in integer type, convert
        # to float
        for k in ['rgb', 'mean_rgb']:
            if k in d_dict.keys():
                d_dict[k] = to_float_rgb(d_dict[k]) if rgb_to_float \
                    else to_byte_rgb(d_dict[k])

        return cls(**d_dict)

    def estimate_instance_centroid(
            self,
            mode: str = 'iou'
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Estimate the centroid position of each target instance
        object, based on the position of the clusters.

        Based on the hypothesis that clusters are relatively
        instance-pure, we approximate the centroid of each object by
        taking the barycenter of the centroids of the clusters
        overlapping with each object, weighed down by their respective
        IoUs.

        NB: This is a proxy and one could design failure cases, when
        clusters are not pure enough.

        :param mode: str
            Method used to estimate the centroids. 'iou' will weigh down
            the centroids of the clusters overlapping each instance by
            their IoU. 'ratio-product' will use the product of the size
            ratios of the overlap wrt the cluster and wrt the instance.
            'overlap' will use the size of the overlap between the
            cluster and the instance.

        :return obj_pos, obj_idx
            obj_pos: Tensor
                Estimated position for each object
            obj_idx: Tensor
                Corresponding object indices
        """
        if self.obj is None:
            return None, None

        return self.obj.estimate_centroid(self.pos, mode=mode)

    def semantic_segmentation_oracle(
            self,
            num_classes: int,
            *metric_args,
            **metric_kwargs
    ) -> SemanticMetricResults:
        """Compute the oracle performance for semantic segmentation,
        when all nodes predict the dominant label among their points.
        This corresponds to the highest achievable performance with the
        partition at hand.

        This expects one of the following attributes:
          - `Data.obj`: holding node overlaps with instance annotations
          - `Data.y`: holding node label histograms

        :param num_classes: int
            Number of valid classes. By convention, we assume
            `y ∈ [0, num_classes-1]` are VALID LABELS, while
            `y < 0` AND `y >= num_classes` ARE VOID LABELS
        :param metric_args:
            Args for the metrics computation
        :param metric_kwargs:
            Kwargs for the metrics computation

        :return: mIoU, pre-class IoU, OA, mAcc
        """
        # Rely on the InstanceData for computation, if any
        if self.obj is not None:
            return self.obj.semantic_segmentation_oracle(
                num_classes, *metric_args, **metric_kwargs)

        # Return None if no labels
        if getattr(self, 'y', None) is None:
            return

        # We expect the network to predict the most frequent label. For
        # clusters where the dominant label is 'void', we expect the
        # network to predict the second most frequent label. In the
        # event where the cluster is 100% 'void', the metric will ignore
        # the prediction, regardless its value
        pred = self.y[:, :num_classes].argmax(dim=1)
        target = self.y

        # Performance evaluation
        from src.metrics import ConfusionMatrix
        cm = ConfusionMatrix(num_classes, *metric_args, **metric_kwargs)
        cm(pred.cpu(), target.cpu())
        metrics = cm.all_metrics()

        return metrics

    def instance_segmentation_oracle(
            self,
            *metric_args,
            **metric_kwargs
    ) -> InstanceMetricResults:
        """Compute the oracle performance for instance segmentation.
        This is a proxy for the highest achievable performance with the
        cluster partition at hand.

        More precisely, for the oracle prediction:
          - each cluster is assigned to the instance it shares the most
            points with
          - clusters assigned to the same instance are merged into a
            single prediction
          - each predicted instance has a score equal to its IoU with
            the assigned target instance

        This expects the following attributes:
          - `Data.obj`: holding node overlaps with instance annotations

        :param metric_args:
            Args for the metrics computation
        :param metric_kwargs:
            Kwargs for the metrics computation

        :return: InstanceMetricResults
        """
        # Rely on the InstanceData for computation, if any
        if self.obj is not None:
            return self.obj.instance_segmentation_oracle(
                *metric_args, **metric_kwargs)
        return

    def panoptic_segmentation_oracle(
            self,
            *metric_args,
            **metric_kwargs
    ) -> PanopticMetricResults:
        """Compute the oracle performance for panoptic segmentation.
        This is a proxy for the highest achievable performance with the
        cluster partition at hand.

        More precisely, for the oracle prediction:
          - each cluster is assigned to the instance it shares the most
            points with
          - clusters assigned to the same instance are merged into a
            single prediction

        This expects the following attributes:
          - `Data.obj`: holding node overlaps with instance annotations

        :param metric_args:
            Args for the metrics computation
        :param metric_kwargs:
            Kwargs for the metrics computation

        :return: PanopticMetricResults
        """
        # Rely on the InstanceData for computation, if any
        if self.obj is not None:
            return self.obj.panoptic_segmentation_oracle(
                *metric_args, **metric_kwargs)
        return

    def show(self, **kwargs):
        """See `src.visualization.show`."""
        # Local import to avoid import loop errors
        from src.visualization import show
        return show(self, **kwargs)


class Batch(PyGBatch, Data):
    """Inherit from torch_geometric.Batch with extensions tailored to
    our specific needs.

    NB: contrary to PyGBatch's dynamic inheritance behavior, we force
    the explicit inheritance to our Data class, to ensure Batch objects
    share all attributes and methods of our Data class throughout the
    codebase.
    """

    @classmethod
    def from_data_list(
            cls,
            data_list: List[Data],
            follow_batch: Optional[List[str]] = None,
            exclude_keys: Optional[List[str]] = None
    ) -> 'Batch':
        """Overwrite torch_geometric from_data_list to be able to handle
        Cluster and InstanceData objects batching.
        """

        # Local hack to avoid being overflowed with pesky warnings
        # see: https://github.com/pyg-team/pytorch_geometric/issues/4848
        with warnings.catch_warnings():
            warnings.simplefilter("ignore")

            for d in data_list:
                d.raise_if_edge_keys()

            # Little trick to prevent Batch.from_data_list from crashing
            # when some Data objects have edges while others don't
            has = [
                i for i, d in enumerate(data_list) if d.edge_index is not None]
            has_not = [
                i for i, d in enumerate(data_list) if d.edge_index is None]

            if len(has) > 0 and len(has_not) > 0:
                device = data_list[0].device
                edge_index = torch.empty((2, 0), device=device).long()

                if data_list[has[0]].edge_attr is not None:
                    dim = data_list[has[0]].edge_attr.shape[1]
                    edge_attr = torch.empty((0, dim), device=device).long()
                else:
                    edge_attr = None

                for i in has_not:
                    data_list[i].edge_index = edge_index
                    data_list[i].edge_attr = edge_attr

            # PyG way of batching does not recognize some local classes such
            # as Cluster and CSRData, so it will accumulate them in lists
            batch = super().from_data_list(
                data_list, follow_batch=follow_batch, exclude_keys=exclude_keys)

        # PyG does not know how to batch our CSRData objects. So the
        # corresponding attributes will contain lists of such objects.
        # We now need to manually convert these to proper corresponding
        # batch objects.
        # Note we will need to do the same in `get_example` to avoid
        # breaking PyG Batch mechanisms
        for k, v in data_list[0].to_dict().items():
            if isinstance(v, CSRData):
                batch[k] = v.get_batch_class().from_list(batch[k])

        return batch

    def to_data_list(self, strict: bool = False) -> List['Data']:
        """Reconstruct the list of `Data` objects that were passed to
        `Batch.from_data_list()`.

        This extends the behavior of PyG's `to_data_list()` by also
        attempting to infer how to un-collate some attributes that may
        have been added to the `Batch` even after its initial
        construction with `from_data_list()`.
        """
        return [
            self.get_example(i, strict=strict) for i in range(self.num_graphs)]

    def get_example(self, idx: int, strict: bool = False) -> List['Data']:
        """Overwrite torch_geometric get_example to be able to handle
        Cluster and InstanceData objects batching.
        """
        # Try to infer how to un-collate attributes that are not in
        # `self._slice_dict` and `self._inc_dict` (i.e. that were not
        # present when `Batch.from_data_list` was initially called
        self._infer_collation(strict=strict)

        # PyG's `Batch.get_example()` does not handle our custom CSRData
        # objects, so we need to call the to_list() for it to receive a
        # list of objects that it can manage
        bckp_dict = {}
        for k in self.keys:
            if isinstance(self[k], CSRData):
                bckp_dict[k] = self[k].clone()
                self[k] = self[k].to_list()

        data = super().get_example(idx)

        # Little hack because PyG's Batch dynamic inheritance mechanism
        # makes Batch return a PyG Data object here, but we want our
        # Data object instead
        data = Data(**data.to_dict())

        # Restore the CSRData objects in self
        for k, v in bckp_dict.items():
            self[k] = v

        return data

    def _infer_collation(self, strict: bool = False):
        """Populate `self._slice_dict` and `self._inc_dict` with
        inferred collation for missing keys.

        Unlike PyG, we want to handle attributes that may have been
        added to the Batch object even if they were not present yet
        when `Batch.from_data_list()` was initially called. To this
        end, we actively search for attributes that are absent from
        `self._slice_dict` and `self._inc_dict` and, check whether
        they are attributes of type node, edge, or other. Then, based
        on the rules of `self.__cat_dim__` and `self.__inc__`, we can
        identify the desirable batching behavior. Finally, we search
        for other node and edge attributes in `self._slice_dict` and
        `self._inc_dict` to infer the number of subgraphs, and the
        number nodes and edges in each, in order to update
        `self._slice_dict` and `self._inc_dict` with appropriate
        values for the missing keys.
        """
        if not hasattr(self, '_slice_dict'):
            raise RuntimeError(
                ("Cannot reconstruct 'Data' object from 'Batch' because "
                 "'Batch' was not created via 'Batch.from_data_list()'"))

        # Look for keys that are missing from `self._slice_dict`
        all_keys = set(self.keys)
        all_node_keys = set(self.node_attrs())
        all_edge_keys = set(self.edge_attrs())
        all_node_csr_keys = {k for k in all_keys if (
                isinstance(self[k], CSRData)
                and self[k].num_groups == self.num_nodes)}
        all_other_keys = (
                all_keys - all_node_keys - all_edge_keys - all_node_csr_keys)
        slice_keys = set(self._slice_dict.keys())
        slice_node_keys = slice_keys.intersection(all_node_keys)
        slice_edge_keys = slice_keys.intersection(all_edge_keys)
        slice_node_csr_keys = slice_keys.intersection(all_node_csr_keys)
        slice_other_keys = slice_keys.intersection(all_other_keys)
        special_keys = set(['_num_graphs', 'ptr', 'batch'])
        missing_keys = all_keys - slice_keys - special_keys
        missing_node_keys = missing_keys.intersection(all_node_keys)
        missing_edge_keys = missing_keys.intersection(all_edge_keys)
        missing_node_csr_keys = missing_keys.intersection(all_node_csr_keys)
        missing_other_keys = missing_keys.intersection(all_other_keys)

        # Convert sets to lists for convenient indexing
        all_keys = list(all_keys)
        all_node_keys = list(all_node_keys)
        all_edge_keys = list(all_edge_keys)
        all_node_csr_keys = list(all_node_csr_keys)
        all_other_keys = list(all_other_keys)
        slice_keys = list(slice_keys)
        slice_node_keys = list(slice_node_keys)
        slice_edge_keys = list(slice_edge_keys)
        slice_node_csr_keys = list(slice_node_csr_keys)
        slice_other_keys = list(slice_other_keys)
        special_keys = list(special_keys)
        missing_keys = list(missing_keys)
        missing_node_keys = list(missing_node_keys)
        missing_edge_keys = list(missing_edge_keys)
        missing_node_csr_keys = list(missing_node_csr_keys)
        missing_other_keys = list(missing_other_keys)

        # If no node keys can be found `self._slice_dict`, we have no
        # way of inferring how to separate batch items for the missing
        # values. If `strict` is set, we throw an error. If not, we
        # ignore the missing keys that cannot be resolved. As a result,
        # the output of `Batch.to_data_list()` will simply not contain
        # the corresponding attributes
        if len(slice_node_keys) == 0 and len(missing_node_keys) > 0:
            if strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the "
                    f"{self.__class__.__name__} object because none of the "
                    f"node attributes {self.node_attrs()} could be found in "
                    f"`self._slice_dict`. Make sure your `Data` objects have "
                    f"at least one node attribute before collating them with "
                    f"`Batch.from_data_list()`.")
            else:
                missing_node_keys = []

        if len(slice_edge_keys) == 0 and len(missing_edge_keys) > 0:
            if strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the "
                    f"{self.__class__.__name__} object because none of the "
                    f"edge attributes {self.edge_attrs()} could be found in "
                    f"`self._slice_dict`. Make sure your `Data` objects have "
                    f"at least one node attribute before collating them with "
                    f"`Batch.from_data_list()`.")
            else:
                missing_edge_keys = []

        if len(slice_node_keys) == 0 and len(missing_node_csr_keys) > 0:
            if strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the "
                    f"{self.__class__.__name__} object because none of the "
                    f"node attributes {self.node_attrs()} could be found in "
                    f"`self._slice_dict`, which prevents inferring the "
                    f"collation for node attributes carrying CSRData objects: "
                    f"{missing_node_csr_keys}. Make sure your `Data` objects "
                    f"have at least one node attribute before collating them "
                    f"with `Batch.from_data_list()`.")
            else:
                missing_node_keys = []

        if len(missing_other_keys) > 0:
            if strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the "
                    f"{self.__class__.__name__} object because some attributes "
                    f"of type 'other' {missing_other_keys} could be not found "
                    f"in `self._slice_dict`. Make sure all your 'other' "
                    f"attributes (i.e. neither node nor edge attributes) are"
                    f"in your `Data` objects before collating them with "
                    f"`Batch.from_data_list()`.")
            else:
                missing_other_keys = []

        # Recover some useful info about the current collation
        num_graphs = self.num_graphs
        if len(slice_node_keys) > 0:
            node_ptr = self._slice_dict[slice_node_keys[0]]
        else:
            node_ptr = None
        if len(slice_edge_keys) > 0:
            edge_ptr = self._slice_dict[slice_edge_keys[0]]
        else:
            edge_ptr = None

        # Update `self._slice_dict` and `self._inc_dict` for each of the
        # missing node keys
        for k in missing_node_keys:
            if self.__inc__(k, self[k]) == 0:
                self._slice_dict[k] = node_ptr
                self._inc_dict[k] = torch.zeros(
                    num_graphs, dtype=torch.long, device=self.device)
                continue
            elif strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the '{k}' attribute "
                    f"because `self.__inc__('{k}') != 0`. Guessing how to "
                    f"restore the increments for each batch item is ambiguous. "
                    f"To collate and un-collate '{k}', make sure your `Data` "
                    f"objects have the '{k}' attribute before collating them "
                    f"with `Batch.from_data_list()`.")

        # Update `self._slice_dict` and `self._inc_dict` for each of the
        # missing edge keys
        for k in missing_edge_keys:
            if self.__inc__(k, self[k]) == 0:
                self._slice_dict[k] = edge_ptr
                self._inc_dict[k] = torch.zeros(
                    num_graphs, dtype=torch.long, device=self.device)
                continue
            elif strict:
                raise ValueError(
                    f"Cannot infer how to un-collate the '{k}' attribute "
                    f"because `self.__inc__('{k}') != 0`. Guessing how to "
                    f"restore the increments for each batch item is ambiguous. "
                    f"To collate and un-collate '{k}', make sure your `Data` "
                    f"objects have the '{k}' attribute before collating them "
                    f"with `Batch.from_data_list()`.")

        # Update `self._slice_dict` and `self._inc_dict` for each of the
        # missing node keys carrying CSRData. Besides, we make sure the
        # corresponding CSRData is actually a CSRBatch, or the
        # appropriate child class
        for k in missing_node_csr_keys:
            self._slice_dict[k] = torch.arange(
                self.num_graphs + 1, device=self.device)
            self._inc_dict[k] = None

            self[k] = self[k].get_batch_class()(
                self[k].pointers,
                *self[k].values,
                dense=False,
                is_index_value=self[k].is_index_value)

            ref_k = list(set(self._slice_dict).intersection(all_node_keys))[0]
            ptr = self._slice_dict[ref_k]
            self[k].__sizes__ = ptr[1:] - ptr[:-1]

    def save(
            self,
            f: Union[h5py.File, h5py.Group],
            y_to_csr: bool = True,
            pos_dtype: torch.dtype = torch.float,
            fp_dtype: torch.dtype = torch.float):
        """Save Batch to HDF5 file.

        :param f: h5 file path of h5py.File or h5py.Group
        :param y_to_csr: bool
            Convert 'y' to CSR format before saving. Only applies if
            'y' is a 2D histogram
        :param pos_dtype: torch dtype
            Data type to which 'pos' should be cast before saving. The
            reason for this separate treatment of 'pos' is that global
            coordinates may be too large and casting to 'fp_dtype' may
            result in hurtful precision loss
        :param fp_dtype: torch dtype
            Data type to which floating point tensors should be cast
            before saving
        :return:
        """
        if not isinstance(f, (h5py.File, h5py.Group)):
            with h5py.File(f, 'w') as file:
                self.save(
                    file,
                    y_to_csr=y_to_csr,
                    pos_dtype=pos_dtype,
                    fp_dtype=fp_dtype)
            return

        assert isinstance(f, (h5py.File, h5py.Group))

        # Data.save stores Data.keys() but ignores some important
        # attributes necessary for Batch.to_data_list()
        super().save(
            f,
            y_to_csr=y_to_csr,
            pos_dtype=pos_dtype,
            fp_dtype=fp_dtype)

        # Need to additionally save attributes allowing to maintain
        # the batching mechanism throughout serialization
        if hasattr(self, '_slice_dict'):
            save_tensor_dict(self._slice_dict, f, '_slice_dict', fp_dtype=fp_dtype)
        if hasattr(self, '_inc_dict'):
            save_tensor_dict(self._inc_dict, f, '_inc_dict', fp_dtype=fp_dtype)
        if hasattr(self, '_num_graphs'):
            f.create_dataset('_num_graphs', data=np.array([self._num_graphs]))

    @classmethod
    def load(cls, *args, **kwargs) -> Union['Batch', 'Data']:
        """Read an HDF5 file and return its content as a Batch object.

        See Data.load()
        """
        return Data.load(*args, **kwargs)