Skip to content

Instantly share code, notes, and snippets.

@andrewfowlie
Last active November 16, 2022 16:24
Show Gist options
  • Save andrewfowlie/da173e2a476945a96039fb14e8b3a38a to your computer and use it in GitHub Desktop.
Save andrewfowlie/da173e2a476945a96039fb14e8b3a38a to your computer and use it in GitHub Desktop.
Convert h5 -> txt format
"""
Make a numpy array
>>> np_array = np.random.random((30, 30))
Write it in h5 format
>>> h5_name = 'np_array.h5'
>>> write_dh5_np(h5_name, np_array)
Read it in h5 format and check contents
>>> np_array_from_h5 = read_hd5_np(h5_name)
>>> np.allclose(np_array_from_h5, np_array)
True
Write h5 file in txt format
>>> txt_name = 'np_array.txt'
>>> txt_name = convert_to_txt(h5_name, txt_name)
Read it in txt format and check contents
>>> np_array_from_txt = np.loadtxt(txt_name)
>>> np.allclose(np_array_from_txt, np_array)
True
"""
import h5py
import numpy as np
import os
import sys
import doctest
def write_dh5_np(h5_name, np_array):
"""
:param h5_name: Name of a h5 file
:type h5_name: str
:param np_array: Array of floats
:type np_array: np.array
"""
assert not os.path.isfile(h5_name), "Won't overwrite {}".format(h5_name)
with h5py.File(h5_name, 'w') as h5_file:
h5_file.create_dataset('np_array', data=np_array)
def read_hd5_np(h5_name):
"""
:param h5_name: Name of a h5 file
:type h5_name: str
:returns: Data in h5 file
:rtype: np.array
"""
with h5py.File(h5_name, 'r') as h5_file:
np_array = h5_file['np_array'][()]
return np_array
def convert_to_txt(h5_name, txt_name=None):
"""
:param h5_name: Name of a h5 file
:type h5_name: str
:param txt_name: Name of a txt file to be created
:type txt_name: str
:returns: Name of a txt file that was created
:rtype: str
"""
if txt_name is None:
strip_name = os.path.splitext(h5_name)[0]
txt_name = '{}.txt'.format(strip_name)
assert not os.path.isfile(txt_name), "Won't overwrite {}".format(txt_name)
np_array = read_hd5_np(h5_name)
np.savetxt(txt_name, np_array)
return txt_name
if __name__ == "__main__":
args = sys.argv[1:]
if not args:
doctest.testmod()
else:
hdf5_name = sys.argv[1]
try:
txt_name = sys.argv[2]
except IndexError:
txt_name = None
convert_to_txt(hdf5_name, txt_name)
@jubin13
Copy link

jubin13 commented Jul 8, 2019

Hello,

How can I add a new h5 file for the script to read and convert.

~Jubin

@andrewfowlie
Copy link
Author

python h5_txt.py my_file.h5

@jubin13
Copy link

jubin13 commented Jul 10, 2019

Hi Andrew,
When I run the python h5_txt.py my_file.h5 command, it says 'np_array' doesn't exist.

H5_error

@CHAITU241
Copy link

Hi Andrew,
When I run the python h5_txt.py my_file.h5 command, it says 'np_array' doesn't exist.

H5_error

Same issue, I'm trying to use this on a h5 saved model

@andrewfowlie
Copy link
Author

The code assumes you are reading a h5 file that was written with this script. If it wasn’t, edit line 58 so that the key for the data is correct. I.e, if it’s called ‘mydata’ replace np_array on that line by mydata.

@v-amiri
Copy link

v-amiri commented Nov 16, 2022

Hi Andrew,

I have the same issue. my file is snap.hd5part . I do not know which lines must I enter the name of my file. replace all the np_array to snap.hdf5part? or?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment