一、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
for i in range(10):
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
row.append()
table.flush()
table.nrows
三、tutorial 2 脚本
from tables import *
from numpy import *
# Describe a particle record
class Particle(IsDescription):
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)
# Get the record object associated with the table:
event = table.row
# Fill the table with 257 events
for i in xrange(257):
# 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
table.flush()
# 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()