Skip to content

Commit

Permalink
static and some test
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Dec 1, 2023
1 parent ea4e24d commit 9a8127d
Show file tree
Hide file tree
Showing 45 changed files with 465 additions and 25 deletions.
29 changes: 16 additions & 13 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,16 +448,17 @@ def consolidate_metadata(self, store):
"""
zarr.consolidate_metadata(store, metadata_key='.zmetadata')

def __get_store_path(self, zarr_object):
@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(zarr_object.store, zarr.storage.ConsolidatedMetadataStore):
fpath = zarr_object.store.store.path
if isinstance(store, zarr.storage.ConsolidatedMetadataStore):
fpath = store.store.path
else:
fpath = zarr_object.store.path
fpath = store.path

return fpath

Expand Down Expand Up @@ -627,7 +628,8 @@ def __get_path(self, builder):
path = "%s%s" % (delim, delim.join(reversed(names)))
return path

def get_zarr_paths(self, zarr_object):
@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
Expand All @@ -637,7 +639,7 @@ def get_zarr_paths(self, 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
fpath = self.__get_store_path(zarr_object)
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
Expand All @@ -651,14 +653,15 @@ def get_zarr_paths(self, zarr_object):
# return the result
return filepath, objectpath

def get_zarr_parent_path(self, zarr_object):
@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 = self.get_zarr_paths(zarr_object)
filepath, objectpath = ZarrIO.get_zarr_paths(zarr_object)
parentpath = os.path.dirname(objectpath)
return parentpath

Expand Down Expand Up @@ -948,7 +951,7 @@ def write_dataset(self, **kwargs): # noqa: C901
if isinstance(data, Array):
# copy the dataset
if link_data:
path = self.__get_store_path(data)
path = self.__get_store_path(data.store)
self.__add_link__(parent, path, data.name, name)
linked = True
dset = None
Expand Down Expand Up @@ -1266,7 +1269,7 @@ def read_builder(self):

def __set_built(self, zarr_obj, builder):
# fpath = zarr_obj.store.path
fpath = self.__get_store_path(zarr_obj)
fpath = self.__get_store_path(zarr_obj.store)
path = zarr_obj.path
path = os.path.join(fpath, path)
self.__built.setdefault(path, builder)
Expand Down Expand Up @@ -1307,7 +1310,7 @@ def __get_built(self, zarr_obj):
:return: Builder in the self.__built cache or None
"""

fpath = self.__get_store_path(zarr_obj)
fpath = self.__get_store_path(zarr_obj.store)
path = zarr_obj.path
path = os.path.join(fpath, path)
return self.__built.get(path, None)
Expand All @@ -1323,7 +1326,7 @@ def __read_group(self, zarr_obj, name=None):
# Create the GroupBuilder
attributes = self.__read_attrs(zarr_obj)
ret = GroupBuilder(name=name, source=self.source, attributes=attributes)
ret.location = self.get_zarr_parent_path(zarr_obj)
ret.location = ZarrIO.get_zarr_parent_path(zarr_obj)

