File: read-table.py

package info (click to toggle)
pytables 3.10.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,228 kB
  • sloc: ansic: 82,212; python: 65,296; cpp: 753; sh: 394; makefile: 100
file content (57 lines) | stat: -rw-r--r-- 1,349 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import sys
from time import time

import numpy as np

import tables as tb

N = 200
NBUNCH = 100_000_000
NREADS = 100_000
filename = "read-table.h5"


class Particle(tb.IsDescription):
    lati = tb.Int32Col()
    longi = tb.Int32Col()
    pressure = tb.Float32Col()
    temperature = tb.Float32Col()


if len(sys.argv) > 1 and sys.argv[1] == "w":
    # Open a file in "w"rite mode
    print(f"Creating {filename} with {NBUNCH * N / 1_000_000} Mrows...")
    t0 = time()
    fileh = tb.open_file(filename, mode="w")
    # Create a new table in newgroup group
    table = fileh.create_table(
        fileh.root,
        "table",
        Particle,
        "A table",
        tb.Filters(complevel=1, complib="blosc2"),
        expectedrows=NBUNCH * N,
    )
    # A bunch of particles
    particles = np.zeros(NBUNCH, dtype=table.dtype)

    # Fill the table with N chunks of particles
    for i in range(N):
        table.append(particles)
    table.flush()
    print(f"Time to create: {time() - t0:.3f}s")
else:
    fileh = tb.open_file(filename)
    table = fileh.root.table

t0 = time()
idxs_to_read = np.random.randint(0, NBUNCH, NREADS)
print(f"Time to create indexes: {time() - t0:.3f}s")

print(f"Reading {NREADS / 1_000} Krows...")
t0 = time()
for i in idxs_to_read:
    row = table[i]
print(f"Time to read: {time() - t0:.3f}s")

fileh.close()