1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
5
>Multidimensional table cells and automatic
9
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
11
TITLE="PyTables User's Guide"
12
HREF="index.html"><LINK
15
HREF="c514.html"><LINK
17
TITLE="Commiting data to tables and arrays"
18
HREF="x921.html"><LINK
20
TITLE="Exercising the Undo/Redo feature"
21
HREF="x1085.html"></HEAD
32
SUMMARY="Header navigation table"
44
> User's Guide: Hierarchical datasets in Python - Release 1.3.2</TH
60
>Chapter 3. Tutorials</TD
81
>3.4. Multidimensional table cells and automatic
88
>Now it's time for a more real-life example (i.e. with
89
errors in the code). We will create two groups that branch
90
directly from the <SAMP
91
CLASS="computeroutput"
95
CLASS="computeroutput"
98
CLASS="computeroutput"
101
will put three tables in each group. In
103
CLASS="computeroutput"
105
> we will put tables based on the
107
CLASS="computeroutput"
109
> descriptor and in <SAMP
110
CLASS="computeroutput"
113
the tables based the <SAMP
114
CLASS="computeroutput"
119
> Afterwards, we will provision the tables with a number of
120
records. Finally, we will read the newly-created table
122
CLASS="computeroutput"
123
>/Events/TEvent3</SAMP
124
> and select some values from it,
125
using a comprehension list.
128
>Look at the next script (you can find it in
130
CLASS="computeroutput"
131
>examples/tutorial2.py</SAMP
132
>). It appears to do all of
133
the above, but it contains some small bugs. Note that this
135
CLASS="computeroutput"
137
> class is not directly related to the
138
one defined in last tutorial; this class is simpler (note,
147
CLASS="computeroutput"
150
CLASS="computeroutput"
155
>We also introduce a new manner to describe a <SAMP
156
CLASS="computeroutput"
159
as a dictionary, as you can see in the <SAMP
160
CLASS="computeroutput"
163
description. See section <A
164
HREF="x1533.html#createTableDescr"
166
> about the different kinds of
167
descriptor objects that can be passed to the
169
CLASS="computeroutput"
175
> from numarray import *
178
# Describe a particle record
179
class Particle(IsDescription):
180
name = StringCol(length=16) # 16-character String
181
lati = IntCol() # integer
182
longi = IntCol() # integer
183
pressure = Float32Col(shape=(2,3)) # array of floats (single-precision)
184
temperature = FloatCol(shape=(2,3)) # array of doubles (double-precision)
186
# Another way to describe the columns of a table
188
"name" : StringCol(length=16),
191
"pressure" : Float32Col(shape=(2,3)),
192
"temperature" : FloatCol(shape=(2,3)),
195
# Open a file in "w"rite mode
196
fileh = openFile("tutorial2.h5", mode = "w")
197
# Get the HDF5 root group
200
for groupname in ("Particles", "Events"):
201
group = fileh.createGroup(root, groupname)
202
# Now, create and fill the tables in the Particles group
203
gparticles = root.Particles
204
# Create 3 new tables
205
for tablename in ("TParticle1", "TParticle2", "TParticle3"):
207
table = fileh.createTable("/Particles", tablename, Particle,
208
"Particles: "+tablename)
209
# Get the record object associated with the table:
211
# Fill the table with data for 257 particles
212
for i in xrange(257):
213
# First, assign the values to the Particle record
214
particle['name'] = 'Particle: %6d' % (i)
216
particle['longi'] = 10 - i
217
########### Detectable errors start here. Play with them!
218
particle['pressure'] = array(i*arange(2*3), shape=(2,4)) # Incorrect
219
#particle['pressure'] = array(i*arange(2*3), shape=(2,3)) # Correct
220
########### End of errors
221
particle['temperature'] = (i**2) # Broadcasting
222
# This injects the Record values
224
# Flush the table buffers
228
for tablename in ("TEvent1", "TEvent2", "TEvent3"):
229
# Create a table in the Events group
230
table = fileh.createTable(root.Events, tablename, Event,
231
"Events: "+tablename)
232
# Get the record object associated with the table:
234
# Fill the table with data on 257 events
235
for i in xrange(257):
236
# First, assign the values to the Event record
237
event['name'] = 'Event: %6d' % (i)
238
event['TDCcount'] = i % (1<<8) # Correct range
239
########### Detectable errors start here. Play with them!
240
#event['xcoord'] = float(i**2) # Correct spelling
241
event['xcoor'] = float(i**2) # Wrong spelling
242
event['ADCcount'] = i * 2 # Correct type
243
#event['ADCcount'] = "sss" # Wrong type
244
########### End of errors
245
event['ycoord'] = float(i)**4
246
# This injects the Record values
252
# Read the records from table "/Events/TEvent3" and select some
253
table = root.Events.TEvent3
254
e = [ p['TDCcount'] for p in table
255
if p['ADCcount'] < 20 and 4 <= p['TDCcount'] < 15 ]
256
print "Last record ==>", p
257
print "Selected values ==>", e
258
print "Total selected records ==> ", len(e)
259
# Finally, close the file (this also will flush all the remaining buffers)
267
NAME="subsection3.4.1"
268
>3.4.1. Shape checking</A
271
>If you look at the code carefully, you'll see that it
272
won't work. You will get the following error:</P
275
> $ python tutorial2.py
276
Traceback (most recent call last):
277
File "tutorial2.py", line 53, in ?
278
particle['pressure'] = array(i*arange(2*3), shape=(2,4)) # Incorrect
279
File "/usr/local/lib/python2.2/site-packages/numarray/numarraycore.py",
282
File "/usr/local/lib/python2.2/site-packages/numarray/generic.py",
283
line 530, in setshape
284
raise ValueError("New shape is not consistent with the old shape")
285
ValueError: New shape is not consistent with the old shape
288
>This error indicates that you are trying to assign an
289
array with an incompatible shape to a table cell. Looking
290
at the source, we see that we were trying to assign an
292
CLASS="computeroutput"
296
CLASS="computeroutput"
298
> element, which was defined with the
300
CLASS="computeroutput"
305
>In general, these kinds of operations are forbidden, with
306
one valid exception: when you assign a <SPAN
313
value to a multidimensional column cell, all the cell
314
elements are populated with the value of the scalar. For
319
> particle['temperature'] = (i**2) # Broadcasting
323
CLASS="computeroutput"
325
> is assigned to all the
326
elements of the <SAMP
327
CLASS="computeroutput"
330
capability is provided by the <SAMP
331
CLASS="computeroutput"
334
package and is known as <SPAN
348
NAME="subsection3.4.2"
349
>3.4.2. Field name checking</A
352
>After fixing the previous error and rerunning the
353
program, we encounter another error:
357
> $ python tutorial2.py
358
Traceback (most recent call last):
359
File "tutorial2.py", line 74, in ?
360
event['xcoor'] = float(i**2) # Wrong spelling
361
File "src/hdf5Extension.pyx",
362
line 1812, in hdf5Extension.Row.__setitem__
363
raise KeyError, "Error setting \"%s\" field.\n %s" % \
364
KeyError: Error setting "xcoor" field.
365
Error was: "exceptions.KeyError: xcoor"
368
>This error indicates that we are attempting to assign a
369
value to a non-existent field in the <SPAN
376
object. By looking carefully at the <SAMP
377
CLASS="computeroutput"
380
class attributes, we see that we misspelled the
382
CLASS="computeroutput"
384
> field (we wrote <SAMP
385
CLASS="computeroutput"
388
instead). This is unusual behavior for Python, as normally
389
when you assign a value to a non-existent instance
390
variable, Python creates a new variable with that
391
name. Such a feature can be dangerous when dealing with an
392
object that contains a fixed list of field names. PyTables
393
checks that the field exists and raises a
395
CLASS="computeroutput"
397
> if the check fails.
405
NAME="subsection3.4.3"
406
>3.4.3. Data type checking</A
409
>Finally, in order to test type checking, we will change
414
> event.ADCcount = i * 2 # Correct type
420
> event.ADCcount = "sss" # Wrong type
423
>This modification will cause the following <SAMP
424
CLASS="computeroutput"
427
exception to be raised when the script is executed:
431
> $ python tutorial2.py
432
Traceback (most recent call last):
433
File "tutorial2.py", line 76, in ?
434
event['ADCcount'] = "sss" # Wrong type
435
File "src/hdf5Extension.pyx",
436
line 1812, in hdf5Extension.Row.__setitem__
437
raise KeyError, "Error setting \"%s\" field.\n %s" % \
438
KeyError: Error setting "ADCcount" field.
439
Error was: "exceptions.TypeError: NA_setFromPythonScalar: bad value type."
442
>You can see the structure created with this (corrected)
444
HREF="x1017.html#tutorial2-tableview"
447
In particular, note the multidimensional column cells in
449
CLASS="computeroutput"
450
>/Particles/TParticle2</SAMP
456
NAME="tutorial2-tableview"
460
>Figure 3.4. Table hierarchy for tutorial 2.</B
466
SRC="tutorial2-tableview.png"></P
476
SUMMARY="Footer navigation table"
515
>Commiting data to tables and arrays</TD
529
>Exercising the Undo/Redo feature</TD
b'\\ No newline at end of file'