Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Aug 14, 2024
1 parent b7be0b6 commit 345b2f6
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/hdmf/build/objectmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,48 +828,55 @@ def __check_dset_spec(self, orig, ext):
spec = ext
return dtype, shape, dims, spec

def __get_matched_dimension(self, data_shape, spec_shape, spec_dtype=None):
# if shape is a list of allowed shapes, find the index of the shape that matches the data
if isinstance(spec_shape[0], list):
match_shape_inds = list()
for i, s in enumerate(spec_shape):
# skip this shape if it has a different number of dimensions from the data
if len(s) != len(data_shape):
continue
# check each dimension. None means any length is allowed
match = True
for j, d in enumerate(data_shape):
if s[j] is not None and s[j] != d:
match = False
break
if match:
match_shape_inds.append(i)
return match_shape_inds

def __get_spec_info(self, data, spec_shape, spec_dims, spec_dtype=None):
"""This will return the dimension labels and shape by matching the data shape to a permissible spec shape."""
if spec_shape is None and spec_dims is None:
return None, None
elif spec_shape is not None and spec_dims is None:
return None, tuple(spec_shape)
elif spec_shape is None and spec_dims is not None:
return spec_dims, None
# elif spec_shape is not None and spec_dims is None:
else:
if spec_dtype is not None and isinstance(spec_dtype, list):
data_shape = (len(data),)
else:
data_shape = get_data_shape(data)
# if shape is a list of allowed shapes, find the index of the shape that matches the data

if isinstance(spec_shape[0], list):
match_shape_inds = list()
for i, s in enumerate(spec_shape):
# skip this shape if it has a different number of dimensions from the data
if len(s) != len(data_shape):
continue
# check each dimension. None means any length is allowed
match = True
for j, d in enumerate(data_shape):
if s[j] is not None and s[j] != d:
match = False
break
if match:
match_shape_inds.append(i)
# use the most specific match -- the one with the fewest Nones
if match_shape_inds:
if len(match_shape_inds) == 1:
return tuple(spec_dims[match_shape_inds[0]]), tuple(spec_shape[match_shape_inds[0]])
else:
count_nones = [len([x for x in spec_shape[k] if x is None]) for k in match_shape_inds]
index_min_count = count_nones.index(min(count_nones))
best_match_ind = match_shape_inds[index_min_count]
return tuple(spec_dims[best_match_ind]), tuple(spec_shape[best_match_ind])
else:
match_shape_inds = self.__get_matched_dimension(data_shape, spec_shape, spec_dtype)
if len(match_shape_inds) == 0:
# no matches found
msg = "Shape of data does not match any allowed shapes in spec '%s'" % self.spec.path
warnings.warn(msg, IncorrectDatasetShapeBuildWarning)
return None, None
elif len(match_shape_inds) == 1:
if spec_dims is not None:
return tuple(spec_dims[match_shape_inds[0]]), tuple(spec_shape[match_shape_inds[0]])
else:
return spec_dims, tuple(spec_shape[match_shape_inds[0]])
else:
count_nones = [len([x for x in spec_shape[k] if x is None]) for k in match_shape_inds]
index_min_count = count_nones.index(min(count_nones))
best_match_ind = match_shape_inds[index_min_count]
if spec_dims is not None:
return tuple(spec_dims[best_match_ind]), tuple(spec_shape[best_match_ind])
else:
return spec_dims, tuple(spec_shape[best_match_ind])

Check warning on line 879 in src/hdmf/build/objectmapper.py

View check run for this annotation

Codecov / codecov/patch

src/hdmf/build/objectmapper.py#L879

Added line #L879 was not covered by tests
else:
if len(data_shape) != len(spec_shape):
msg = "Shape of data does not match shape in spec '%s'" % self.spec.path
Expand All @@ -886,7 +893,10 @@ def __get_spec_info(self, data, spec_shape, spec_dims, spec_dtype=None):
warnings.warn(msg, IncorrectDatasetShapeBuildWarning)
return None, None
# shape is a single list of allowed dimension lengths
return tuple(spec_dims), tuple(spec_shape)
if spec_dims is not None:
return tuple(spec_dims), tuple(spec_shape)
else:
return None, tuple(spec_shape)

def __is_reftype(self, data):
if (isinstance(data, AbstractDataChunkIterator) or
Expand Down

0 comments on commit 345b2f6

Please sign in to comment.