Skip to content

Instantly share code, notes, and snippets.

@egcode
Created August 21, 2019 07:32
Show Gist options
  • Save egcode/7f9e322ced1cd415718f6297d7afaed1 to your computer and use it in GitHub Desktop.
Save egcode/7f9e322ced1cd415718f6297d7afaed1 to your computer and use it in GitHub Desktop.
h5
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import h5py
import numpy as np
### Requirement: name, path, embedding
'''
person1_name
person1_subgroup_1
file_path '/path/to/file1'
embedding [4.5, 2.1, 9.9]
person1_subgroup_2
file_path '/path/to/file123'
embedding [84.5, 32.32, 10.1]
person2_name
person2_subgroup_1
file_path '/path/to/file4444'
embedding [1.1, 2.1, 2.9]
person2_subgroup_2
file_path '/path/to/file1123123'
embedding [3.0, 41.1, 56.621]
'''
#### Writing To File
with h5py.File('dataset-file.h5', 'a') as f:
#### Person1 Folder
person1_grp = f.create_group('person1_name')
person1_subgroup_1 = person1_grp.create_group('person1_subgroup_1')
person1_subgroup_1.create_dataset('embedding', data=[4.5, 2.1, 9.9])
person1_subgroup_1.attrs["file_path"] = np.string_('/path/to/file1')
person1_subgroup_2 = person1_grp.create_group('person1_subgroup_2')
person1_subgroup_2.create_dataset('embedding', data=[84.5, 32.32, 10.1])
person1_subgroup_2.attrs["file_path"] = np.string_('/path/to/file123')
### ADD one more object
with h5py.File('dataset-file.h5', 'a') as f:
#### Person1 Folder
person2_grp = f.create_group('person2_name')
person2_subgroup_1 = person2_grp.create_group('person2_subgroup_1')
person2_subgroup_1.create_dataset('embedding', data=[1.1, 2.1, 2.9])
person2_subgroup_1.attrs["file_path"] = np.string_('/path/to/file4444')
person2_subgroup_2 = person2_grp.create_group('person2_subgroup_2')
person2_subgroup_2.create_dataset('embedding', data=[3.0, 41.1, 56.621])
person2_subgroup_2.attrs["file_path"] = np.string_('/path/to/file1123123')
## Reading From File
with h5py.File('dataset-file.h5', 'r') as f:
for person_group in f.keys():
print("personName: " + str(person_group))
for subgroup in f[person_group].keys() :
print("\tsubgroup: " + str(subgroup))
print("\t\tembedding data: " + str(f[person_group][subgroup]['embedding'][:]))
print("\t\tpath data: " + str(f[person_group][subgroup].attrs['file_path']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment