user2734434 - 8 months ago 66

Python Question

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)

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

and later do something like

`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']`

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

`np.load('myresults')`

`a+b`

`3.4142135623730949 (=2+sqrt(2))`

Answer

```
locals().update(npzfile)
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 `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 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. 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(...)
```

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.