Created
January 4, 2018 18:57
-
-
Save mdraw/e6ba3a8b0bd7b6423dcd0c383fd58665 to your computer and use it in GitHub Desktop.
How to convert to elektronn3's expected hdf5 data set layout
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
#!/usr/bin/env python3 | |
# Example script to (in-place!) convert a data set from the standard layout | |
# expected by ELEKTRONN2 (z, x, y) to the new layout that elektronn3 uses | |
# (C, D, H, W). | |
# Remember to make a backup of your hdf5s before running this. | |
# The new data set format can still be read by ELEKTRONN2. | |
# This example assumes you want to convert the neuro_data_zxy example data set | |
# and execute the script in its directory. | |
# The lines for flipping the last axes is commented out because it is not | |
# relevant for our test data (this may be different with other data sets). | |
import h5py | |
fnames = [ # Explicit file names for clarity | |
'barrier_int16_0.h5', | |
'barrier_int16_1.h5', | |
'barrier_int16_2.h5', | |
'raw_0.h5', | |
'raw_1.h5', | |
'raw_2.h5', | |
] | |
## Uncomment to automatically find all hdf5 files in the CWD: | |
# fnames = [f for f in os.listdir() if f.endswith('.h5')] | |
for fname in fnames: | |
with h5py.File(fname) as f: | |
for k in f.keys(): | |
val = f[k].value.copy() | |
print('Before:', f[k]) | |
if val.ndim == 3: | |
val = val[None] # Prepend single channel axis | |
## Uncomment the following line to flip the last two axes | |
## (...,x,y) -> (...,y,x) or in PyTorch notation: (...,W,H) -> (...,H,W) | |
# val = val.swapaxes(-1, -2) | |
del f[k] | |
f.create_dataset(k, data=val, chunks=True) # Compression could lead to issues | |
print('After:', f[k]) | |
print(' Chunk size:', f[k].chunks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment