~ubuntu-branches/ubuntu/natty/pytables/natty-updates

« back to all changes in this revision

Viewing changes to doc/html/x1017.html

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2006-06-28 10:45:03 UTC
  • mfrom: (1.2.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20060628104503-cc251q5o5j3e2k10
  * Fixed call to pyversions in debian/rules which failed on recent versions 
    of pyversions
  * Fixed clean rule in debian/rules which left the stamp files behind
  * Acknowledge NMU
  * Added Alexandre Fayolle to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
 
2
<HTML
 
3
><HEAD
 
4
><TITLE
 
5
>Multidimensional table cells and automatic
 
6
          sanity checks</TITLE
 
7
><META
 
8
NAME="GENERATOR"
 
9
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
10
REL="HOME"
 
11
TITLE="PyTables User's Guide"
 
12
HREF="index.html"><LINK
 
13
REL="UP"
 
14
TITLE="Tutorials"
 
15
HREF="c514.html"><LINK
 
16
REL="PREVIOUS"
 
17
TITLE="Commiting data to tables and arrays"
 
18
HREF="x921.html"><LINK
 
19
REL="NEXT"
 
20
TITLE="Exercising the Undo/Redo feature"
 
21
HREF="x1085.html"></HEAD
 
22
><BODY
 
23
CLASS="sect1"
 
24
BGCOLOR="#FFFFFF"
 
25
TEXT="#000000"
 
26
LINK="#0000FF"
 
27
VLINK="#840084"
 
28
ALINK="#0000FF"
 
29
><DIV
 
30
CLASS="NAVHEADER"
 
31
><TABLE
 
32
SUMMARY="Header navigation table"
 
33
WIDTH="100%"
 
34
BORDER="0"
 
35
CELLPADDING="0"
 
36
CELLSPACING="0"
 
37
><TR
 
38
><TH
 
39
COLSPAN="3"
 
40
ALIGN="center"
 
41
><SPAN
 
42
CLASS="markup"
 
43
>PyTables</SPAN
 
44
> User's Guide: Hierarchical datasets in Python - Release 1.3.2</TH
 
45
></TR
 
46
><TR
 
47
><TD
 
48
WIDTH="10%"
 
49
ALIGN="left"
 
50
VALIGN="bottom"
 
51
><A
 
52
HREF="x921.html"
 
53
ACCESSKEY="P"
 
54
>Prev</A
 
55
></TD
 
56
><TD
 
57
WIDTH="80%"
 
58
ALIGN="center"
 
59
VALIGN="bottom"
 
60
>Chapter 3. Tutorials</TD
 
61
><TD
 
62
WIDTH="10%"
 
63
ALIGN="right"
 
64
VALIGN="bottom"
 
65
><A
 
66
HREF="x1085.html"
 
67
ACCESSKEY="N"
 
68
>Next</A
 
69
></TD
 
70
></TR
 
71
></TABLE
 
72
><HR
 
73
ALIGN="LEFT"
 
74
WIDTH="100%"></DIV
 
75
><DIV
 
76
CLASS="sect1"
 
77
><H1
 
78
CLASS="sect1"
 
79
><A
 
80
NAME="section3.4"
 
81
>3.4. Multidimensional table cells and automatic
 
82
          sanity checks</A
 
83
></H1
 
84
><A
 
85
NAME="secondExample"
 
86
></A
 
87
><P
 
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"
 
92
>root</SAMP
 
93
> node,
 
94
          <SAMP
 
95
CLASS="computeroutput"
 
96
>Particles</SAMP
 
97
> and <SAMP
 
98
CLASS="computeroutput"
 
99
>Events</SAMP
 
100
>. Then, we
 
101
          will put three tables in each group. In
 
102
          <SAMP
 
103
CLASS="computeroutput"
 
104
>Particles</SAMP
 
105
> we will put tables based on the
 
106
          <SAMP
 
107
CLASS="computeroutput"
 
108
>Particle</SAMP
 
109
> descriptor and in <SAMP
 
110
CLASS="computeroutput"
 
111
>Events</SAMP
 
112
>,
 
113
          the tables based the <SAMP
 
114
CLASS="computeroutput"
 
115
>Event</SAMP
 
116
> descriptor.
 
117
        </P
 
118
><P
 
119
>&#13;    Afterwards, we will provision the tables with a number of
 
120
          records. Finally, we will read the newly-created table
 
121
          <SAMP
 
122
CLASS="computeroutput"
 
123
>/Events/TEvent3</SAMP
 
124
> and select some values from it,
 
125
          using a comprehension list.
 
126
        </P
 
127
><P
 
128
>Look at the next script (you can find it in
 
129
          <SAMP
 
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
 
134
          <SAMP
 
135
CLASS="computeroutput"
 
136
>Particle</SAMP
 
137
> class is not directly related to the
 
138
          one defined in last tutorial; this class is simpler (note,
 
139
          however, the <SPAN
 
140
CLASS="emphasis"
 
141
><I
 
142
CLASS="emphasis"
 
143
>multidimensional</I
 
144
></SPAN
 
145
> columns called
 
146
          <SAMP
 
147
CLASS="computeroutput"
 
148
>pressure</SAMP
 
149
> and <SAMP
 
150
CLASS="computeroutput"
 
151
>temperature</SAMP
 
152
>).
 
153
        </P
 
154
><P
 
155
>We also introduce a new manner to describe a <SAMP
 
156
CLASS="computeroutput"
 
157
>Table</SAMP
 
158
>
 
159
          as a dictionary, as you can see in the <SAMP
 
160
CLASS="computeroutput"
 
161
>Event</SAMP
 
162
>
 
163
          description. See section <A
 
164
HREF="x1533.html#createTableDescr"
 
165
>4.2.2</A
 
166
> about the different kinds of
 
167
          descriptor objects that can be passed to the
 
168
          <SAMP
 
169
CLASS="computeroutput"
 
170
>createTable()</SAMP
 
171
> method.
 
172
        </P
 
173
><PRE
 
174
CLASS="screen"
 
175
>&#13;from numarray import *
 
176
from tables import *
 
177
 
 
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)
 
185
 
 
186
# Another way to describe the columns of a table
 
187
Event = {
 
188
    "name"        : StringCol(length=16),
 
189
    "lati"        : IntCol(),
 
190
    "longi"       : IntCol(),
 
191
    "pressure"    : Float32Col(shape=(2,3)),
 
192
    "temperature" : FloatCol(shape=(2,3)),
 
193
    }
 
194
 
 
195
# Open a file in "w"rite mode
 
196
fileh = openFile("tutorial2.h5", mode = "w")
 
197
# Get the HDF5 root group
 
198
root = fileh.root
 
199
# Create the groups:
 
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"):
 
206
    # Create a table
 
207
    table = fileh.createTable("/Particles", tablename, Particle,
 
208
                           "Particles: "+tablename)
 
209
    # Get the record object associated with the table:
 
210
    particle = table.row
 
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)
 
215
        particle['lati'] = 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
 
223
        particle.append()
 
224
    # Flush the table buffers
 
225
    table.flush()
 
226
 
 
227
# Now Events:
 
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:
 
233
    event = table.row
 
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&#60;&#60;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
 
247
        event.append()
 
248
 
 
249
    # Flush the buffers
 
250
    table.flush()
 
251
 
 
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'] &#60; 20 and 4 &#60;= p['TDCcount'] &#60; 15 ]
 
256
print "Last record ==&#62;", p
 
257
print "Selected values ==&#62;", e
 
258
print "Total selected records ==&#62; ", len(e)
 
259
# Finally, close the file (this also will flush all the remaining buffers)
 
260
fileh.close()
 
261
        </PRE
 
262
><DIV
 
263
CLASS="sect2"
 
264
><H2
 
265
CLASS="sect2"
 
266
><A
 
267
NAME="subsection3.4.1"
 
268
>3.4.1. Shape checking</A
 
269
></H2
 
270
><P
 
271
>If you look at the code carefully, you'll see that it
 
272
            won't work. You will get the following error:</P
 
273
><PRE
 
274
CLASS="screen"
 
275
>&#13;$ 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",
 
280
 line 281, in array
 
281
  a.setshape(shape)
 
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
 
286
        </PRE
 
287
><P
 
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
 
291
            array of shape <SAMP
 
292
CLASS="computeroutput"
 
293
>(2,4)</SAMP
 
294
> to a
 
295
            <SAMP
 
296
CLASS="computeroutput"
 
297
>pressure</SAMP
 
298
> element, which was defined with the
 
299
            shape <SAMP
 
300
CLASS="computeroutput"
 
301
>(2,3)</SAMP
 
302
>.
 
303
          </P
 
304
><P
 
305
>In general, these kinds of operations are forbidden, with
 
306
            one valid exception: when you assign a <SPAN
 
307
CLASS="emphasis"
 
308
><I
 
309
CLASS="emphasis"
 
310
>scalar</I
 
311
></SPAN
 
312
>
 
313
            value to a multidimensional column cell, all the cell
 
314
            elements are populated with the value of the scalar. For
 
315
            example:
 
316
          </P
 
317
><PRE
 
318
CLASS="screen"
 
319
>&#13;        particle['temperature'] = (i**2)    # Broadcasting
 
320
          </PRE
 
321
><P
 
322
>The value <SAMP
 
323
CLASS="computeroutput"
 
324
>i**2</SAMP
 
325
> is assigned to all the
 
326
            elements of the <SAMP
 
327
CLASS="computeroutput"
 
328
>temperature</SAMP
 
329
> table cell. This
 
330
            capability is provided by the <SAMP
 
331
CLASS="computeroutput"
 
332
>numarray</SAMP
 
333
>
 
334
            package and is known as <SPAN
 
335
CLASS="emphasis"
 
336
><I
 
337
CLASS="emphasis"
 
338
>broadcasting</I
 
339
></SPAN
 
340
>.
 
341
          </P
 
342
></DIV
 
343
><DIV
 
344
CLASS="sect2"
 
345
><H2
 
346
CLASS="sect2"
 
347
><A
 
348
NAME="subsection3.4.2"
 
349
>3.4.2. Field name checking</A
 
