How to use PyTables


In [1]:
from ipywidgets import interact
import numpy as np

from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()


Loading BokehJS ...

Import modules


In [2]:
from tables import *
import numpy as np

Write a HDF5 file


In [3]:
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-precision)

In [4]:
h5file = open_file("tutorial1.h5", mode = "w", title = "Test file")

In [5]:
group = h5file.create_group("/", 'detector', 'Detector information')

In [6]:
table = h5file.create_table(group, 'readout', Particle, "Readout example")

In [7]:
particle = table.row

In [8]:
for i in range(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()

In [9]:
table.flush()

In [10]:
h5file.close()

Read a HDF5 file


In [11]:
h5file = open_file('tutorial1.h5', mode='r')

In [12]:
print(h5file)


tutorial1.h5 (File) 'Test file'
Last modif.: 'Wed Oct 25 18:06:21 2017'
Object Tree: 
/ (RootGroup) 'Test file'
/detector (Group) 'Detector information'
/detector/readout (Table(10,)) 'Readout example'

Walk nodes


In [13]:
for node in h5file:
    print(node)


/ (RootGroup) 'Test file'
/detector (Group) 'Detector information'
/detector/readout (Table(10,)) 'Readout example'

Walk groups


In [14]:
for group in h5file.walk_groups():
    print(group)


/ (RootGroup) 'Test file'
/detector (Group) 'Detector information'

In [15]:
table = h5file.root.detector.readout

In [16]:
y = table[:]['pressure']
x = np.arange(len(y))

Plot


In [17]:
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')

In [18]:
r = p.line(x, y)

In [19]:
t = show(p)



In [ ]: