はじめに
ここでは、h5pyの3.6.0で確認を行った。また、google colab
で動かして確認した。
元データの作成
import h5py
import numpy as np
expression_matrix = np.random.rand(20000, 8)
sample_name = ['sample_' + str(x + 1) for x in range(8)]
書き込みの例
out_file = './h5pysample.h5'
outfh = h5py.File(out_file, 'w')
outfh.create_dataset('expmat', data = expression_matrix)
outfh.create_dataset('sample_name', data = sample_name)
outfh.flush()
outfh.close()
読み出しの例
フォーマットとして出てくる<f8
は、little-endian floating point 8byte(64bit)の意味である。Numpyのデータフォーマットに従っている。
in_file = './h5pysample.h5'
infh = h5py.File(in_file, 'r')
print(infh.keys())
## <KeysViewHDF5 ['expmat', 'sample_name']
print(infh['expmat'])
## <HDF5 dataset "expmat": shape (20000, 8), type "<f8">
print(infh['expmat'][::])
expression_matrix_2 = np.array(infh['expmat'])
sample_name_2 = list(infh['sample_name'])
infh.close()
検索
HDF5データの検索で、テーブルリストに該当名称があるかないかは、grep
で検索できる。上でいうところのexpmat
等で存在するテーブルを確認することが出来る。
参考資料
- HDF5 for Python
-
バイナリーデータファイルのフォーマット HDF を扱う Python ライブラリー
- h5py 2.xの時点の記事である。(現在は、h5py 3.xとなっている) 参考にしたが、
Datasets
オブジェクトでは.value
が動かない。このため[::]
等を使う。説明は、3.0のリリースノートに記載されている。
- h5py 2.xの時点の記事である。(現在は、h5py 3.xとなっている) 参考にしたが、
- The HDF Group
- numpy