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

« back to all changes in this revision

Viewing changes to doc/html/c514.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
>Tutorials</TITLE
 
6
><META
 
7
NAME="GENERATOR"
 
8
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
 
9
REL="HOME"
 
10
TITLE="PyTables User's Guide"
 
11
HREF="index.html"><LINK
 
12
REL="UP"
 
13
TITLE="The PyTables Core
 
14
        Library"
 
15
HREF="p35.html"><LINK
 
16
REL="PREVIOUS"
 
17
TITLE="Binary installation (Windows)"
 
18
HREF="x457.html"><LINK
 
19
REL="NEXT"
 
20
TITLE="Browsing the object tree
 
21
        "
 
22
HREF="x762.html"></HEAD
 
23
><BODY
 
24
CLASS="chapter"
 
25
BGCOLOR="#FFFFFF"
 
26
TEXT="#000000"
 
27
LINK="#0000FF"
 
28
VLINK="#840084"
 
29
ALINK="#0000FF"
 
30
><DIV
 
31
CLASS="NAVHEADER"
 
32
><TABLE
 
33
SUMMARY="Header navigation table"
 
34
WIDTH="100%"
 
35
BORDER="0"
 
36
CELLPADDING="0"
 
37
CELLSPACING="0"
 
38
><TR
 
39
><TH
 
40
COLSPAN="3"
 
41
ALIGN="center"
 
42
><SPAN
 
43
CLASS="markup"
 
44
>PyTables</SPAN
 
45
> User's Guide: Hierarchical datasets in Python - Release 1.3.2</TH
 
46
></TR
 
47
><TR
 
48
><TD
 
49
WIDTH="10%"
 
50
ALIGN="left"
 
51
VALIGN="bottom"
 
52
><A
 
53
HREF="x457.html"
 
54
ACCESSKEY="P"
 
55
>Prev</A
 
56
></TD
 
57
><TD
 
58
WIDTH="80%"
 
59
ALIGN="center"
 
60
VALIGN="bottom"
 
61
></TD
 
62
><TD
 
63
WIDTH="10%"
 
64
ALIGN="right"
 
65
VALIGN="bottom"
 
66
><A
 
67
HREF="x762.html"
 
68
ACCESSKEY="N"
 
69
>Next</A
 
70
></TD
 
71
></TR
 
72
></TABLE
 
73
><HR
 
74
ALIGN="LEFT"
 
75
WIDTH="100%"></DIV
 
76
><DIV
 
77
CLASS="chapter"
 
78
><H1
 
79
><A
 
80
NAME="chapter3"
 
81
></A
 
82
>Chapter 3. Tutorials</H1
 
83
><A
 
84
NAME="usage"
 
85
></A
 
86
><TABLE
 
87
BORDER="0"
 
88
WIDTH="100%"
 
89
CELLSPACING="0"
 
90
CELLPADDING="0"
 
91
CLASS="EPIGRAPH"
 
92
><TR
 
93
><TD
 
94
WIDTH="45%"
 
95
>&nbsp;</TD
 
96
><TD
 
97
WIDTH="45%"
 
98
ALIGN="LEFT"
 
99
VALIGN="TOP"
 
100
><I
 
101
><P
 
102
><I
 
103
>Ser�s la clau que obre tots els panys, 
 
104
        ser�s la llum, la llum il.limitada,  ser�s conf� on
 
105
        l'aurora comen�a, ser�s forment, escala il.luminada!</I
 
106
></P
 
107
></I
 
108
></TD
 
109
></TR
 
110
><TR
 
111
><TD
 
112
WIDTH="45%"
 
113
>&nbsp;</TD
 
114
><TD
 
115
WIDTH="45%"
 
116
ALIGN="RIGHT"
 
117
VALIGN="TOP"
 
118
><I
 
119
><SPAN
 
120
CLASS="attribution"
 
121
>&#8212;M'aclame a tu Lyrics: Vicent
 
122
        Andr�s i Estell�s Music: Ovidi Montllor</SPAN
 
123
></I
 
124
></TD
 
125
></TR
 
126
></TABLE
 
127
><P
 
128
>This chapter consists of a series of simple yet comprehensive
 
129
        tutorials that will enable you to understand
 
130
        <SAMP
 
131
CLASS="computeroutput"
 
132
>PyTables</SAMP
 
133
>' main features. If you would like more
 
134
        information about some particular instance variable, global
 
135
        function, or method, look at the doc strings or go to the
 
136
        library reference in <A
 
137
HREF="c1381.html#libraryReference"
 
138
>chapter�4</A
 
139
>. If you are reading
 
140
        this in PDF or HTML formats, follow the corresponding
 
141
        hyperlink near each newly introduced entity.
 
142
      </P
 
143
><P
 
144
>Please, note that throughout this document the terms
 
145
        <SPAN
 
146
CLASS="emphasis"
 
147
><I
 
148
CLASS="emphasis"
 
149
>column</I
 
150
></SPAN
 
151
> and <SPAN
 
152
CLASS="emphasis"
 
153
><I
 
154
CLASS="emphasis"
 
155
>field</I
 
156
></SPAN
 
157
> will be used
 
158
        interchangeably, as will the terms <SPAN
 
159
CLASS="emphasis"
 
160
><I
 
161
CLASS="emphasis"
 
162
>row</I
 
163
></SPAN
 
164
> and
 
165
        <SPAN
 
166
CLASS="emphasis"
 
167
><I
 
168
CLASS="emphasis"
 
169
>record</I
 
170
></SPAN
 
171
>.
 
172
      </P
 
173
><DIV
 
174
CLASS="sect1"
 
175
><H1
 
176
CLASS="sect1"
 
177
><A
 
178
NAME="section3.1"
 
179
>3.1. Getting started</A
 
180
></H1
 
181
><P
 
182
>In this section, we will see how to define our own records
 
183
          in Python and save collections of them (i.e. a <SPAN
 
184
CLASS="emphasis"
 
185
><I
 
186
CLASS="emphasis"
 
187
>table</I
 
188
></SPAN
 
189
>) into a file. Then we will select
 
190
          some of the data in the table using Python cuts and create
 
191
          <SAMP
 
192
CLASS="computeroutput"
 
