Skip to content

Instantly share code, notes, and snippets.

@hardingnj
Last active August 29, 2015 14:03
Show Gist options
  • Save hardingnj/c203bfb940fde6bee19c to your computer and use it in GitHub Desktop.
Save hardingnj/c203bfb940fde6bee19c to your computer and use it in GitHub Desktop.
Python compression test
#! /usr/bin/python
#
# This example creates and writes GZIP compressed dataset.
#
import h5py
import numpy as np
#
# Create files
file_gzip = h5py.File('gzip.h5','w')
file_lzf = h5py.File('lzf.h5' ,'w')
#
# Create /DS1 dataset; in order to use compression, dataset has to be chunked.
#
dataset_gzip = file_gzip.create_dataset('DS1',(32,64),'i',chunks=(4,8),compression='gzip',compression_opts=9)
dataset_lzf = file_lzf.create_dataset('DS1',(32,64),'i',chunks=(4,8),compression='lzf');
#
# Initialize data.
#
data = np.zeros((32,64))
for i in range(32):
for j in range(64):
data[i][j]= i*j-j
#
# Write data.
#
print "Writing data..."
dataset_lzf[...] = data
file_lzf.close()
dataset_gzip[...] = data
file_gzip.close()
#
# Read data back; display compression properties and dataset max value.
#
test_file_gzip = h5py.File('gzip.h5','r')
dataset_t_gzip = test_file_gzip['DS1']
print "Compression method is", dataset_t_gzip.compression
print "Compression parameter is", dataset_t_gzip.compression_opts
data = dataset_t_gzip[...]
print "Maximum value in", dataset_t_gzip.name, "is:", max(data.ravel())
test_file_gzip.close()
test_file_lzf = h5py.File('lzf.h5','r')
dataset_t_lzf = test_file_lzf['DS1']
print "Compression method is", dataset_t_lzf.compression
print "Compression parameter is", dataset_t_lzf.compression_opts
data = dataset_t_lzf[...]
print "Maximum value in", dataset_t_lzf.name, "is:", max(data.ravel())
test_file_lzf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment