我正在尝试使用 Struct 模块从二进制文件中读取浮点数,然后将它们存储在 numpy 3D 数组中。当我将它作为独立脚本运行时,它工作正常。但是当我从另一个脚本中将它作为类的函数调用时(之后import) 它给出了值错误这是我的代码。
import struct
from numpy import *
class DCD_read:
def read_cord(self,total_atoms,dcd_data):
cord_data=dcd_data[276:len(dcd_data)] ## binary data string
byte=0
count=0
total_frames=info_dict['nset']
coord=numpy.zeros((total_frames,total_atoms,3)) ## 3d array
for frames in range(0,total_frames):
for atoms in range(0,total_atoms):
x = list(struct.unpack('<f',cord_data[60+byte:64+byte])) ###reading float
byte+=4
y = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
byte+=4
z = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
byte+=4
ls=x
ls.extend(y)
ls.extend(z)
coord[frames][atoms]=ls
return coord
错误:
Traceback (most recent call last):
File "C:\Users\Hira\Documents\PROJECT\md.py", line 24, in <module>
coord=dcd.read_cord(total_atoms,dcd_data)
File "C:\Users\Hira\Documents\PROJECT\DCD_read.py", line 51, in read_cord
coord=numpy.zeros((total_frames,total_atoms,3))
File "C:\Python27\numpy\core\numeric.py", line 148, in ones
a = empty(shape, dtype, order)
ValueError: negative dimensions are not allowed
md.py 是主要(调用脚本),而 DCD_read.py 是模块。这是 md.py 的代码(主脚本)
from DCD_read import *
import numpy
dcd_file=open('frame3.dcd',"rb")
dcd_data=dcd_file.read()
dcd=read_dcd()
total_atoms=6141
coord=dcd.read_cord(total_atoms,dcd_data)
请问有人能帮忙吗???我希望我能完全清楚地解释它。谢谢