193
>numarray</SAMP
 
194
> arrays to store this selection as
 
195
          separate objects in a tree.
 
196
        </P
 
197
><P
 
198
>&#13;    In <SPAN
 
199
CLASS="emphasis"
 
200
><I
 
201
CLASS="emphasis"
 
202
>examples/tutorial1-1.py</I
 
203
></SPAN
 
204
> you will find the
 
205
          working version of all the code in this
 
206
          section. Nonetheless, this tutorial series has been written
 
207
          to allow you reproduce it in a Python interactive console. I
 
208
          encourage you to do parallel testing and inspect the created
 
209
          objects (variables, docs, children objects, etc.) during the
 
210
          course of the tutorial!
 
211
        </P
 
212
><DIV
 
213
CLASS="sect2"
 
214
><H2
 
215
CLASS="sect2"
 
216
><A
 
217
NAME="subsection3.1.1"
 
218
>3.1.1. Importing <SPAN
 
219
CLASS="markup"
 
220
>tables</SPAN
 
221
>
 
222
            objects</A
 
223
></H2
 
224
><P
 
225
>Before starting you need to import the
 
226
            public objects in the <SAMP
 
227
CLASS="computeroutput"
 
228
>tables</SAMP
 
229
> package. You
 
230
            normally do that by executing:
 
231
          </P
 
232
><PRE
 
233
CLASS="screen"
 
234
>&#13;&#62;&#62;&#62; import tables
 
235
          </PRE
 
236
><P
 
237
>This is the recommended way to import <SAMP
 
238
CLASS="computeroutput"
 
239
>tables</SAMP
 
240
>
 
241
            if you don't want to pollute your namespace. However,
 
242
            <SAMP
 
243
CLASS="computeroutput"
 
244
>PyTables</SAMP
 
245
> has a very reduced set of
 
246
            first-level primitives, so you may consider using the
 
247
            alternative:
 
248
          </P
 
249
><PRE
 
250
CLASS="screen"
 
251
>&#13;&#62;&#62;&#62; from tables import *
 
252
          </PRE
 
253
><P
 
254
>which will export in your caller application namespace the
 
255
            following functions: <SAMP
 
256
CLASS="computeroutput"
 
257
>openFile()</SAMP
 
258
>, <SAMP
 
259
CLASS="computeroutput"
 
260
>copyFile()</SAMP
 
261
>,
 
262
            <SAMP
 
263
CLASS="computeroutput"
 
264
>isHDF5File()</SAMP
 
265
>, <SAMP
 
266
CLASS="computeroutput"
 
267
>isPyTablesFile()</SAMP
 
268
> and
 
269
            <SAMP
 
270
CLASS="computeroutput"
 
271
>whichLibVersion()</SAMP
 
272
>. This is a rather reduced set
 
273
            of functions, and for convenience, we will use this
 
274
            technique to access them.
 
275
          </P
 
276
><P
 
277
>If you are going to work with <SAMP
 
278
CLASS="computeroutput"
 
279
>numarray</SAMP
 
280
> (or
 
281
            <SAMP
 
282
CLASS="computeroutput"
 
283
>NumPy</SAMP
 
284
> or <SAMP
 
285
CLASS="computeroutput"
 
286
>Numeric</SAMP
 
287
>) arrays (and
 
288
            normally, you will) you will also need to import functions
 
289
            from them.  So most <SAMP
 
290
CLASS="computeroutput"
 
291
>PyTables</SAMP
 
292
> programs begin
 
293
            with:
 
294
          </P
 
295
><PRE
 
296
CLASS="screen"
 
297
>&#13;&#62;&#62;&#62; import tables        # but in this tutorial we use "from tables import *"
 
298
&#62;&#62;&#62; import numarray      # or "import numpy" or "import Numeric"
 
299
          </PRE
 
300
></DIV
 
301
><DIV
 
302
CLASS="sect2"
 
303
><H2
 
304
CLASS="sect2"
 
305
><A
 
306
NAME="subsection3.1.2"
 
307
>3.1.2. Declaring a Column Descriptor</A
 
308
></H2
 
309
><P
 
310
>Now, imagine that we have a particle detector and we want
 
311
            to create a table object in order to save data
 
312
            retrieved from it. You need first to define the table, the
 
313
            number of columns it has, what kind of object is contained
 
314
            in each column, and so on.
 
315
          </P
 
316
><P
 
317
>Our particle detector has a TDC (Time to Digital
 
318
            Converter) counter with a dynamic range of 8 bits and an
 
319
            ADC (Analogical to Digital Converter) with a range of 16
 
320
            bits. For these values, we will define 2 fields in our
 
321
            record object called <SAMP
 
322
CLASS="computeroutput"
 
323
>TDCcount</SAMP
 
324
> and
 
325
            <SAMP
 
326
CLASS="computeroutput"
 
327
>ADCcount</SAMP
 
328
>. We also want to save the grid
 
329
            position in which the particle has been detected, so we
 
330
            will add two new fields called <SAMP
 
331
CLASS="computeroutput"
 
332
>grid_i</SAMP
 
333
> and
 
334
            <SAMP
 
335
CLASS="computeroutput"
 
336
>grid_j</SAMP
 
337
>. Our instrumentation also can obtain
 
338
            the pressure and energy of the particle. The resolution of
 
339
            the pressure-gauge allows us to use a simple-precision
 
340
            float to store <SAMP
 
341
CLASS="computeroutput"
 
342
>pressure</SAMP
 
343
> readings, while the
 
344
            <SAMP
 
345
CLASS="computeroutput"
 
346
>energy</SAMP
 
347
> value will need a double-precision
 
348
            float. Finally, to track the particle we want to assign it
 
349
            a name to identify the kind of the particle it is and a
 
350
            unique numeric identifier. So we will add two more fields:
 
351
            <SAMP
 
352
CLASS="computeroutput"
 
353
>name</SAMP
 
354
> will be a string of up to 16 characters,
 
355
            and <SAMP
 
356
CLASS="computeroutput"
 
357
>idnumber</SAMP
 
358
> will be an integer of 64 bits
 
359
            (to allow us to store records for extremely large numbers
 
360
            of particles).  
 
361
          </P
 
362
><P
 
