2015-03-23 13 views
8

C'è un modo per memorizzare un array in un file hdf5, che è troppo grande per essere caricato in memoria?Come archiviare un array nel file hdf5 che è troppo grande per essere caricato in memoria?

se faccio qualcosa di simile

f = h5py.File('test.hdf5','w') 
f['mydata'] = np.zeros(2**32) 

ottengo un errore di memoria.

+3

Date un'occhiata a [hyperslabs] (http://docs.h5py.org/en/latest/high/dataset.html#chunked-storage). È possibile, ma dovresti scrivere in "blocchi" e rendere il file hdf5 chunkable. – Mathias711

+3

http://docs.h5py.org/en/latest/high/dataset.html#chunked-storage –

risposta

6

In base allo documentation, è possibile utilizzare create_dataset per creare un array Chunked memorizzato in hdf5. Esempio:

>>> import h5py 
>>> f = h5py.File('test.h5', 'w') 
>>> arr = f.create_dataset('mydata', (2**32,), chunks=True) 
>>> arr 
<HDF5 dataset "mydata": shape (4294967296,), type "<f4"> 

Affettare le HDF5 dataset rendimenti NumPy-array.

>>> arr[:10] 
array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32) 
>>> type(arr[:10]) 
numpy.array 

È possibile impostare valori come per un array Numpy.

>>> arr[3:5] = 3 
>>> arr[:6] 
array([ 0., 0., 0., 3., 3., 0.], dtype=float32) 

Non so se questo è il modo più efficiente, ma è possibile iterare l'intero array in blocchi. E per esempio, impostandolo a valori casuali:

>>> import numpy as np 
>>> for i in range(0, arr.size, arr.chunks[0]): 
     arr[i: i+arr.chunks[0]] = np.random.randn(arr.chunks[0]) 
>>> arr[:5] 
array([ 0.62833798, 0.03631227, 2.00691652, -0.16631022, 0.07727782], dtype=float32) 
+0

Cosa succede se la dimensione del set di dati non è nota in anticipo? Può essere fatto in modalità append? – mrgloom

+0

@mrgloom Forse questo è adatto alle tue esigenze? https://stackoverflow.com/a/25656175/3635816 – RickardSjogren

Problemi correlati