-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.lua
More file actions
1446 lines (1228 loc) · 41.8 KB
/
Copy pathloader.lua
File metadata and controls
1446 lines (1228 loc) · 41.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
Dataset's metadata loader classes.
--]]
local argcheck = require 'argcheck'
local hdf5 = require 'hdf5'
local dbcollection = require 'dbcollection.env'
local string_ascii = require 'dbcollection.utils.string_ascii'
local DataLoader = torch.class('dbcollection.DataLoader', dbcollection)
local SetLoader = torch.class('dbcollection.SetLoader', dbcollection)
local FieldLoader = torch.class('dbcollection.FieldLoader', dbcollection)
---------------------------------------------------------------------------------------------------
--[[ Split a string w.r.t. a single or a sequence of characters ]]
local function split_str(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
local i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
local function get_value_id_in_list(val, list)
for i=1, #list do
if list[i] == val then
return i
end
end
return nil
end
local function is_val_in_table(value, source)
for key, val in pairs(source) do
if val == value then
return true
end
end
return false
end
local function concat_shape_string(source, new_string, is_not_last)
local output = source .. new_string
if is_not_last then
output = output .. ', '
end
return output
end
local function get_data_shape(size)
local shape="("
for j=1, #size do
shape = concat_shape_string(shape, size[j], j < #size)
end
shape = shape .. ')'
return shape
end
local function get_atomic_indexes(dim)
local idx = {}
for i=1, dim do
table.insert(idx, {1,1})
end
return idx
end
local function get_data_type_hdf5(hdf5_dataset, size)
local idx = get_atomic_indexes(#size)
local data_sample = hdf5_dataset:partial(unpack(idx))
return torch.type(data_sample)
end
---------------------------------------------------------------------------------------------------
function DataLoader:__init(...)
local initcheck = argcheck{
pack=true,
help=[[
Dataset metadata loader class.
This class contains several methods to fetch data from a hdf5 file
by using simple, easy to use functions for (meta)data handling.
Parameters
----------
name : str
Name of the dataset.
task : str
Name of the task.
data_dir : str
Path of the dataset's data directory on disk.
hdf5_filepath : str
Path of the metadata cache file stored on disk.
Attributes
----------
db_name : str
Name of the dataset.
task : str
Name of the task.
data_dir : str
Path of the dataset's data directory on disk.
hdf5_filepath : str
Path of the hdf5 metadata file stored on disk.
hdf5_file : h5py._hl.files.File
hdf5 file object handler.
root_path : str
Default data group of the hdf5 file.
sets : table
List of set loaders for each set split (e.g. train, test, val, etc.)
_sets : table
List of names of set splits (e.g. train, test, val, etc.)
object_fields : table
Data field names for each set split.
]],
{name="name", type="string",
help="Name of the dataset."},
{name="task", type="string",
help="Name of the task."},
{name="data_dir", type="string",
help="Path of the dataset's data directory on disk."},
{name="hdf5_filepath", type="string",
help="Path of the metadata cache file stored on disk."}
}
local args = initcheck(...)
self.db_name = args.name
self.task = args.task
self.data_dir = args.data_dir
self.hdf5_filepath = args.hdf5_filepath
self.file = self:_open_hdf5_file()
self.root_path = '/'
self._sets = self:_get_set_names()
self.object_fields = self:_get_object_fields()
-- make links for all groups (train/val/test/etc.) for easier access
self.sets = self:_set_SetLoaders()
end
function DataLoader:_open_hdf5_file()
return hdf5.open(self.hdf5_filepath, 'r')
end
function DataLoader:_get_set_names()
local sets = {}
local group_default = self.file:read(self.root_path)
for k, v in pairs(group_default._children) do
table.insert(sets, k)
end
return sets
end
function DataLoader:_get_object_fields()
local object_fields = {}
for _, set in pairs(self._sets) do
object_fields[set] = self:_get_object_fields_data_from_set(set)
end
return object_fields
end
function DataLoader:_get_object_fields_data_from_set(set)
local hdf5_dataset_path = self.root_path .. set ..'/object_fields'
local object_fields_data = self.file:read(hdf5_dataset_path):all()
if object_fields_data:dim() == 1 then
object_fields_data = object_fields_data:view(1,-1)
end
return string_ascii.convert_ascii_to_str(object_fields_data)
end
function DataLoader:_set_SetLoaders()
local sets = {}
for _, set in pairs(self._sets) do
local hdf5_group_path = self.root_path .. set
sets[set] = dbcollection.SetLoader(self:_get_hdf5_group(hdf5_group_path))
end
return sets
end
function DataLoader:_get_hdf5_group(path)
return self.file:read(path)
end
function DataLoader:get(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves data from the dataset's hdf5 metadata file.
This method retrieves the i'th data from the hdf5 file with the
same 'field' name. Also, it is possible to retrieve multiple values
by inserting a list/tuple of number values as indexes.
Parameters
----------
set : str
Name of the set.
field : str
Name of the field.
index : number/table, optional
Index number of the field. If it is a list, returns the data
for all the value indexes of that list.
Returns
-------
torch.*Tensor
Numpy array containing the field's data.
]],
{name="set", type="string",
help="Name of the set."},
{name="field", type="string",
help="Name of the field."},
{name="index", type="table", default={},
help="Index number of the field. If it is a list, returns the data " ..
"for all the value indexes of that list.",
opt = true}
}
-- Workaround to manage have multiple types for the same input.
-- First the input checks if it is a number. If the input arg
-- is not a number, do a second argument parsing to check if the
-- second type matches the input argument.
local initcheck_ = argcheck{
quiet=true,
pack=true,
{name="set", type="string"},
{name="field", type="string"},
{name="index", type="number"}
}
local status, args = initcheck_(...)
if not status then
args = initcheck(...)
end
self:_check_if_set_is_valid(args.set)
return self.sets[args.set]:get(args.field, args.index)
end
function DataLoader:_check_if_set_is_valid(set)
local is_set_name_valid = is_val_in_table(set, self._sets)
assert(is_set_name_valid, ('Set %s does not exist for this dataset.'):format(set))
end
function DataLoader:object(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves a list of all fields' indexes/values of an object composition.
Retrieves the data's ids or contents of all fields of an object.
It basically works as calling the get() method for each individual field
and then groups all values into a list w.r.t. the corresponding order of
the fields.
Parameters
----------
set : str
Name of the set.
index : number/table, optional
Index number of the field. If it is a list, returns the data
for all the value indexes of that list. If no index is used,
it returns the entire data field array.
convert_to_value : bool, optional
If False, outputs a list of indexes. If True,
it outputs a list of arrays/values instead of indexes.
Returns
-------
table
Returns a list of indexes or, if convert_to_value is True,
a list of data arrays/values.
]],
{name="set", type="string",
help="Name of the set."},
{name="index", type="table", default={},
help="Index number of the field. If it is a list, returns the data " ..
"for all the value indexes of that list.",
opt=true},
{name="convert_to_value", type="boolean", default=false,
help="If False, outputs a list of indexes. If True, " ..
"it outputs a list of arrays/values instead of indexes.",
opt=true}
}
-- Workaround to manage have multiple types for the same input.
-- First the input checks if it is a number. If the input arg
-- is not a number, do a second argument parsing to check if the
-- second type matches the input argument.
local initcheck_ = argcheck{
quiet=true,
pack=true,
{name="set", type="string"},
{name="index", type="number"},
{name="convert_to_value", type="boolean", default=false, opt=true}
}
local status, args = initcheck_(...)
if not status then
args = initcheck(...)
end
self:_check_if_set_is_valid(args.set)
return self.sets[args.set]:object(args.index, args.convert_to_value)
end
function DataLoader:size(...)
local initcheck = argcheck{
pack=true,
help=[[
Size of a field.
Returns the number of the elements of a field.
Parameters
----------
set : str, optional
Name of the set.
field : str, optional
Name of the field in the metadata file.
Returns
-------
table
Returns the size of a field.
]],
{name="set", type="string",
help="Name of the set.",
opt=true},
{name="field", type="string", default='object_ids',
help="Name of the field in the metadata file.",
opt = true}
}
local args = initcheck(...)
if args.set then
return self:_get_set_size(args.set, args.field)
else
return self:_get_set_size_all(args.field)
end
end
function DataLoader:_get_set_size(set, field)
assert(field, 'Must input a field')
self:_check_if_set_is_valid(set)
return self.sets[set]:size(field)
end
function DataLoader:_get_set_size_all(field)
assert(field, 'Must input a field')
local out = {}
for _, set_name in pairs(self._sets) do
out[set_name] = self:_get_set_size(set_name, field)
end
return out
end
function DataLoader:list(...)
local initcheck = argcheck{
pack=true,
help=[[
List of all field names of a set.
Parameters
----------
set : str, optional
Name of the set.
Returns
-------
table
List of all data fields of the dataset.
]],
{name="set", type="string",
help="Name of the set.",
opt=true}
}
local args = initcheck(...)
if args.set then
return self:_get_set_list(args.set)
else
return self:_get_set_list_all()
end
end
function DataLoader:_get_set_list(set)
self:_check_if_set_is_valid(set)
return self.sets[set]:list()
end
function DataLoader:_get_set_list_all()
local out = {}
for _, set_name in pairs(self._sets) do
out[set_name] = self.sets[set_name]:list()
end
return out
end
function DataLoader:object_field_id(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves the index position of a field in the 'object_ids' list.
This method returns the position of a field in the 'object_ids' object.
If the field is not contained in this object, it returns a null value.
Parameters
----------
set : str
Name of the set.
field : str
Name of the field in the metadata file.
Returns
-------
number
Index of the field in the 'object_ids' list.
]],
{name="set", type="string",
help="Name of the set."},
{name="field", type="string",
help="Name of the field in the metadata file."}
}
local args = initcheck(...)
self:_check_if_set_is_valid(args.set)
return self.sets[args.set]:object_field_id(args.field)
end
function DataLoader:info(...)
local initcheck = argcheck{
pack=true,
help=[[
Prints information about all data fields of a set.
Displays information of all fields of a set group inside the hdf5
metadata file. This information contains the name of the field, as well
as the size/shape of the data, the data type and if the field is
contained in the 'object_ids' list.
If no 'set_name' is provided, it displays information for all available
sets.
This method only shows the most useful information about a set/fields
internals, which should be enough for most users in helping to
determine how to use/handle a specific dataset with little effort.
Parameters
----------
set : str, optional
Name of the set.
]],
{name="set", type="string",
help="Name of the set.",
opt=true}
}
local args = initcheck(...)
if args.set then
self:_get_set_info(args.set)
else
self:_get_set_info_all()
end
end
function DataLoader:_get_set_info(set)
self:_check_if_set_is_valid(set)
self.sets[set]:info()
end
function DataLoader:_get_set_info_all()
for _, set_name in pairs(self._sets) do
self.sets[set_name]:info()
end
end
function DataLoader:__len__()
return #self._sets
end
function DataLoader:__tostring__()
return ("DataLoader: \'%s\' (\'%s\' task)"):format(self.db_name, self.task)
end
---------------------------------------------------------------------------------------------------
function SetLoader:__init(...)
local initcheck = argcheck{
pack=true,
help=[[
Set metadata loader class.
This class contains several methods to fetch data from a specific
set (group) in a hdf5 file. It contains useful information about a
specific group and also several methods to fetch data.
Parameters
----------
hdf5_group : hdf5.HDF5Group
hdf5 group object handler.
Attributes
----------
data : hdf5.HDF5Group
hdf5 group object handler.
set : str
Name of the set.
fields : table
List of all field loaders of the set.
_fields : table
List of all field names of the set.
object_fields : table
List of all field names of the set contained by the 'object_ids' list.
nelems : number
Number of rows in 'object_ids'.
]],
{name="hdf5_group", type="hdf5.HDF5Group",
help="hdf5 group object handler."}
}
local args = initcheck(...)
self.hdf5_group = args.hdf5_group
self.set = self:_get_set_name()
self.object_fields = self:_get_object_fields()
self.nelems = self:_get_num_elements()
self._fields = self:_get_field_names()
self.fields = self:_load_hdf5_fields() -- add all hdf5 datasets as data fields
end
function SetLoader:_get_set_name()
local hdf5_object_str = hdf5._getObjectName(self.hdf5_group._groupID)
local str = split_str(hdf5_object_str, '/')
return str[1]
end
function SetLoader:_get_object_fields()
local object_fields_data = self:_get_hdf5_dataset_data('object_fields')
local output = string_ascii.convert_ascii_to_str(object_fields_data)
if type(output) == 'string' then
output = {output}
end
return output
end
function SetLoader:_get_field_names()
local fields = {}
for k, v in pairs(self.hdf5_group._children) do
table.insert(fields, k)
end
table.sort(fields)
return fields
end
function SetLoader:_get_hdf5_dataset_data(name)
local hdf5_dataset = self:_get_hdf5_dataset(name)
return hdf5_dataset:all()
end
function SetLoader:_get_hdf5_dataset(name)
return self.hdf5_group:getOrCreateChild(name)
end
function SetLoader:_get_num_elements()
local hdf5_dataset = self:_get_hdf5_dataset('object_ids')
local size = hdf5_dataset:dataspaceSize()
return size[1]
end
function SetLoader:_load_hdf5_fields()
local fields = {}
for _, field in pairs(self._fields) do
local obj_id = get_value_id_in_list(field, self.object_fields)
local hdf5_dataset = self:_get_hdf5_dataset(field)
fields[field] = dbcollection.FieldLoader(hdf5_dataset, obj_id)
end
return fields
end
function SetLoader:get(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves data from the dataset's hdf5 metadata file.
This method retrieves the i'th data from the hdf5 file with the
same 'field' name. Also, it is possible to retrieve multiple values
by inserting a list/tuple of number values as indexes.
Parameters
----------
field : str
Field name.
index : number/table, optional
Index number of the field. If it is a list, returns the data
for all the value indexes of that list.
Returns
-------
torch.*Tensor
Tensor array containing the field's data.
]],
{name="field", type="string",
help="Name of the dataset."},
{name="index", type="table", default={},
help="Index number of the field. If it is a list, returns the data " ..
"for all the value indexes of that list.",
opt=true}
}
-- Workaround to manage have multiple types for the same input.
-- First the input checks if it is a number. If the input arg
-- is not a number, do a second argument parsing to check if the
-- second type matches the input argument.
local initcheck_ = argcheck{
quiet=true,
pack=true,
{name="field", type="string"},
{name="index", type="number"}
}
local status, args = initcheck_(...)
if not status then
args = initcheck(...)
end
local is_field_valid = is_val_in_table(args.field, self._fields)
assert(is_field_valid, ('Field \'%s\' does not exist in the \'%s\' set.'):format(args.field, self.set))
return self.fields[args.field]:get(args.index)
end
function SetLoader:object(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves a list of all fields' indexes/values of an object composition.
Retrieves the data's ids or contents of all fields of an object.
It basically works as calling the get() method for each individual field
and then groups all values into a list w.r.t. the corresponding order of
the fields.
Parameters
----------
index : number/table, optional
Index number of the field. If it is a list, returns the data
for all the value indexes of that list. If no index is used,
it returns the entire data field array.
convert_to_value : bool, optional
If False, outputs a list of indexes. If True,
it outputs a list of arrays/values instead of indexes.
Returns
-------
table
Returns a list of indexes or, if convert_to_value is True,
a list of data arrays/values.
]],
{name="index", type="table",
help="Index number of the field.",
opt=true},
{name="convert_to_value", type="boolean", default=false,
help="If False, outputs a list of indexes. If True, " ..
"it outputs a list of arrays/values instead of indexes.",
opt=true}
}
-- Workaround to manage have multiple types for the same input.
-- First the input checks if it is a number. If the input arg
-- is not a number, do a second argument parsing to check if the
-- second type matches the input argument.
local initcheck_ = argcheck{
quiet=true,
pack=true,
{name="index", type="number"},
{name="convert_to_value", type="boolean", opt=true}
}
local status, args = initcheck_(...)
if not status then
args = initcheck(...)
end
local indexes = self:_get_object_indexes(args.index)
if args.convert_to_value then
indexes = self:_convert(indexes)
end
return indexes
end
function SetLoader:_get_object_indexes(idx)
self:_validate_object_idx(idx)
return self:get('object_ids', idx)
end
function SetLoader:_validate_object_idx(idx)
if idx then
if type(idx) == 'number' then
assert(idx >= 1, ('idx must be >=1: %d'):format(idx))
elseif type(idx) == 'table' then
assert(self:_is_greater_than_zero(idx), ('Table must have indexes >= 1.'))
else
error(('Must insert a table or number as input: %s'):format(type(idx)))
end
end
end
function SetLoader:_is_greater_than_zero(idx)
local min = 1
for k, v in pairs(idx) do
min = math.min(min, v)
end
return min == 1
end
function SetLoader:_convert(idx)
--[[
Retrieve data from the dataset's hdf5 metadata file in the original format.
This method fetches all indices of an object(s), and then it looks up for the
value for each field in 'object_ids' for a certain index(es), and then it
groups the fetches data into a single list.
Parameters
----------
idx : int/table
Index number of the field. If it is a list, returns the data
for all the indexes of that list as values.
Returns
-------
str/int/table
Value/list of a field from the metadata cache file.
]]
assert(idx)
local idx_ = idx
if idx:nDimension() == 1 then
idx_ = idx_:view(1, -1)
end
local output = {}
local num_samples = idx_:size(1)
for i=1, num_samples do
local data = self:_get_object_field_data_from_idx(idx_[i])
table.insert(output, data)
end
return output
end
function SetLoader:_get_object_field_data_from_idx(idx)
local data = {}
for k, field in ipairs(self.object_fields) do
if idx[k] >= 0 then
-- because python is 0-indexed, we need to increment
-- the hdf5 data elements by one to get the correct index
table.insert(data, self:get(field, idx[k] + 1))
else
table.insert(data, {})
end
end
return data
end
function SetLoader:size(...)
local initcheck = argcheck{
pack=true,
help=[[
Size of a field.
Returns the number of the elements of a field.
Parameters
----------
field : str, optional
Name of the field in the metadata file.
Returns
-------
table
Returns the size of the field.
]],
{name="field", type="string", default='object_ids',
help="Name of the dataset.",
opt=true}
}
local args = initcheck(...)
if args.field ~= 'object_ids' then
local is_field_valid = is_val_in_table(args.field, self.object_fields)
assert(is_field_valid, ('Field \'%s\' does not exist in the \'%s\' set.'):format(field, self.set))
end
return self.fields[args.field]:size()
end
function SetLoader:list(...)
local initcheck = argcheck{
pack=true,
help=[[
List of all field names.
Returns
-------
table
List of all data field names of the dataset.
]]
}
local args = initcheck(...)
return self._fields
end
function SetLoader:object_field_id(...)
local initcheck = argcheck{
pack=true,
help=[[
Retrieves the index position of the field in the 'object_ids' list.
This method returns the position of the field in the 'object_ids' object.
If the field is not contained in this object, it returns a null value.
Parameters
----------
field : str
Name of the field in the metadata file.
Returns
-------
number
Index of the field in the 'object_ids' list.
]],
{name="field", type="string",
help="Name of the field in the metadata file."}
}
local args = initcheck(...)
self:_validate_object_field_id_input(args.field)
local idx = self.fields[args.field]:object_field_id()
assert(idx, ('Field \'%s\' does not exist in \'_object_fields\''):format(args.field))
return idx
end
function SetLoader:_validate_object_field_id_input(field)
assert(field, 'Must input a valid field.')
assert(is_val_in_table(field, self.object_fields),
('Field \'%s\' does not exist \'object_fields\' set.')
:format(field, self.set))
end
function SetLoader:info(...)
local initcheck = argcheck{
pack=true,
help=[[
Prints information about the data fields of a set.
Displays information of all fields available like field name,
size and shape of all sets. If a 'set_name' is provided, it
displays only the information for that specific set.
This method provides the necessary information about a data set
internals to help determine how to use/handle a specific field.
]]
}
local args = initcheck(...)
print(('\n> Set: %s'):format(self.set))
self:_set_fields_info()
self:_print_fields_info()
self:_print_lists_info()
end
function SetLoader:_set_fields_info()
if self._fields_info == nil then
self:_init_info_vars()
self:_set_info_data()
self:_set_max_sizes()
end
end
function SetLoader:_init_info_vars()
self._fields_info = {}
self._lists_info = {}
self._sizes_info = self:_init_max_sizes()
end
function SetLoader:_init_max_sizes()
return {
name = 0,
shape = 0,
type = 0,
name_list = 0,
shape_list = 0,
type_list = 0,
}
end
function SetLoader:_set_info_data()
for i=1, #self._fields do
self:_set_field_data(self._fields[i])
end
end
function SetLoader:_set_field_data(field)
assert(field)
if self:_is_field_a_list(field) then
self:_set_list_info_metadata(field)
else
self:_set_field_info_metadata(field)
end
end
function SetLoader:_is_field_a_list(field)
assert(field)
if field:match('list_') then
return true
else
return false
end
end
function SetLoader:_set_list_info_metadata(field)
local shape, dtype = self:_get_field_shape_type(field)
self:_set_list_metadata(field, shape, dtype)
end
function SetLoader:_get_field_shape_type(field)
assert(field)
local hd5_dataset = self:_get_hdf5_dataset(field)
local size = hd5_dataset:dataspaceSize()
local shape = get_data_shape(size)
local dtype = get_data_type_hdf5(hd5_dataset, size)
return shape, dtype
end
function SetLoader:_set_list_metadata(field, shape, dtype)
assert(field)
assert(shape)
assert(dtype)
table.insert(self._lists_info, {
name = field,
shape = ('shape = %s'):format(shape),
type = ('dtype = %s'):format(dtype)
})
end
function SetLoader:_set_field_info_metadata(field)
local shape, dtype = self:_get_field_shape_type(field)
self:_set_field_metadata(field, shape, dtype)
end
function SetLoader:_set_field_metadata(field, shape, dtype)
assert(field)
assert(shape)
assert(dtype)
local s_obj = ''
if is_val_in_table(field, self.object_fields) then
s_obj = ("(in 'object_ids', position = %d)"):format(self:object_field_id(field))
end
table.insert(self._fields_info, {
name = field,
shape = ('shape = %s'):format(shape),
type = ('dtype = %s'):format(dtype),
obj = s_obj
})
end
function SetLoader:_set_max_sizes()
self:_set_max_sizes_fields()
self:_set_max_sizes_lists()
end
function SetLoader:_set_max_sizes_fields()