Created
May 18, 2012 19:17
-
-
Save taldcroft/2727129 to your computer and use it in GitHub Desktop.
PyTables example
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
import tables | |
def make_h5_file(dat, filename, colname, n_rows): | |
"""Make a new HDF5 table to hold numpy array data ``dat`` | |
""" | |
filters = tables.Filters(complevel=5, complib='zlib') | |
h5 = tables.openFile(filename, mode='w', filters=filters) | |
h5shape = (0,) + dat.shape[1:] | |
h5type = tables.Atom.from_dtype(dat.dtype) | |
h5.createEArray(h5.root, 'data', h5type, h5shape, title=colname, | |
expectedrows=n_rows) | |
h5.close() | |
def append_h5_file(dat, filename): | |
"""Append data to an existing HDF5 table | |
""" | |
h5 = tables.openFile(filename, mode='a') | |
h5.root.data.append(dat) | |
h5.close() | |
def read_h5_file(h5_slice, filename): | |
"""Read a slice of an HDF5 file""" | |
h5 = tables.openFile(filename) | |
dat = h5.root.data[h5_slice] | |
h5.close() | |
return dat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment