user2734434 user2734434 - 5 months ago 43x
Python Question

IPython: how to automagically load npz file and assign values to variables?

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

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

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

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');

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

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

import numpy as np
a = 2
b = np.sqrt(2)

and later do something like

npzfile = np.load('myresultsinpython')

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


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


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


to access the variable's value.

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?

Something like
and be able to do
and get
3.4142135623730949 (=2+sqrt(2))
in return.

a   # and/or b

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.

By the way, you can also load and save MATLAB .mat files. Use 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 has an expression form of the load command, that loads the data into a structure

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

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.

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.

The SO question that Anurag cited points to a save_ipython_variables package. It saves variables with pickle, and loads them by doing something like

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

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.