350
></H2
 
351
><P
 
352
>After fixing the previous error and rerunning the
 
353
          program, we encounter another error:
 
354
          </P
 
355
><PRE
 
356
CLASS="screen"
 
357
>&#13;$ 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"
 
366
          </PRE
 
367
><P
 
368
>This error indicates that we are attempting to assign a
 
369
            value to a non-existent field in the <SPAN
 
370
CLASS="emphasis"
 
371
><I
 
372
CLASS="emphasis"
 
373
>event</I
 
374
></SPAN
 
375
> table
 
376
            object. By looking carefully at the <SAMP
 
377
CLASS="computeroutput"
 
378
>Event</SAMP
 
379
>
 
380
            class attributes, we see that we misspelled the
 
381
            <SAMP
 
382
CLASS="computeroutput"
 
383
>xcoord</SAMP
 
384
> field (we wrote <SAMP
 
385
CLASS="computeroutput"
 
386
>xcoor</SAMP
 
387
>
 
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
 
394
            <SAMP
 
395
CLASS="computeroutput"
 
396
>KeyError</SAMP
 
397
> if the check fails.
 
398
          </P
 
399
></DIV
 
400
><DIV
 
401
CLASS="sect2"
 
402
><H2
 
403
CLASS="sect2"
 
404
><A
 
405
NAME="subsection3.4.3"
 
406
>3.4.3. Data type checking</A
 
407
></H2
 
408
><P
 
409
>Finally, in order to test type checking, we will change
 
410
            the next line:
 
411
          </P
 
412
><PRE
 
413
CLASS="screen"
 
414
>&#13;      event.ADCcount = i * 2        # Correct type
 
415
          </PRE
 
416
><P
 
417
>to read:</P
 
418
><PRE
 
419
CLASS="screen"
 
420
>&#13;      event.ADCcount = "sss"          # Wrong type
 
421
          </PRE
 
422
><P
 
423
>This modification will cause the following <SAMP
 
424
CLASS="computeroutput"
 
425
>TypeError</SAMP
 
426
>
 
427
            exception to be raised when the script is executed:
 
428
          </P
 
429
><PRE
 
430
CLASS="screen"
 
431
>&#13;$ 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."
 
440
          </PRE
 
441
><P
 
442
>You can see the structure created with this (corrected)
 
443
            script in <A
 
444
HREF="x1017.html#tutorial2-tableview"
 
445
>figure�3.4</A
 
446
>.
 
447
            In particular, note the multidimensional column cells in
 
448
            table <SAMP
 
449
CLASS="computeroutput"
 
450
>/Particles/TParticle2</SAMP
 
451
>.
 
452
          </P
 
453
><DIV
 
454
CLASS="figure"
 
455
><A
 
456
NAME="tutorial2-tableview"
 
457
></A
 
458
><P
 
459
><B
 
460
>Figure 3.4. Table hierarchy for tutorial 2.</B
 
461
></P
 
462
><DIV
 
463
CLASS="mediaobject"
 
464
><P
 
465
><IMG
 
466
SRC="tutorial2-tableview.png"></P
 
467
></DIV
 
468
></DIV
 
469
></DIV
 
470
></DIV
 
471
><DIV
 
472
CLASS="NAVFOOTER"
 
473
><HR
 
474
ALIGN="LEFT"
 
475
WIDTH="100%"><TABLE
 
476
SUMMARY="Footer navigation table"
 
477
WIDTH="100%"
 
478
BORDER="0"
 
479
CELLPADDING="0"
 
480
CELLSPACING="0"
 
481
><TR
 
482
><TD
 
483
WIDTH="33%"
 
484
ALIGN="left"
 
485
VALIGN="top"
 
486
><A
 
487
HREF="x921.html"
 
488
ACCESSKEY="P"
 
489
>Prev</A
 
490
></TD
 
491
><TD
 
492
WIDTH="34%"
 
493
ALIGN="center"
 
494
VALIGN="top"
 
495
><A
 
496
HREF="index.html"
 
497
ACCESSKEY="H"
 
498
>Home</A
 
499
></TD
 
500
><TD
 
501
WIDTH="33%"
 
502
ALIGN="right"
 
503
VALIGN="top"
 
504
><A
 
505
HREF="x1085.html"
 
506
ACCESSKEY="N"
 
507
>Next</A
 
508
></TD
 
509
></TR
 
510
><TR
 
511
><TD
 
512
WIDTH="33%"
 
513
ALIGN="left"
 
514
VALIGN="top"
 
515
>Commiting data to tables and arrays</TD
 
516
><TD
 
517
WIDTH="34%"
 
518
ALIGN="center"
 
519
VALIGN="top"
 
520
><A
 
521
HREF="c514.html"
 
522
ACCESSKEY="U"
 
523
>Up</A
 
524
></TD
 
525
><TD
 
526
WIDTH="33%"
 
527
ALIGN="right"
 
528
VALIGN="top"
 
529
>Exercising the Undo/Redo feature</TD
 
530
></TR
 
531
></TABLE
 
532
></DIV
 
533
></BODY
 
534
></HTML
 
535
>
 
 
b'\\ No newline at end of file'