Skip to content

Commit

Permalink
The tutorial chapter has been updated to explain the new iterators in…
Browse files Browse the repository at this point in the history
… Table,

Group and File, multidimensional columns in tables and the new row deletion
capability.


git-svn-id: http://www.pytables.org/svn/pytables/trunk@151 1b98710c-d8ec-0310-ae81-f5f2bcd8cb94
  • Loading branch information
Francesc Alted committed Jul 27, 2003
1 parent 3ed1b08 commit 04a12e6
Show file tree
Hide file tree
Showing 18 changed files with 452 additions and 324 deletions.
Binary file modified doc/usersguide.pdf
Binary file not shown.
Binary file removed doc/xml/tutorial-h5.jpg
Binary file not shown.
Binary file added doc/xml/tutorial1-tableview.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed doc/xml/tutorial2-h5.jpg
Binary file not shown.
Binary file added doc/xml/tutorial2-tableview.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
594 changes: 349 additions & 245 deletions doc/xml/usersguide.xml

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions examples/array1.py
@@ -1,4 +1,5 @@
from Numeric import *
from numarray import *
from numarray import strings
from tables import *

# Open a new empty HDF5 file
Expand All @@ -7,17 +8,17 @@
root = fileh.root

# Create an array
a = array(['1', '2', '4'], "c")
a = strings.array(['1', '2', '4'])
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_c', a, "Character array")

# Create other
a = array([-1, 2, 4], "1")
a = array([-1, 2, 4], Int8)
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_1', a, "Signed byte array")

# This is amusing, just create another one ;-)
a = array([-1, 2, 4], "b")
a = array([-1, 2, 4], UInt8)
# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'array_b', a, "Unsigned byte array")

Expand Down
2 changes: 1 addition & 1 deletion examples/array2.py
@@ -1,4 +1,4 @@
from Numeric import *
from numarray import *
from tables import *

# Open a new empty HDF5 file
Expand Down
7 changes: 4 additions & 3 deletions examples/array3.py
@@ -1,4 +1,5 @@
from Numeric import *
from numarray import *
from numarray import strings
from tables import *

# Open a new empty HDF5 file
Expand All @@ -8,10 +9,10 @@

# Create a large array
#a = reshape(array(range(2**16), "s"), (2,) * 16)
a = ones((2,) * 8, "c")
a = ones((2,) * 8, Int8)
print "About to write array a"
print " with shape: ==>", a.shape
print " and typecode ==> %c" % a.typecode()
print " and type: ==> %s" % a.type()

# Save it on the HDF5 file
hdfarray = fileh.createArray(root, 'carray', a, "Large array")
Expand Down
23 changes: 13 additions & 10 deletions examples/array4.py
@@ -1,4 +1,4 @@
from Numeric import *
from numarray import *
from tables import *

basedim = 4
Expand All @@ -8,15 +8,14 @@
# Get the root group
group = fileh.root
# Set the type codes to test
#typecodes = ["c", 'b', '1', 's', 'w', 'i', 'u', 'l', 'f', 'd']
# Reduce the set of typecodes because numarray miss some
typecodes = ['c', 'b', '1', 's', 'i', 'l', 'f', 'd']
#typecodes = ['c', 'b', '1', 's', 'i', 'l', 'f', 'd']
typecodes = [Int8, UInt8, Int16, Int, Float32, Float]
i = 1
for typecode in typecodes:
# Create an array of typecode, with incrementally bigger ranges
a = ones((basedim,) * i, typecode)
# Save it on the HDF5 file
dsetname = 'array_' + typecode
dsetname = 'array_' + str(typecode)
hdfarray = fileh.createArray(group, dsetname, a, "Large array")
print "Created dataset:", hdfarray
# Create a new group
Expand All @@ -33,17 +32,21 @@
# Get the root group
group = fileh.root
# Get the metadata on the previosly saved arrays
for i in range(1,len(typecodes)):
for i in range(1,len(typecodes)+1):
# Create an array for later comparison
a = ones((basedim,) * (i), typecodes[i-1])
# Get the dset object hangin from group
dset = getattr(group, 'array_' + typecodes[i-1])
dset = getattr(group, 'array_' + str(typecodes[i-1]))
print "Info from dataset:", repr(dset)
# Read the actual data in array
b = dset.read()
print "Array b read from file. Shape: ==>", b.shape,
print ". Typecode ==> %c" % b.typecode()
assert a == b
print "Array b read from file. Shape ==>", b.shape,
print ". Type ==> %s" % b.type()
# Test if the original and read arrays are equal
if allclose(a, b):
print "Good: Read array is equal to the original"
else:
print "Error: Read array and the original differs!"
# Iterate over the next group
group = getattr(group, 'group' + str(i))

Expand Down
2 changes: 1 addition & 1 deletion examples/table-tree.py
Expand Up @@ -81,7 +81,7 @@ class Particle(IsDescription):
"Pressure column", atomictype=0)
print "gcolumns.pressure type ==> ", gcolumns.pressure.type

