Created
July 10, 2014 15:18
-
-
Save hardingnj/7716e2e0b711b695c027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# | |
# This example creates and writes GZIP compressed dataset. | |
# | |
import h5py | |
import numpy as np | |
import random | |
import string | |
# | |
nrow = 1000000; | |
ncol = 20; | |
# 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',(nrow,ncol),'a10',chunks=(4,8),compres | |
sion='gzip',compression_opts=9) | |
dataset_lzf = file_lzf.create_dataset('DS1',(nrow,ncol),'a10',chunks=(4,8),compress | |
ion='lzf'); | |
# | |
# Initialize data. | |
# | |
data = np.array([[''] * ncol] * nrow) | |
for i in range(nrow): | |
for j in range(ncol): | |
data[i][j] = random.choice(string.letters) | |
# 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