# read sub groups
for sub_name, sub_group in zarr_obj.groups():
Expand Down Expand Up @@ -1418,7 +1421,7 @@ def __read_dataset(self, zarr_obj, name):
if name is None:
name = str(os.path.basename(zarr_obj.name))
ret = DatasetBuilder(name, **kwargs) # create builder object for dataset
ret.location = self.get_zarr_parent_path(zarr_obj)
ret.location = ZarrIO.get_zarr_parent_path(zarr_obj)
self._written_builders.set_written(ret) # record that the builder has been written
self.__set_built(zarr_obj, ret)
return ret
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/example.zarr/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
".specloc": "specifications",
"colnames": [
"first_name",
"last_name",
"phone_number"
],
"data_type": "DynamicTable",
"description": "a table containing data/metadata about users, one user per row",
"namespace": "hdmf-common",
"object_id": "ea83daef-37db-4b95-af84-6f5d840423f6"
}
3 changes: 3 additions & 0 deletions tests/unit/example.zarr/.zgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
24 changes: 24 additions & 0 deletions tests/unit/example.zarr/first_name/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"chunks": [
2
],
"compressor": {
"blocksize": 0,
"clevel": 5,
"cname": "lz4",
"id": "blosc",
"shuffle": 1
},
"dtype": "|O",
"fill_value": 0,
"filters": [
{
"id": "vlen-utf8"
}
],
"order": "C",
"shape": [
2
],
"zarr_format": 2
}
7 changes: 7 additions & 0 deletions tests/unit/example.zarr/first_name/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data_type": "VectorData",
"description": "the first name of the user",
"namespace": "hdmf-common",
"object_id": "cdbf89b5-00bd-44e5-a350-2b4ba549dc3c",
"zarr_dtype": "str"
}
Binary file added tests/unit/example.zarr/first_name/0
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/unit/example.zarr/id/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"chunks": [
2
],
"compressor": {
"blocksize": 0,
"clevel": 5,
"cname": "lz4",
"id": "blosc",
"shuffle": 1
},
"dtype": "<i8",
"fill_value": 0,
"filters": null,
"order": "C",
"shape": [
2
],
"zarr_format": 2
}
6 changes: 6 additions & 0 deletions tests/unit/example.zarr/id/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"data_type": "ElementIdentifiers",
"namespace": "hdmf-common",
"object_id": "6b8eebde-6762-43da-8d0c-885f3aef95dc",
"zarr_dtype": "int64"
}
Binary file added tests/unit/example.zarr/id/0
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/unit/example.zarr/last_name/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"chunks": [
2
],
"compressor": {
"blocksize": 0,
"clevel": 5,
"cname": "lz4",
"id": "blosc",
"shuffle": 1
},
"dtype": "|O",
"fill_value": 0,
"filters": [
{
"id": "vlen-utf8"
}
],
"order": "C",
"shape": [
2
],
"zarr_format": 2
}
7 changes: 7 additions & 0 deletions tests/unit/example.zarr/last_name/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data_type": "VectorData",
"description": "the last name of the user",
"namespace": "hdmf-common",
"object_id": "ff302d07-62c1-4e84-b50a-75f4d8586e88",
"zarr_dtype": "str"
}
Binary file added tests/unit/example.zarr/last_name/0
Binary file not shown.
24 changes: 24 additions & 0 deletions tests/unit/example.zarr/phone_number/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"chunks": [
3
],
"compressor": {
"blocksize": 0,
"clevel": 5,
"cname": "lz4",
"id": "blosc",
"shuffle": 1
},
"dtype": "|O",
"fill_value": 0,
"filters": [
{
"id": "vlen-utf8"
}
],
"order": "C",
"shape": [
3
],
"zarr_format": 2
}
7 changes: 7 additions & 0 deletions tests/unit/example.zarr/phone_number/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"data_type": "VectorData",
"description": "the phone number of the user",
"namespace": "hdmf-common",
"object_id": "a3ef9627-dd8a-4715-a144-afe6b62c400a",
"zarr_dtype": "str"
}
Binary file added tests/unit/example.zarr/phone_number/0
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/unit/example.zarr/phone_number_index/.zarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"chunks": [
2
],
"compressor": {
"blocksize": 0,
"clevel": 5,
"cname": "lz4",
"id": "blosc",
"shuffle": 1
},
"dtype": "|u1",
"fill_value": 0,
"filters": null,
"order": "C",
"shape": [
2
],
"zarr_format": 2
}
16 changes: 16 additions & 0 deletions tests/unit/example.zarr/phone_number_index/.zattrs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data_type": "VectorIndex",
"description": "Index for VectorData 'phone_number'",
"namespace": "hdmf-common",
"object_id": "fbf1016a-f086-4d9a-9660-eb2c84a5e36c",
"target": {
"value": {
"object_id": null,
"path": "/phone_number",
"source": ".",
"source_object_id": "ea83daef-37db-4b95-af84-6f5d840423f6"
},
"zarr_dtype": "object"
},
"zarr_dtype": "uint8"
}
Binary file added tests/unit/example.zarr/phone_number_index/0
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/unit/example.zarr/specifications/.zgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
3 changes: 3 additions & 0 deletions tests/unit/example.zarr/specifications/hdmf-common/.zgroup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_format": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"chunks": [
1
],
"compressor": null,
"dtype": "|O",
"fill_value": 0,
"filters": [
{
"allow_nan": true,
"check_circular": true,
"encoding": "utf-8",
"ensure_ascii": true,
"id": "json2",
"indent": null,
"separators": [
",",
":"
],
"skipkeys": false,
"sort_keys": true,
"strict": true
}
],
"order": "C",
"shape": [
1
],
"zarr_format": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_dtype": "scalar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["{\"datasets\":[{\"doc\":\"An abstract data type for a dataset.\",\"data_type_def\":\"Data\"}],\"groups\":[{\"doc\":\"An abstract data type for a group storing collections of data and metadata. Base type for all data and metadata containers.\",\"data_type_def\":\"Container\"},{\"groups\":[{\"doc\":\"Container objects held within this SimpleMultiContainer.\",\"quantity\":\"*\",\"data_type_inc\":\"Container\"}],\"datasets\":[{\"doc\":\"Data objects held within this SimpleMultiContainer.\",\"quantity\":\"*\",\"data_type_inc\":\"Data\"}],\"doc\":\"A simple Container for holding onto multiple containers.\",\"data_type_inc\":\"Container\",\"data_type_def\":\"SimpleMultiContainer\"}]}","|O",[1]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"chunks": [
1
],
"compressor": null,
"dtype": "|O",
"fill_value": 0,
"filters": [
{
"allow_nan": true,
"check_circular": true,
"encoding": "utf-8",
"ensure_ascii": true,
"id": "json2",
"indent": null,
"separators": [
",",
":"
],
"skipkeys": false,
"sort_keys": true,
"strict": true
}
],
"order": "C",
"shape": [
1
],
"zarr_format": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"zarr_dtype": "scalar"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
["{\"namespaces\":[{\"doc\":\"Common data structures provided by HDMF\",\"schema\":[{\"source\":\"base\"},{\"source\":\"table\"},{\"source\":\"sparse\"}],\"name\":\"hdmf-common\",\"full_name\":\"HDMF Common\",\"version\":\"1.8.0\",\"author\":[\"Andrew Tritt\",\"Oliver Ruebel\",\"Ryan Ly\",\"Ben Dichter\"],\"contact\":[\"[email protected]\",\"[email protected]\",\"[email protected]\",\"[email protected]\"]}]}","|O",[1]]
Loading

0 comments on commit 9a8127d

Please sign in to comment.