Skip to content

Commit

Permalink
Write references in compound datasets as an array rather than iterati…
Browse files Browse the repository at this point in the history
…vely
  • Loading branch information
sneakers-the-rat committed Dec 14, 2023
1 parent 6a3ab98 commit 31d0d07
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/hdmf_zarr/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,21 +1011,42 @@ def write_dataset(self, **kwargs): # noqa: C901
type_str.append(self.__serial_dtype__(t)[0])

if len(refs) > 0:
dset = parent.require_dataset(name,
shape=(len(data), len(data[0])),
dtype=object,
object_codec=self.__codec_cls(),
**options['io_settings'])

self._written_builders.set_written(builder) # record that the builder has been written
dset.attrs['zarr_dtype'] = type_str

# gather items to write
new_items = []
for j, item in enumerate(data):
new_item = list(item)
for i in refs:
new_item[i] = self.__get_ref(item[i], export_source=export_source)
new_items.append(new_item)

dset[...] = np.array(new_items)
new_items.append(tuple(new_item))

# Create dtype for storage, replacing values to match hdmf's hdf5 behavior
new_dtype = []
for field in options['dtype']:
if field['dtype'] is str:
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']))
dtype = np.dtype(new_dtype)

# cast and store compound dataset
arr = np.array(new_items, dtype=dtype)
dset = parent.require_dataset(
name,
shape=(len(arr),),
dtype=dtype,
object_codec=self.__codec_cls(),
**options['io_settings']
)
dset.attrs['zarr_dtype'] = type_str
dset[...] = arr
else:
# write a compound datatype
dset = self.__list_fill__(parent, name, data, options)
Expand Down

0 comments on commit 31d0d07

Please sign in to comment.