IPython将变量加载到工作区:您能想到比这更好的解决方案吗? [英] IPython loading variables to workspace: can you think of a better solution than this?

查看:97
本文介绍了IPython将变量加载到工作区:您能想到比这更好的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从MATLAB迁移到ipython,在迈出这一步之前,我正在通过我的最小工作流程来确保我每天在MATLAB上执行的数据处理操作都可以在ipython上使用。

I'm migrating from MATLAB to ipython and before taking the leap I'm going through my minimal workflow to make sure every operation I perform daily on MATLAB for data crunching is available on ipython.

我目前停留在通过单行命令保存和加载numpy数组的基本任务,例如MATLAB:

I'm currently stuck on the very basic task of saving and loading numpy arrays via a one-line command, such as MATLAB's:

>>> save('myresults.mat','a','b','c')
>>> load('myresults.mat')

特别是,我喜欢的MATLAB的加载命令是只有它读取
数据文件,但它将变量加载到工作区中,开始使用它们不需要任何其他内容。请注意,例如,numpy.load()不是这种情况,它需要另一行能够将加载的值分配给工作空间变量。 [参见: IPython:如何自动化加载npz文件并为变量赋值?]

In particular, what I like about MATLAB's load command is that not only it reads the data file but it loads the variables into the workspace, nothing else is needed to start working with them. Note that this is not the case with, for instance, numpy.load(), which requires another line to be able to assign the loaded values to the workspace variables. [ See: IPython: how to automagically load npz file and assign values to variables? ]

根据对该问题的答案和评论,我想出了这个肮脏的坏工程 - 丑陋的编码但工作的解决方案。我知道它不漂亮,我想知道你是否能想出这个[1]的正确版本。

Based on the answers and comments to that question, I came up with this dirty-bad-engineering-ugly-coding-but-working solution. I know it's not pretty, and I would like to know if you can come up with the correct version of this [1].

我把它放入iocustom.py:

I put this into iocustom.py:

def load(filename):
    ip = get_ipython()
    ip.ex("import numpy as np")
    ip.ex("locals().update(np.load('" + filename + "'))") 

以便我可以从ipython会话中运行:

so that I can run, from the ipython session:

import iocustom
load('myresults.npz')

并且变量被转储到工作区。

and the variables are dumped to the workspace.

我发现很难相信没有内置的等价物对此,更难以认为3线功能是最佳解决方案。如果你能提出更正确的方法,我将非常感激。

I find it hard to believe there's nothing built-in equivalent to this, and it's even harder to think that that 3-line function is the optimal solution. I would be very grateful if you could please suggest a more correct way of doing this.

请记住:


  • 我正在寻找一种解决方案,它也适用于脚本和函数。

  • 我知道有泡菜但我拒绝使用多行代码作为一个简单的保存和/或加载命令。

  • 我知道scipy提供了savemat和loadmat,但是我想完全迁移,即不使用mat文件但是使用numpy数组。

提前感谢所有你的帮助。

Thanks in advance for all your help.

[1]顺便说一句:使用ipython的人如何轻松地保存和加载一组numpy数组?经过几个小时的谷歌搜索,我似乎找不到这个日常任务的简单直接的解决方案。

[1] BTW: how do people working with ipython save and load a set of numpy arrays easily? After hours of googling I cannot seem to find a simple and straightforward solution for this daily task.

推荐答案

如果我将其保存为 load_on_run.py

import argparse
import numpy as np
if __name__=='__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('-l','--list', help='list variables', action='store_true')
    parser.add_argument('filename')
    __args = parser.parse_args()
    data = np.load(__args.filename)
    locals().update(data)
    del parser, data, argparse, np
    if __args.list:
        print([k for k in locals() if not k.startswith('__')])
    del __args

然后在 ipython 我可以用%run

In [384]: %run load_on_run testarrays.npz -l
['array2', 'array3', 'array4', 'array1']
In [385]: array3
Out[385]: array([-10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1])

它巧妙地将文件中的数组加载到 ipython 工作区。

It neatly loads the arrays from the file into the ipython workspace.

我正在利用魔术%run 运行脚本,留下所有函数和它在主命名空间中定义的变量。我没有研究它是如何做到的。

I'm taking advantage of the fact that magic %run runs a script, leaving all functions and variables defined by it in the main namespace. I haven't looked into how it does this.

脚本只需要一些参数,加载文件(到目前为止只有 .npz ),并使用 locals()。update 技巧将其变量放入本地命名空间。然后我清除了不必要的变量和模块,只留下新加载的变量和模块。

The script just takes a few arguments, loads the file (so far only .npz), and uses the locals().update trick to put its variables into the local namespace. Then I clear out the unnecessary variables and modules, leaving only the newly loaded ones.

我可以为%run load_on_run定义一个别名

我还可以设想一个沿着这些行的脚本,它允许你从导入加载变量:来自< script> import *

I can also imagine a script along these lines that lets you load variables with an import: from <script> import *.

这篇关于IPython将变量加载到工作区:您能想到比这更好的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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