一、tutorial 1
1.定義表類Particle
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() # 32-bit integer
grid_j = Int32Col() # 32-bit integer
pressure = Float32Col() # float (single-precision)
energy = Float64Col() # double (double)
2.建立檔案
h5file = open_file("c:/tutorial1.h5", mode = "w", title = "Test file")
3.根建立組detector
group = h5file.create_group("/", 'detector', 'Detector information')
4.組建立表readout
table = h5file.create_table(group, 'readout', Particle, "Readout example")
5.擷取行指針
particle = table.row
6.填充資料
for i in xrange(100):
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()
7.歸檔緩存資料
table.flush()
8.擷取表引用
table = h5file.root.detector.readout
9.使用filter擷取條件資料
1)通用方式
pressure = [x['pressure'] for x in table.iterrows() if x['TDCcount'] > 3 and 20 <= x['pressure'] < 50]
>>> pressure
[25.0, 36.0, 49.0]
2)核心和索引查詢
names = [x['name'] for x in table.where("""(TDCcount>3)&(20<=pressure)&(pressure<50)""")]
>>> names
['Particle: 5', 'Particle: 6', 'Particle: 7']
10.建立group columns
gcolumns = h5file.create_group(h5file.root, "columns", "Pressure and Name")
11.插入資料
pressure資料
h5file.create_array(gcolumns, 'pressure', pressure,"Pressure column selection")
names資料
h5file.create_array(gcolumns, 'name', names, "Name column selection")
12.關閉檔案
h5file.close()
二、tutorial 訓練
1.Enum
import tables
colorList = ['red', 'green', 'blue', 'white', 'black']
colors = tables.Enum(colorList)
print "Value of 'red' and 'white':", (colors.red, colors.white)
print "Value of 'red' and 'white':", (colors['red'], colors['white'])
print "Name of value %s:" % colors.red, colors(colors.red)
h5f = tables.open_file('c:/enum.h5', 'w')
class BallExt(tables.IsDescription):
ballTime = tables.Time32Col()
ballColor = tables.EnumCol(colors, 'black', base='uint8')
group = h5f.create_group("/", 'extractions', 'Random ball extractions')
tbl = h5f.create_table(group,'enumtable', BallExt, "Enum Table Example")
row = tbl.row
import time
import random
now = time.time()
for i in range(10):
row['ballTime'] = now + i
row['ballColor'] = colors[random.choice(colorList)] # notice this
row.append()
2. earray
workingDays = {'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5}
dayRange = tables.EnumAtom(workingDays, 'Mon', base='uint16')
earr = h5f.create_earray('/', 'days', dayRange, (0, 2), title="Working day ranges")
earr.flavor = 'python'
wdays = earr.get_enum()
earr.append([(wdays.Mon, wdays.Fri), (wdays.Wed, wdays.Fri)])
earr.append([(wdays.Mon, 1234)])
3.nested struct
from tables import *
class Info(IsDescription):
"""A sub-structure of Test"""
_v_pos = 2 # The position in the whole structure
name = StringCol(10)
value = Float64Col(pos=0)
colors = Enum(['red', 'green', 'blue'])
class NestedDescr(IsDescription):
"""A description that has several nested columns"""
color = EnumCol(colors, 'red', base='uint32')
info1 = Info()
class info2(IsDescription):
_v_pos = 1
name = StringCol(10)
value = Float64Col(pos=0)
class info3(IsDescription):
x = Float64Col(dflt=1)
y = UInt8Col(dflt=1)
fileh = open_file("c:/nested-tut.h5", "w")
table = fileh.create_table(fileh.root, 'table', NestedDescr)
row = table.row
row['color'] = colors[['red', 'green', 'blue'][i%3]]
row['info1/name'] = "name1-%s" % i
row['info2/name'] = "name2-%s" % i
row['info2/info3/y'] = i
# All the rest will be filled with defaults
table.nrows
三、tutorial 2 腳本
from numpy import *
# Describe a particle record
name = StringCol(itemsize=16) # 16-character string
lati = Int32Col() # integer
longi = Int32Col() # integer
pressure = Float32Col(shape=(2,3)) # array of floats (single-precision)
temperature = Float64Col(shape=(2,3)) # array of doubles (double-precision)
# Native NumPy dtype instances are also accepted
Event = dtype([
("name" , "S16"),
("TDCcount" , uint8),
("ADCcount" , uint16),
("xcoord" , float32),
("ycoord" , float32)
])
# Open a file in "w"rite mode
fileh = open_file(r"c:/tutorial2.h5", mode = "w")
# Get the HDF5 root group
root = fileh.root
# Create the groups:
for groupname in ("Particles", "Events"):
group = fileh.create_group(root, groupname)
# Now, create and fill the tables in Particles group
gparticles = root.Particles
# Create 3 new tables
for tablename in ("TParticle1", "TParticle2", "TParticle3"):
# Create a table
table = fileh.create_table("/Particles", tablename, Particle, "Particles: "+tablename)
# Get the record object associated with the table:
particle = table.row
# Fill the table with 257 particles
for i in xrange(257):
# First, assign the values to the Particle record
particle['name'] = 'Particle: %6d' % (i)
particle['lati'] = i
particle['longi'] = 10 - i
########### Detectable errors start here. Play with them!
particle['pressure'] = i**2 # Incorrect
#particle['pressure'] = array(i*arange(2*3)).reshape((2,3)) # Correct
########### End of errors
particle['temperature'] = (i**2) # Broadcasting
# This injects the Record values
particle.append()
# Flush the table buffers
table.flush()
# Now, go for Events:
for tablename in ("TEvent1", "TEvent2", "TEvent3"):
# Create a table in Events group
table = fileh.create_table(root.Events, tablename, Event, "Events: "+tablename)
event = table.row
# Fill the table with 257 events
# First, assign the values to the Event record
event['name'] = 'Event: %6d' % (i)
event['TDCcount'] = i % (1<<8) # Correct range
########### Detectable errors start here. Play with them!
event['xcoord'] = float(i**2) # Wrong spelling
#event['xcoord'] = float(i**2) # Correct spelling
event['ADCcount'] = i*2# Wrong type
#event['ADCcount'] = i * 2 # Correct type
########### End of errors
event['ycoord'] = float(i)**4
# This injects the Record values
event.append()
# Flush the buffers
# Read the records from table "/Events/TEvent3" and select some
table = root.Events.TEvent3
e = [ p['TDCcount'] for p in table if p['ADCcount'] < 20 and 4 <= p['TDCcount'] < 15 ]
print "Last record ==>", p
print "Selected values ==>", e
print "Total selected records ==> ", len(e)
# Finally, close the file (this also will flush all the remaining buffers!)
fileh.close()
本文轉自 pgmia 51CTO部落格,原文連結:http://blog.51cto.com/heyiyi/1241345