Skip to content

Commit

Permalink
Use explicit type map instead of stringlike types
Browse files Browse the repository at this point in the history
Expand check for string types
Make 'else' a failure condition in `__resolve_dtype_helper__` (rather than implicitly assuming list)
  • Loading branch information
sneakers-the-rat committed Dec 27, 2023
1 parent 793e8fd commit 7174615
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
15 changes: 12 additions & 3 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1023,17 +1023,24 @@ def write_dataset(self, **kwargs): # noqa: C901
new_items.append(tuple(new_item))

# Create dtype for storage, replacing values to match hdmf's hdf5 behavior
# ---
# TODO: Replace with a simple one-liner once __resolve_dtype_helper__ is
# compatible with zarr's need for fixed-length string dtypes.
# dtype = self.__resolve_dtype_helper__(options['dtype'])

new_dtype = []
for field in options['dtype']:
if field['dtype'] is str:
if field['dtype'] is str or field['dtype'] in (
'str', 'text', 'utf', 'utf8', 'utf-8', 'isodatetime'
):
new_dtype.append((field['name'], 'U25'))
elif isinstance(field['dtype'], dict):
# eg. for some references, dtype will be of the form
# {'target_type': 'Baz', 'reftype': 'object'}
# which should just get serialized as an object
new_dtype.append((field['name'], 'O'))
else:
new_dtype.append((field['name'], field['dtype']))
new_dtype.append((field['name'], self.__resolve_dtype_helper__(field['dtype'])))
dtype = np.dtype(new_dtype)

# cast and store compound dataset
Expand Down Expand Up @@ -1171,8 +1178,10 @@ def __resolve_dtype_helper__(cls, dtype):
return cls.__dtypes.get(dtype)
elif isinstance(dtype, dict):
return cls.__dtypes.get(dtype['reftype'])
else:
elif isinstance(dtype, list):
return np.dtype([(x['name'], cls.__resolve_dtype_helper__(x['dtype'])) for x in dtype])
else:
raise ValueError(f'Cant resolve dtype {dtype}')

@classmethod
def get_type(cls, data):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/base_tests_zarrio.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def test_read_reference_compound(self):
read_builder = self.root['ref_dataset']

# ensure the array was written as a compound array
ref_dtype = np.dtype([('id', '<i8'), ('name', '<U25'), ('reference', 'O')])
ref_dtype = np.dtype([('id', '<i4'), ('name', '<U25'), ('reference', 'O')])
self.assertEqual(read_builder.data.dataset.dtype, ref_dtype)

# Load the elements of each entry in the compound dataset and compar the index, string, and referenced array
Expand Down

0 comments on commit 7174615

Please sign in to comment.