[Numpy-discussion] how-to "put" RAM-based numarray into memmap

Francesc Alted falted at openlc.org
Fri Nov 21 10:06:09 CST 2003


A Divendres 21 Novembre 2003 18:28, Chris Barker va escriure:
> Sebastian Haase wrote:
> > Hi,
> > Suppose I have a 500MB-ram Computer and a 300MB ram-only (standard)
> > numarray.
> > Now I would like to "save" that onto harddrive (with a small header up
> > front
>
> How about:
>
> f = open(filename, 'wb')
> f.write(MyHeader)
> A.tofile(f)
>
> To read it back in, you need to know where your header ends, by either
> parsing it or using one of the same size every time, then you can use
> fromfile() to create an array form it.

You can also use pytables for doing that more comfortably:

>>> a=zeros([2,3,3])
>>> import tables
>>> fileh=tables.openFile("test.h5", "w")
>>> fileh.createArray(fileh.root, "array1", a, "This is a small test array")
/array1 (Array(2, 3, 3)) 'This is a small test array'
  type = Int32
  itemsize = 4
  flavor = 'NumArray'
  byteorder = 'little'
>>> fileh.close()

To read it back you only have to do:

>>> fileh=tables.openFile("test.h5", "r")
>>> a=fileh.root.array1.read()
>>> a
array([[[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]],

       [[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]])

that's all. All the headers with info about the object you are saving
are automatically saved and retrieved behind the scenes.

You can get pytables from http://pytables.sf.net

Cheers,

-- 
Francesc Alted





More information about the Numpy-discussion mailing list