Implement skipping in convert_attrdict_to_np_structured_array(attr_value: dict) when dictionary values are not scalar. This ensures compatible values are transfered while the rest simply dicarded.

This commit is contained in:
2025-06-10 16:03:01 +02:00
parent 7d710c1e62
commit f555f7f199
2 changed files with 13 additions and 6 deletions

View File

@ -237,21 +237,27 @@ def convert_attrdict_to_np_structured_array(attr_value: dict):
Returns
-------
new_attr_value : ndarray or str
Numpy structured array with UTF-8 encoded fields. Returns 'missing' if
new_attr_value : ndarray
Numpy structured array with UTF-8 encoded fields. Returns np.array(['missing'], dtype=[str]) if
the input dictionary is empty.
"""
if not isinstance(attr_value,dict):
raise ValueError(f'Input paremeter {attr_value} must be a dictionary of scalar values.')
dtype = []
values_list = []
max_length = max(len(str(attr_value[key])) for key in attr_value.keys())
for key in attr_value.keys():
if key != 'rename_as':
for key, val in attr_value.items():
# Verify if 'rename_as' is still used in metadata revision
if key != 'rename_as' and isinstance(val, (int, float, str)):
dtype.append((key, f'S{max_length}'))
values_list.append(attr_value[key])
else:
print(f"Skipping unsupported type for key {key}: {type(val)}")
if values_list:
new_attr_value = np.array([tuple(values_list)], dtype=dtype)
else:
new_attr_value = 'missing'
new_attr_value = np.array(['missing'], dtype=[str])
return new_attr_value