Skip to content

Commit

Permalink
add recursive string type for arrays of arbitrary dim
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Aug 19, 2024
1 parent d975445 commit 5279c9c
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/hdmf/build/objectmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,11 +598,17 @@ def __get_data_type(cls, spec):

def __convert_string(self, value, spec):
"""Convert string types to the specified dtype."""
def __apply_string_type(value, string_type):
if isinstance(value, (list, tuple, np.ndarray, DataIO)):
return [__apply_string_type(item, string_type) for item in value]
else:
return string_type(value)

ret = value
if isinstance(spec, AttributeSpec):
if 'text' in spec.dtype:
if spec.shape is not None or spec.dims is not None:
ret = list(map(str, value))
ret = __apply_string_type(value, str)
else:
ret = str(value)
elif isinstance(spec, DatasetSpec):
Expand All @@ -617,10 +623,8 @@ def __convert_string(self, value, spec):
def string_type(x):
return x.isoformat() # method works for both date and datetime
if string_type is not None:
if spec.shape is not None and len(spec.shape) > 1:
ret = [[string_type(item) for item in sublist] for sublist in value]
elif spec.shape is not None or spec.dims is not None:
ret = list(map(string_type, value))
if spec.shape is not None or spec.dims is not None:
ret = __apply_string_type(value, string_type)
else:
ret = string_type(value)
# copy over any I/O parameters if they were specified
Expand Down

0 comments on commit 5279c9c

Please sign in to comment.