-
Notifications
You must be signed in to change notification settings - Fork 6
/
backend.py
1515 lines (1396 loc) · 67.4 KB
/
backend.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
"""Module with the Zarr-based I/O-backend for HDMF"""
# Python imports
import os
import warnings
import numpy as np
import tempfile
import logging
# Zarr imports
import zarr
from zarr.hierarchy import Group
from zarr.core import Array
from zarr.storage import (DirectoryStore,
TempStore,
NestedDirectoryStore)
import numcodecs
# HDMF-ZARR imports
from .utils import (ZarrDataIO,
ZarrReference,
ZarrSpecWriter,
ZarrSpecReader,
ZarrIODataChunkIteratorQueue)
from .zarr_utils import BuilderZarrReferenceDataset, BuilderZarrTableDataset
# HDMF imports
from hdmf.backends.io import HDMFIO
from hdmf.backends.errors import UnsupportedOperation
from hdmf.backends.utils import (NamespaceToBuilderHelper,
WriteStatusTracker)
from hdmf.utils import (docval,
getargs,
popargs,
get_docval,
get_data_shape)
from hdmf.build import (Builder,
GroupBuilder,
DatasetBuilder,
LinkBuilder,
BuildManager,
RegionBuilder,
ReferenceBuilder,
TypeMap)
from hdmf.data_utils import AbstractDataChunkIterator
from hdmf.spec import (RefSpec,
DtypeSpec,
NamespaceCatalog)
from hdmf.query import HDMFDataset
from hdmf.container import Container
# Module variables
ROOT_NAME = 'root'
"""
Name of the root builder for read/write
"""
SPEC_LOC_ATTR = '.specloc'
"""
Reserved attribute storing the path to the Group where the schema for the file are cached
"""
DEFAULT_SPEC_LOC_DIR = 'specifications'
"""
Default name of the group where specifications should be cached
"""
SUPPORTED_ZARR_STORES = (DirectoryStore,
TempStore,
NestedDirectoryStore)
"""
Tuple listing all Zarr storage backends supported by ZarrIO
"""
class ZarrIO(HDMFIO):
@staticmethod
def can_read(path):
try:
# TODO: how to use storage_options? Maybe easier to just check for ".zarr" suffix
zarr.open(path, mode="r")
return True
except Exception:
return False
@docval({'name': 'path',
'type': (str, *SUPPORTED_ZARR_STORES),
'doc': 'the path to the Zarr file or a supported Zarr store'},
{'name': 'manager', 'type': BuildManager, 'doc': 'the BuildManager to use for I/O', 'default': None},
{'name': 'mode', 'type': str,
'doc': 'the mode to open the Zarr file with, one of ("w", "r", "r+", "a", "w-") '
'the mode r- is used to force open without consolidated metadata in read only mode.'},
{'name': 'synchronizer', 'type': (zarr.ProcessSynchronizer, zarr.ThreadSynchronizer, bool),
'doc': 'Zarr synchronizer to use for parallel I/O. If set to True a ProcessSynchronizer is used.',
'default': None},
{'name': 'object_codec_class', 'type': None,
'doc': 'Set the numcodec object codec class to be used to encode objects.'
'Use numcodecs.pickles.Pickle by default.',
'default': None},
{'name': 'storage_options', 'type': dict,
'doc': 'Zarr storage options to read remote folders',
'default': None})
def __init__(self, **kwargs):
self.logger = logging.getLogger('%s.%s' % (self.__class__.__module__, self.__class__.__qualname__))
path, manager, mode, synchronizer, object_codec_class, storage_options = popargs(
'path', 'manager', 'mode', 'synchronizer', 'object_codec_class', 'storage_options', kwargs)
if manager is None:
manager = BuildManager(TypeMap(NamespaceCatalog()))
if isinstance(synchronizer, bool):
if synchronizer:
sync_path = tempfile.mkdtemp()
self.__synchronizer = zarr.ProcessSynchronizer(sync_path)
else:
self.__synchronizer = None
else:
self.__synchronizer = synchronizer
self.__mode = mode
self.__path = path
self.__file = None
self.__storage_options = storage_options
self.__built = dict()
self._written_builders = WriteStatusTracker() # track which builders were written (or read) by this IO object
self.__dci_queue = None # Will be initialized on call to io.write
# Codec class to be used. Alternates, e.g., =numcodecs.JSON
self.__codec_cls = numcodecs.pickles.Pickle if object_codec_class is None else object_codec_class
source_path = self.__path
if isinstance(self.__path, SUPPORTED_ZARR_STORES):
source_path = self.__path.path
super().__init__(manager, source=source_path)
@property
def file(self):
"""
The Zarr zarr.hierarchy.Group (or zarr.core.Array) opened by the backend.
May be None in case open has not been called yet, e.g., if no data has been
read or written yet via this instance.
"""
return self.__file
@property
def path(self):
"""The path to the Zarr file as set by the user"""
return self.__path
@property
def abspath(self):
"""The absolute path to the Zarr file"""
return os.path.abspath(self.source)
@property
def synchronizer(self):
return self.__synchronizer
@property
def object_codec_class(self):
return self.__codec_cls
def open(self):
"""Open the Zarr file"""
if self.__file is None:
# Within zarr, open_consolidated only allows the mode to be 'r' or 'r+'.
# As a result, when in other modes, the file will not use consolidated metadata.
if self.__mode != 'r':
# When we consolidate metadata, we use ConsolidatedMetadataStore.
# This interface does not allow for setting items.
# In the doc string, it says it is "read only". As a result, we cannot use r+ with consolidate_metadata.
# r- is only an internal mode in ZarrIO to force the use of regular open. For Zarr we need to
# use the regular mode r when r- is specified
mode_to_use = self.__mode if self.__mode != 'r-' else 'r'
self.__file = zarr.open(store=self.path,
mode=mode_to_use,
synchronizer=self.__synchronizer,
storage_options=self.__storage_options)
else:
self.__file = self.__open_file_consolidated(store=self.path,
mode=self.__mode,
synchronizer=self.__synchronizer,
storage_options=self.__storage_options)
def close(self):
"""Close the Zarr file"""
self.__file = None
return
def is_remote(self):
"""Return True if the file is remote, False otherwise"""
from zarr.storage import FSStore
if isinstance(self.file.store, FSStore):
return True
else:
return False
@classmethod
@docval({'name': 'namespace_catalog',
'type': (NamespaceCatalog, TypeMap),
'doc': 'the NamespaceCatalog or TypeMap to load namespaces into'},
{'name': 'path',
'type': (str, *SUPPORTED_ZARR_STORES),
'doc': 'the path to the Zarr file or a supported Zarr store'},
{'name': 'storage_options', 'type': dict,
'doc': 'Zarr storage options to read remote folders',
'default': None},
{'name': 'namespaces', 'type': list, 'doc': 'the namespaces to load', 'default': None}
)
def load_namespaces(cls, namespace_catalog, path, storage_options, namespaces=None):
'''
Load cached namespaces from a file.
'''
# TODO: how to use storage_options here?
f = zarr.open(path, mode='r', storage_options=storage_options)
if SPEC_LOC_ATTR not in f.attrs:
msg = "No cached namespaces found in %s" % path
warnings.warn(msg)
else:
spec_group = f[f.attrs[SPEC_LOC_ATTR]]
if namespaces is None:
namespaces = list(spec_group.keys())
for ns in namespaces:
ns_group = spec_group[ns]
latest_version = list(ns_group.keys())[-1]
ns_group = ns_group[latest_version]
reader = ZarrSpecReader(ns_group)
namespace_catalog.load_namespaces('namespace', reader=reader)
@docval(
{'name': 'container', 'type': Container, 'doc': 'the Container object to write'},
{'name': 'cache_spec', 'type': bool, 'doc': 'cache specification to file', 'default': True},
{'name': 'link_data', 'type': bool,
'doc': 'If not specified otherwise link (True) or copy (False) Datasets', 'default': True},
{'name': 'exhaust_dci', 'type': bool,
'doc': 'exhaust DataChunkIterators one at a time. If False, add ' +
'them to the internal queue self.__dci_queue and exhaust them concurrently at the end',
'default': True},
{
"name": "number_of_jobs",
"type": int,
"doc": (
"Number of jobs to use in parallel during write "
"(only works with GenericDataChunkIterator-wrapped datasets)."
),
"default": 1,
},
{
"name": "max_threads_per_process",
"type": int,
"doc": (
"Limits the number of threads used by each process. The default is None (no limits)."
),
"default": None,
},
{
"name": "multiprocessing_context",
"type": str,
"doc": (
"Context for multiprocessing. It can be None (default), 'fork' or 'spawn'. "
"Note that 'fork' is only available on UNIX systems (not Windows)."
),
"default": None,
},
{
"name": "consolidate_metadata",
"type": bool,
"doc": (
"Consolidate metadata into a single .zmetadata file in the root group to accelerate read."
),
"default": True,
}
)
def write(self, **kwargs):
"""Overwrite the write method to add support for caching the specification and parallelization."""
cache_spec, number_of_jobs, max_threads_per_process, multiprocessing_context = popargs(
"cache_spec", "number_of_jobs", "max_threads_per_process", "multiprocessing_context", kwargs
)
self.__dci_queue = ZarrIODataChunkIteratorQueue(
number_of_jobs=number_of_jobs,
max_threads_per_process=max_threads_per_process,
multiprocessing_context=multiprocessing_context,
)
super(ZarrIO, self).write(**kwargs)
if cache_spec:
self.__cache_spec()
def __cache_spec(self):
"""Internal function used to cache the spec in the current file"""
ref = self.__file.attrs.get(SPEC_LOC_ATTR)
spec_group = None
if ref is not None:
spec_group = self.__file[ref]
else:
path = DEFAULT_SPEC_LOC_DIR # do something to figure out where the specifications should go
spec_group = self.__file.require_group(path)
self.__file.attrs[SPEC_LOC_ATTR] = path
ns_catalog = self.manager.namespace_catalog
for ns_name in ns_catalog.namespaces:
ns_builder = NamespaceToBuilderHelper.convert_namespace(ns_catalog, ns_name)
namespace = ns_catalog.get_namespace(ns_name)
if namespace.version is None:
group_name = '%s/unversioned' % ns_name
else:
group_name = '%s/%s' % (ns_name, namespace.version)
ns_group = spec_group.require_group(group_name)
writer = ZarrSpecWriter(ns_group)
ns_builder.export('namespace', writer=writer)
@docval(
*get_docval(HDMFIO.export),
{'name': 'cache_spec', 'type': bool, 'doc': 'whether to cache the specification to file', 'default': True},
{
"name": "number_of_jobs",
"type": int,
"doc": (
"Number of jobs to use in parallel during write "
"(only works with GenericDataChunkIterator-wrapped datasets)."
),
"default": 1,
},
{
"name": "max_threads_per_process",
"type": int,
"doc": (
"Limits the number of threads used by each process. The default is None (no limits)."
),
"default": None,
},
{
"name": "multiprocessing_context",
"type": str,
"doc": (
"Context for multiprocessing. It can be None (default), 'fork' or 'spawn'. "
"Note that 'fork' is only available on UNIX systems (not Windows)."
),
"default": None,
},
)
def export(self, **kwargs):
"""Export data read from a file from any backend to Zarr.
See :py:meth:`hdmf.backends.io.HDMFIO.export` for more details.
"""
if self.__mode != 'w':
raise UnsupportedOperation("Cannot export to file %s in mode '%s'. Please use mode 'w'."
% (self.source, self.__mode))
src_io = getargs('src_io', kwargs)
write_args, cache_spec = popargs('write_args', 'cache_spec', kwargs)
number_of_jobs, max_threads_per_process, multiprocessing_context = popargs(
"number_of_jobs", "max_threads_per_process", "multiprocessing_context", kwargs
)
self.__dci_queue = ZarrIODataChunkIteratorQueue(
number_of_jobs=number_of_jobs,
max_threads_per_process=max_threads_per_process,
multiprocessing_context=multiprocessing_context,
)
if not isinstance(src_io, ZarrIO) and write_args.get('link_data', True):
raise UnsupportedOperation(f"Cannot export from non-Zarr backend { src_io.__class__.__name__} " +
"to Zarr with write argument link_data=True. "
+ "Set write_args={'link_data': False}")
write_args['export_source'] = src_io.source # pass export_source=src_io.source to write_builder
ckwargs = kwargs.copy()
ckwargs['write_args'] = write_args
super().export(**ckwargs)
if cache_spec:
self.__cache_spec()
def get_written(self, builder, check_on_disk=False):
"""
Return True if this builder has been written to (or read from) disk by this IO object, False otherwise.
:param builder: Builder object to get the written flag for
:type builder: Builder
:param check_on_disk: Check that the builder has been physically written to disk not just flagged as written
by this I/O backend
:type check_on_disk: bool
:return: True if the builder is found in self._written_builders using the builder ID, False otherwise. If
check_on_disk is enabled then the function cals get_builder_exists_on_disk in addition to verify
that the builder has indeed been written to disk.
"""
written = self._written_builders.get_written(builder)
if written and check_on_disk:
written = written and self.get_builder_exists_on_disk(builder=builder)
return written
@docval({'name': 'builder', 'type': Builder, 'doc': 'The builder of interest'})
def get_builder_exists_on_disk(self, **kwargs):
"""
Convenience function to check whether a given builder exists on disk in this Zarr file.
"""
builder = getargs('builder', kwargs)
builder_path = self.get_builder_disk_path(builder=builder, filepath=None)
exists_on_disk = os.path.exists(builder_path)
return exists_on_disk
@docval({'name': 'builder', 'type': Builder, 'doc': 'The builder of interest'},
{'name': 'filepath', 'type': str,
'doc': 'The path to the Zarr file or None for this file', 'default': None})
def get_builder_disk_path(self, **kwargs):
builder, filepath = getargs('builder', 'filepath', kwargs)
basepath = filepath if filepath is not None else self.source
builder_path = os.path.join(basepath, self.__get_path(builder).lstrip("/"))
return builder_path
@docval(
{'name': 'builder', 'type': GroupBuilder, 'doc': 'the GroupBuilder object representing the NWBFile'},
{
'name': 'link_data',
'type': bool,
'doc': 'If not specified otherwise link (True) or copy (False) Zarr Datasets',
'default': True
},
{
'name': 'exhaust_dci',
'type': bool,
'doc': (
'Exhaust DataChunkIterators one at a time. If False, add '
'them to the internal queue self.__dci_queue and exhaust them concurrently at the end'
),
'default': True,
},
{
'name': 'export_source',
'type': str,
'doc': 'The source of the builders when exporting',
'default': None,
},
{
"name": "consolidate_metadata",
"type": bool,
"doc": (
"Consolidate metadata into a single .zmetadata file in the root group to accelerate read."
),
"default": True,
}
)
def write_builder(self, **kwargs):
"""Write a builder to disk."""
f_builder, link_data, exhaust_dci, export_source, consolidate_metadata = getargs(
'builder', 'link_data', 'exhaust_dci', 'export_source', 'consolidate_metadata', kwargs
)
for name, gbldr in f_builder.groups.items():
self.write_group(
parent=self.__file,
builder=gbldr,
link_data=link_data,
exhaust_dci=exhaust_dci,
export_source=export_source,
)
for name, dbldr in f_builder.datasets.items():
self.write_dataset(
parent=self.__file,
builder=dbldr,
link_data=link_data,
exhaust_dci=exhaust_dci,
export_source=export_source,
)
self.write_attributes(self.__file, f_builder.attributes) # the same as set_attributes in HDMF
self.__dci_queue.exhaust_queue() # Write any remaining DataChunkIterators that have been queued
self._written_builders.set_written(f_builder)
self.logger.debug("Done writing %s '%s' to path '%s'" %
(f_builder.__class__.__qualname__, f_builder.name, self.source))
# Consolidate metadata for the entire file after everything has been written
if consolidate_metadata:
zarr.consolidate_metadata(store=self.path)
@staticmethod
def __get_store_path(store):
"""
Method to retrieve the path from the Zarr storage.
ConsolidatedMetadataStore wraps around other Zarr Store objects, requiring a check to
retrieve the path.
"""
if isinstance(store, zarr.storage.ConsolidatedMetadataStore):
fpath = store.store.path
else:
fpath = store.path
return fpath
def __open_file_consolidated(self,
store,
mode,
synchronizer=None,
storage_options=None):
"""
This method will check to see if the metadata has been consolidated.
If so, use open_consolidated.
"""
# This check is just a safeguard for possible errors in the future. But this should never happen
if mode == 'r-':
raise ValueError('Mode r- not allowed for reading with consolidated metadata')
try:
return zarr.open_consolidated(store=store,
mode=mode,
synchronizer=synchronizer,
storage_options=storage_options)
except KeyError: # A KeyError is raised when the '/.zmetadata' does not exist
return zarr.open(store=store,
mode=mode,
synchronizer=synchronizer,
storage_options=storage_options)
@docval({'name': 'parent', 'type': Group, 'doc': 'the parent Zarr object'},
{'name': 'builder', 'type': GroupBuilder, 'doc': 'the GroupBuilder to write'},
{'name': 'link_data', 'type': bool,
'doc': 'If not specified otherwise link (True) or copy (False) Zarr Datasets', 'default': True},
{'name': 'exhaust_dci', 'type': bool,
'doc': 'exhaust DataChunkIterators one at a time. If False, add ' +
'them to the internal queue self.__dci_queue and exhaust them concurrently at the end',
'default': True},
{'name': 'export_source', 'type': str,
'doc': 'The source of the builders when exporting', 'default': None},
returns='the Group that was created', rtype='Group')
def write_group(self, **kwargs):
"""Write a GroupBuider to file"""
parent, builder, link_data, exhaust_dci, export_source = getargs(
'parent', 'builder', 'link_data', 'exhaust_dci', 'export_source', kwargs
)
if self.get_written(builder):
group = parent[builder.name]
else:
group = parent.require_group(builder.name)
subgroups = builder.groups
if subgroups:
for subgroup_name, sub_builder in subgroups.items():
self.write_group(
parent=group,
builder=sub_builder,
link_data=link_data,
exhaust_dci=exhaust_dci,
)
datasets = builder.datasets
if datasets:
for dset_name, sub_builder in datasets.items():
self.write_dataset(
parent=group,
builder=sub_builder,
link_data=link_data,
exhaust_dci=exhaust_dci,
export_source=export_source,
)
# write all links (haven implemented)
links = builder.links
if links:
for link_name, sub_builder in links.items():
self.write_link(group, sub_builder)
attributes = builder.attributes
self.write_attributes(group, attributes)
self._written_builders.set_written(builder) # record that the builder has been written
return group
@docval({'name': 'obj', 'type': (Group, Array), 'doc': 'the Zarr object to add attributes to'},
{'name': 'attributes',
'type': dict,
'doc': 'a dict containing the attributes on the Group or Dataset, indexed by attribute name'},
{'name': 'export_source', 'type': str,
'doc': 'The source of the builders when exporting', 'default': None})
def write_attributes(self, **kwargs):
"""Set (i.e., write) the attributes on a given Zarr Group or Array."""
obj, attributes, export_source = getargs('obj', 'attributes', 'export_source', kwargs)
for key, value in attributes.items():
# Case 1: list, set, tuple type attributes
if isinstance(value, (set, list, tuple)) or (isinstance(value, np.ndarray) and np.ndim(value) != 0):
# Convert to tuple for writing (e.g., numpy arrays are not JSON serializable)
if isinstance(value, np.ndarray):
tmp = tuple(value.tolist())
else:
tmp = tuple(value)
# Attempt write of the attribute
try:
obj.attrs[key] = tmp
# Numpy scalars and bytes are not JSON serializable. Try to convert to a serializable type instead
except TypeError as e:
try:
tmp = tuple([i.item()
if (isinstance(i, np.generic) and not isinstance(i, np.bytes_))
else i.decode("utf-8")
if isinstance(i, (bytes, np.bytes_))
else i
for i in value])
obj.attrs[key] = tmp
except: # noqa: E722
raise TypeError(str(e) + " type=" + str(type(value)) + " data=" + str(value)) from e
# Case 2: References
elif isinstance(value, (Container, Builder, ReferenceBuilder)):
# TODO: Region References are not yet supported
# if isinstance(value, RegionBuilder):
# type_str = 'region'
# refs = self._create_ref(value.builder)
if isinstance(value, (ReferenceBuilder, Container, Builder)):
type_str = 'object'
if isinstance(value, Builder):
refs = self._create_ref(value, export_source)
else:
refs = self._create_ref(value.builder, export_source)
tmp = {'zarr_dtype': type_str, 'value': refs}
obj.attrs[key] = tmp
# Case 3: Scalar attributes
else:
# Attempt to write the attribute
try:
obj.attrs[key] = value
# Numpy scalars and bytes are not JSON serializable. Try to convert to a serializable type instead
except TypeError as e:
try:
val = value.item if isinstance(value, np.ndarray) else value
val = value.item() \
if (isinstance(value, np.generic) and not isinstance(value, np.bytes_)) \
else val.decode("utf-8") \
if isinstance(value, (bytes, np.bytes_)) \
else val
obj.attrs[key] = val
except: # noqa: E722
msg = str(e) + "key=" + key + " type=" + str(type(value)) + " data=" + str(value)
raise TypeError(msg) from e
def __get_path(self, builder):
"""Get the path to the builder.
If builder.location is set then it is used as the path, otherwise the function
determines the path by constructing it iteratively from the parents of the
builder.
"""
if builder.location is not None:
path = os.path.normpath(os.path.join(builder.location, builder.name)).replace("\\", "/")
else:
curr = builder
names = list()
while curr is not None and curr.name != ROOT_NAME:
names.append(curr.name)
curr = curr.parent
delim = "/"
path = "%s%s" % (delim, delim.join(reversed(names)))
return path
@staticmethod
def get_zarr_paths(zarr_object):
"""
For a Zarr object find 1) the path to the main zarr file it is in and 2) the path to the object within the file
:param zarr_object: Object for which we are looking up the path
:type zarr_object: Zarr Group or Array
:return: Tuple of two string with: 1) path of the Zarr file and 2) full path within the zarr file to the object
"""
# In Zarr the path is a combination of the path of the store and the path of the object. So we first need to
# merge those two paths, then remove the path of the file, add the missing leading "/" and then compute the
# directory name to get the path of the parent
fpath = ZarrIO._ZarrIO__get_store_path(zarr_object.store)
fullpath = os.path.normpath(os.path.join(fpath, zarr_object.path)).replace("\\", "/")
# To determine the filepath we now iterate over the path and check if the .zgroup object exists at
# a level, indicating that we are still within the Zarr file. The first level we hit where the parent
# directory does not have a .zgroup means we have found the main file
filepath = fullpath
while os.path.exists(os.path.join(os.path.dirname(filepath), ".zgroup")):
filepath = os.path.dirname(filepath)
# From the fullpath and filepath we can now compute the objectpath within the zarr file as the relative
# path from the filepath to the object
objectpath = "/" + os.path.relpath(fullpath, filepath)
# return the result
return filepath, objectpath
@staticmethod
def get_zarr_parent_path(zarr_object):
"""
Get the location of the parent of a zarr_object within the file
:param zarr_object: Object for which we are looking up the path
:type zarr_object: Zarr Group or Array
:return: String with the path
"""
filepath, objectpath = ZarrIO.get_zarr_paths(zarr_object)
parentpath = os.path.dirname(objectpath)
return parentpath
@staticmethod
def is_zarr_file(path):
"""
Check if the given path defines a Zarr file
:param path: Full path to main directory
:return: Bool
"""
if os.path.exists(path):
if os.path.isdir(path):
if os.path.exists(os.path.join(path, ".zgroup")):
return True
return False
def __is_ref(self, dtype):
if isinstance(dtype, DtypeSpec):
return self.__is_ref(dtype.dtype)
elif isinstance(dtype, RefSpec):
return True
elif isinstance(dtype, np.dtype):
return False
else:
return dtype == DatasetBuilder.OBJECT_REF_TYPE or dtype == DatasetBuilder.REGION_REF_TYPE
def resolve_ref(self, zarr_ref):
"""
Get the full path to the object linked to by the zarr reference
The function only constructs the links to the targe object, but it does not check if the object exists
:param zarr_ref: Dict with `source` and `path` keys or a `ZarrReference` object
:return: 1) name of the target object
2) the target zarr object within the target file
"""
# Extract the path as defined in the zarr_ref object
if zarr_ref.get('source', None) is None:
source_file = str(zarr_ref['path'])
else:
source_file = str(zarr_ref['source'])
# Resolve the path relative to the current file
if not self.is_remote():
source_file = os.path.abspath(os.path.join(self.source, source_file))
else:
# get rid of extra "/" and "./" in the path root and source_file
root_path = str(self.path).rstrip("/")
source_path = str(source_file).lstrip(".")
source_file = root_path + source_path
object_path = zarr_ref.get('path', None)
if object_path:
target_name = os.path.basename(object_path)
else:
target_name = ROOT_NAME
target_zarr_obj = self.__open_file_consolidated(store=source_file,
mode='r',
storage_options=self.__storage_options)
if object_path is not None:
try:
target_zarr_obj = target_zarr_obj[object_path]
except Exception:
raise ValueError("Found bad link to object %s in file %s" % (object_path, source_file))
# Return the create path
return target_name, target_zarr_obj
def _create_ref(self, ref_object, export_source=None):
"""
Create a ZarrReference object that points to the given container
:param ref_object: the object to be referenced
:type ref_object: Builder, Container, ReferenceBuilder
:returns: ZarrReference object
"""
if isinstance(ref_object, RegionBuilder): # or region is not None: TODO: Add to support regions
raise NotImplementedError("Region references are currently not supported by ZarrIO")
if isinstance(ref_object, Builder):
if isinstance(ref_object, LinkBuilder):
builder = ref_object.target_builder
else:
builder = ref_object
elif isinstance(ref_object, ReferenceBuilder):
builder = ref_object.builder
else:
builder = self.manager.build(ref_object)
path = self.__get_path(builder)
# TODO Add to get region for region references.
# Also add {'name': 'region', 'type': (slice, list, tuple),
# 'doc': 'the region reference indexing object', 'default': None},
# if isinstance(ref_object, RegionBuilder):
# region = ref_object.region
# get the object id if available
object_id = builder.get('object_id', None)
# determine the object_id of the source by following the parents of the builder until we find the root
# the root builder should be the same as the source file containing the reference
curr = builder
while curr is not None and curr.name != ROOT_NAME:
curr = curr.parent
if curr:
source_object_id = curr.get('object_id', None)
# We did not find ROOT_NAME as a parent. This should only happen if we have an invalid
# file as a source, e.g., if during testing we use an arbitrary builder. We check this
# anyways to avoid potential errors just in case
else:
source_object_id = None
warn_msg = "Could not determine source_object_id for builder with path: %s" % path
warnings.warn(warn_msg)
# by checking os.isdir makes sure we have a valid link path to a dir for Zarr. For conversion
# between backends a user should always use export which takes care of creating a clean set of builders.
source = (builder.source
if (builder.source is not None and os.path.isdir(builder.source))
else self.source)
# Make the source relative to the current file
# TODO: This check assumes that all links are internal links on export.
# Need to deal with external links on export.
if export_source is not None:
# Make sure the source of the reference is now towards the new file
# and not the original source when exporting.
source = '.'
else:
source = os.path.relpath(os.path.abspath(source), start=self.abspath)
# Return the ZarrReference object
ref = ZarrReference(
source=source,
path=path,
object_id=object_id,
source_object_id=source_object_id)
return ref
def __add_link__(self, parent, target_source, target_path, link_name):
"""
Add a link to the file
:param parent: The parent Zarr group containing the link
:type parent: zarr.hierarchy.Group
:param target_source: Source path within the Zarr file to the linked object
:type target_source: str
:param target_path: Path to the Zarr file containing the linked object
:param link_name: Name of the link
:type link_name: str
"""
if 'zarr_link' not in parent.attrs:
parent.attrs['zarr_link'] = []
zarr_link = list(parent.attrs['zarr_link'])
zarr_link.append({'source': target_source, 'path': target_path, 'name': link_name})
parent.attrs['zarr_link'] = zarr_link
@docval({'name': 'parent', 'type': Group, 'doc': 'the parent Zarr object'},
{'name': 'builder', 'type': LinkBuilder, 'doc': 'the LinkBuilder to write'})
def write_link(self, **kwargs):
parent, builder = getargs('parent', 'builder', kwargs)
if self.get_written(builder):
self.logger.debug("Skipping LinkBuilder '%s' already written to parent group '%s'"
% (builder.name, parent.name))
return
self.logger.debug("Writing LinkBuilder '%s' to parent group '%s'" % (builder.name, parent.name))
name = builder.name
target_builder = builder.builder
# Get the reference
zarr_ref = self._create_ref(target_builder)
# EXPORT WITH LINKS: Fix link source
# if the target and source are both the same, then we need to ALWAYS use ourselves as a source
# When exporting from one source to another, the LinkBuilders.source are not updated, i.e,. the
# builder.source and target_builder.source are not being updated and point to the old file, but
# for internal links (a.k.a, SoftLinks) they will be the same and our target will be part of
# our new file, so we can safely replace the source
if builder.source == target_builder.source:
zarr_ref.source = "." # Link should be relative to self
# EXPORT WITH LINKS: Make sure target is written. If is not then if the target points to a
# non-Zarr source, then we need to copy the data instead of writing a
# link to the data
# When exporting from a different backend, then we may encounter external links to
# other datasets, groups (or links) in another file. Since they are from another
# backend, we must ensure that those targets are copied as well, so we check here
# if our target_builder has been written and write it if it doesn't
# TODO: Review the logic for when we need to copy data and when to link it. We may need the export_source?
"""
skip_link = False
if not self.get_written(target_builder):
if not self.is_zarr_file(target_builder.source):
# We need to copy the target in place of the link so we need to
# change the name of target_builder to match the link instead
temp = copy(target_builder.name)
target_builder._Builder__name = name
# Skip writing the link since we copied the data into place
skip_link = True
if isinstance(target_builder, DatasetBuilder):
self.write_dataset(parent=parent, builder=target_builder)
elif isinstance(target_builder, GroupBuilder):
self.write_group(parent=parent, builder=target_builder)
elif isinstance(target_builder, LinkBuilder):
self.write_link(parent=parent, builder=target_builder)
target_builder._Builder__name = temp
# REGULAR LINK I/O:
# Write the actual link as we should in most cases. Skip it only if we copied the
# data from an external source in place instead
if not skip_link:
self.__add_link__(parent, zarr_ref.source, zarr_ref.path, name)
"""
self.__add_link__(parent, zarr_ref.source, zarr_ref.path, name)
self._written_builders.set_written(builder) # record that the builder has been written
@classmethod
def __setup_chunked_dataset__(cls, parent, name, data, options=None):
"""
Setup a dataset for writing to one-chunk-at-a-time based on the given DataChunkIterator. This
is a helper function for write_dataset()
:param parent: The parent object to which the dataset should be added
:type parent: Zarr Group or File
:param name: The name of the dataset
:type name: str
:param data: The data to be written.
:type data: AbstractDataChunkIterator
:param options: Dict with options for creating a dataset. available options are 'dtype' and 'io_settings'
:type options: dict
"""
io_settings = {}
if options is not None:
if 'io_settings' in options:
io_settings = options.get('io_settings')
# Define the chunking options if the user has not set them explicitly. We need chunking for the iterative write.
if 'chunks' not in io_settings:
recommended_chunks = data.recommended_chunk_shape()
io_settings['chunks'] = True if recommended_chunks is None else recommended_chunks
# Define the shape of the data if not provided by the user
if 'shape' not in io_settings:
io_settings['shape'] = data.recommended_data_shape()
if 'dtype' not in io_settings:
if (options is not None) and ('dtype' in options):
io_settings['dtype'] = options['dtype']
else:
io_settings['dtype'] = data.dtype
if isinstance(io_settings['dtype'], str):
# map to real dtype if we were given a string
io_settings['dtype'] = cls.__dtypes.get(io_settings['dtype'])
try:
dset = parent.create_dataset(name, **io_settings)
dset.attrs['zarr_dtype'] = np.dtype(io_settings['dtype']).str
except Exception as exc:
raise Exception("Could not create dataset %s in %s" % (name, parent.name)) from exc
return dset
@docval({'name': 'parent', 'type': Group, 'doc': 'the parent Zarr object'}, # noqa: C901
{'name': 'builder', 'type': DatasetBuilder, 'doc': 'the DatasetBuilder to write'},
{'name': 'link_data', 'type': bool,
'doc': 'If not specified otherwise link (True) or copy (False) Zarr Datasets', 'default': True},
{'name': 'exhaust_dci', 'type': bool,
'doc': 'exhaust DataChunkIterators one at a time. If False, add ' +
'them to the internal queue self.__dci_queue and exhaust them concurrently at the end',
'default': True},
{'name': 'force_data', 'type': None,
'doc': 'Used internally to force the data being used when we have to load the data', 'default': None},
{'name': 'export_source', 'type': str,
'doc': 'The source of the builders when exporting', 'default': None},
returns='the Zarr array that was created', rtype=Array)
def write_dataset(self, **kwargs): # noqa: C901
parent, builder, link_data, exhaust_dci, export_source = getargs(
'parent', 'builder', 'link_data', 'exhaust_dci', 'export_source', kwargs
)
force_data = getargs('force_data', kwargs)
if exhaust_dci and self.__dci_queue is None:
self.__dci_queue = ZarrIODataChunkIteratorQueue()
if self.get_written(builder):
return None
name = builder.name
data = builder.data if force_data is None else force_data
options = dict()
# Check if data is a h5py.Dataset to infer I/O settings if necessary
if ZarrDataIO.is_h5py_dataset(data):
# Wrap the h5py.Dataset in ZarrDataIO with chunking and compression settings inferred from the input data
data = ZarrDataIO.from_h5py_dataset(h5dataset=data)
# Separate data values and io_settings for write
if isinstance(data, ZarrDataIO):
options['io_settings'] = data.io_settings
link_data = data.link_data
data = data.data
else:
options['io_settings'] = {}
if builder.dimension_labels is not None:
builder.attributes['_ARRAY_DIMENSIONS'] = builder.dimension_labels
attributes = builder.attributes
options['dtype'] = builder.dtype
linked = False
# Write a regular Zarr array
dset = None
if isinstance(data, Array):
# copy the dataset
if link_data:
path = self.__get_store_path(data.store)
self.__add_link__(parent, path, data.name, name)
linked = True
dset = None
else:
zarr.copy(data, parent, name=name)
dset = parent[name]
# When converting data between backends we may see an HDMFDataset, e.g., a H55ReferenceDataset, with references
elif isinstance(data, HDMFDataset):
# If we have a dataset of containers we need to make the references to the containers
if len(data) > 0 and isinstance(data[0], Container):
ref_data = [self._create_ref(data[i], export_source=export_source) for i in range(len(data))]
shape = (len(data), )
type_str = 'object'
dset = parent.require_dataset(name,
shape=shape,
dtype=object,
object_codec=self.__codec_cls(),
**options['io_settings'])
dset.attrs['zarr_dtype'] = type_str
dset[:] = ref_data
self._written_builders.set_written(builder) # record that the builder has been written
# If we have a regular dataset, then load the data and write the builder after load