Implemented delete attribute feature for review and simplified code.

This commit is contained in:
2024-04-24 17:02:18 +02:00
parent ec5703bcaa
commit a9146e5afc

View File

@ -193,7 +193,21 @@ def second_save_metadata_review(review_yaml_file_path, reviewer_attrs):
print('Nothing to commit.') print('Nothing to commit.')
def parse_attribute(attr_value):
dtype = []
values_list = []
max_length = 100
for key in attr_value.keys():
if (not key=='rename_as'):
dtype.append((key,f'S{max_length}'))
values_list.append(attr_value[key])
if len(values_list)>2:
new_attr_value = np.array([tuple(values_list)],dtype=dtype)
else:
new_attr_value = attr_value['value']
return new_attr_value
def third_update_hdf5_file_with_review(input_hdf5_file, yalm_review_file, reviewer_attrs = {}, hdf5_upload : bool = False): def third_update_hdf5_file_with_review(input_hdf5_file, yalm_review_file, reviewer_attrs = {}, hdf5_upload : bool = False):
@ -240,41 +254,37 @@ def third_update_hdf5_file_with_review(input_hdf5_file, yalm_review_file, review
for attr_name, attr_value in yaml_obj['attributes'].items(): for attr_name, attr_value in yaml_obj['attributes'].items():
#attr_value = yaml_obj['attributes'][attr_name] #attr_value = yaml_obj['attributes'][attr_name]
if not isinstance(attr_value,dict):
attr_value = {'rename_as':attr_name, 'value':attr_value, 'delete': False}
if attr_value.get('delete',False) and (attr_name in hdf5_obj.attrs.keys()):
hdf5_obj.attrs.__delitem__(attr_name)
continue
# Check whether attr_name belongs to the existing attributes of hdf5_obj # Check whether attr_name belongs to the existing attributes of hdf5_obj
if attr_name in hdf5_obj.attrs.keys(): if attr_name in hdf5_obj.attrs.keys():
#else: # renaming attribute and possibly change of value assigment #else: # renaming attribute and possibly change of value assigment
if isinstance(attr_value,dict):
# Retreive possibly new attribute's name and value
new_attr_name = attr_value.get('rename_as',attr_name) # if 'rename_as' is a key in attr_value returns the value, otherwise it return the existing value
dtype = [] #if isinstance(attr_value,dict):
values_list = [] # # Retreive possibly new attribute's name and value
max_length = 100 new_attr_name = attr_value.get('rename_as',attr_name) # if 'rename_as' is a key in attr_value returns the value, otherwise it return the existing value
for key in attr_value.keys():
if (not key=='rename_as'):
dtype.append((key,f'S{max_length}'))
values_list.append(attr_value.get(key,hdf5_obj.attrs[attr_name]))
if len(values_list)>2:
new_attr_value = np.array([tuple(values_list)],dtype=dtype)
else:
new_attr_value = attr_value.get('value',hdf5_obj.attrs[attr_name])
hdf5_obj.attrs[new_attr_name] = new_attr_value hdf5_obj.attrs[new_attr_name] = parse_attribute(attr_value)
# Remove from hdf5_obj.attrs attribute w/ name: attr_name if # Remove from hdf5_obj.attrs attribute w/ name: attr_name if
# yaml indicates a renaming of the attribute. # yaml indicates a renaming of the attribute.
if not (new_attr_name == attr_name): if not (new_attr_name == attr_name):
hdf5_obj.attrs.__delitem__(attr_name) hdf5_obj.attrs.__delitem__(attr_name)
else: #else:
hdf5_obj.attrs[attr_name] = attr_value # hdf5_obj.attrs[attr_name] = attr_value
else: # attribute inclusion else: # attribute inclusion
if isinstance(attr_value,dict): #if isinstance(attr_value,dict):
# Retreive new attribute's name and value # Retreive new attribute's name and value
new_attr_name = attr_value.get('rename_as',attr_name) # if 'rename_as' is a key in attr_value returns the value, otherwise it return the existing value # new_attr_name = attr_value.get('rename_as',attr_name) # if 'rename_as' is a key in attr_value returns the value, otherwise it return the existing value
new_attr_value = attr_value.get('value',np.nan) # TODO: let the user know why np.nan might have been assigned # new_attr_value = attr_value.get('value',np.nan) # TODO: let the user know why np.nan might have been assigned
hdf5_obj.attrs[new_attr_name] = new_attr_value hdf5_obj.attrs[attr_name] = parse_attribute(attr_value)
else: #else:
hdf5_obj.attrs[attr_name] = attr_value # hdf5_obj.attrs[attr_name] = attr_value
# Recreate/or update yaml representation of updated input_hdf5_file. # Recreate/or update yaml representation of updated input_hdf5_file.
output_yml_filename_path = hdf5_vis.take_yml_snapshot_of_hdf5_file(input_hdf5_file) output_yml_filename_path = hdf5_vis.take_yml_snapshot_of_hdf5_file(input_hdf5_file)