363
>Having determined our columns and their types, we can now
 
364
            declare a new <SAMP
 
365
CLASS="computeroutput"
 
366
>Particle</SAMP
 
367
> class that will
 
368
            contain all this information:
 
369
          </P
 
370
><PRE
 
371
CLASS="screen"
 
372
>&#13;&#62;&#62;&#62; class Particle(IsDescription):
 
373
...     name      = StringCol(16)   # 16-character String
 
374
...     idnumber  = Int64Col()      # Signed 64-bit integer
 
375
...     ADCcount  = UInt16Col()     # Unsigned short integer
 
376
...     TDCcount  = UInt8Col()      # unsigned byte
 
377
...     grid_i    = Int32Col()      # integer
 
378
...     grid_j    = IntCol()        # integer (equivalent to Int32Col)
 
379
...     pressure  = Float32Col()    # float  (single-precision)
 
380
...     energy    = FloatCol()      # double (double-precision)
 
381
...
 
382
&#62;&#62;&#62;
 
383
          </PRE
 
384
><P
 
385
>This definition class is self-explanatory.  Basically,
 
386
            you declare a class variable for each field you need.  As
 
387
            its value you assign an instance of the appropriate
 
388
            <SAMP
 
389
CLASS="computeroutput"
 
390
>Col</SAMP
 
391
> subclass, according to the kind of column
 
392
            defined (the data type, the length, the shape, etc).  See
 
393
            the <A
 
394
HREF="x4389.html#ColClassDescr"
 
395
>section�4.16.2</A
 
396
> for a
 
397
            complete description of these subclasses. See also <A
 
398
HREF="a6585.html#datatypesSupported"
 
399
>appendix�A</A
 
400
> for a list of
 
401
            data types supported by the <SAMP
 
402
CLASS="computeroutput"
 
403
>Col</SAMP
 
404
> constructor.
 
405
          </P
 
406
><P
 
407
>From now on, we can use <SAMP
 
408
CLASS="computeroutput"
 
409
>Particle</SAMP
 
410
> instances
 
411
            as a descriptor for our detector data table. We will see
 
412
            later on how to pass this object to construct the table.
 
413
            But first, we must create a file where all the actual data
 
414
            pushed into our table will be saved.
 
415
          </P
 
416
></DIV
 
417
><DIV
 
418
CLASS="sect2"
 
419
><H2
 
420
CLASS="sect2"
 
421
><A
 
422
NAME="subsection3.1.3"
 
423
>3.1.3. Creating a <SPAN
 
424
CLASS="markup"
 
425
>PyTables</SPAN
 
426
> file from scratch</A
 
427
></H2
 
428
><P
 
429
>Use the first-level <SAMP
 
430
CLASS="computeroutput"
 
431
>openFile</SAMP
 
432
> (see <A
 
433
HREF="c1381.html#openFileDescr"
 
434
>4.1.2</A
 
435
>) function to create a 
 
436
            <SAMP
 
437
CLASS="computeroutput"
 
438
>PyTables</SAMP
 
439
> file:
 
440
          </P
 
441
><PRE
 
442
CLASS="screen"
 
443
>&#13;&#62;&#62;&#62; h5file = openFile("tutorial1.h5", mode = "w", title = "Test file")
 
444
          </PRE
 
445
><P
 
446
><SAMP
 
447
CLASS="computeroutput"
 
448
>openFile</SAMP
 
449
> (<A
 
450
HREF="c1381.html#openFileDescr"
 
451
>see�4.1.2</A
 
452
>) is one of the objects
 
453
            imported by the "<SAMP
 
454
CLASS="computeroutput"
 
455
>from tables import *</SAMP
 
456
>"
 
457
            statement. Here, we are saying that we want to create a
 
458
            new file in the current working directory called
 
459
            "<SAMP
 
460
CLASS="computeroutput"
 
461
>tutorial1.h5</SAMP
 
462
>" in "<SAMP
 
463
CLASS="computeroutput"
 
464
>w</SAMP
 
465
>"rite mode
 
466
            and with an descriptive title string ("<SAMP
 
467
CLASS="computeroutput"
 
468
>Test
 
469
            file</SAMP
 
470
>").  This function attempts to open the file,
 
471
            and if successful, returns the <SAMP
 
472
CLASS="computeroutput"
 
473
>File</SAMP
 
474
> (<A
 
475
HREF="x1533.html#FileClassDescr"
 
476
>see�4.2</A
 
477
>) object instance
 
478
            <SAMP
 
479
CLASS="computeroutput"
 
480
>h5file</SAMP
 
481
>.  The root of the object tree is
 
482
            specified in the instance's <SAMP
 
483
CLASS="computeroutput"
 
484
>root</SAMP
 
485
> attribute.
 
486
          </P
 
487
></DIV
 
488
><DIV
 
489
CLASS="sect2"
 
490
><H2
 
491
CLASS="sect2"
 
492
><A
 
493
NAME="subsection3.1.4"
 
494
>3.1.4. Creating a new group</A
 
495
></H2
 
496
><P
 
497
>Now, to better organize our data, we will create a group
 
498
            called <SPAN
 
499
CLASS="emphasis"
 
500
><I
 
501
CLASS="emphasis"
 
502
>detector</I
 
503
></SPAN
 
504
> that branches from the root
 
505
            node. We will save our particle data table in this group.
 
506
          </P
 
507
><PRE
 
508
CLASS="screen"
 
509
>&#13;&#62;&#62;&#62; group = h5file.createGroup("/", 'detector', 'Detector information')
 
510
          </PRE
 
511
><P
 
512
>Here, we have taken the <SAMP
 
513
CLASS="computeroutput"
 
514
>File</SAMP
 
515
> instance
 
516
            <SAMP
 
517
CLASS="computeroutput"
 
518
>h5file</SAMP
 
519
> and invoked its
 
520
            <SAMP
 
521
CLASS="computeroutput"
 
522
>createGroup</SAMP
 
523
> method (<A
 
524
HREF="x1533.html#createGroupDescr"
 
525
>see�4.2.2</A
 
526
>) to create a new group
 
527
            called <SPAN
 
528
CLASS="emphasis"
 
529
><I
 
530
CLASS="emphasis"
 
531
>detector</I
 
532
></SPAN
 
533
> branching from "<SPAN
 
534
CLASS="emphasis"
 
535
><I
 
536
CLASS="emphasis"
 
537
>/</I
 
538
></SPAN
 
539
>"
 
540
            (another way to refer to the <SAMP
 
541
CLASS="computeroutput"
 
542
>h5file.root</SAMP
 
543
>
 
544
            object we mentioned above). This will create a new
 
545
            <SAMP
 
546
CLASS="computeroutput"
 
547
>Group</SAMP
 
548
> (see<A
 
549
HREF="x2546.html#GroupClassDescr"
 
550
>4.4</A
 
551
>) object instance that will
 
552
            be assigned to the variable <SAMP
 
553
CLASS="computeroutput"
 
554
>group</SAMP
 
555
>.
 
556
          </P
 
557
></DIV
 
558
><DIV
 
559
CLASS="sect2"
 
560
><H2
 
561
CLASS="sect2"
 
562
><A
 
563
NAME="subsection3.1.5"
 
564
>3.1.5. Creating a new table</A
 
565
></H2
 
566
><P
 
567
>Let's now create a <SAMP
 
568
CLASS="computeroutput"
 
569
>Table</SAMP
 
570
> (see <A
 
571
HREF="x2981.html#TableClassDescr"
 
572
>4.6</A
 
573
>) object as a branch off the
 
574
            newly-created group. We do that by calling the
 
575
            <SAMP
 
576
CLASS="computeroutput"
 
577
>createTable</SAMP
 
578
> (see <A
 
579
HREF="x1533.html#createTableDescr"
 
580
>4.2.2</A
 
581
>) method of the
 
582
            <SAMP
 
583
CLASS="computeroutput"
 
584
>h5file</SAMP
 
585
> object:
 
586
          </P
 
587
><PRE
 
588
CLASS="screen"
 
589
>&#13;&#62;&#62;&#62; table = h5file.createTable(group, 'readout', Particle, "Readout example")
 
590
          </PRE
 
591
><P
 
592
>We create the <SAMP
 
593
CLASS="computeroutput"
 
594
>Table</SAMP
 
595
> instance under
 
596
            <SAMP
 
597
CLASS="computeroutput"
 
598
>group</SAMP
 
599
>.  We assign this table the node name
 
600
            "<SPAN
 
601
CLASS="emphasis"
 
602
><I
 
603
CLASS="emphasis"
 
604
>readout</I
 
605
></SPAN
 
606
>".  The <SAMP
 
607
CLASS="computeroutput"
 
608
>Particle</SAMP
 
609
> class
 
610
            declared before is the <SPAN
 
611
CLASS="emphasis"
 
612
><I
 
613
CLASS="emphasis"
 
614
>description</I
 
615
></SPAN
 
616
> parameter (to
 
617
            define the columns of the table) and finally we set
 
618
            "<SPAN
 
619
CLASS="emphasis"
 
620
><I
 
621
CLASS="emphasis"
 
622
>Readout example</I
 
623
></SPAN
 
624
>" as the <SAMP
 
625
CLASS="computeroutput"
 
626
>Table</SAMP
 
627
>
 
628
            title. With all this information, a new <SAMP
 
629
CLASS="computeroutput"
 
630
>Table</SAMP
 
631
>
 
632
            instance is created and assigned to the variable
 
633
            <SPAN
 
634
CLASS="emphasis"
 
635
><I
 
636
CLASS="emphasis"
 
637
>table</I
 
638
></SPAN
 
639
>.
 
640
          </P
 
641
><P
 
642
>If you are curious about how the object tree looks right
 
643
            now, simply <SAMP
 
644
CLASS="computeroutput"
 
645
>print</SAMP
 
646
> the <SAMP
 
647
CLASS="computeroutput"
 
648
>File</SAMP
 
649
>
 
650
            instance variable <SPAN
 
651
CLASS="emphasis"
 
652
><I
 
653
CLASS="emphasis"
 
654
>h5file</I
 
655
></SPAN
 
656
>, and examine the output:
 
657
          </P
 
658
><PRE
 
659
CLASS="screen"
 
660
>&#13;&#62;&#62;&#62; print h5file
 
661
Filename: 'tutorial1.h5' Title: 'Test file' Last modif.: 'Sun Jul 27 14:00:13 2003'
 
662
/ (Group) 'Test file'
 
663
/detector (Group) 'Detector information'
 
664
/detector/readout (Table(0,)) 'Readout example'
 
665
 
 
666
          </PRE
 
667
><P
 
668
>As you can see, a dump of the object tree is displayed.
 
669
            It's easy to see the <SAMP
 
670
CLASS="computeroutput"
 
671
>Group</SAMP
 
672
> and
 
673
            <SAMP
 
674
CLASS="computeroutput"
 
675
>Table</SAMP
 
676
> objects we have just created. If you
 
677
            want more information, just type the variable containing the
 
678
            <SAMP
 
679
CLASS="computeroutput"
 
680
>File</SAMP
 
681
> instance:
 
682
          </P
 
683
><PRE
 
684
CLASS="screen"
 
685
>&#13;&#62;&#62;&#62; h5file
 
686
File(filename='tutorial1.h5', title='Test file', mode='w', trMap={}, rootUEP='/')
 
687
/ (Group) 'Test file'
 
688
/detector (Group) 'Detector information'
 
689
/detector/readout (Table(0,)) 'Readout example'
 
690
  description := {
 
691
    "ADCcount": Col('UInt16', shape=1, itemsize=2, dflt=0),
 
692
    "TDCcount": Col('UInt8', shape=1, itemsize= 1, dflt=0),
 
693
    "energy": Col('Float64', shape=1, itemsize=8, dflt=0.0),
 
694
    "grid_i": Col('Int32', shape=1, itemsize=4, dflt=0),
 
695
    "grid_j": Col('Int32', shape=1, itemsize=4, dflt=0),
 
696
    "idnumber": Col('Int64', shape=1, itemsize=8, dflt=0),
 
697
    "name": Col('CharType', shape=1, itemsize=16, dflt=None),
 
698
    "pressure": Col('Float32', shape=1, itemsize=4, dflt=0.0) }
 
699
  byteorder := little
 
700
 
 
701
          </PRE
 
702
><P
 
703
>More detailed information is displayed about each object
 
704
            in the tree. Note how <SAMP
 
705
CLASS="computeroutput"
 
706
>Particle</SAMP
 
707
>, our table
 
708
            descriptor class, is printed as part of the
 
709
            <SPAN
 
710
CLASS="emphasis"
 
711
><I
 
712
CLASS="emphasis"
 
713
>readout</I
 
714
></SPAN
 
715
> table description information. In
 
716
            general, you can obtain much more information about the
 
717
            objects and their children by just printing them. That
 
718
            introspection capability is very useful, and I recommend
 
719
            that you use it extensively.
 
720
          </P
 
721
><P
 
722
>The time has come to fill this table with some
 
723
            values. First we will get a pointer to the
 
724
            <SAMP
 
725
CLASS="computeroutput"
 
726
>Row</SAMP
 
727
> (see <A
 
728
HREF="x2981.html#RowClassDescr"
 
729
>4.6.4</A
 
730
>)
 
731
            instance of this <SAMP
 
732
CLASS="computeroutput"
 
733
>table</SAMP
 
734
> instance:
 
735
          </P
 
736
><PRE
 
737
CLASS="screen"
 
738
>&#13;&#62;&#62;&#62; particle = table.row
 
739
          </PRE
 
740
><P
 
741
>The <SAMP
 
742
CLASS="computeroutput"
 
743
>row</SAMP
 
744
> attribute of <SAMP
 
745
CLASS="computeroutput"
 
746
>table</SAMP
 
747
>
 
748
            points to the <SAMP
 
749
CLASS="computeroutput"
 
750
>Row</SAMP
 
751
> instance that will be used
 
752
            to write data rows into the table. We write data simply by
 
753
            assigning the <SAMP
 
754
CLASS="computeroutput"
 
755
>Row</SAMP
 
756
> instance the values for
 
757
            each row as if it were a dictionary (although it is
 
758
            actually an <SPAN
 
759
CLASS="emphasis"
 
760
><I
 
761
CLASS="emphasis"
 
762
>extension class</I
 
763
></SPAN
 
764
>), using the column
 
765
            names as keys.
 
766
          </P
 
767
><P
 
768
>Below is an example of how to write rows:
 
769
          </P
 
770
><PRE
 
771
CLASS="screen"
 
772
>&#13;&#62;&#62;&#62; for i in xrange(10):
 
773
...     particle['name']  = 'Particle: %6d' % (i)
 
774
...     particle['TDCcount'] = i % 256
 
775
...     particle['ADCcount'] = (i * 256) % (1 &#60;&#60; 16)
 
776
...     particle['grid_i'] = i
 
777
...     particle['grid_j'] = 10 - i
 
778
...     particle['pressure'] = float(i*i)
 
779
...     particle['energy'] = float(particle['pressure'] ** 4)
 
780
...     particle['idnumber'] = i * (2 ** 34)
 
781
...     particle.append()
 
782
...
 
783
&#62;&#62;&#62;
 
784
          </PRE
 
785
><P
 
786
>This code should be easy to understand. The lines inside
 
787
            the loop just assign values to the different columns in
 
788
            the Row instance <SAMP
 
789
CLASS="computeroutput"
 
790
>particle</SAMP
 
791
> (<A
 
792
HREF="x2981.html#RowClassDescr"
 
793
>see�4.6.4</A
 
794
>). A call to its
 
795
            <SAMP
 
796
CLASS="computeroutput"
 
797
>append()</SAMP
 
798
> method writes this
 
799
            information to the <SAMP
 
800
CLASS="computeroutput"
 
801
>table</SAMP
 
802
> I/O buffer.
 
803
          </P
 
804
><P
 
805
>After we have processed all our data, we should flush the
 
806
            table's I/O buffer if we want to write all
 
807
            this data to disk. We achieve that by calling the
 
808
            <SAMP
 
809
CLASS="computeroutput"
 
810
>table.flush()</SAMP
 
811
> method.
 
812
          </P
 
813
><PRE
 
814
CLASS="screen"
 
815
>&#13;&#62;&#62;&#62; table.flush()
 
816
          </PRE
 
817
></DIV
 
818
><DIV
 
819
CLASS="sect2"
 
820
><H2
 
821
CLASS="sect2"
 
822
><A
 
823
NAME="subsection3.1.6"
 
824
>3.1.6. Reading (and selecting) data in a table</A
 
825
></H2
 
826
><A
 
827
NAME="readingAndSelectingUsage"
 
828
></A
 
829
><P
 
830
>Ok. We have our data on disk, and now we need to access
 
831
            it and select from specific columns the values we are
 
832
            interested in. See the example below:
 
833
          </P
 
834
><PRE
 
835
CLASS="screen"
 
836
>&#13;&#62;&#62;&#62; table = h5file.root.detector.readout
 
837
&#62;&#62;&#62; pressure = [ x['pressure'] for x in table.iterrows()
 
838
...              if x['TDCcount']&#62;3 and 20&#60;=x['pressure']&#60;50 ]
 
839
&#62;&#62;&#62; pressure
 
840
[25.0, 36.0, 49.0]
 
841
          </PRE
 
842
><P
 
843
>The first line creates a "shortcut"
 
844
            to the <SPAN
 
845
CLASS="emphasis"
 
846
><I
 
847
CLASS="emphasis"
 
848
>readout</I
 
849
></SPAN
 
850
> table deeper on the
 
851
            object tree. As you can see, we use the <SPAN
 
852
CLASS="emphasis"
 
853
><I
 
854
CLASS="emphasis"
 
855
>natural naming</I
 
856
></SPAN
 
857
> schema to access
 
858
            it. We also could have used the
 
859
            <SAMP
 
860
CLASS="computeroutput"
 
861
>h5file.getNode()</SAMP
 
862
> method, as we will do
 
863
            later on.
 
864
          </P
 
865
><P
 
866
>You will recognize the last two lines as a Python list
 
867
            comprehension. It loops over the rows in <SPAN
 
868
CLASS="emphasis"
 
869
><I
 
870
CLASS="emphasis"
 
871
>table</I
 
872
></SPAN
 
873
> as
 
874
            they are provided by the <SAMP
 
875
CLASS="computeroutput"
 
876
>table.iterrows()</SAMP
 
877
>
 
878
            iterator (see <A
 
879
HREF="x2981.html#Table.iterrows"
 
880
>4.6.2</A
 
881
>). The
 
882
            iterator returns values until all the data in table is
 
883
            exhausted. These rows are filtered using the expression:
 
884
            <PRE
 
885
CLASS="screen"
 
886
>&#13;        x['TDCcount'] &#62; 3 and x['pressure'] &#60;50
 
887
            </PRE
 
888
>
 
889
            We select the value of the <SAMP
 
890
CLASS="computeroutput"
 
891
>pressure</SAMP
 
892
> column from
 
893
            filtered records to create the final list and assign it to
 
894
            <SAMP
 
895
CLASS="computeroutput"
 
896
>pressure</SAMP
 
897
> variable.
 
898
          </P
 
899
><P
 
900
>We could have used a normal <SAMP
 
901
CLASS="computeroutput"
 
902
>for</SAMP
 
903
> loop to
 
904
            accomplish the same purpose, but I find comprehension
 
905
            syntax to be more compact and elegant.
 
906
          </P
 
907
><P
 
908
>Let's select the <SAMP
 
909
CLASS="computeroutput"
 
910
>name</SAMP
 
911
> column for the same
 
912
            set of cuts:
 
913
          </P
 
914
><PRE
 
915
CLASS="screen"
 
916
>&#13;&#62;&#62;&#62; names=[ x['name'] for x in table if x['TDCcount']&#62;3 and 20&#60;=x['pressure']&#60;50 ]
 
917
&#62;&#62;&#62; names
 
918
['Particle:      5', 'Particle:      6', 'Particle:      7']
 
919
          </PRE
 
920
><P
 
921
>Note how we have omitted the <SAMP
 
922
CLASS="computeroutput"
 
923
>iterrows()</SAMP
 
924
> call
 
925
            in the list comprehension. The <SAMP
 
926
CLASS="computeroutput"
 
927
>Table</SAMP
 
928
> class
 
929
            has an implementation of the special method
 
930
            <SAMP
 
931
CLASS="computeroutput"
 
932
>__iter__()</SAMP
 
933
> that iterates over all the rows in
 
934
            the table. In fact, <SAMP
 
935
CLASS="computeroutput"
 
936
>iterrows()</SAMP
 
937
> internally
 
938
            calls this special <SAMP
 
939
CLASS="computeroutput"
 
940
>__iter__()</SAMP
 
941
> method.
 
942
            Accessing all the rows in a table using this method is
 
943
            very convenient, especially when working with the data
 
944
            interactively.
 
945
          </P
 
946
><P
 
947
>That's enough about selections. The next section will show
 
948
            you how to save these selected results to a file.
 
949
          </P
 
950
></DIV
 
951
><DIV
 
952
CLASS="sect2"
 
953
><H2
 
954
CLASS="sect2"
 
955
><A
 
956
NAME="subsection3.1.7"
 
957
>3.1.7. Creating new array objects</A
 
958
></H2
 
959
><P
 
960
>In order to separate the selected data from the mass of
 
961
            detector data, we will create a new group
 
962
            <SAMP
 
963
CLASS="computeroutput"
 
964
>columns</SAMP
 
965
> branching off the root
 
966
            group. Afterwards, under this group, we will create two
 
967
            arrays that will contain the selected data. First, we
 
968
            create the group:
 
969
          </P
 
970
><PRE
 
971
CLASS="screen"
 
972
>&#13;&#62;&#62;&#62; gcolumns = h5file.createGroup(h5file.root, "columns", "Pressure and Name")
 
973
          </PRE
 
974
><P
 
975
>Note that this time we have specified the first parameter
 
976
            using <SPAN
 
977
CLASS="emphasis"
 
978
><I
 
979
CLASS="emphasis"
 
980
>natural naming</I
 
981
></SPAN
 
982
>
 
983
            (<SAMP
 
984
CLASS="computeroutput"
 
985
>h5file.root</SAMP
 
986
>) instead of with an absolute
 
987
            path string ("/").
 
988
          </P
 
989
><P
 
990
>Now, create the first of the two <SAMP
 
991
CLASS="computeroutput"
 
992
>Array</SAMP
 
993
>
 
994
            objects we've just mentioned:
 
995
          </P
 
996
><PRE
 
997
CLASS="screen"
 
998
>&#13;&#62;&#62;&#62; h5file.createArray(gcolumns, 'pressure', array(pressure),
 
999
...                     "Pressure column selection")
 
1000
/columns/pressure (Array(3,)) 'Pressure column selection'
 
1001
  type = Float64
 
1002
  itemsize = 8
 
1003
  flavor = 'numarray'
 
1004
  byteorder = 'little'
 
1005
          </PRE
 
1006
><P
 
1007
>We already know the first two parameters of the
 
1008
            <SAMP
 
1009
CLASS="computeroutput"
 
1010
>createArray</SAMP
 
1011
> (see <A
 
1012
HREF="x1533.html#createArrayDescr"
 
1013
>4.2.2</A
 
1014
>) methods (these are the
 
1015
            same as the first two in <SAMP
 
1016
CLASS="computeroutput"
 
1017
>createTable</SAMP
 
1018
>): they
 
1019
            are the parent group <SPAN
 
1020
CLASS="emphasis"
 
1021
><I
 
1022
CLASS="emphasis"
 
1023
>where</I
 
1024
></SPAN
 
1025
> <SAMP
 
1026
CLASS="computeroutput"
 
1027
>Array</SAMP
 
1028
>
 
1029
            will be created and the <SAMP
 
1030
CLASS="computeroutput"
 
1031
>Array</SAMP
 
1032
> instance
 
1033
            <SPAN
 
1034
CLASS="emphasis"
 
1035
><I
 
1036
CLASS="emphasis"
 
1037
>name</I
 
1038
></SPAN
 
1039
>.  The third parameter is the <SPAN
 
1040
CLASS="emphasis"
 
1041
><I
 
1042
CLASS="emphasis"
 
1043
>object</I
 
1044
></SPAN
 
1045
>
 
1046
            we want to save to disk. In this case, it is a
 
1047
            <SAMP
 
1048
CLASS="computeroutput"
 
1049
>numarray</SAMP
 
1050
> array that is built from the
 
1051
            selection list we created before.  The fourth parameter is
 
1052
            the <SPAN
 
1053
CLASS="emphasis"
 
1054
><I
 
1055
CLASS="emphasis"
 
1056
>title</I
 
1057
></SPAN
 
1058
>.
 
1059
          </P
 
1060
><P
 
1061
>Now, we will save the second array. It contains the list
 
1062
            of strings we selected before: we save this object as-is,
 
1063
            with no further conversion.
 
1064
          </P
 
1065
><PRE
 
1066
CLASS="screen"
 
1067
>&#13;&#62;&#62;&#62; h5file.createArray(gcolumns, 'name', names, "Name column selection")
 
1068
/columns/name Array(4,) 'Name column selection'
 
1069
  type = 'CharType'
 
1070
  itemsize = 16
 
1071
  flavor = 'List'
 
1072
  byteorder = 'little'
 
1073
          </PRE
 
1074
><P
 
1075
>As you can see, <SAMP
 
1076
CLASS="computeroutput"
 
1077
>createArray()</SAMP
 
1078
> accepts
 
1079
            <SPAN
 
1080
CLASS="emphasis"
 
1081
><I
 
1082
CLASS="emphasis"
 
1083
>names</I
 
1084
></SPAN
 
1085
> (which is a regular Python list) as an
 
1086
            <SPAN
 
1087
CLASS="emphasis"
 
1088
><I
 
1089
CLASS="emphasis"
 
1090
>object</I
 
1091
></SPAN
 
1092
> parameter. Actually, it accepts a variety
 
1093
            of different regular objects (see <A
 
1094
HREF="x1533.html#createArrayDescr"
 
1095
>4.2.2</A
 
1096
>) as parameters. The
 
1097
            <SAMP
 
1098
CLASS="computeroutput"
 
1099
>flavor</SAMP
 
1100
> attribute (see the output above) saves
 
1101
            the original kind of object that was saved. Based on this
 
1102
            <SPAN
 
1103
CLASS="emphasis"
 
1104
><I
 
1105
CLASS="emphasis"
 
1106
>flavor</I
 
1107
></SPAN
 
1108
>, <SAMP
 
1109
CLASS="computeroutput"
 
1110
>PyTables</SAMP
 
1111
> will be able to
 
1112
            retrieve exactly the same object from disk later on.
 
1113
          </P
 
1114
><P
 
1115
>Note that in these examples, the <SAMP
 
1116
CLASS="computeroutput"
 
1117
>createArray</SAMP
 
1118
>
 
1119
            method returns an <SAMP
 
1120
CLASS="computeroutput"
 
1121
>Array</SAMP
 
1122
> instance that is not
 
1123
            assigned to any variable. Don't worry, this is intentional
 
1124
            to show the kind of object we have created by displaying
 
1125
            its representation.  The <SAMP
 
1126
CLASS="computeroutput"
 
1127
>Array</SAMP
 
1128
> objects have
 
1129
            been attached to the object tree and saved to disk, as you
 
1130
            can see if you print the complete object tree:
 
1131
          </P
 
1132
><PRE
 
1133
CLASS="screen"
 
1134
>&#13;&#62;&#62;&#62; print h5file
 
1135
Filename: 'tutorial1.h5' Title: 'Test file' Last modif.: 'Sun Jul 27 14:00:13 2003'
 
1136
/ (Group) 'Test file'
 
1137
/columns (Group) 'Pressure and Name'
 
1138
/columns/name (Array(3,)) 'Name column selection'
 
1139
/columns/pressure (Array(3,)) 'Pressure column selection'
 
1140
/detector (Group) 'Detector information'
 
1141
/detector/readout (Table(10,)) 'Readout example'
 
1142
 
 
1143
          </PRE
 
1144
></DIV
 
1145
><DIV
 
1146
CLASS="sect2"
 
1147
><H2
 
1148
CLASS="sect2"
 
1149
><A
 
1150
NAME="subsection3.1.8"
 
1151
>3.1.8. Closing the file and looking at its content</A
 
1152
></H2
 
1153
><P
 
1154
>To finish this first tutorial, we use the
 
1155
            <SAMP
 
1156
CLASS="computeroutput"
 
1157
>close</SAMP
 
1158
> method of the h5file <SAMP
 
1159
CLASS="computeroutput"
 
1160
>File</SAMP
 
1161
>
 
1162
            object to close the file before exiting Python:
 
1163
          </P
 
1164
><PRE
 
1165
CLASS="screen"
 
1166
>&#13;&#62;&#62;&#62; h5file.close()
 
1167
&#62;&#62;&#62; ^D
 
1168
          </PRE
 
1169
><P
 
1170
>You have now created your first <SAMP
 
1171
CLASS="computeroutput"
 
1172
>PyTables</SAMP
 
1173
>
 
1174
            file with a table and two arrays. You can examine it with
 
1175
            any generic HDF5 tool, such as <SAMP
 
1176
CLASS="computeroutput"
 
1177
>h5dump</SAMP
 
1178
> or
 
1179
            <SAMP
 
1180
CLASS="computeroutput"
 
1181
>h5ls</SAMP
 
1182
>. Here is what the
 
1183
            <SAMP
 
1184
CLASS="computeroutput"
 
1185
>tutorial1.h5</SAMP
 
1186
> looks like when read with the
 
1187
            <SAMP
 
1188
CLASS="computeroutput"
 
1189
>h5ls</SAMP
 
1190
> program:
 
1191
          </P
 
1192
><PRE
 
1193
CLASS="screen"
 
1194
>&#13;$ h5ls -rd tutorial1.h5
 
1195
/columns                 Group
 
1196
/columns/name            Dataset {3}
 
1197
    Data:
 
1198
        (0) "Particle:      5", "Particle:      6", "Particle:      7"
 
1199
/columns/pressure        Dataset {3}
 
1200
    Data:
 
1201
        (0) 25, 36, 49
 
1202
/detector                Group
 
1203
/detector/readout        Dataset {10/Inf}
 
1204
    Data:
 
1205
        (0) {0, 0, 0, 0, 10, 0, "Particle:      0", 0},
 
1206
        (1) {256, 1, 1, 1, 9, 17179869184, "Particle:      1", 1},
 
1207
        (2) {512, 2, 256, 2, 8, 34359738368, "Particle:      2", 4},
 
1208
        (3) {768, 3, 6561, 3, 7, 51539607552, "Particle:      3", 9},
 
1209
        (4) {1024, 4, 65536, 4, 6, 68719476736, "Particle:      4", 16},
 
1210
        (5) {1280, 5, 390625, 5, 5, 85899345920, "Particle:      5", 25},
 
1211
        (6) {1536, 6, 1679616, 6, 4, 103079215104, "Particle:      6", 36},
 
1212
        (7) {1792, 7, 5764801, 7, 3, 120259084288, "Particle:      7", 49},
 
1213
        (8) {2048, 8, 16777216, 8, 2, 137438953472, "Particle:      8", 64},
 
1214
        (9) {2304, 9, 43046721, 9, 1, 154618822656, "Particle:      9", 81}
 
1215
          </PRE
 
1216
><P
 
1217
>Here's the outputs as displayed by the "ptdump"
 
1218
           <SAMP
 
1219
CLASS="computeroutput"
 
1220
>PyTables</SAMP
 
1221
> utility (located in
 
1222
           <SAMP
 
1223
CLASS="computeroutput"
 
1224
>utils/</SAMP
 
1225
> directory):
 
1226
          </P
 
1227
><PRE
 
1228
CLASS="screen"
 
1229
>&#13;$ ptdump tutorial1.h5
 
1230
Filename: 'tutorial1.h5' Title: 'Test file' Last modif.: 'Sun Jul 27 14:40:51 2003'
 
1231
/ (Group) 'Test file'
 
1232
/columns (Group) 'Pressure and Name'
 
1233
/columns/name (Array(3,)) 'Name column selection'
 
1234
/columns/pressure (Array(3,)) 'Pressure column selection'
 
1235
/detector (Group) 'Detector information'
 
1236
/detector/readout (Table(10,)) 'Readout example'
 
1237
 
 
1238
          </PRE
 
1239
><P
 
1240
>You can pass the <SAMP
 
1241
CLASS="computeroutput"
 
1242
>-v</SAMP
 
1243
> or <SAMP
 
1244
CLASS="computeroutput"
 
1245
>-d</SAMP
 
1246
>
 
1247
            options to <SAMP
 
1248
CLASS="computeroutput"
 
1249
>ptdump</SAMP
 
1250
> if you want more
 
1251
            verbosity. Try them out!
 
1252
          </P
 
1253
><P
 
1254
>Also, in <A
 
1255
HREF="c514.html#tutorial1-1-tableview"
 
1256
>figure�3.1</A
 
1257
>,
 
1258
            you can admire how the <SAMP
 
1259
CLASS="computeroutput"
 
1260
>tutorial1.h5</SAMP
 
1261
> looks
 
1262
            like using the <A
 
1263
HREF="http://www.carabos.com/products/vitables.html"
 
1264
TARGET="_top"
 
1265
>ViTables
 
1266
            </A
 
1267
> graphical interface .
 
1268
          </P
 
1269
><DIV
 
1270
CLASS="figure"
 
1271
><A
 
1272
NAME="tutorial1-1-tableview"
 
1273
></A
 
1274
><P
 
1275
><B
 
1276
>Figure 3.1. The initial version of the data file for tutorial 1,
 
1277
              with a view of the data objects.
 
1278
            </B
 
1279
></P
 
1280
><DIV
 
1281
CLASS="mediaobject"
 
1282
><P
 
1283
><IMG
 
1284
SRC="tutorial1-1-tableview.png"></P
 
1285
></DIV
 
1286
></DIV
 
1287
></DIV
 
1288
></DIV
 
1289
></DIV
 
1290
><DIV
 
1291
CLASS="NAVFOOTER"
 
1292
><HR
 
1293
ALIGN="LEFT"
 
1294
WIDTH="100%"><TABLE
 
1295
SUMMARY="Footer navigation table"
 
1296
WIDTH="100%"
 
1297
BORDER="0"
 
1298
CELLPADDING="0"
 
1299
CELLSPACING="0"
 
1300
><TR
 
1301
><TD
 
1302
WIDTH="33%"
 
1303
ALIGN="left"
 
1304
VALIGN="top"
 
1305
><A
 
1306
HREF="x457.html"
 
1307
ACCESSKEY="P"
 
1308
>Prev</A
 
1309
></TD
 
1310
><TD
 
1311
WIDTH="34%"
 
1312
ALIGN="center"
 
1313
VALIGN="top"
 
1314
><A
 
1315
HREF="index.html"
 
1316
ACCESSKEY="H"
 
1317
>Home</A
 
1318
></TD
 
1319
><TD
 
1320
WIDTH="33%"
 
1321
ALIGN="right"
 
1322
VALIGN="top"
 
1323
><A
 
1324
HREF="x762.html"
 
1325
ACCESSKEY="N"
 
1326
>Next</A
 
1327
></TD
 
1328
></TR
 
1329
><TR
 
1330
><TD
 
1331
WIDTH="33%"
 
1332
ALIGN="left"
 
1333
VALIGN="top"
 
1334
>Binary installation (Windows)</TD
 
1335
><TD
 
1336
WIDTH="34%"
 
1337
ALIGN="center"
 
1338
VALIGN="top"
 
1339
><A
 
1340
HREF="p35.html"
 
1341
ACCESSKEY="U"
 
1342
>Up</A
 
1343
></TD
 
1344
><TD
 
1345
WIDTH="33%"
 
1346
ALIGN="right"
 
1347
VALIGN="top"
 
1348
>Browsing the <SPAN
 
1349
CLASS="emphasis"
 
1350
><I
 
1351
CLASS="emphasis"
 
1352
>object tree</I
 
1353
></SPAN
 
1354
></TD
 
1355
></TR
 
1356
></TABLE
 
1357
></DIV
 
1358
></BODY
 
1359
></HTML
 
1360
>
 
 
b'\\ No newline at end of file'