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:
@ -3,4 +3,5 @@ exclude_paths:
|
|||||||
- .ipynb_checkpoints
|
- .ipynb_checkpoints
|
||||||
- .renku
|
- .renku
|
||||||
- .git
|
- .git
|
||||||
# - params
|
# - params
|
||||||
|
- .Trash
|
@ -237,21 +237,27 @@ def convert_attrdict_to_np_structured_array(attr_value: dict):
|
|||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
new_attr_value : ndarray or str
|
new_attr_value : ndarray
|
||||||
Numpy structured array with UTF-8 encoded fields. Returns 'missing' if
|
Numpy structured array with UTF-8 encoded fields. Returns np.array(['missing'], dtype=[str]) if
|
||||||
the input dictionary is empty.
|
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 = []
|
dtype = []
|
||||||
values_list = []
|
values_list = []
|
||||||
max_length = max(len(str(attr_value[key])) for key in attr_value.keys())
|
max_length = max(len(str(attr_value[key])) for key in attr_value.keys())
|
||||||
for key in attr_value.keys():
|
for key, val in attr_value.items():
|
||||||
if key != 'rename_as':
|
# 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}'))
|
dtype.append((key, f'S{max_length}'))
|
||||||
values_list.append(attr_value[key])
|
values_list.append(attr_value[key])
|
||||||
|
else:
|
||||||
|
print(f"Skipping unsupported type for key {key}: {type(val)}")
|
||||||
if values_list:
|
if values_list:
|
||||||
new_attr_value = np.array([tuple(values_list)], dtype=dtype)
|
new_attr_value = np.array([tuple(values_list)], dtype=dtype)
|
||||||
else:
|
else:
|
||||||
new_attr_value = 'missing'
|
new_attr_value = np.array(['missing'], dtype=[str])
|
||||||
|
|
||||||
return new_attr_value
|
return new_attr_value
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user