# Do the same with TDCcount
# Do the same with TDCcount, but with a numarray object
TDC = [ p['TDCcount'] for p in table.iterrows() ]
print "TDC ==>", TDC
print "TDC shape ==>", array(TDC).shape
Expand Down
11 changes: 5 additions & 6 deletions examples/tutorial1-1.py
Expand Up @@ -7,7 +7,7 @@


import sys
from Numeric import *
from numarray import *
from tables import *


Expand Down Expand Up @@ -58,7 +58,6 @@ class Particle(IsDescription):
particle['grid_j'] = 10 - i
particle['pressure'] = float(i*i)
particle['energy'] = float(particle['pressure'] ** 4)
#particle['energy'] = 4
particle['idnumber'] = i * (2 ** 34)
# Insert a new particle record
particle.append()
Expand All @@ -72,26 +71,26 @@ class Particle(IsDescription):
# Read actual data from table. We are interested in collecting pressure values
# on entries where TDCcount field is greater than 3 and pressure less than 50
pressure = [ x['pressure'] for x in table
if x['TDCcount'] > 3 and x['pressure'] < 50 ]
if x['TDCcount']>3 and 20<=x['pressure']<50 ]
print "Last record read:"
print x
print "Field pressure elements satisfying the cuts ==>", pressure

# Read also the names with the same cuts
names = [ x['name'] for x in table
if x['TDCcount'] > 3 and x['pressure'] < 50 ]
if x['TDCcount'] > 3 and 20 <= x['pressure'] < 50 ]

print
print '-**-**-**-**-**-**- array object creation -**-**-**-**-**-**-**-'

print "Creating a new group called '/columns' to hold new arrays"
gcolumns = h5file.createGroup(h5file.root, "columns", "Pressure and Name")

print "Creating a Numeric array called 'pressure' under '/columns' group"
print "Creating an array called 'pressure' under '/columns' group"
h5file.createArray(gcolumns, 'pressure', array(pressure),
"Pressure column selection")

print "Creating another Numeric array called 'name' under '/columns' group"
print "Creating another array called 'name' under '/columns' group"
h5file.createArray('/columns', 'name', names, "Name column selection")

# Close the file
Expand Down
53 changes: 35 additions & 18 deletions examples/tutorial1-2.py
Expand Up @@ -7,7 +7,7 @@


import sys
from Numeric import *
from numarray import *
from tables import *

# Filename to work with
Expand Down Expand Up @@ -36,28 +36,33 @@
print node
print

# List all the Leafs (using File iterator) on tree
print "Leafs in file:"
for node in h5file(classname="Leaf"):
print node
print

# Now, only list all the groups on tree
print "Groups in file:"
for group in h5file(classname="Group"):
print group
print

# List only the arrays (using Group iterator) hanging from /
print "Arrays in /:"
for array in h5file.root(classname="Array", recursive=1):
# List only the arrays hanging from /
print "Arrays in file (I):"
for group in h5file.walkGroups("/"):
for array in h5file.listNodes(group, classname = 'Array'):
print array

# This do the same result
print "Arrays in file (II):"
for array in h5file("/", "Array"):
print array
print
# And finally, list only leafs on /detector group (there should be one!)
print "Leafs in group '/detector' (I):"
for leaf in h5file.listNodes("/detector", 'Leaf'):
print leaf

# Other way using iterators and natural naming
print "Leafs in group '/detector' (II):"
for leaf in h5file.root.detector('Leaf'):
print leaf

# And finally, list only tables on /detector group (there should be one!)
print "Tables in group '/detector':"
for table in h5file.listNodes("/detector", 'Leaf'):
print table


print
Expand Down Expand Up @@ -123,7 +128,7 @@
print "Number of rows in table:", table.nrows
print "Table variable names with their type and shape:"
for name in table.colnames:
print " ", name, ':=', table.coltypes[name], table.colshapes[name]
print name, ':= %s, %s' % (table.coltypes[name], table.colshapes[name])
print

# Get the object in "/columns pressure"
Expand All @@ -140,7 +145,7 @@
# Read the 'name' Array actual data
nameArray = h5file.root.columns.name.read()

# Check the kind of object we have created (they should be Numeric arrays)
# Check the kind of object we have created (they should be numarray arrays)
print "pressureArray is an object of type:", type(pressureArray)
print "nameArray is an object of type:", type(nameArray)
print
Expand Down Expand Up @@ -186,11 +191,23 @@
# Flush this table
table.flush()

# Print the data using the table iterator:
for r in table:
print "%-16s | %11.1f | %11.4g | %6d | %6d | %8d |" % \
(r['name'], r['pressure'], r['energy'], r['grid_i'], r['grid_j'],
r['TDCcount'])

print
print "Total number of entries in resulting table:", table.nrows

print
print '-**-**-**-**- remove records from a table -**-**-**-**-**-'

# Delete some rows on the Table (yes, rows can be removed!)
table.removeRows(5,10)

