Sven Schreiber wrote:
belinda thom schrieb:
Also, since numpy borrows from matlab,
not really -- pylab is specifically designed to be similar to matlab, numpy is not -- and the matlib is left over from Numeric, and I don't think it was all that well maintained there, either.
Maybe you know that already, but in scipy there is something like
scipy.io.read_array and write_array which is very similiar to mpl's
load/save (IIRC).
IMHO something like that would be a welcome addition to numpy, but I
have learned that adding features to numpy is quite controversial...
(oh, we're on the mpl list right now, ok)
It's an obvious thing to have in a comprehensive package -- maybe not so much in numpy. numpy does have the core tools to build that sort of thing, however.
In the end I wrote my own csv read and write functions (see post in
other thread), because I didn't see why my code should depend on having
scipy or mpl installed just because of 20 or 30 lines of code.
Again, that is another example of why SciPy should be better modularized -- what if it just depended on having scipy.io installed?
(AFAIK numpy fromfile and tofile are for binary data, not text files.
Don't know if you want that.)
As mentioned, fromfile and tofile do basic text file support -- but not preserving the shape of the array. However, what it does provide is very good performance for reading/writing lots of numbers to/from text files -- they are great tools for building your own text file parser/generator. In fact, your csv reader/writer and much of scipy.io could probably benefit from using them (I don't think it does now, as the text file support is new to numpy).
I don't know if someone has written it yet, but a load/save pair that put a small header with type and shape info in a file, then dumped the contents with tofile(...sep=something) could be pretty handy, and fast.
So if you never use numpy standalone, I'd say go for mpl's load/save.
What about pickle? I'm pretty sure pickle works, doesn't require any additional packages, and preserves the format of the array:
import numpy as N
import cPickle
>>> a = N.arange(24, dtype=N.float)
>>> a.shape = (2,3,4)
>>> a
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[ 12., 13., 14., 15.],
[ 16., 17., 18., 19.],
[ 20., 21., 22., 23.]]])
>>> cPickle.dump(a, file("test.pickle",'wb'))
>>> b = cPickle.load(file("test.pickle",'rb'))
>>> b
array([[[ 0., 1., 2., 3.],
[ 4., 5., 6., 7.],
[ 8., 9., 10., 11.]],
[[ 12., 13., 14., 15.],
[ 16., 17., 18., 19.],
[ 20., 21., 22., 23.]]])
This is one of those cases where "There should be one obvious way to do it" is failing
-Chris
···
--
Christopher Barker, Ph.D.
Oceanographer
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker@...259...