将.h5(HDF5)文件转换为.zarr格式时,出现TypeError错误

时间:2020-07-22 10:08:20

标签: python typeerror hdf5 zarr

我正在尝试将.h5文件转换为.zarr格式,但出现以下错误

TypeError: Object of type bytes_ is not JSON serializable

我在下面发布代码

import h5py
import zarr
from sys import stdout

source = h5py.File('file.h5', 'r')
dest = zarr.open('file.zarr', 'w')
zarr.convenience.copy_all(source, dest, log=stdout, dry_run=False, if_exists='replace')

我检查了zarr文档,一些github问题,但不知道如何解决此错误。

我将链接添加到我已经看过但找不到(或说实话)的地方

https://zarr.readthedocs.io/en/stable/api/convenience.html#zarr.convenience.copy_all

https://github.com/zarr-developers/zarr-python/issues/87

这是错误的回溯

Traceback (most recent call last):
  File "/home/prk/Documents/IISER-stuff/scripts/h5_to_zarr.py", line 8, in <module>
    zarr.convenience.copy_all(source, dest, log=stdout, dry_run=False, if_exists='replace')
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/convenience.py", line 1063, in copy_all
    c, s, b = _copy(
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/convenience.py", line 903, in _copy
    ds.attrs.update(source.attrs)
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/attrs.py", line 120, in update
    self._write_op(self._update_nosync, *args, **kwargs)
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/attrs.py", line 74, in _write_op
    return f(*args, **kwargs)
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/attrs.py", line 131, in _update_nosync
    self._put_nosync(d)
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/attrs.py", line 113, in _put_nosync
    self.store[self.key] = json_dumps(d)
  File "/home/prk/Documents/IISER-stuff/venv/lib/python3.8/site-packages/zarr/util.py", line 24, in json_dumps
    return json.dumps(o, indent=4, sort_keys=True, ensure_ascii=True,
  File "/usr/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/lib/python3.8/json/encoder.py", line 201, in encode
    chunks = list(chunks)
  File "/usr/lib/python3.8/json/encoder.py", line 431, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.8/json/encoder.py", line 405, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.8/json/encoder.py", line 438, in _iterencode
    o = _default(o)
  File "/usr/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes_ is not JSON serializable

1 个答案:

答案 0 :(得分:0)

您是否有资源尝试使用此命令行工具:https://github.com/saalfeldlab/n5-utils

安装后呼叫

n5-copy -i file.h5 -o file.zarr

JSON导出器如何处理字节元数据可能只是一个小故障,这至少可以为您提供所需的副本。