我想在hdf5文件中创建两个组。第一组为/h5md 组描述组,/粒子/脂质组为group2描述组。前者只包含一个直接属性'version‘(=1.0)和两个组creator和author及其属性,因此这里没有数据集。
在粒子/脂类组中,唯一缺失的位是框组盒组描述。最小信息是两个属性:维度(=3)和边界条件,例如字符串数组("none“、"none”、"none")。在我们的例子中,我们实际上是有周期边界的,因此字符串数组应该是(“周期性”、“周期性”、“周期性”),并且必须提供数据集的边缘。在每个帧的最后一行的文件文件中给出了盒的大小,它大约是61.42836 61.42836 8.47704,并且在模拟过程中略有变化。这意味着边缘数据集也是时间依赖的,即它有maxshape=(None,3)。
我想这个问题已经有了明确的定义。我需要根据描述创建这两个小组。我已经创建了第一组和第二组,参见下面的代码!并将属性赋予/h5md中的版本组,代码可以正常工作,但当我试图访问该属性时,它在该属性中没有显示任何内容!
import struct
import numpy as np
import h5py
import re
# First part generate convert the .gro -> .h5 .
csv_file = 'com'
fmtstring = '7s 8s 5s 7s 7s 7s'
fieldstruct = struct.Struct(fmtstring)
parse = fieldstruct.unpack_from
#define a np.dtype for gro array/dataset (hard-coded for now)
gro_dt = np.dtype([('col1', 'S7'), ('col2', 'S8'), ('col3', int),
('col4', float), ('col5', float), ('col6', float)])
with open(csv_file, 'r') as f, \
h5py.File('xaa.h5', 'w') as hdf:
# open group for position data
particles_grp = hdf.require_group('particles/lipids/positions')
h5md_grp = hdf.require_group('h5md/version/author/creator')
h5md_grp.attrs['version'] = 1.0
# datasets with known sizes
ds_time = particles_grp.create_dataset('time', dtype="f", shape=(0,), maxshape=(None,), compression='gzip', shuffle=True)
ds_step = particles_grp.create_dataset('step', dtype=np.uint64, shape=(0,), maxshape=(None,), compression='gzip', shuffle=True)
ds_value = None
step = 0
while True:
header = f.readline()
m = re.search("t= *(.*)$", header)
if m:
time = float(m.group(1))
else:
print("End Of File")
break
# get number of data rows, i.e., number of particles
nparticles = int(f.readline())
# read data lines and store in array
arr = np.empty(shape=(nparticles, 3), dtype=np.float32)
for row in range(nparticles):
fields = parse( f.readline().encode('utf-8') )
# arr[row]['col1'] = fields[0].strip()
# arr[row]['col2'] = fields[1].strip()
# arr[row]['col3'] = int(fields[2])
arr[row] = np.array((float(fields[3]), float(fields[4]), float(fields[5])))
if nparticles > 0:
# create a resizable dataset upon the first iteration
if not ds_value:
ds_value = particles_grp.create_dataset('value', dtype=np.float32,
shape=(0, nparticles, 3), maxshape=(None, nparticles, 3),
chunks=(1, nparticles, 3), compression='gzip', shuffle=True)
# append this sample to the datasets
ds_time.resize(step + 1, axis=0)
ds_step.resize(step + 1, axis=0)
ds_value.resize(step + 1, axis=0)
ds_time[step] = time
ds_step[step] = step
ds_value[step] = arr
#particles_grp[f'dataset_{step:04}'] = ds
#ds= hdf.create_dataset(f'dataset_{step:04}', data=arr,compression='gzip')
#create attributes for this dataset / time step
# hdr_tokens = header.split()
#particles_grp['ds'] = ds
#particles_grp[f'dataset_{step:04}'] = ds
# ds.attrs['raw_header'] = header
#ds.attrs['Generated by'] = hdr_tokens[2]
#ds.attrs['P/L'] = hdr_tokens[4].split('=')[1]
# ds.attrs['Time'] = hdr_tokens[6]
footer = f.readline()
step += 1
#=============================================================================
读取hdf5文件的代码
with h5py.File('xaa.h5', 'r') as ff:
base_items = list(ff.keys())
print('Items in the base directory: ', base_items)
value = ff.get('h5md/version')
#dataset = np.array(value)
#print("The shape of the value", value.shape)
print(value.get_id('h5md/version/'))
#print(list(ff.attrs.keys()))
发布于 2021-09-19 16:19:40
您需要使用与创建它们时相同的组名和属性名。根据代码打印属性的简单代码:
with h5py.File('xaa.h5', 'r') as ff:
h5md_grp = ff['h5md/version/author/creator']
print(h5md_grp.attrs['version'])
将“文件版本”作为全局属性添加到h5py文件对象中的代码,然后检索并打印:
with h5py.File('xaa.h5', 'w') as ff:
....
ff.attrs['version'] = 1.0
....
with h5py.File('xaa.h5', 'r') as ff:
print(ff.attrs['version'])
https://stackoverflow.com/questions/69242841
复制相似问题