เมทริกซ์ HDF5 ผนวกใน python

ตัวอย่างเช่น เรามีเมทริกซ์ (เช่น เราต้องการเก็บอาร์เรย์ numpy) และเราเก็บไว้ในไฟล์ HDF5 แต่จากนั้นเราต้องการขยายเมทริกซ์โดยการต่อท้ายแถวบางแถวที่ส่วนท้ายของเมทริกซ์ดั้งเดิม (คำนึงว่าเมทริกซ์ดั้งเดิมสามารถมีได้มาก ใหญ่ประมาณสิบ Gb และไม่สามารถโหลดลงใน RAM ได้)

นอกจากนี้เรายังต้องการมีความสามารถในการอ่านสองสามแถวจากเมทริกซ์จากจุดใดก็ได้ (อาจเรียกว่าสไลซ์ (?)) โดยไม่ต้องโหลดเมทริกซ์ทั้งหมดใน RAM

ใครช่วยยกตัวอย่างว่าสามารถทำได้ใน python ได้อย่างไร?

อัปเดต:

ฉันคิดว่าอีกตัวเลือกหนึ่งคือ numpy.memmap แต่ ดูเหมือนว่าจะไม่มีการผนวก

นี่ดูเหมือนจะเป็นตัวเลือกเช่นกัน แต่ทำงานได้กับข้อมูลไบนารีดิบ แต่ฉันต้องการเข้าถึงเมทริกซ์ นอกจากนี้ ฉันไม่รู้ว่าจะต้องผนวกในกรณีนี้อย่างไร


person mrgloom    schedule 11.10.2013    source แหล่งที่มา


คำตอบ (1)


หากคุณกำลังจะทำงานกับไฟล์ 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()
person Paul    schedule 11.10.2013