首页 > 解决方案 > 将 .h5 (HDF5) 文件转换为 .zarr 格式时出现 TypeError

问题描述

我正在尝试将 .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

标签: pythontypeerrorhdf5zarr

解决方案


你有资源来试试这个命令行工具吗:https ://github.com/saalfeldlab/n5-utils

安装后调用

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

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


推荐阅读