15.12.2012 Views

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

scipy tutorial - Baustatik-Info-Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

How do I start?<br />

SciPy Reference Guide, Release 0.8.dev<br />

You may have a .mat file that you want to read into Scipy. Or, you want to pass some variables from Scipy / Numpy<br />

into Matlab.<br />

To save us using a Matlab license, let’s start in Octave. Octave has Matlab-compatible save / load functions. Start<br />

Octave (octave at the command line for me):<br />

octave:1> a = 1:12<br />

a =<br />

1 2 3 4 5 6 7 8 9 10 11 12<br />

octave:2> a = reshape(a, [1 3 4])<br />

a =<br />

ans(:,:,1) =<br />

1 2 3<br />

ans(:,:,2) =<br />

4 5 6<br />

ans(:,:,3) =<br />

7 8 9<br />

ans(:,:,4) =<br />

10 11 12<br />

octave:3> save -6 octave_a.mat a % Matlab 6 compatible<br />

octave:4> ls octave_a.mat<br />

octave_a.mat<br />

Now, to Python:<br />

>>> mat_contents = sio.loadmat(’octave_a.mat’)<br />

/home/mb312/usr/local/lib/python2.5/site-packages/<strong>scipy</strong>/io/Matlab/mio.py:84: FutureWarning: Using str<br />

return MatFile5Reader(byte_stream, **kwargs)<br />

>>> print mat_contents<br />

{’a’: array([[[ 1., 4., 7., 10.],<br />

[ 2., 5., 8., 11.],<br />

[ 3., 6., 9., 12.]]]), ’__version__’: ’1.0’, ’__header__’: ’MATLAB 5.0 MAT-file, writte<br />

>>> oct_a = mat_contents[’a’]<br />

>>> print oct_a<br />

[[[ 1. 4. 7. 10.]<br />

[ 2. 5. 8. 11.]<br />

[ 3. 6. 9. 12.]]]<br />

>>> print oct_a.shape<br />

(1, 3, 4)<br />

We’ll get to the deprecation warning in a second. Now let’s try the other way round:<br />

1.11. File IO (<strong>scipy</strong>.io) 81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!