Last active
December 17, 2015 20:37
-
-
Save bmorris3/f2efa316b8ae589a2d6b to your computer and use it in GitHub Desktop.
Sample HDF5 experiments
This file contains hidden or 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 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() |
This file contains hidden or 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 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