Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Nov 30, 2023
1 parent 6a41f5c commit b754203
Show file tree
Hide file tree
Showing 2 changed files with 1,480 additions and 19 deletions.
79 changes: 60 additions & 19 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,22 @@ def object_codec_class(self):
def open(self):
"""Open the Zarr file"""
if self.__file is None:
self.__file = zarr.open(store=self.path,
mode=self.__mode,
synchronizer=self.__synchronizer,
storage_options=self.__storage_options)
# self.__file = zarr.open(store=self.path,
# mode=self.__mode,
# synchronizer=self.__synchronizer,
# storage_options=self.__storage_options)
# # breakpoint()
if self.__mode == 'w':
self.__file = zarr.open(store=self.path,
mode=self.__mode,
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"""
Expand Down Expand Up @@ -427,9 +439,10 @@ def write_builder(self, **kwargs):
(f_builder.__class__.__qualname__, f_builder.name, self.source))

# Consolidate metadata for the entire file after everything has been written
zarr.consolidate_metadata(self.path, metadata_key='.zmetadata')
# breakpoint()
zarr.consolidate_metadata(store=self.path)

def consolidate_metadata(self):
def consolidate_metadata(self, store):
"""
When a file is written, the metadata within the file is consolidated automatically.
If there are any metadata changes, the user needs to consolidate the metadata again
Expand All @@ -438,22 +451,31 @@ def consolidate_metadata(self):
Consolidate all metadata for groups and arrays within the given store into a
single resource and put it under .zmetadata.
"""
zarr.consolidate_metadata(self.path, metadata_key='.zmetadata')
zarr.consolidate_metadata(store, metadata_key='.zmetadata')

def open_file_consolidated(self):
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
"""
if os.path.isfile(self.path+'/.zmetadata'):
zarr.open_consolidated(store=self.path,
mode=self.__mode,)
try:
temp = os.path.isfile(self.path+'/.zmetadata')
except TypeError:
temp = os.path.isfile(self.path.path+'/.zmetadata')
if temp:
return zarr.open_consolidated(store=store,
mode=mode,
synchronizer=synchronizer,
storage_options=storage_options)
else:
msg = "Could not find consolidated metadata."
warnings.warn(msg)

zarr.open(store=self.path,
mode=self.__mode,
synchronizer=self.__synchronizer)
return zarr.open(store=self.path,
mode=self.__mode,
synchronizer=self.__synchronizer)

@docval({'name': 'parent', 'type': Group, 'doc': 'the parent Zarr object'},
{'name': 'builder', 'type': GroupBuilder, 'doc': 'the GroupBuilder to write'},
Expand Down Expand Up @@ -604,7 +626,11 @@ def get_zarr_paths(zarr_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
fullpath = os.path.normpath(os.path.join(zarr_object.store.path, zarr_object.path)).replace("\\", "/")
if isinstance(zarr_object.store, zarr.storage.ConsolidatedMetadataStore):
fpath = zarr_object.store.store.path
else:
fpath = zarr_object.store.path
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
Expand Down Expand Up @@ -915,7 +941,11 @@ def write_dataset(self, **kwargs): # noqa: C901
if isinstance(data, Array):
# copy the dataset
if link_data:
self.__add_link__(parent, data.store.path, data.name, name)
if isinstance(data.store, zarr.storage.ConsolidatedMetadataStore):
path = data.store.store.path
else:
path = data.store.path
self.__add_link__(parent, path, data.name, name)
linked = True
dset = None
else:
Expand Down Expand Up @@ -1231,7 +1261,11 @@ def read_builder(self):
return f_builder

def __set_built(self, zarr_obj, builder):
fpath = zarr_obj.store.path
# fpath = zarr_obj.store.path
if isinstance(zarr_obj.store, zarr.storage.ConsolidatedMetadataStore):
fpath = zarr_obj.store.store.path
else:
fpath = zarr_obj.store.path
path = zarr_obj.path
path = os.path.join(fpath, path)
self.__built.setdefault(path, builder)
Expand Down Expand Up @@ -1271,12 +1305,19 @@ def __get_built(self, zarr_obj):
:type zarr_obj: Zarr Group or Dataset
:return: Builder in the self.__built cache or None
"""
fpath = zarr_obj.store.path

if isinstance(zarr_obj.store, zarr.storage.ConsolidatedMetadataStore):
fpath = zarr_obj.store.store.path
else:
fpath = zarr_obj.store.path

# fpath = zarr_obj.store.path
path = zarr_obj.path
path = os.path.join(fpath, path)
return self.__built.get(path, None)

def __read_group(self, zarr_obj, name=None):
# breakpoint()
ret = self.__get_built(zarr_obj)
if ret is not None:
return ret
Expand Down
Loading

0 comments on commit b754203

Please sign in to comment.