IPython:如何自动加载 npz 文件并将值分配给变量?

2022-01-24 00:00:00 python numpy ipython save load

问题描述

我是 Python 新手,我急切地希望从 MATLAB 迁移到 IPython,这是我在实验室进行数据分析的首选语言.

I'm new to Python and I'm eagerly migrating from MATLAB to IPython as my preferred language for data analysis at the lab.

在 MATLAB 中,经过一段时间的数据处理后,我会这样做

In MATLAB, after a session of data crunching, I would do

>>> save('myresults.mat','x','y','z');

并将变量 x、y 和 z 的结果保存在名为myresults.mat"的文件中.稍后,我可以简单地说:

and save the results of the variables x, y and z in a file called 'myresults.mat'. Later on, I could simply say:

>>> load('myresults');

并且 MATLAB 将加载 .mat 文件并将变量的存储值分配给当前工作区.

and MATLAB would load the .mat file AND assign the stored values of the variables to the current workspace.

我最近了解到我可以使用 numpy 为 IPython 做类似的事情,即:

I've recently learned that I can do similarly for IPython using numpy, namely:

import numpy as np
a = 2
b = np.sqrt(2)
np.savez('myresultsinpython',a,b)

然后做类似的事情

npzfile = np.load('myresultsinpython')

但是,我得到的是一个对象,我可以通过以下方式访问我的变量:

However, what I get is an object from which I can access my variables by:

npzfile['arr_1']

等等,但我丢失了有关变量原始名称的所有信息.我知道我可以通过这样做来保存文件

etc., but I loose all info on the original names of the variables. I know I could save the file by doing

np.savez('myresultsinpython',a=a,b=b)

但这不是很有用,因为我仍然需要做一些类似的事情:

but this is not that useful, as I still have to do something like:

npzfile['a']

访问变量的值.

如何加载文件并在我的工作区中创建变量并根据文件中存储的值分配它们的值?

How do I both load the file and have the variables created in my workspace and their values assigned according to the values stored in the file?

类似于 np.load('myresults') 并且能够执行 a+b 并获得 3.4142135623730949 (=2+sqrt(2)) 作为回报.

Something like np.load('myresults') and be able to do a+b and get 3.4142135623730949 (=2+sqrt(2)) in return.


解决方案

locals().update(npzfile)
a   # and/or b

在 Ipython 会话中,locals() 是一个大字典,其中包含您定义的变量、输入历史记录行和各种输出.updatenpzfile 的字典值添加到较大的值.

In the Ipython session, locals() is a large dictionary with variables the you've defined, the input history lines, and various outputs. update adds the dictionary values of npzfile to that larger one.

顺便说一句,您还可以加载和保存 MATLAB .mat 文件.使用 scipy.io.loadmatsavemat.它处理 v4 (Level 1.0)、v6 和 v7 到 7.2 文件.但是你有同样的问题 - 结果是一本字典.

By the way, you can also load and save MATLAB .mat files. Use scipy.io.loadmat and savemat. It handles v4 (Level 1.0), v6 and v7 to 7.2 files. But you have same issue - the result is a dictionary.

Octave 有一个加载命令的表达式形式,它将数据加载到一个结构中

Octave has an expression form of the load command, that loads the data into a structure

S = 加载(文件"、选项"、v1"、v2"、...)

S = load ("file", "options", "v1", "v2", ...)

一般而言,numpyipython 与 MATLAB 的保存工作区快照"的想法不同.我主要编写脚本来生成(和加载)必要的数据,并将它们作为独立的 python 会话运行或在 ipython 中运行它们.ipython 确实有一个很好的历史功能.您可能还会发现 ipython notebook 工具很有用.我没用过.

In general numpy and ipython does not have the same sort of 'save a snapshot of the workspace' idea that MATLAB does. I mostly write scripts to generate (and load) the necessary data, and run them either as stand alone python sessions or run them from with in ipython. ipython does have a good history feature. You might also find the ipython notebook tool useful. I haven't used that.

在函数内部,locals() 与外部不同.它对每个功能都是本地的".有一个 globals() 字典可能允许在函数之外设置变量.但不鼓励这种编程风格.

Within a function, locals() is different from outside it. It is 'local' to each function. There is a globals() dictionary that might allow setting variables outside the function. But this style of programming is not encouraged.

Anurag 引用的 SO 问题指向 save_ipython_variables 包.https://pypi.python.org/pypi/save_ipython_variables/0.0.3它使用 pickle 保存变量,并通过执行类似

The SO question that Anurag cited points to a save_ipython_variables package. https://pypi.python.org/pypi/save_ipython_variables/0.0.3 It saves variables with pickle, and loads them by doing something like

exec '__builtins__[name] = pickle.load(...)

通过使用 exec__builtins__,它推动了一些安全的编程界限.所以谨慎使用.但是一个小测试在我的环境中确实有效.相同的技术可能适用于 npz 文件.

With use of exec and __builtins__ it is pushing some safe programming boundaries. So use with caution. But a small test does work in my environment. The same technique might work with npz files.

相关文章