# Print some table columns, for comparison with array data
print "Some columns on enlarged table:"
print "Some columns in final table:"
print
# Print the headers
print "%-16s | %11s | %11s | %6s | %6s | %8s |" % \
Expand All @@ -206,7 +223,7 @@
r['TDCcount'])

print
print "Total number of entries after appending new rows:", table.nrows
print "Total number of entries in final table:", table.nrows

# Close the file
h5file.close()
20 changes: 9 additions & 11 deletions examples/tutorial2.py
Expand Up @@ -5,15 +5,16 @@
"""

from numarray import *
from tables import *

# Describe a particle record
class Particle(IsDescription):
name = StringCol(length=16) # 16-character String
lati = IntCol() # integer
longi = IntCol() # integer
pressure = Float32Col() # float (single-precision)
temperature = FloatCol() # double (double-precision)
pressure = Float32Col(shape=(2,3)) # array of floats (single-precision)
temperature = FloatCol(shape=(2,3)) # array of doubles (double-precision)

# Another way to describe the columns of a table
Event = {
Expand Down Expand Up @@ -48,8 +49,8 @@ class Particle(IsDescription):
particle['name'] = 'Particle: %6d' % (i)
particle['lati'] = i
particle['longi'] = 10 - i
particle['pressure'] = float(i*i)
particle['temperature'] = float(i**2)
particle['pressure'] = array(i*arange(2*3), shape=(2,3))
particle['temperature'] = array((i**2)*arange(2*3, shape=(2,3)))
# This injects the Record values
particle.append()

Expand All @@ -67,15 +68,12 @@ class Particle(IsDescription):
for i in xrange(257):
# First, assign the values to the Event record
event['name'] = 'Event: %6d' % (i)
# Range checks no longer works on 0.4. Hope that
# next version of numarray can support that!
#event['TDCcount'] = i # Wrong range.
event['TDCcount'] = i % (1<<8) # Correct range
########### Detectable errors start here. Play with them!
#event['xcoord'] = float(i**2) # Correct spelling
event['xcoor'] = float(i**2) # Wrong spelling
#event['ADCcount'] = i * 2 # Correct type
event['ADCcount'] = "s" # Wrong type
event['xcoord'] = float(i**2) # Correct spelling
#event['xcoor'] = float(i**2) # Wrong spelling
event['ADCcount'] = i * 2 # Correct type
#event['ADCcount'] = "s" # Wrong type
########### End of errors
event['ycoord'] = float(i)**4
# This injects the Record values
Expand Down
8 changes: 4 additions & 4 deletions tables/AttributeSet.py
Expand Up @@ -5,7 +5,7 @@
# Author: Francesc Alted - falted@openlc.org
#
# $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/tables/AttributeSet.py,v $
# $Id: AttributeSet.py,v 1.12 2003/07/16 20:17:56 falted Exp $
# $Id: AttributeSet.py,v 1.13 2003/07/27 20:40:16 falted Exp $
#
########################################################################

Expand All @@ -31,7 +31,7 @@
"""

__version__ = "$Revision: 1.12 $"
__version__ = "$Revision: 1.13 $"

import warnings, types, cPickle
import hdf5Extension
Expand Down Expand Up @@ -298,7 +298,7 @@ def __str__(self):
classname = self.__class__.__name__
# The attrribute names
attrnumber = len(self._v_attrnames)
return "%s (%s): %s attributes" % (pathname, classname, attrnumber)
return "%s (%s), %s attributes" % (pathname, classname, attrnumber)

def __repr__(self):
"""A detailed string representation for this object."""
Expand All @@ -307,6 +307,6 @@ def __repr__(self):
for attr in self._v_attrnames ]
attrlist = '[%s]' % (',\n '.join(rep))

return "%s\n %s" % \
return "%s:\n %s" % \
(str(self), attrlist)

9 changes: 7 additions & 2 deletions tables/File.py
Expand Up @@ -4,7 +4,7 @@
# Author: Francesc Alted - falted@openlc.org
#
# $Source: /home/ivan/_/programari/pytables/svn/cvs/pytables/pytables/tables/File.py,v $
# $Id: File.py,v 1.47 2003/07/26 18:42:53 falted Exp $
# $Id: File.py,v 1.48 2003/07/27 20:40:16 falted Exp $
#
########################################################################

Expand All @@ -31,7 +31,7 @@
"""

__version__ = "$Revision: 1.47 $"
__version__ = "$Revision: 1.48 $"
format_version = "1.1" # File format version we write
compatible_formats = [] # Old format versions we can read

Expand Down Expand Up @@ -620,6 +620,11 @@ def _iterTree(self, where="/", classname=""):
if classname == "Group":
for group in self.walkGroups(where):
yield group
elif classname in [None, ""]:
yield self.getNode(where, "")
for group in self.walkGroups(where):
for leaf in self.listNodes(group, ""):
yield leaf
else:
for group in self.walkGroups(where):
for leaf in self.listNodes(group, classname):
Expand Down

0 comments on commit 04a12e6

Please sign in to comment.