Skip to content

Instantly share code, notes, and snippets.

@bmorris3
Last active December 17, 2015 20:37
Show Gist options
  • Save bmorris3/f2efa316b8ae589a2d6b to your computer and use it in GitHub Desktop.
Save bmorris3/f2efa316b8ae589a2d6b to your computer and use it in GitHub Desktop.
Sample HDF5 experiments
import h5py
import numpy as np
# Hack for interactive use with iPython notebook:
try:
f.close()
except (NameError, ValueError):
pass
# Create archive
f = h5py.File('test.hdf5', 'w')
n_times = 20000
n_wavelengths = 2000
# Create a "dataset" which is like a numpy.ndarray
spectra = f.create_dataset('spectra', dtype=np.float64,
shape=(n_times, n_wavelengths)) #, compression="lzf")
# Fill it with data like you would do for a numpy.ndarray
for i in range(n_times):
spectra[i, :] = np.random.randn(n_wavelengths)
# Save associated metadata:
wavelengths = np.linspace(100, 1000, n_wavelengths)
spectra.attrs['wavelengths'] = wavelengths
f.close()
import h5py
import numpy as np
import matplotlib.pyplot as plt
# Open the file stream
f = h5py.File('test.hdf5', 'r')
# Get the dataset that you care about
spectra = f['spectra']
# Collect its metadata
wavelengths = spectra.attrs['wavelengths']
# access one spectrum
plt.plot(wavelengths, spectra[4, :])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment