2

I have 1970 .npy files as features for MSVD dataset. I want to create one .hdf5 file from these numpy files.

import os 
import numpy as np
import hdf5


TRAIN_FEATURE_DIR = "MSVD"   

for filename in os.listdir(TRAIN_FEATURE_DIR):
    f = np.load(os.path.join(TRAIN_FEATURE_DIR, filename))
...
10
  • Do you know how to load those npy files? How to iterate over the directory? How to create a HDF5 file with h5py? And create groups and datasets? There pieces are all there. Commented Dec 13, 2022 at 17:43
  • @hpaulj I know to load and iterate files only, but I do not know how to create groups and datasets Commented Dec 13, 2022 at 17:55
  • Show us how you have applied that knowledge to the current problem. Commented Dec 13, 2022 at 17:55
  • The h5py documentation is pretty good. But experiment with simple cases first. Commented Dec 13, 2022 at 18:00
  • 1
    We have a pickled object, f. I have no idea what is in the loaded file, nor what attributes f has. You've not cited any h5py documentation, and have not shown any attempts to create an .hdf5 file. I don't know what result you were hoping your question would elicit. Be specific, help us to help you. Commented Dec 13, 2022 at 18:32

1 Answer 1

2

Creating a dataset from an array is easy. Example below loops over all .npy files in a folder and creates 1 dataset for each array. (FYI, I prefer glob.iglob() to get the filenames using a wildcard.) Dataset name is the same as the filename.

import glob 
import numpy as np
import h5py

with h5py.File('SO_74788877.h5','w') as h5f:
    for filename in glob.iglob('*.npy'):
        arr = np.load(filename)
        h5f.create_dataset(filename,data=arr)

This code shows how to access the dataset names and values from the H5 file created above. (dataset is a dataset object which behaves like a numpy array in many instances):

with h5py.File('SO_74788877.h5','r') as h5f:
    for name, dataset in h5f.items():
        print(name, dataset.shape, dataset.dtype)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.