Is it possible to create an .npy file without allocating the corresponding array in memory first?
I need to create and work with a large numpy array, too big to create in memory. Numpy supports memory mapping, but as far as I can see my options are either:
-
Create a memmapped file using numpy.memmap. This creates the file directly on disk without allocating memory, but doesn’t store the metadata, so when I re-map the file later I need to know its dtype, shape, etc. In the following, notice that not specifying the shape results in the memmap being interpreted as flat array:
In [77]: x=memmap('/tmp/x', int, 'w+', shape=(3,3)) In [78]: x Out[78]: memmap([[0, 0, 0], [0, 0, 0], [0, 0, 0]]) In [79]: y=memmap('/tmp/x', int, 'r') In [80]: y Out[80]: memmap([0, 0, 0, 0, 0, 0, 0, 0, 0]) -
Create an array in memory, save it using numpy.save, after which it can be loaded in memmapped mode. This records metadata with the array data on disk, but requires that memory be allocated for the entire array at least once.
I had the same question and was disappointed when I read Sven’s reply. Seems as though numpy would be missing out on some key functionality if you couldn’t have a huge array on file and work on little pieces of it at a time. Your case seems to be close to one of the use cases in the origional rational for making the .npy format (see: http://svn.scipy.org/svn/numpy/trunk/doc/neps/npy-format.txt).
I then ran into numpy.lib.format, which seems to be full useful goodies. I have no idea why this functionality is not available from the numpy root package. The key advantage over HDF5 is that this ships with numpy.