From b5c93227d1760399ebcd6e5ebb2fa923c22ed165 Mon Sep 17 00:00:00 2001 From: Oliver Ruebel Date: Thu, 11 Jan 2024 01:26:44 -0800 Subject: [PATCH] Add unit test for conversion of byte fillvalue to string --- src/hdmf_zarr/utils.py | 8 ++++---- tests/unit/test_zarrdataio.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/hdmf_zarr/utils.py b/src/hdmf_zarr/utils.py index bde22296..19bf75b9 100644 --- a/src/hdmf_zarr/utils.py +++ b/src/hdmf_zarr/utils.py @@ -485,14 +485,14 @@ def from_h5py_dataset(h5dataset, **kwargs): :returns: ZarrDataIO object wrapping the dataset """ filters = ZarrDataIO.hdf5_to_zarr_filters(h5dataset) - fillvalue = h5dataset.fillvalue if 'fillvalue' not in kwargs else kwargs.pop('fillvalue') - if isinstance(fillvalue, bytes): # bytes are not JSON serializable so use string instead - fillvalue = str(fillvalue) + fillval = h5dataset.fillvalue if 'fillvalue' not in kwargs else kwargs.pop('fillvalue') + if isinstance(fillval, bytes): # bytes are not JSON serializable so use string instead + fillval = fillval.decode("utf-8") chunks = h5dataset.chunks if 'chunks' not in kwargs else kwargs.pop('chunks') re = ZarrDataIO( data=h5dataset, filters=filters, - fillvalue=fillvalue, + fillvalue=fillval, chunks=chunks, **kwargs) return re diff --git a/tests/unit/test_zarrdataio.py b/tests/unit/test_zarrdataio.py index 0fc2f88c..bb5a1fe2 100644 --- a/tests/unit/test_zarrdataio.py +++ b/tests/unit/test_zarrdataio.py @@ -216,4 +216,22 @@ def test_from_h5py_dataset(self): self.assertIsInstance(re_zarrdataio.io_settings['filters'][0], numcodecs.Shuffle) self.assertIsInstance(re_zarrdataio.io_settings['filters'][1], numcodecs.Zlib) # Close the HDF5 file + h5file.close() + + def test_from_h5py_datase_bytes_fillvaluet(self): + """ + Test ZarrDataIO.from_h5py_dataset with a fillvalue that is in bytes, which needs to be handled + separately since bytes are not JSON serializable by default + """ + h5file = h5py.File(self.hdf_filename, mode='a') + # print(np.arange(10, dtype=np.int8).tobytes()) + h5dset = h5file.create_dataset( + name='test_str', + data=[b'hello', b'world', b'go'], + fillvalue=b'None') + re_zarrdataio = ZarrDataIO.from_h5py_dataset(h5dset) + # Test that all settings are being presevered when creating the ZarrDataIO object + self.assertIsInstance(re_zarrdataio, ZarrDataIO) + self.assertEqual(re_zarrdataio.io_settings['fill_value'], str("None")) + # Close the HDF5 file h5file.close() \ No newline at end of file