Skip to content

Instantly share code, notes, and snippets.

@fschwar4
Last active April 10, 2024 07:49
Show Gist options
  • Save fschwar4/2a32ff5301c372d67fce489916c0de3f to your computer and use it in GitHub Desktop.
Save fschwar4/2a32ff5301c372d67fce489916c0de3f to your computer and use it in GitHub Desktop.
Copy out only a small part of the original raw data from a MCS recording. Preserves all meta data.
import h5py as h5
file_path = 'data.h5'
stream_path = '/Data/Recording_0/AnalogStream/Stream_'
# set boundaries for data extraction
start_s = 0 # start second
end_s = 10 # end second
sampling_rate = 25e3 # Hz
start = int(start_s * sampling_rate)
stop = int(end_s * sampling_rate)
with h5.File(file_path, 'r+') as f:
for i in range(5):
data_path = stream_path + str(i) + '/ChannelData'
length_info_path = stream_path + str(i) + '/ChannelDataTimeStamps'
try:
f[data_path]
print('Stream ', i)
except KeyError:
print('Stopped after stream ', i)
break
print('Original shape: ', f[data_path].shape)
# save compression info to apply to new dataset
compressor_, compressor_kwargs = f[data_path].compression, f[data_path].compression_opts
# load relevant data
data = f[data_path][:, start:stop]
print('New shape: ', data.shape)
assert f[data_path].shape[0] == data.shape[0] and f[data_path].shape[1] >= data.shape[1]
# copy attributes
attr_dict = dict(f[data_path].attrs) # should be empty
# delete the old data
del f[data_path]
# create a new dataset
f.create_dataset(data_path, data=data, compression=compressor_, compression_opts=compressor_kwargs)
for key, value in attr_dict.items():
f[data_path].attrs[key] = value
# update the length info
f[length_info_path][0][2] = stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment