-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
1243 lines (1059 loc) · 59.3 KB
/
dataset.py
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
# PNN Library: Dataset classes
# Imports
import os
import re
import random
import inspect
import itertools
from enum import Enum, Flag, auto
from collections import namedtuple, Counter
from typing import Callable, Optional, NamedTuple, Tuple, Sequence, Union, Dict, Any
import PIL.Image
import torch.utils.data
from ppyutil.classes import EnumFI
from ppyutil.contextman import ReentrantMeta
####################
### Helper types ###
####################
# Dataset type enumeration
class DatasetType(Enum):
Train = auto() # Training dataset
Valid = auto() # Validation dataset
Test = auto() # Test dataset
# Dataset type tuple base class
__DatasetTuple = namedtuple('DatasetTuple', 'train valid test') # Format: Arbitrary but should be the same for each field
# Dataset type tuple class
class DatasetTuple(__DatasetTuple):
@classmethod
def keys(cls):
return cls._fields
def items(self):
return self._asdict().items()
def get(self, dataset_type):
if dataset_type == DatasetType.Train:
return self.train
elif dataset_type == DatasetType.Valid:
return self.valid
elif dataset_type == DatasetType.Test:
return self.test
else:
raise ValueError(f"Unrecognised dataset type value (should be of type DatasetType enum): {dataset_type}")
# Load stage enumeration
class LoadStage(Enum):
LoadRaw = auto()
Transform = auto()
Tensorize = auto()
# Load stage tuple base class
__LoadStageTuple = namedtuple('LoadStageTuple', 'raw tfrm tensor') # Format: Arbitrary but should be the same for each field
# Load stage tuple class
class LoadStageTuple(__LoadStageTuple):
@classmethod
def keys(cls):
return cls._fields
def items(self):
return self._asdict().items()
def get(self, load_stage):
if load_stage == LoadStage.LoadRaw:
return self.raw
elif load_stage == LoadStage.Transform:
return self.tfrm
elif load_stage == LoadStage.Tensorize:
return self.tensor
else:
raise ValueError(f"Unrecognised load stage value (should be of type LoadStage enum): {load_stage}")
# Resource type enumeration base
class ResourceTypeBase(namedtuple('ResourceType', 'value ext required'), EnumFI):
pass
# Channel type enumeration base
class ChannelTypeBase(namedtuple('ChannelType', 'value count tfrm_raw_deps tfrm_tfrm_deps tensor_tfrm_deps tensor_tensor_deps tensor_range'), EnumFI):
count: int
def __new__(cls, *args, **kwargs):
subbed_args = list(args[:3])
for arg in args[3:]:
if not arg:
subbed_args.append(arg)
else:
subbed_arg = []
for value_tuple in arg:
if isinstance(value_tuple, tuple) and not isinstance(value_tuple, Enum):
value = value_tuple[0]
if isinstance(value, auto):
value = value.value
subbed_arg.append(cls._value2member_map_[value])
else:
subbed_arg.append(value_tuple)
subbed_args.append(tuple(subbed_arg))
# noinspection PyUnresolvedReferences
return super().__new_member__(cls, *subbed_args, **kwargs)
# Channel options enumeration base
class ChannelOptBase(namedtuple('ChannelOpt', 'value stage'), EnumFI):
pass
# Channel specification class
class ChannelSpec(NamedTuple):
type: ChannelTypeBase
opts: Union[Dict[ChannelOptBase, Any], Tuple]
# Data specification class
class DataSpec(NamedTuple):
channels: Sequence[ChannelSpec]
groups: Union[bool, Sequence[Tuple[int, ...]]] # False (no groups), True (all in one group), or sequence of Tuple[int, ...] specifying channel indices to group together
# Channel index class
class ChannelIndex(NamedTuple):
group: int # First group index in which the channel occurs regardless of options
channel: int # Channel index within the group
slice: int # Slice start index within the group
slice_end: int # Slice end index within the group (1 greater than the last index that is actually part of the channel)
count: int # Number of slices that the given channel takes up
# Include-exclude specification tuple
IncludeExclude = namedtuple('IncludeExclude', 'include exclude') # Format: None or Set (falsey => include all found), None or Set (falsey => exclude none)
INCLUDE_EXCLUDE_NONE = IncludeExclude(None, None)
# Input/target kind flag
class IOKind(Flag):
NoKind = 0
Input = auto()
Target = auto()
InputDep = auto()
TargetDep = auto()
# Agenda item tuple classes
RawAgendaItem = namedtuple('RawAgendaItem', 'resource kind opts func') # Format: ChannelSpec[Enum, Tuple], IOKind, Dict[Enum, Any], Callable
TfrmAgendaItem = namedtuple('TfrmAgendaItem', 'channel kind opts raw_deps tfrm_deps func') # Format: ChannelSpec[Enum, Tuple], IOKind, Dict[Enum, Any], Tuple[ChannelSpec[Enum, Tuple], ...], Tuple[ChannelSpec[Enum, Tuple], ...], Callable
TensorizeAgendaItem = namedtuple('TensorizeAgendaItem', 'channel kind opts tfrm_deps tensor_deps func') # Format: ChannelSpec[Enum, Tuple], IOKind, Dict[Enum, Any], Tuple[ChannelSpec[Enum, Tuple], ...], Tuple[ChannelSpec[Enum, Tuple], ...], Callable
###########################
### Data spec functions ###
###########################
# Resolve the grouping of a data specification to a standardised List of Tuple[int, ...], where every channel occurs in at least one group, and there is at least one channel
def resolve_data_spec_groups(data_spec):
if data_spec.groups is False:
actual_groups = [(i,) for i in range(len(data_spec.channels))]
elif data_spec.groups is True:
actual_groups = [tuple(range(len(data_spec.channels)))]
else:
missing_indices = set(range(len(data_spec.channels)))
for group in data_spec.groups:
missing_indices -= set(group)
actual_groups = list(tuple(group) for group in data_spec.groups)
actual_groups.extend((i,) for i in sorted(missing_indices))
resolved_spec = DataSpec(channels=data_spec.channels, groups=actual_groups)
check_data_spec_resolved(resolved_spec)
return resolved_spec
# Check a data specification against the strict channel and resolved groups conditions
def check_data_spec_resolved(data_spec, exception_if_bad=True):
issues = []
if not isinstance(data_spec, DataSpec):
issues.append(f"Object is not of type DataSpec: {data_spec}")
else:
if not data_spec.channels:
issues.append("There are no channels => Need at least one")
for c, channel in enumerate(data_spec.channels):
if not isinstance(channel, ChannelSpec):
issues.append(f"Channel {c} is not of type ChannelSpec: {channel}")
else:
if not isinstance(channel.type, ChannelTypeBase):
issues.append(f"Channel {c} is not of type ChannelTypeBase: {channel.type}")
elif channel.type.tfrm_raw_deps is not None and not all(isinstance(dep, ResourceTypeBase) for dep in channel.type.tfrm_raw_deps):
issues.append(f"Channel {c} has raw-to-transformed dependencies that are not of type ResourceTypeBase: {channel.type.tfrm_raw_deps}")
elif channel.type.tfrm_tfrm_deps is not None and not all(isinstance(dep, ChannelTypeBase) for dep in channel.type.tfrm_tfrm_deps):
issues.append(f"Channel {c} has transformed-to-transformed dependencies that are not of type ChannelTypeBase: {channel.type.tfrm_tfrm_deps}")
elif channel.type.tensor_tfrm_deps is not None and not all(dep is None or isinstance(dep, ChannelTypeBase) for dep in channel.type.tensor_tfrm_deps):
issues.append(f"Channel {c} has transformed-to-tensor dependencies that are not of type ChannelTypeBase: {channel.type.tensor_tfrm_deps}")
elif channel.type.tensor_tensor_deps is not None and not all(isinstance(dep, ChannelTypeBase) for dep in channel.type.tensor_tensor_deps):
issues.append(f"Channel {c} has tensor-to-tensor dependencies that are not of type ChannelTypeBase: {channel.type.tensor_tensor_deps}")
if not isinstance(channel.opts, dict):
issues.append(f"Channel {c} options is not a dict: {channel.opts}")
elif not all(isinstance(opt, ChannelOptBase) for opt in channel.opts):
issues.append(f"Channel {c} has options that are not of type ChannelOptBase: {channel.opts}")
if not isinstance(data_spec.groups, list):
issues.append(f"Resolved groups specification is not a list: {data_spec.groups}")
elif not data_spec.groups:
issues.append(f"Resolved groups specification is empty")
else:
groups_union = set()
for g, group in enumerate(data_spec.groups):
if not isinstance(group, tuple):
issues.append(f"Group {g} is not a tuple: {group}")
elif not group:
issues.append(f"Group {g} is empty")
else:
if not all(isinstance(c, int) for c in group):
issues.append(f"Group {g} has non-int entries: {group}")
groups_union |= set(group)
if groups_union != set(range(len(data_spec.channels))):
issues.append(f"Group specifications do not include all channels, or include non-existent channels: {groups_union}")
if issues and exception_if_bad:
raise ValueError("DataSpec does not meet the strict channel and resolved groups conditions:\n - " + '\n - '.join(issues))
return issues
# Generate a channel map from a resolved data specification
def generate_channel_map(data_spec):
# data_spec = Resolved data specification
# Return a Dict[dataset.ChannelTypeBase, dataset.ChannelIndex] that maps every specified channel to its location in the final group
channel_map = {}
for g, group in enumerate(data_spec.groups):
slice_index = 0
for c, channel in enumerate(group):
channel_type = data_spec.channels[channel].type
if channel_type not in channel_map:
channel_map[channel_type] = ChannelIndex(group=g, channel=c, slice=slice_index, slice_end=slice_index + channel_type.count, count=channel_type.count)
slice_index += channel_type.count
return channel_map
# Ungroup data based on a resolved data specification
def ungroup_data(data_spec, data, index):
# data_spec = Resolved data specification
# data = Data that follows the given data specification
# index = Index/slice to select for the first dimension of each encountered tensor (None => First dimension is already the channel dimension, Negative int => Select everything)
# Return the ungrouped data in the format Dict[ChannelSpec (flattened), Tensor]
ungrouped_data = {}
for g, group_indices in enumerate(data_spec.groups):
group_data = data[g]
slice_index = 0
for channel_index in group_indices:
channel_spec = data_spec.channels[channel_index]
channel_slice = slice(slice_index, slice_index + channel_spec.type.count)
slice_index += channel_spec.type.count
if index is None:
channel_data = group_data[channel_slice, ...]
elif index < 0:
channel_data = group_data[:, channel_slice, ...]
else:
channel_data = group_data[index, channel_slice, ...]
channel_spec_flat = ChannelSpec(channel_spec.type, tuple(sorted(channel_spec.opts.items())))
ungrouped_data[channel_spec_flat] = channel_data
return ungrouped_data
######################
### Staged dataset ###
######################
# Staged dataset class
class StagedDataset(torch.utils.data.Dataset):
#
# Construction
#
def __init__(self, root_dir, dataset_type, reqd_inputs, reqd_targets, limit_to=None, limit_hard=False, limit_sorted=False, limit_seed=0, load_kwargs=None):
# root_dir = Dataset root directory path (ROOT)
# dataset_type = Type of dataset (DatasetType enum = Train, Valid, Test)
# reqd_inputs = Required model inputs (expects resolved dataset.DataSpec)
# reqd_targets = Required model targets (expects resolved dataset.DataSpec)
# limit_to = If a positive integer is provided, limit the dataset by allowing only limit_to samples (see the remaining limit_* arguments for options as to how this is to be implemented)
# limit_hard = If true, truncate the dataset to limit_to samples, otherwise repeat samples as often as necessary to still have the same original dataset size
# limit_sorted = Whether to preserve sample order when limiting the dataset
# limit_seed = Integer seed to use for the deterministic selection of samples to limit to
# load_kwargs = Extra keyword arguments to pass to the load_samples method
self.root = os.path.expanduser(root_dir)
if not os.path.isdir(self.root):
raise NotADirectoryError(f"Specified root path is not a valid directory: {self.root}")
self.dataset_type = dataset_type
self.reqd_inputs = reqd_inputs
self.reqd_targets = reqd_targets
self.agenda, self.input_group_specs, self.target_group_specs, reqd_res_types = self.process_reqd_channels(self.reqd_inputs, self.reqd_targets, LoadStageTuple(raw=self.get_raw_func, tfrm=self.get_tfrm_func, tensor=self.get_tensorize_func))
self.target_tensor_types = {item.channel.type for item in self.agenda.tensor if IOKind.Target in item.kind}
self.samples, self.load_details = self.load_samples(reqd_res_types, **(load_kwargs or {}))
self.total_samples = len(self.samples)
self._extra_data_indices = None
self.limit_to = min(int(limit_to), self.total_samples) if limit_to and limit_to >= 1 else 0
self.limit_hard = limit_hard
self.limit_sorted = limit_sorted
self.limit_random = random.Random(limit_seed)
if self.limit_to:
self.limited_indices = self.limit_random.sample(range(self.total_samples), self.limit_to)
if self.limit_sorted:
self.limited_indices.sort()
else:
self.limited_indices = None
self.sample_size = self.limit_to if self.limit_to and self.limit_hard else self.total_samples
self.init_dataset(self)
@staticmethod
def process_reqd_channels(reqd_inputs: DataSpec, reqd_targets, get_funcs):
# reqd_inputs = Required model inputs (expects resolved dataset.DataSpec)
# reqd_targets = Required model targets (expects resolved dataset.DataSpec)
# get_funcs = LoadStageTuple of Callables used to get the required stage functions
# Return LoadStageTuple agenda, input groups specification, target groups specification, set of required resource types
input_group_specs = tuple(tuple(ChannelSpec(reqd_inputs.channels[index].type, tuple(sorted(reqd_inputs.channels[index].opts.items()))) for index in group) for group in reqd_inputs.groups)
target_group_specs = tuple(tuple(ChannelSpec(reqd_targets.channels[index].type, tuple(sorted(reqd_targets.channels[index].opts.items()))) for index in group) for group in reqd_targets.groups)
input_tensor_channels, input_dep_tensor_channels = StagedDataset._resolve_tensor_channels(input_group_specs)
target_tensor_channels, target_dep_tensor_channels = StagedDataset._resolve_tensor_channels(target_group_specs)
reqd_tensor_channels = input_dep_tensor_channels | target_dep_tensor_channels
tensorize_agenda = []
tensorized_channels = set()
tfrm_channel_kind = {}
while reqd_tensor_channels:
num_left = len(reqd_tensor_channels)
for channel in sorted(reqd_tensor_channels):
if channel.type.tensor_tfrm_deps is None or channel.type.tensor_tensor_deps is None:
raise ValueError(f"A specified channel type is invalid as a tensorized value: {channel.type}")
tfrm_opts = tuple(opt for opt in channel.opts if opt[0].stage.value <= LoadStage.Transform.value)
tensor_deps = tuple(ChannelSpec(dep, channel.opts) for dep in channel.type.tensor_tensor_deps)
if all(dep in tensorized_channels for dep in tensor_deps):
reqd_tensor_channels.remove(channel)
tensorized_channels.add(channel)
tfrm_deps = tuple(ChannelSpec(dep or channel.type, tfrm_opts) for dep in channel.type.tensor_tfrm_deps)
kind_dep = (IOKind.InputDep if channel in input_dep_tensor_channels else IOKind.NoKind) | (IOKind.TargetDep if channel in target_dep_tensor_channels else IOKind.NoKind)
kind = (IOKind.Input if channel in input_tensor_channels else IOKind.NoKind) | (IOKind.Target if channel in target_tensor_channels else IOKind.NoKind) | kind_dep
tensorize_agenda.append(TensorizeAgendaItem(channel=channel, kind=kind, opts=dict(channel.opts), tfrm_deps=tfrm_deps, tensor_deps=tensor_deps, func=get_funcs.tensor(channel.type)))
for dep_channel in tfrm_deps:
tfrm_channel_kind[dep_channel] = tfrm_channel_kind.get(dep_channel, IOKind.NoKind) | kind_dep
if len(reqd_tensor_channels) == num_left:
raise ValueError("There is some cycle or bad value(s) in the tensor-to-tensor dependencies")
if not tensorize_agenda:
raise ValueError("Should have at least one tensorize agenda step")
reqd_tfrm_channels = set()
tfrm_channels_to_process = set(tfrm_channel_kind.keys())
while tfrm_channels_to_process:
tfrm_channel = tfrm_channels_to_process.pop()
if tfrm_channel not in reqd_tfrm_channels:
reqd_tfrm_channels.add(tfrm_channel)
if tfrm_channel.type.tfrm_tfrm_deps:
for channel_type in tfrm_channel.type.tfrm_tfrm_deps:
dep_channel = ChannelSpec(channel_type, tfrm_channel.opts)
tfrm_channels_to_process.add(dep_channel)
tfrm_channel_kind[dep_channel] = tfrm_channel_kind.get(dep_channel, IOKind.NoKind) | tfrm_channel_kind[tfrm_channel]
tfrm_agenda = []
tfrmed_channels = set()
raw_resource_kind = {}
while reqd_tfrm_channels:
num_left = len(reqd_tfrm_channels)
for channel in sorted(reqd_tfrm_channels):
if channel.type.tfrm_raw_deps is None or channel.type.tfrm_tfrm_deps is None:
raise ValueError(f"A specified channel type is invalid as a transformed value: {channel.type}")
raw_opts = tuple(opt for opt in channel.opts if opt[0].stage.value <= LoadStage.LoadRaw.value)
tfrm_deps = tuple(ChannelSpec(dep, channel.opts) for dep in channel.type.tfrm_tfrm_deps)
if all(dep in tfrmed_channels for dep in tfrm_deps):
reqd_tfrm_channels.remove(channel)
tfrmed_channels.add(channel)
raw_deps = tuple(ChannelSpec(dep, raw_opts) for dep in channel.type.tfrm_raw_deps)
kind = tfrm_channel_kind[channel]
tfrm_agenda.append(TfrmAgendaItem(channel=channel, kind=kind, opts=dict(channel.opts), raw_deps=raw_deps, tfrm_deps=tfrm_deps, func=get_funcs.tfrm(channel.type)))
for dep_resource in raw_deps:
raw_resource_kind[dep_resource] = raw_resource_kind.get(dep_resource, IOKind.NoKind) | kind
if len(reqd_tfrm_channels) == num_left:
raise ValueError("There is some cycle or bad value(s) in the transformed-to-transformed dependencies")
if not tfrm_agenda:
raise ValueError("Should have at least one transform agenda step")
raw_agenda = []
for resource, kind in sorted(raw_resource_kind.items()):
raw_agenda.append(RawAgendaItem(resource=resource, kind=kind, opts=dict(resource.opts), func=get_funcs.raw(resource.type)))
if not raw_agenda:
raise ValueError("Should have at least one raw agenda step")
reqd_res_types = set(resource.type for resource in raw_resource_kind.keys())
if not reqd_res_types:
raise ValueError("Should have at least one required resource type")
return LoadStageTuple(raw_agenda, tfrm_agenda, tensorize_agenda), input_group_specs, target_group_specs, reqd_res_types
@staticmethod
def _resolve_tensor_channels(group_specs):
tensor_channels = {channel for channel_tuple in group_specs for channel in channel_tuple}
tensor_channels_to_process = tensor_channels.copy()
dep_tensor_channels = set()
while tensor_channels_to_process:
tensor_channel = tensor_channels_to_process.pop()
if tensor_channel not in dep_tensor_channels:
dep_tensor_channels.add(tensor_channel)
if tensor_channel.type.tensor_tensor_deps:
for channel_type in tensor_channel.type.tensor_tensor_deps:
tensor_channels_to_process.add(ChannelSpec(channel_type, tensor_channel.opts))
return tensor_channels, dep_tensor_channels
def get_agenda(self, stage=None):
if stage is None:
return self.agenda
else:
return self.agenda.get(stage)
def load_samples(self, reqd_res_types, **kwargs):
# reqd_res_types = Set of required resource types
# kwargs = Further required keyword arguments
# Return a deterministically sorted list of the samples, where each entry in the list is Tuple[sample_key, Dict[res_type, path]], and a list of string details about the loaded samples
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
def get_load_details(self):
return self.load_details
#
# Get samples
#
def __getitem__(self, index):
# index = Index of the sample to retrieve
# Return same as get_sample() for the required sample index
return self.get_sample(index=index)
def __len__(self):
# Return number of samples
return self.sample_size
def rand_index(self):
# Return a valid random sample index
return random.randrange(self.sample_size)
def resolve_index(self, index):
# index = Sample index to wrap to valid range, or if None choose a valid random index
# Return an index guaranteed to be valid
if index is None:
return self.rand_index()
elif isinstance(index, int):
return index % self.sample_size
else:
return self.resolve_key(index)
def resolve_key(self, sample_key):
# sample_key = Sample key to search for in the samples list (raises an exception if not found)
try:
return next(index for index, entry in enumerate(self.samples) if entry[0] == sample_key)
except StopIteration:
raise KeyError(f"Sample key not found: {sample_key}")
def get_sample_entry(self, index, dset=None):
# index = Index of the sample entry to retrieve
# dset = Dataset that this request originated from
# Return the sample entry in the format Tuple[sample_key, Dict[res_type, path]]
dset_is_self = dset is None or dset is self
if index is None or index < 0 or index >= self.total_samples or (dset_is_self and index >= self.sample_size):
raise IndexError()
elif dset_is_self and self.limited_indices:
index = self.limited_indices[index % self.limit_to]
return self.samples[index]
def get_sample_raw(self, index=None, sample_entry=None, dset=None, **kwargs):
# index = Index of the raw sample to retrieve
# sample_entry = Sample entry to load the raw data for
# dset = Dataset that this request originated from
# kwargs = Keyword arguments to pass up the chain of get methods
# Return same as get_sample_entry(), raw data as Dict[resource, value]
if dset is None:
dset = self
if sample_entry is None:
# noinspection PyArgumentList
sample_entry = self.get_sample_entry(index=index, dset=dset, **kwargs)
self.prepare_raw_funcs(dset, sample_entry)
sample_key, res_path_dict = sample_entry
raw_data = {item.resource: item.func(dset=dset, key=sample_key, path=res_path_dict.get(item.resource.type), opts=item.opts) for item in self.agenda.raw}
return sample_entry, raw_data
def get_sample_tfrmed(self, index=None, sample_raw=None, dset=None, **kwargs):
# index = Index of the transformed sample to retrieve
# sample_raw = Raw sample to transform
# dset = Dataset that this request originated from
# kwargs = Keyword arguments to pass up the chain of get methods (e.g. sample_entry)
# Return same as get_sample_raw(), transformed data as Dict[channel, value]
if dset is None:
dset = self
if sample_raw is None:
sample_raw = self.get_sample_raw(index=index, dset=dset, **kwargs)
self.prepare_tfrm_funcs(dset, sample_raw)
raw_data = sample_raw[-1]
tfrmed_data = {}
for item in self.agenda.tfrm:
tfrmed_data[item.channel] = item.func(dset=dset, raw_data={resource.type: raw_data[resource] for resource in item.raw_deps}, tfrmed_data={channel.type: tfrmed_data[channel] for channel in item.tfrm_deps}, opts=item.opts)
return *sample_raw, tfrmed_data
def get_sample_tensor(self, index=None, sample_tfrmed=None, dset=None, **kwargs):
# index = Index of the tensorized sample to retrieve
# sample_tfrmed = Transformed sample to tensorize
# dset = Dataset that this request originated from
# kwargs = Keyword arguments to pass up the chain of get methods (e.g. sample_raw)
# Return same as get_sample_tfrmed(), tensor data as Dict[channel, value]
if dset is None:
dset = self
if sample_tfrmed is None:
sample_tfrmed = self.get_sample_tfrmed(index=index, dset=dset, **kwargs)
self.prepare_tensorize_funcs(dset, sample_tfrmed)
tfrmed_data = sample_tfrmed[-1]
tensor_data = {}
for item in self.agenda.tensor:
tensor_data[item.channel] = item.func(dset=dset, tfrmed_data={channel.type: tfrmed_data[channel] for channel in item.tfrm_deps}, tensor_data={channel.type: tensor_data[channel] for channel in item.tensor_deps}, opts=item.opts)
return *sample_tfrmed, tensor_data
def get_sample_grouped(self, index=None, sample_tensor=None, dset=None, batchify=False, **kwargs):
# index = Index of the grouped sample to retrieve
# sample_tensor = Tensorized sample to finalise
# dset = Dataset that this request originated from
# batchify = Whether to return sample data as a size 1 batch
# kwargs = Keyword arguments to pass up the chain of get methods (e.g. sample_tfrmed)
# Return same as get_sample_tensor(), grouped input data as tuple of tensor values, grouped target data as tuple of tensor values
if sample_tensor is None:
sample_tensor = self.get_sample_tensor(index=index, dset=dset, **kwargs)
tensor_data = sample_tensor[-1]
input_data = self._group_sample(tensor_data, self.input_group_specs, batchify)
target_data = self._group_sample(tensor_data, self.target_group_specs, batchify)
if batchify:
return *((data,) for data in sample_tensor), input_data, target_data
else:
return *sample_tensor, input_data, target_data
def get_sample(self, index=None, sample_grouped=None, dset=None, **kwargs):
# index = Index of the sample to retrieve
# sample_grouped = Grouped sample to return the input/target data for
# dset = Dataset that this request originated from
# kwargs = Keyword arguments to pass up the chain of get methods (e.g. sample_tensor, batchify)
# Return sample input data, sample target data, and the data associated with any configured extra indices
if sample_grouped is None:
sample_grouped = self.get_sample_grouped(index=index, dset=dset, **kwargs)
# noinspection PyProtectedMember
extra_data_indices = dset._extra_data_indices if dset is not None else self._extra_data_indices
if extra_data_indices is None:
return sample_grouped[-2:]
else:
return tuple(sample_grouped[index] for index in itertools.chain((-2, -1), extra_data_indices))
@staticmethod
def _group_sample(tensor_data, group_specs, batchify):
# tensor_data = Dict of tensor values
# group_specs = Tuple[Tuple[ChannelSpec[Enum, Tuple], ...], ...]
# batchify = Whether to return the grouped sample as a size 1 batch
group_generator = (torch.cat(tuple(tensor_data[channel] for channel in group_spec), dim=0) if len(group_spec) > 1 else tensor_data[group_spec[0]] for group_spec in group_specs)
return tuple(tensor.unsqueeze(0) for tensor in group_generator) if batchify else tuple(group_generator)
def get_extra_data_indices(self):
return self._extra_data_indices
def set_extra_data_indices(self, extra_data_indices):
self._extra_data_indices = extra_data_indices
def extra_data_indices(self, extra_data_indices):
return self.ExtraDataIndicesCM(self, extra_data_indices)
class ExtraDataIndicesCM(metaclass=ReentrantMeta):
def __init__(self, dataset, extra_data_indices):
self._dataset = dataset
self._old_extra_data_indices = self._dataset.get_extra_data_indices()
self._new_extra_data_indices = extra_data_indices
def __enter__(self):
self._dataset.set_extra_data_indices(self._new_extra_data_indices)
return self._dataset
def __exit__(self, exc_type, exc_val, exc_tb):
self._dataset.set_extra_data_indices(self._old_extra_data_indices)
return False
#
# Override interface
#
def init_dataset(self, dset):
# dset = Dataset that this request originated from
pass
def get_res_type(self, name):
# name = Name to retrieve the corresponding resource type of
# Return the corresponding resource type or None if no such resource type exists
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
def get_raw_func(self, res_type):
# res_type = Desired resource type to load
# Return a raw loader callable to be used like raw_func(dset=StagedDataset, key=Any, path=str, opts=Dict[Enum]) that returns the raw loaded data for the given resource type
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
def get_tfrm_func(self, channel_type):
# channel_type = Desired channel type to transform
# Return a transform callable to be used like tfrm_func(dset=StagedDataset, raw_data=Dict[Enum], tfrmed_data=Dict[Enum], opts=Dict[Enum]) that returns the transformed data for the given channel type
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
def get_tensorize_func(self, channel_type):
# channel_type = Desired channel type to tensorize
# Return a tensorize callable to be used like tensorize_func(dset=StagedDataset, tfrmed_data=Dict[Enum], tensor_data=Dict[Enum], opts=Dict[Enum]) that returns the tensor data for the given channel type
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
def prepare_raw_funcs(self, dset, sample_entry):
# dset = Dataset that this request originated from
# sample_entry = Sample entry that needs to be loaded by the raw funcs
pass
def prepare_tfrm_funcs(self, dset, sample_raw):
# dset = Dataset that this request originated from
# sample_raw = Raw sample data that needs to be transformed by the tfrm funcs
pass
def prepare_tensorize_funcs(self, dset, sample_tfrmed):
# dset = Dataset that this request originated from
# sample_tfrmed = Transformed sample data that needs to be tensorized by the tensorize funcs
pass
def annotate_raw(self, resource, value, raw_data, dset=None):
# resource = Resource that should be annotated (ChannelSpec)
# value = Value of the specified resource to modify in-place (will generally be a copy or modified/converted version of the corresponding raw value in raw_data)
# raw_data = All raw data for the sample (source data for the annotation)
# dset = Dataset that this request originated from (None => self)
pass
def annotate_tfrmed(self, channel, value, tfrmed_data, dset=None):
# channel = Channel that should be annotated (ChannelSpec)
# value = Value of the specified channel to modify in-place (will generally be a copy or modified/converted version of the corresponding transformed value in tfrmed_data)
# tfrmed_data = All transformed data for the sample (source data for the annotation)
# dset = Dataset that this request originated from (None => self)
pass
def annotate_tensor(self, channel, value, tfrmed_data, tensor_data, dset=None):
# channel = Channel that should be annotated (ChannelSpec)
# value = Value of the specified channel to modify in-place (will generally be a copy or modified/converted version of the corresponding transformed/tensorized value in tfrmed_data/tensor_data)
# tfrmed_data = All transformed data for the sample (source data for the annotation)
# tensor_data = All tensorized data for the sample (source data for the annotation)
# dset = Dataset that this request originated from (None => self)
pass
# Viewable staged dataset class
# noinspection PyAbstractClass
class ViewableStagedDataset(StagedDataset):
#
# Construction
#
def __init__(self, *args, partition_limit_to=None, partition_ratios=DatasetTuple(0.7, 0.15, 0.15), partition_seed=0, **kwargs):
# args = See StagedDataset.__init__()
# partition_limit_to = DatasetTuple of non-negative sample count limits (see the remaining limit_* arguments for options as to how this is to be implemented)
# partition_ratios = DatasetTuple of ratios in which to partition the dataset into TVS parts (does not need to sum to 1, but each component should be non-negative)
# partition_seed = Integer seed to use for the deterministic random partitioning into TVS parts of the dataset
# kwargs = See StagedDataset.__init__()
super().__init__(*args, **kwargs)
self.partition_limit_to = DatasetTuple(
train=int(partition_limit_to.train) if partition_limit_to and partition_limit_to.train >= 1 else 0,
valid=int(partition_limit_to.valid) if partition_limit_to and partition_limit_to.valid >= 1 else 0,
test=int(partition_limit_to.test) if partition_limit_to and partition_limit_to.test >= 1 else 0,
)
self.partition_indices, self.partition_sizes = self.partition_samples(partition_ratios, partition_seed)
def partition_samples(self, partition_ratios, partition_seed):
# partition_ratios = DatasetTuple of ratios in which to partition the dataset into TVS parts (does not need to sum to 1, but each component should be non-negative)
# partition_seed = Integer seed to use for the deterministic random partitioning into TVS parts of the dataset
# Return DatasetTuple of partition indices, DatasetTuple of partition sizes (after limiting)
if partition_ratios.train < 0 or partition_ratios.valid < 0 or partition_ratios.test < 0:
raise ValueError("Partition ratios must all be non-negative")
ratio_sum = partition_ratios.train + partition_ratios.valid + partition_ratios.test
if ratio_sum <= 0:
raise ValueError("Partition ratio sum must be positive")
ratio_valid = partition_ratios.valid / ratio_sum
ratio_test = partition_ratios.test / ratio_sum
num_valid = round(ratio_valid * self.total_samples)
num_test = round(ratio_test * self.total_samples)
num_train = self.total_samples - num_valid - num_test
if num_train < 0:
num_train = 0
num_test = self.total_samples - num_valid
assert num_train + num_valid + num_test == self.total_samples and num_train >= 0 and num_valid >= 0 and num_test >= 0
indices_train = []
indices_valid = []
indices_test = []
num_train_left = num_train
num_valid_left = num_valid
num_test_left = num_test
partition_random = random.Random(partition_seed)
for index in range(self.total_samples):
prob = partition_random.random()
num_samples_left = self.total_samples - index
if prob < num_train_left / num_samples_left:
indices_train.append(index)
num_train_left -= 1
elif prob < (num_train_left + num_valid_left) / num_samples_left:
indices_valid.append(index)
num_valid_left -= 1
else:
indices_test.append(index)
num_test_left -= 1
assert len(indices_train) == num_train and len(indices_valid) == num_valid and len(indices_test) == num_test
if self.partition_limit_to.train >= 1:
indices_train = self.limit_random.sample(indices_train, min(self.partition_limit_to.train, num_train))
if self.limit_sorted:
indices_train.sort()
if self.limit_hard:
num_train = len(indices_train)
if self.partition_limit_to.valid >= 1:
indices_valid = self.limit_random.sample(indices_valid, min(self.partition_limit_to.valid, num_valid))
if self.limit_sorted:
indices_valid.sort()
if self.limit_hard:
num_valid = len(indices_valid)
if self.partition_limit_to.test >= 1:
indices_test = self.limit_random.sample(indices_test, min(self.partition_limit_to.test, num_test))
if self.limit_sorted:
indices_test.sort()
if self.limit_hard:
num_test = len(indices_test)
return DatasetTuple(indices_train, indices_valid, indices_test), DatasetTuple(num_train, num_valid, num_test)
def create_views(self):
# Return a DatasetTuple of newly created views
return DatasetTuple(
self.create_view_by_indices(DatasetType.Train, self.partition_indices.train, sample_size=self.partition_sizes.train),
self.create_view_by_indices(DatasetType.Valid, self.partition_indices.valid, sample_size=self.partition_sizes.valid),
self.create_view_by_indices(DatasetType.Test, self.partition_indices.test, sample_size=self.partition_sizes.test)
)
def create_view(self, dataset_type):
# dataset_type = Required view
return self.create_view_by_indices(dataset_type, self.partition_indices.get(dataset_type), sample_size=self.partition_sizes.get(dataset_type))
def create_view_by_indices(self, dataset_type, indices, sample_size=None):
# dataset_type = Required dataset view type (DatasetType enum = Train, Valid, Test)
# indices = List of indices to include in the view
# sample_size = Number of indices to truncate or extend the view to
# Return the newly created view
dset = StagedDatasetView(self, dataset_type, indices, sample_size=sample_size)
self.init_dataset(dset)
return dset
# Staged dataset view class
# noinspection PyAbstractClass
class StagedDatasetView(StagedDataset):
#
# Construction
#
# noinspection PyMissingConstructor
def __init__(self, dataset, dataset_type, sample_indices, sample_size=None):
# dataset = StagedDataset instance this is a view of
# dataset_type = Required dataset view type (DatasetType enum = Train, Valid, Test)
# sample_indices = List[int] specifying the required sample indices in the main dataset
# sample_size = Number of indices to truncate or extend the view to
self.dataset = dataset
self.root = self.dataset.root
self.dataset_type = dataset_type
self.reqd_inputs = self.dataset.reqd_inputs
self.reqd_targets = self.dataset.reqd_targets
self.input_group_specs = self.dataset.input_group_specs
self.target_group_specs = self.dataset.target_group_specs
self.agenda = self.dataset.agenda
self.target_tensor_types = self.dataset.target_tensor_types
self.load_details = self.dataset.load_details
self._extra_data_indices = None
self.sample_indices = sample_indices
self.sample_size = len(self.sample_indices) if sample_size is None else sample_size
if self.sample_size < len(self.sample_indices):
self.sample_indices = self.sample_indices[:self.sample_size]
self.actual_size = len(self.sample_indices)
#
# Get samples
#
def resolve_key(self, sample_key):
try:
return next(view_index for view_index, main_index in enumerate(self.sample_indices) if self.dataset.samples[main_index][0] == sample_key)
except StopIteration:
raise KeyError(f"Sample key not found: {sample_key}")
def get_sample_entry(self, index, dset=None):
return self.dataset.get_sample_entry(self._convert_index(index), dset=dset or self)
def get_sample_raw(self, index=None, sample_entry=None, dset=None, **kwargs):
return self.dataset.get_sample_raw(index=self._convert_index(index), sample_entry=sample_entry, dset=dset or self, **kwargs)
def get_sample_tfrmed(self, index=None, sample_raw=None, dset=None, **kwargs):
return self.dataset.get_sample_tfrmed(index=self._convert_index(index), sample_raw=sample_raw, dset=dset or self, **kwargs)
def get_sample_tensor(self, index=None, sample_tfrmed=None, dset=None, **kwargs):
return self.dataset.get_sample_tensor(index=self._convert_index(index), sample_tfrmed=sample_tfrmed, dset=dset or self, **kwargs)
def get_sample_grouped(self, index=None, sample_tensor=None, dset=None, batchify=False, **kwargs):
return self.dataset.get_sample_grouped(index=self._convert_index(index), sample_tensor=sample_tensor, dset=dset or self, batchify=batchify, **kwargs)
def get_sample(self, index=None, sample_grouped=None, dset=None, **kwargs):
return self.dataset.get_sample(index=self._convert_index(index), sample_grouped=sample_grouped, dset=dset or self, **kwargs)
def _convert_index(self, index):
if index is None:
return None
elif index < 0 or index >= self.sample_size:
raise IndexError()
return self.sample_indices[index % self.actual_size]
#
# Override interface
#
def init_dataset(self, dset):
self.dataset.init_dataset(dset)
def get_res_type(self, name):
return self.dataset.get_res_type(name)
def get_raw_func(self, res_type):
return self.dataset.get_raw_func(res_type)
def get_tfrm_func(self, channel_type):
return self.dataset.get_tfrm_func(channel_type)
def get_tensorize_func(self, channel_type):
return self.dataset.get_tensorize_func(channel_type)
def prepare_raw_funcs(self, dset, sample_entry):
self.dataset.prepare_raw_funcs(dset, sample_entry)
def prepare_tfrm_funcs(self, dset, sample_raw):
self.dataset.prepare_tfrm_funcs(dset, sample_raw)
def prepare_tensorize_funcs(self, dset, sample_tfrmed):
self.dataset.prepare_tensorize_funcs(dset, sample_tfrmed)
def annotate_raw(self, resource, value, raw_data, dset=None):
self.dataset.annotate_raw(resource, value, raw_data, dset=dset or self)
def annotate_tfrmed(self, channel, value, tfrmed_data, dset=None):
self.dataset.annotate_tfrmed(channel, value, tfrmed_data, dset=dset or self)
def annotate_tensor(self, channel, value, tfrmed_data, tensor_data, dset=None):
self.dataset.annotate_tensor(channel, value, tfrmed_data, tensor_data, dset=dset or self)
####################
### Flat dataset ###
####################
# Flat dataset class
# noinspection PyAbstractClass
class FlatDataset(StagedDataset):
#
# Construction
#
def __init__(self, root_dir, dataset_type, res_type, reqd_inputs, reqd_targets, limit_to=None, limit_hard=False, limit_sorted=False, limit_seed=0):
# root_dir = Path of directory to load data from
# dataset_type = Type of dataset (DatasetType enum = Train, Valid, Test)
# res_type = Resource type of the files to load from the root directory
# reqd_inputs = Required model inputs (expects resolved dataset.DataSpec)
# reqd_targets = Required model targets (expects resolved dataset.DataSpec)
# limit_to = If a positive integer is provided, limit the dataset by allowing only limit_to samples (see the remaining limit_* arguments for options as to how this is to be implemented)
# limit_hard = If True, truncate the dataset to limit_to samples, otherwise repeat samples as often as necessary to still have the same original dataset size
# limit_sorted = Whether to preserve sample order when limiting the dataset
# limit_seed = Integer seed to use for the deterministic selection of samples to limit to
self.res_type = res_type
self.preloaded_data = {}
super().__init__(root_dir, dataset_type, reqd_inputs, reqd_targets, limit_to=limit_to, limit_hard=limit_hard, limit_sorted=limit_sorted, limit_seed=limit_seed)
# noinspection PyMethodOverriding
def load_samples(self, reqd_res_types):
# reqd_res_types = Set of required resource types
# Return a deterministically sorted list of the samples, where each entry in the list is Tuple[sample_key, Dict[res_type, path]], and a list of string details about the loaded samples
check_file_func = self.get_check_file_func(self.res_type)
# noinspection PyUnresolvedReferences
samples = sorted((entry.name, {self.res_type: entry.path}) for entry in os.scandir(self.root) if entry.is_file() and entry.name.lower().endswith(self.res_type.ext) and (check_file_func is None or check_file_func(entry)))
for res_type in reqd_res_types:
if res_type != self.res_type:
self.preloaded_data[res_type] = self.preload_resource(res_type)
return samples, []
#
# Override interface
#
# noinspection PyMethodMayBeStatic, PyUnusedLocal
def get_check_file_func(self, res_type) -> Optional[Callable]:
# res_type = Resource type that the file should be checked for
# Return None or a check file callable to be used like check_file(file=os.DirEntry)
return None
def preload_resource(self, res_type):
# res_type = Resource type that should be preloaded
# Return the preloaded resource value
raise NotImplementedError(f"Class {self.__class__.__name__} has not implemented the {inspect.currentframe().f_code.co_name}() function")
############################
### Subset-based dataset ###
############################
# Subset-based dataset class
# noinspection PyAbstractClass, PyUnresolvedReferences
class SubsetDataset(ViewableStagedDataset):
# The assumed format of the dataset folder is:
# - Samples: ROOT/SAMPLES_DIR/RESOURCE/SUBSET/*.EXT
# - Blacklists: ROOT/BLACKLISTS_DIR/BLACKLIST/SUBSET/*BLACKLIST_EXT
SAMPLES_DIR = 'samples'
BLACKLISTS_DIR = 'blacklists'
BLACKLIST_EXT = '.list'
#
# Construction
#
def __init__(self, root_dir, reqd_inputs, reqd_targets, limit_to=None, limit_hard=False, limit_sorted=False, limit_seed=0, partition_limit_to=None, partition_ratios=DatasetTuple(0.7, 0.15, 0.15), partition_seed=0, res_suffix_inex_map=None, subsets_inex=INCLUDE_EXCLUDE_NONE, blacklist_type_inex=INCLUDE_EXCLUDE_NONE, blacklist_name_inex=INCLUDE_EXCLUDE_NONE, sample_regex_inex=INCLUDE_EXCLUDE_NONE):
# root_dir = Dataset root directory path (ROOT)
# reqd_inputs = Required model inputs (expects resolved dataset.DataSpec)
# reqd_targets = Required model targets (expects resolved dataset.DataSpec)
# limit_to = If a positive integer is provided, limit the dataset by allowing only limit_to samples (see the remaining limit_* arguments for options as to how this is to be implemented)
# limit_hard = If True, truncate the dataset to limit_to samples, otherwise repeat samples as often as necessary to still have the same original dataset size
# limit_sorted = Whether to preserve sample order when limiting the dataset
# limit_seed = Integer seed to use for the deterministic selection of samples to limit to
# partition_limit_to = DatasetTuple of non-negative sample count limits (see the remaining limit_* arguments for options as to how this is to be implemented)
# partition_ratios = DatasetTuple of ratios in which to partition the dataset into TVS parts (does not need to sum to 1, but each component should be non-negative)
# partition_seed = Integer seed to use for the deterministic random partitioning into TVS parts of the dataset
# res_suffix_inex_map = Dict[res_type, IncludeExclude] of resource directory suffixes to load
# subsets_inex = IncludeExclude of subset strings to load
# blacklist_type_inex = IncludeExclude of blacklist types to consider during loading
# blacklist_name_inex = IncludeExclude of blacklist file names to consider during loading (no extension)
# sample_regex_inex = IncludeExclude of sample name regex patterns to restrict the loaded samples to
super().__init__(root_dir, DatasetType.Train, reqd_inputs, reqd_targets, limit_to=limit_to, limit_hard=limit_hard, limit_sorted=limit_sorted, limit_seed=limit_seed, partition_limit_to=partition_limit_to, partition_ratios=partition_ratios, partition_seed=partition_seed, load_kwargs={'res_suffix_inex_map': res_suffix_inex_map, 'subsets_inex': subsets_inex, 'blacklist_type_inex': blacklist_type_inex, 'blacklist_name_inex': blacklist_name_inex, 'sample_regex_inex': sample_regex_inex})
# noinspection PyMethodOverriding
def load_samples(self, reqd_res_types, res_suffix_inex_map, subsets_inex, blacklist_type_inex, blacklist_name_inex, sample_regex_inex):
# reqd_res_types = Set of required resource types
# res_suffix_inex_map = Dict[res_type, IncludeExclude] of resource directory suffixes to load
# subsets_inex = IncludeExclude of subset strings to load
# blacklist_type_inex = IncludeExclude of blacklist types to consider during loading
# blacklist_name_inex = IncludeExclude of blacklist file names to consider during loading (no extension)
# sample_regex_inex = IncludeExclude of sample name regex patterns to restrict the loaded samples to
# Return a deterministically sorted list of the samples, where each entry in the list is Tuple[sample_key, Dict[res_type, path]], and a list of string details about the loaded samples
sorted_reqd_res_types = sorted(reqd_res_types)
details = [
"Required resource types: " + ', '.join(res_type.name for res_type in sorted_reqd_res_types if res_type.required),
"Optional resource types: " + ', '.join(res_type.name for res_type in sorted_reqd_res_types if not res_type.required)
]
def res_type_from_dir(rdir_name):
rdir_name_parts = rdir_name.split('#', maxsplit=1)
if len(rdir_name_parts) <= 1:
rtype_name = rdir_name
rtype_suffix = '?'
else:
rtype_name, rtype_suffix = rdir_name_parts
return self.get_res_type(rtype_name), rtype_suffix
res_dir_names = set()
res_type_res_dirs = {res_type: [] for res_type in reqd_res_types}
res_type_subsets_map = {}
samples_path = os.path.join(self.root, self.SAMPLES_DIR)
for entry in os.scandir(samples_path):
if entry.is_dir():
res_type, res_type_suffix = res_type_from_dir(entry.name)
if res_type:
if res_type in reqd_res_types:
res_suffix_inex = res_suffix_inex_map.get(res_type, INCLUDE_EXCLUDE_NONE) if res_suffix_inex_map else INCLUDE_EXCLUDE_NONE
if (not res_suffix_inex.exclude or res_type_suffix not in res_suffix_inex.exclude) and (not res_suffix_inex.include or res_type_suffix in res_suffix_inex.include):
res_dir_names.add(entry.name)
subset_entries = {subentry for subentry in os.scandir(entry.path) if subentry.is_dir()}
res_type_res_dirs[res_type].append((res_type_suffix, entry.name, {(subentry.name, subentry.path) for subentry in subset_entries}))
res_dir_subsets = (subentry.name for subentry in subset_entries)
res_type_subsets = res_type_subsets_map.get(res_type, None)
if res_type_subsets is None:
res_type_subsets_map[res_type] = set(res_dir_subsets)
else: