这个具体问题源于尝试处理由MATLAB算法生成的大型数据集,以便我可以使用
python算法处理它们.
背景:我在MATLAB中有大型数组(通常是20x20x40x15000 [i,j,k,帧]),我想在python中使用它们.所以我将数组保存到* .mat文件并使用scipy.io.loadmat(fname)将* .mat文件读入numpy数组.但是,出现了一个问题,如果我尝试在python中加载整个* .mat文件,则会发生内存错误.为了解决这个问题,我将* .mat文件分割成碎片,这样我就可以将这些碎片一次加载到python数组中.如果我按帧分割* .mat,我现在有15,000 * .mat文件很快就会变得很麻烦(至少在windows中).所以我的解决方案是使用压缩文件.
问题:我是否可以使用scipy直接从压缩文件中读取* .mat文件,而无需先将文件解压缩到当前工作目录?
规格:Python 2.7,windows xp
当前代码:
import scipy.io
import zipfile
import numpy as np
def readZip(zfilename,dim,frames):
data=np.zeros((dim[0],dim[1],dim[2],frames),dtype=np.float32)
zfile = zipfile.ZipFile( zfilename, "r" )
i=0
for info in zfile.infolist():
fname = info.filename
zfile.extract(fname)
mat=scipy.io.loadmat(fname)
data[:,:,:,i]=mat['export']
mat.clear()
i=i+1
return data
试过的代码:
mat=scipy.io.loadmat(zfile.read(fname))
产生此错误:
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
mat=scipy.io.loadmat(zfile.open(fname))
产生此错误:
fileobj.seek(0)
UnsupportedOperation: seek
有关处理数据的任何其他建议表示赞赏.
谢谢!