From 7367da84b9cd9c6654fcd36eb3f4f9d19ccca32f Mon Sep 17 00:00:00 2001 From: Florez Ospina Juan Felipe Date: Wed, 22 May 2024 20:11:54 +0200 Subject: [PATCH] Simplified code by updating HDF5 attributes using .update() dict method (inherited from dict type). --- src/hdf5_lib.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/hdf5_lib.py b/src/hdf5_lib.py index 1e2868c..202dffc 100644 --- a/src/hdf5_lib.py +++ b/src/hdf5_lib.py @@ -410,19 +410,21 @@ def create_hdf5_file_from_filesystem_path(output_filename : str, try: # Create group and add their attributes h5file[group_name].create_group(name=file_dict['name']) - for key in file_dict['attributes_dict'].keys(): + #for key in file_dict['attributes_dict'].keys(): # Represent string values as fixed length strings in the HDF5 file, which need # to be decoded as string when we read them. It provides better control than variable strings, # at the expense of flexibility. # https://docs.h5py.org/en/stable/strings.html - value = file_dict['attributes_dict'][key] - if isinstance(value,str): - utf8_type = h5py.string_dtype('utf-8', len(value)) - value = np.array(value.encode('utf-8'),dtype=utf8_type) + # value = file_dict['attributes_dict'][key] + # if isinstance(value,str): + # utf8_type = h5py.string_dtype('utf-8', len(value)) + # value = np.array(value.encode('utf-8'),dtype=utf8_type) - h5file[group_name][file_dict['name']].attrs.create(name=key, - data=value) + # h5file[group_name][file_dict['name']].attrs.create(name=key, + # data=value) + + h5file[group_name][file_dict['name']].attrs.update(file_dict['attributes_dict']) # Add datasets to just created group for dataset in file_dict['datasets']: @@ -430,7 +432,11 @@ def create_hdf5_file_from_filesystem_path(output_filename : str, data = dataset['data'], #dtype = file_dict['dtype'], shape = dataset['shape']) - + # Add dataset's attributes + #for attr_key in dataset.get('attributes',{}).keys(): + # h5file[group_name][file_dict['name']][dataset['name']].attrs[attr_key] = dataset.get('attributes',{})[attr_key] + attributes = dataset.get('attributes', {}) + h5file[group_name][file_dict['name']][dataset['name']].attrs.update(attributes) except Exception as inst: # TODO: log when a file could not be stored as a dataset print(inst)