I have to convert a H5
file to .txt
file. I found the code on Github that converts the same. I am trying to input my own Hdf5
file. The below-posted code gives an error and does not convert.
R
python h5_txt.py thermal_images.h5
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)
Keyerror: "Unable to open object (object 'np_array'does not exist)"
The code is working as intended. You can follow these steps to make it work for you.
Create a file mainfile.py and add the following code
import numpy as np
import helpers as h
np_array = np.random.random((30, 30));
h5_name = 'np_array.h5';
h.write_dh5_np(h5_name, np_array);
np_array_from_h5 = h.read_hd5_np(h5_name);
np.allclose(np_array_from_h5, np_array);
txt_name = 'np_array.txt'
txt_name = h.convert_to_txt(h5_name, txt_name)
np_array_from_txt = np.loadtxt(txt_name)
np.allclose(np_array_from_txt, np_array)
Now create a file helpers.py in the same folder and add the following code in it
import os
import sys
import doctest
import h5py
import numpy as np
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
Execute the mainfile.py and your code will start working. You were not passing your file to the method that actually converts the hdf5 file to txt so it was giving you an error. Hope it helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.