Last active
August 2, 2017 18:26
-
-
Save ajtritt/929d750e92f9b2bd5a3e84e5e7e856ba to your computer and use it in GitHub Desktop.
Using a slice to label a dimension with HDF5
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 os | |
import h5py | |
import numpy as np | |
# Create compound data type | |
# a table with columns channel ID, xyz-coordinates, and impedence | |
coord_dt = np.dtype([('x', np.float), ('y', np.float), ('z', np.float)]) | |
ch_dt = np.dtype([("id", np.int), ("coord", coord_dt), ("imp", np.float)]) | |
# Make a fake channel table | |
a = np.zeros((10,), dtype=ch_dt) | |
for i in range(len(a)): | |
a[i]['id'] = i | |
a[i]['coord'][0] = 1.0*i | |
a[i]['coord'][1] = 2.0*i | |
a[i]['coord'][2] = 3.0*i | |
a[i]['imp'] = 0.1*i | |
# Now use these things to do fun stuff! | |
fname ='test_cpd_dtype.h5' | |
if os.path.exists(fname): | |
os.remove(fname) | |
f = h5py.File(fname) | |
# Cache the type to file in case we want to use it throughout the file | |
f['channel_row_t'] = ch_dt | |
# Get the named type from file so we can use it | |
ch_row_dt = f['channel_row_t'] | |
# Write our fake channel table to disk using | |
# our named (aka 'committed') compound type | |
channels = f.create_dataset('channels', (10,), dtype=ch_row_dt) | |
channels[:] = a | |
# Create some fake voltage recordings | |
np.random.seed(1) | |
voltage = f.create_dataset('voltage', data=np.random.random((3,20)), dtype=np.float) | |
# Create dimensions scales from a region reference | |
## First create a special data type for a region reference dataset | |
regref_dt = h5py.special_dtype(ref=h5py.RegionReference) | |
## Next create the dataset... | |
voltage_scale = f.create_dataset('voltage_channels', shape=(), dtype=regref_dt) | |
## and fill it in with a region reference | |
vchannels = [3,7,9] | |
voltage_scale[...] = channels.regionref[vchannels] | |
## Now turn label this thing as a dimension scale... | |
voltage.dims.create_scale(voltage_scale, 'Channels') | |
## and then add it to our voltage data | |
voltage.dims[0].attach_scale(voltage_scale) | |
f.close() | |
# Verify that we can read this back in and identify what we did | |
f = h5py.File(fname, 'r') | |
voltage = f['voltage'] | |
print("'voltage' dataset should have channels from %s" % ", ".join(str(x) for x in vchannels)) | |
vchannels = voltage.dims[0]['Channels'][()] | |
if isinstance(vchannels, h5py.RegionReference): | |
print('dimension scale is region reference, retrieving data.') | |
vchannels = f[vchannels][vchannels] | |
print("'voltage' dataset has data from channels %s" % ", ".join(str(x) for x in vchannels['id'])) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment