~ubuntu-branches/debian/experimental/h5py/experimental

« back to all changes in this revision

Viewing changes to examples/compression.py

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2009-09-24 11:08:03 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090924110803-hz0en6f43x8opgj4
Tags: 1.2.1-2
Build-depend on hdf5 >= 1.8.3 and enable api 1.8 (Closes: #548049)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
"""
 
3
    Example demonstrating how to use compression and other special options
 
4
    for storing datasets in HDF5.
 
5
 
 
6
    Compression is supported in HDF5 via a "filter pipeline" which is applied
 
7
    to data as it is written to and read from disk.  Each dataset in the
 
8
    file has its own pipeline, which allows the compression strategy to be
 
9
    specified on a per-dataset basis.
 
10
 
 
11
    Compression is only available for the actual data, and not for attributes
 
12
    or metadata.
 
13
 
 
14
    As of h5py 1.1, three compression techniques are available, "gzip", "lzf",
 
15
    and "szip".  The non-compression filters "shuffle" and "fletcher32" are
 
16
    also available.  See the docstring for the module h5py.filters for more
 
17
    information.
 
18
 
 
19
    Please note LZF is a h5py-only filter.  While reference C source is
 
20
    available, other HDF5-aware applications may be unable to read data in
 
21
    this format.
 
22
"""
 
23
 
 
24
import os
 
25
 
 
26
import numpy as np
 
27
import h5py
 
28
import h5py.filters
 
29
 
 
30
SHAPE = (100,100,100,20)
 
31
DTYPE = np.dtype('i')
 
32
SIZE = np.product(SHAPE)
 
33
 
 
34
f = h5py.File('compress_test.hdf5','w')
 
35
 
 
36
mydata = np.arange(SIZE,dtype=DTYPE).reshape(SHAPE)
 
37
 
 
38
datasets = []
 
39
 
 
40
print "Creating dataset with gzip"
 
41
dset = f.create_dataset("gzipped", data=mydata, compression="gzip",
 
42
                         compression_opts=4)   # compression_opts is optional
 
43
datasets.append(dset)
 
44
 
 
45
print "Creating dataset with LZF"
 
46
dset = f.create_dataset("lzfcompressed", data=mydata, compression="lzf")
 
47
datasets.append(dset)
 
48
 
 
49
if 'szip' in h5py.filters.encode:       # Not distributed with all versions of HDF5
 
50
    print "Creating dataset with SZIP"
 
51
    dset = f.create_dataset("szipped", data=mydata, compression="szip",
 
52
                             compression_opts=('nn',8))
 
53
    datasets.append(dset)
 
54
 
 
55
print "Creating dataset with LZF and error detection"
 
56
dset = f.create_dataset("gzip_error_detection", data=mydata,
 
57
                        compression="gzip", fletcher32=True)
 
58
datasets.append(dset)
 
59
 
 
60
print "Creating uncompressed dataset"
 
61
dset = f.create_dataset("uncompressed", data=mydata)
 
62
datasets.append(dset)
 
63
 
 
64
f.flush()
 
65
 
 
66
def showsettings(dataset):
 
67
    """ Demonstrate the public attributes of datasets """
 
68
 
 
69
    print "="*60
 
70
    print "Dataset      ", dataset.name
 
71
    print '-'*30
 
72
    print "Shape        ", dataset.shape
 
73
    print "Chunk size   ", dataset.chunks
 
74
    print "Datatype     ", dataset.dtype
 
75
    print '-'*30
 
76
    print "Compression  ", dataset.compression
 
77
    print "Settings     ", dataset.compression_opts
 
78
    print '-'*32
 
79
    print "Shuffle      ", dataset.shuffle
 
80
    print "Fletcher32   ", dataset.fletcher32
 
81
 
 
82
for x in datasets:
 
83
    showsettings(x)
 
84
 
 
85
f.close()
 
86
 
 
87
 
 
88