web-dev-qa-db-ja.com

python Hdf5ファイルを使用して、同じ構造を維持する新しいファイルに部分的にコピーするにはどうすればよいですか?

次のような大きなhdf5ファイルがあります。

A/B/dataset1, dataset2
A/C/dataset1, dataset2
A/D/dataset1, dataset2
A/E/dataset1, dataset2

.。

それだけで新しいファイルを作成したい:A/B/dataset1、dataset2 A/C/dataset1、dataset2

Pythonで最も簡単な方法は何ですか?

やった:

fs = h5py.File('source.h5', 'r')
fd = h5py.File('dest.h5', 'w')
fs.copy('group B', fd)

問題は、dest.h5を取得することです。

B/dataset1, dataset2

そして、私は有向木の一部が欠けていること。

15
graham

fs.copy('A/B', fd)はパス/A/B/fdにコピーせず、グループBのみをコピーします(ご存知のとおり)。したがって、最初に残りのパスを作成する必要があります。

fd.create_group('A')
fs.copy('A/B', fd['/A'])

または、グループを頻繁に使用する場合:

fd_A = fd.create_group('A')
fs.copy('A/B', fd_A)

これにより、グループBfs['/A/B']からfd['/A']にコピーされます。

In [1]: fd['A/B'].keys()
Out[1]: [u'dataset1', u'dataset2']

これを自動的に行う方法は次のとおりです。

# Get the name of the parent for the group we want to copy
group_path = fs['/A/B'].parent.name

# Check that this group exists in the destination file; if it doesn't, create it
# This will create the parents too, if they don't exist
group_id = fd.require_group(group_path)

# Copy fs:/A/B/ to fd:/A/G
fs.copy('/A/B', group_id, name="G")

print(fd['/A/G'].keys())
# [u'dataset1', u'dataset2']
23
Yossarian