【发布时间】:2021-11-09 16:33:45
【问题描述】:
我想创建一个路径为“粒子/脂质/位置”的组,其中包含两个数据集,例如粒子是主要组,脂质是包含脂质名称的数据集,位置将包含每个帧中脂质的位置。我尝试了这个,但在之前的答案code source 的代码的第 40 行中出现以下错误:
ValueError: Unable to create group (name already exists)
import struct
import numpy as np
import h5py
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:
step = 0
while True:
header = f.readline()
if not header:
print("End Of File")
break
else:
print(header)
# get number of data rows
no_rows = int(f.readline())
arr = np.empty(shape=(no_rows,), dtype=gro_dt)
for row in range(no_rows):
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]['col4'] = float(fields[3])
arr[row]['col5'] = float(fields[4])
arr[row]['col6'] = float(fields[5])
if arr.shape[0] > 0:
# Create a froup to store positions
particles_grp = hdf.create_group('particles/lipids/positions')
# create a dataset for THIS time step
ds= particles_grp.create_dataset(f'dataset_{step:04}', data=arr,compression='gzip')
#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
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
小数据文件链接在这里data file。在本代码中,每一帧都存储在 dataset1、2...中。我希望将这些数据集存储在粒子组中。我不太确定这是否是以后最好的方法,因为我想使用这些框架进行进一步的计算!谢谢!!
【问题讨论】:
-
这里的脂质是什么?