从gromacs文件中读取数据并将其写入hdf5文件格式 [英] Reading data from gromacs file and write it to the hdf5 file format

查看:0
本文介绍了从gromacs文件中读取数据并将其写入hdf5文件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试逐行读取.gro文件中的数据,并希望将其写入.h5文件格式的数据。但获取TypeError:"No conversion path ford type: type('<U7')"。我猜读取的数据是字符串格式的。我尝试使用np.arrares将其转换为数组,但不起作用。有谁能帮我解决这个问题吗?或者,有没有更好的方法来读取数据?我无法使用np.loadtxt,因为数据大小约为50 GB。

.gro文件的格式如下

Generated by trjconv : P/L=1/400 t=   0.00000
11214
    1P1     aP1    1  80.48  35.36   4.25
    2P1     aP1    2  37.45   3.92   3.96
Generated by trjconv : P/L=1/400 t=   10.00000
11214
    1P1     aP1    1  80.48  35.36   4.25
    2P1     aP1    2  37.45   3.92   3.96
Generated by trjconv : P/L=1/400 t=   20.00000
11214
    1P1     aP1    1  80.48  35.36   4.25
    2P1     aP1    2  37.45   3.92   3.96
Generated by trjconv : P/L=1/400 t=   30.00000
11214
    1P1     aP1    1  80.48  35.36   4.25
    2P1     aP1    2  37.45   3.92   3.96
Generated by trjconv : P/L=1/400 t=   40.00000
11214
    1P1     aP1    1  80.48  35.36   4.25
    2P1     aP1    2  37.45   3.92   3.96

错误:

ValueError: Some errors were detected !
    Line #5 (got 7 columns instead of 6)
    Line #6 (got 1 columns instead of 6)
    Line #9 (got 7 columns instead of 6)
    Line #10 (got 1 columns instead of 6)
    Line #13 (got 7 columns instead of 6)
    Line #14 (got 1 columns instead of 6)
    Line #17 (got 7 columns instead of 6)
    Line #18 (got 1 columns instead of 6)

以下是我的小代码:

import h5py
import numpy as np
# First step is to read .gro file
f = open('pep.gro', 'r')
data = f.readlines()
for line in data:
    reading = line.split()
    #print(type(reading))
    #dat = np.array(reading).astype(int)

# Next step is to write the data to .h5 file
with h5py.File('pep1.h5', 'w') as hdf:
    hdf.create_dataset('dataset1', data=reading)

HDF5

首先创建包含大量行的推荐答案数据集[shape=(1_000_000)],然后使用maxshape参数使其可扩展。值maxshape=(None,)将允许无限行。我定义了一个简单的数据类型来匹配您的数据。如果需要,可以自动为不同的文件格式创建匹配的数据类型。

您收到了Unicode错误,因为h5py不支持将字符串作为Unicode数据。(默认情况下,NumPy从字符串创建Unicode数据。)解决此限制的方法是预先为数组定义一个数据类型(使用‘S#’,其中NumPy具有&q;<;U&q;。)您将在创建数据集和读取数据时使用此数据类型(见下文)。

接下来使用np.genfromtxt将直接读入到NumPy数组中。使用skip_headermax_rows参数递增读取。将dtype参数包括在用于创建上述数据集的数据类型中。

为了测试增量读取,我将您的文件扩展到54行(用于3个读取循环)。出于性能原因,您可能希望使用更大的值来读取50 GB(将incr设置为您可以读取到内存中的值--从100_000行开始)。

以下代码:(修改为跳过前两行

import h5py
import numpy as np

#define a np.dtype for gro array/dataset (hard-coded for now)
gro_dt = np.dtype([('col1', 'S4'), ('col2', 'S4'), ('col3', int), 
                   ('col4', float), ('col5', float), ('col6', float)])

# Next, create an empty .h5 file with the dtype
with h5py.File('pep1.h5', 'w') as hdf:
    ds= hdf.create_dataset('dataset1', dtype=gro_dt, shape=(20,), maxshape=(None,)) 

    # Next read line 1 of .gro file
    f = open('pep.gro', 'r')
    data = f.readlines()
    ds.attrs["Source"]=data[0]
    f.close()

    # loop to read rows from 2 until end
    skip, incr, row0 = 2, 20, 0 
    read_gro = True
    while read_gro:
        arr = np.genfromtxt('pep.gro', skip_header=skip, max_rows=incr, dtype=gro_dt)
        rows = arr.shape[0]
        if rows == 0:
            read_gro = False 
        else:    
            if row0+rows > ds.shape[0] :
                ds.resize((row0+rows,))
            ds[row0:row0+rows] = arr
            skip += rows
            row0 += rows

这篇关于从gromacs文件中读取数据并将其写入hdf5文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