分层数据格式5¶
- 文件扩展名
.h5
,.hdf5
hdf5的文件结构包括两种主要的对象类型:
- 数据集集合
同质类型的多维数组。
- 组
可以保存数据集或其他组的容器结构。
这样,我们最终得到的数据格式有点像文件系统。
对hdf5的python支持是由于 h5py package ,可通过安装
pip install h5py
并在通过导入后使用
import h5py
文件对象¶
要打开HDF5文件,建议使用
with h5py.File('my_file.h5', 'r') as f:
# interact with the file object
其中第一个参数是HDF5文件的路径,第二个参数是打开文件的模式。可用模式包括
r
只读,文件必须存在
r+
读/写,文件必须存在
w
创建文件,如果存在则截断
w-
orx
创建文件,如果存在则失败
a
读/写(如果存在),否则创建
默认文件模式为 a
.
危险
类似于 open()
打开HDF5文件还有另一种方法:
f = h5py.File('my_file.h5', 'r')
# interact with the file object
f.close()
这有一个缺点,即您必须自己负责关闭文件。因为hdf5文件的二进制性质本质上意味着一个损坏的位可能导致 丢失文件中的所有数据。 因此,我们强烈建议使用上下文管理器使用前面提到的方法。
每个文件对象也是表示文件根组的HDF5组对象。
组¶
HDF5文件格式的层次结构是由组实现的。与常规文件系统上的目录类似,它们可以帮助您在HDF5文件中组织数据。如前所述,文件对象由 File
初始化。创建新组是通过调用 create_group()
HDF5的方法 Group
对象:
with h5py.File('my_file.h5', 'w') as f:
print('Name of group:', f.name)
nested_group = f.create_group('nested group')
print('Name of nested group:', nested_group.name)
recursively_created_group = f.create_group('/this/is/deep')
print(
'Name of recursively created group:',
recursively_created_group.name)
其结果如下:
Name of group: /
Name of nested group: /nested group
Name of recursively created group: /this/is/deep
使用文件也可以与使用 dict
对象,因为它们为遍历组提供索引语法,支持迭代和 keys()
, values()
和 items()
语法:
with h5py.File('my_file.h5', 'w') as f:
f.create_group('/favorite/group/one')
f.create_group('/favorite/group/two')
f.create_group('/favorite/group/three')
group = f['/favorite/group']
for subgroup_name in sorted(group):
print(subgroup_name)
subgroup_one = group['one']
print(subgroup_one.name)
for subgroup_name, subgroup in sorted(group.items()):
print(subgroup_name, subgroup.name)
one
three
two
/favorite/group/one
one /favorite/group/one
three /favorite/group/three
two /favorite/group/two
数据集集合¶
创建数据集是通过调用 create_dataset
方法。尽管我们可以在HDF5中创建空数据集,并在以后用数据填充它们,但我们更通常只希望将已经处理过的数据存储在一个numpy数组中。可以这样做:
my_array = np.array(5*5*3*3*3).reshape(5, 5, 3, 3, 3)
with h5py.File('my_file.h5', 'w') as f:
group = f.create_group('my_group')
dset = group.create_dataset('my_dataset', data=my_array)
with h5py.File('my_file.h5', 'r') as f:
retrieved_array = np.array(f['my_group/my_dataset'])
print(np.allclose(my_array, retrieved_array))
结果是 True
.处理数据类型是为了转储和加载数据而自动完成的——对于常规数组和结构化数组都是如此。
由于我们存储的数据往往会变得很大,我们可以利用HDF5提供的压缩选项。h5py使这个保持简单,并在 compression
提供关键字,后跟一个1到9之间的数字,指示CPU时间/压缩权衡,从“最小压缩”到“最密集压缩”。
my_array = np.array(5*5*3*3*3).reshape(5, 5, 3, 3, 3)
with h5py.File('my_file.h5', 'w') as f:
group = f.create_group('my_group')
dset = group.create_dataset('my_dataset', data=my_array, compression=9)
属性¶
每个组和数据集可以有属性。属性是元数据,可以使用 attributes
组或数据集对象的属性。
md_dtype = [
('atom_id', np.int32),
('type', np.string_, 2),
('position', np.float64, 3),
('velocity', np.float64, 3)
]
md_data = np.array(
[
(0, 'He', (5.7222e-07, 4.8811e-09, 2.0415e-07), (-29.245, 100.45, 128.28)),
(1, 'He', (9.7710e-07, 3.6371e-07, 4.7311e-07), (-199.26, 232.75, -534.38)),
(2, 'Ar', (6.4989e-07, 6.7873e-07, 9.5000e-07), (-1.5592, -378.76, 84.091)),
(3, 'Ar', (5.9024e-08, 3.7138e-07, 7.3455e-08), (342.82, 156.82, -38.991)),
(4, 'He', (7.6746e-07, 8.3017e-08, 4.8520e-07), (-30.45, -379.75, -336.32)),
(5, 'Ar', (1.7226e-07, 4.6023e-07, 4.7356e-08), (-311.51, -429.39, -694.74)),
(6, 'Ar', (9.6394e-07, 7.2845e-07, 8.8623e-07), (-82.636, 45.098, -10.626)),
(7, 'He', (5.4450e-07, 4.6373e-07, 6.2270e-07), (158.89, 258.58, -151.5)),
(8, 'He', (7.9322e-07, 9.4700e-07, 3.5194e-08), (-197.03, 156.74, -185.2)),
(9, 'Ar', (2.7797e-07, 1.6487e-07, 8.2403e-07), (-38.65, -696.32, 216.42)),
(10, 'He', (1.1842e-07, 6.3244e-07, 5.0958e-07), (-149.63, 422.88, -76.309)),
(11, 'Ar', (2.0359e-07, 8.3369e-07, 9.6348e-07), (484.57, -267.41, -352.54)),
(12, 'He', (5.1019e-07, 2.2470e-07, 2.3846e-08), (-231.92, -99.51, 32.77)),
(13, 'Ar', (3.5383e-07, 8.4581e-07, 7.2340e-07), (-303.95, 47.316, 222.53)),
(14, 'He', (3.8515e-07, 2.8940e-07, 5.6028e-07), (233.08, 254.18, 429.83)),
(15, 'He', (1.5842e-07, 9.8225e-07, 5.7859e-07), (199.63, 203.11, -425.6)),
(16, 'He', (3.6831e-07, 7.6520e-07, 2.9884e-07), (66.341, 222.32, -97.653)),
(17, 'He', (2.8696e-07, 1.5129e-07, 6.4060e-07), (90.358, -67.459, -64.782)),
(18, 'He', (1.0325e-07, 9.9012e-07, 3.4381e-07), (71.108, 11.06, 15.912)),
(19, 'Ar', (4.3929e-07, 7.5363e-07, 9.9974e-07), (239.19, 173.83, 335.29))
],
dtype=md_dtype)
with h5py.File('md_results.h5') as f:
# Generic information regarding the file/the simulation
f.attrs['units'] = 'All quantities are in SI units.'
f.attrs['atom-types'] = json.dumps(
{
'He': 'Helium',
'Ar': 'Argon'
})
f.attrs['He potential'] = json.dumps(
{
'type': 'Lennard-Jones',
'parameters': {
'epsilon/k_B': 10.22,
'sigma': 256e-12
}
})
f.attrs['Ar potential'] = json.dumps(
{
'type': 'Lennard-Jones',
'parameters': {
'epsilon/k_B': 120,
'sigma': 341e-12
}
})
f.attrs['system size'] = np.array([100e-6, 200e-6, 300e-6])
f.attrs['boundary conditions'] = json.dumps(
['periodic', 'periodic', 'periodic'])
# Information specific to this dataset
dset = f.create_dataset('0000', data=md_data, compression=9)
dset.attrs['step'] = 0
dset.attrs['time'] = 0
HDFView¶
The HDF Group 提供用于浏览和编辑HDF5文件的工具: HDFView .通常,它可以通过操作系统的包管理器安装,然后通过
hdfview <path-to-hdf5-file>