如何使用 scipy.io.savemat 附加到 .mat 文件? [英] How to append to .mat file using scipy.io.savemat?

查看:21
本文介绍了如何使用 scipy.io.savemat 附加到 .mat 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以当我使用 savemat 命令时,它往往会覆盖文件.有没有可能的方法来追加而不是覆盖?我知道一种解决方法是将所有内容都放入一个列表中,然后将其转换为字典.这对我不起作用,因为我正在努力提高 RAM 效率.做在线搜索我发现这个 怎么不使用 scipy.io.savemat() 时覆盖 .mat 文件?

so when I use the savemat command it tends to overwrite the file. Is there a possible way to append instead of overwriting? I know a work-around would be to put everything into a list, and then convert it to a dictionary. That won't work for me because I am trying to be RAM efficient. Doing online search I found this How NOT to overwrite the .mat file when using scipy.io.savemat()?

这也行不通,因为它涉及在您的 ram 内存中提取一个数据文件以将其附加到每个循环中,从速度的角度来看这似乎很愚蠢.

that won't work either because it involves pulling a data file in your ram memory to append it every single loop, which seems stupid from the speed perspective.

我想过附加到一个 numpy 二进制文件,然后将其拉入并将其保存到 .mat 文件中?我不确定这是否会比第一个选项更高效.

I thought about appending to a numpy binary file, then pulling that in and saving it to a .mat file? I am not sure if this would be more RAM efficient than the first option though.

谢谢!

推荐答案

根据 savemat 文档:

file_name : str 或类似文件的对象

file_name : str or file-like object

所以你可以在追加模式下打开文件,然后写,例如

So you can open the file in append mode, and write, e.g.

io.savemat('temp.mat',{'data':np.ones(10)})  # write
with open('temp.mat','ab') as f:
    io.savemat(f, {'newdata':np.arange(5)})   # append
print io.loadmat('temp.mat').keys()           # read
# dict_keys(['data', '__globals__', 'newdata', '__header__', '__version__'])

或完整说明:

{'data': array([[ 1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.,  1.]]),
 '__globals__': [],
 'newdata': array([[0, 1, 2, 3, 4]]),
 '__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Fri Mar 13 14:14:33 2015',
 '__version__': '1.0'}

https://github.com/scipy/scipy/blob/master/scipy/io/matlab/mio5.py#L34 表明数据文件中有函数时附加有问题,但这表明附加不是'如果我们只是保存数组,这是一个问题.但也许可以进一步搜索 scipy 问题.

A note in https://github.com/scipy/scipy/blob/master/scipy/io/matlab/mio5.py#L34 suggests that there is a problem with appending when there's a function in the data file, but this indicates that appending isn't a problem if we are just saving arrays. But maybe further search of the scipy issues is in order.

这篇关于如何使用 scipy.io.savemat 附加到 .mat 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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