Skip to content

Commit

Permalink
Fix reading dtype from zarr dataset (#72)
Browse files Browse the repository at this point in the history
* Fix reading dtype from zarr dataset
---------

Co-authored-by: Oliver Ruebel <oruebel@users.noreply.github.com>
  • Loading branch information
alejoe91 and oruebel committed May 9, 2023
1 parent 708e1a7 commit 1607862
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ def __setup_chunked_dataset__(cls, parent, name, data, options=None):
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
Expand Down Expand Up @@ -1099,11 +1100,18 @@ def __read_dataset(self, zarr_obj, name):
if ret is not None:
return ret

if 'zarr_dtype' not in zarr_obj.attrs:
if 'zarr_dtype' in zarr_obj.attrs:
zarr_dtype = zarr_obj.attrs['zarr_dtype']
elif hasattr(zarr_obj, 'dtype'): # Fallback for invalid files that are mssing zarr_type
zarr_dtype = zarr_obj.dtype
warnings.warn(
"Inferred dtype from zarr type. Dataset missing zarr_dtype: " + str(name) + " " + str(zarr_obj)
)
else:
raise ValueError("Dataset missing zarr_dtype: " + str(name) + " " + str(zarr_obj))

kwargs = {"attributes": self.__read_attrs(zarr_obj),
"dtype": zarr_obj.attrs['zarr_dtype'],
"dtype": zarr_dtype,
"maxshape": zarr_obj.shape,
"chunks": not (zarr_obj.shape == zarr_obj.chunks),
"source": self.source}
Expand Down

0 comments on commit 1607862

Please sign in to comment.