2013-10-11 1 views
1

예를 들어 우리는 행렬을 가지고 있습니다 (예를 들어 numpy 배열을 저장하고 싶습니다). HDF5 파일에 저장하지만, 원래 행렬의 끝에 몇 개의 행을 추가하여 행렬을 확장하려고합니다 (take 원래의 행렬은 매우 크고 수십 Gb가 될 수 있고 RAM에로드 될 수 없습니다.)HDF5 행렬이 파이썬으로 덧붙여 짐

또한 어떤 시점에서 행렬로부터 몇 행을 읽는 기능을 원합니다 (slice (?)) RAM에 전체 행렬을로드하지 않고.

누구나 파이썬으로 어떻게 할 수 있습니까?

UPDATE :

나는 또 다른 옵션은 numpy.memmap 생각하지만, 더 APPEND가없는 것 같다.

This도 옵션이 있지만 원시 이진 데이터로 작동하지만 matrix.Also에 액세스 할 수있는 권한을 갖고 싶습니다.이 경우 추가 할 방법을 모르겠습니다.

답변

0

HDF5 파일로 작업하려면 Pytables와 같은 사용 가능한 라이브러리 중 하나를 사용하는 것이 좋습니다. 나는 여기에 자습서를 게시하고 단순화하고있다 : http://pytables.github.io/usersguide/tutorials.html

from tables import * 

# Define a user record to characterize some kind of particles 
class Particle(IsDescription): 
    name  = StringCol(16) # 16-character String 
    idnumber = Int64Col()  # Signed 64-bit integer 
    ADCcount = UInt16Col()  # Unsigned short integer 
    TDCcount = UInt8Col()  # unsigned byte 
    grid_i = Int32Col()  # integer 
    grid_j = Int32Col()  # integer 
    pressure = Float32Col() # float (single-precision) 
    energy = FloatCol()  # double (double-precision) 

filename = "test.h5" 
# Open a file in "w"rite mode 
h5file = openFile(filename, mode = "w", title = "Test file") 
# Create a new group under "/" (root) 
group = h5file.createGroup("/", 'detector', 'Detector information') 
# Create one table on it 
table = h5file.createTable(group, 'readout', Particle, "Readout example") 
# Fill the table with 10 particles 
particle = table.row 
for i in xrange(10): 
    particle['name'] = 'Particle: %6d' % (i) 
    particle['TDCcount'] = i % 256 
    particle['ADCcount'] = (i * 256) % (1 << 16) 
    particle['grid_i'] = i 
    particle['grid_j'] = 10 - i 
    particle['pressure'] = float(i*i) 
    particle['energy'] = float(particle['pressure'] ** 4) 
    particle['idnumber'] = i * (2 ** 34) 
    # Insert a new particle record 
    particle.append() 
# Close (and flush) the file 
h5file.close() 

#now we will append some data to the table, after taking some slices 
f=tables.openFile(filename, mode="a") 
f.root.detector 
f.root.detector.readout 
f.root.detector.readout[1::3] 
f.root.detector.readout.attrs.TITLE 
ro = f.root.detector.readout 

#generators work 
[row['energy'] for row in ro.where('pressure > 10')] 


#append some data 
table = f.root.detector.readout 
particle = table.row 
for i in xrange(10, 15): 
    particle['name'] = 'Particle: %6d' % (i) 
    particle['TDCcount'] = i % 256 
    particle['ADCcount'] = (i * 256) % (1 << 16) 
    particle['grid_i'] = i 
    particle['grid_j'] = 10 - i 
    particle['pressure'] = float(i*i) 
    particle['energy'] = float(particle['pressure'] ** 4) 
    particle['idnumber'] = i * (2 ** 34) 
    particle.append() 
table.flush() 
f.close()