Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slight refactor to Vector Index get #1248

Merged
merged 9 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# HDMF Changelog

## [Unreleased]
## HDMF 4.0.1

### Enhancements
- Optimized `get` within `VectorIndex` to be more efficient when retrieving a dataset of references. @mavaylon1 [#1248](https://github.com/hdmf-dev/hdmf/pull/1248)

### Changed
- `hdmf.monitor` is unused and undocumented. It has been deprecated and will be removed in HDMF 5.0. @rly [#1245](https://github.com/hdmf-dev/hdmf/pull/1245)
Expand Down
34 changes: 28 additions & 6 deletions src/hdmf/common/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def add_row(self, arg, **kwargs):
"""
self.add_vector(arg, **kwargs)

def __get_slice(self, arg):
start = 0 if arg == 0 else self.data[arg - 1]
end = self.data[arg]
return slice(start, end)

def __getitem_helper(self, arg, **kwargs):
"""
Internal helper function used by __getitem__ to retrieve a data value from self.target
Expand All @@ -168,9 +173,8 @@ def __getitem_helper(self, arg, **kwargs):
:param kwargs: any additional arguments to *get* method of the self.target VectorData
:return: Scalar or list of values retrieved
"""
start = 0 if arg == 0 else self.data[arg - 1]
end = self.data[arg]
return self.target.get(slice(start, end), **kwargs)
slices = self.__get_slice(arg)
return self.target.get(slices, **kwargs)

def __getitem__(self, arg):
"""
Expand Down Expand Up @@ -199,8 +203,27 @@ def get(self, arg, **kwargs):
arg = np.where(arg)[0]
indices = arg
ret = list()
for i in indices:
ret.append(self.__getitem_helper(i, **kwargs))
if len(indices) > 0:
# Note: len(indices) == 0 for test_to_hierarchical_dataframe_empty_tables.
# This is an edge case test for to_hierarchical_dataframe() on empty tables.
# When len(indices) == 0, ret is expected to be an empty list, defined above.
try:
data = self.target.get(slice(None), **kwargs)
except IndexError:
"""
Note: TODO: test_to_hierarchical_dataframe_indexed_dtr_on_last_level.
This is the old way to get the data and not an untested feature.
"""
for i in indices:
ret.append(self.__getitem_helper(i, **kwargs))

return ret

slices = [self.__get_slice(i) for i in indices]
if isinstance(data, pd.DataFrame):
ret = [data.iloc[s] for s in slices]
else:
ret = [data[s] for s in slices]
return ret


Expand Down Expand Up @@ -1453,7 +1476,6 @@ def get(self, arg, index=False, df=True, **kwargs):
return ret
elif isinstance(arg, (list, slice, np.ndarray)):
idx = arg

# get the data at the specified indices
if isinstance(self.data, (tuple, list)) and isinstance(idx, (list, np.ndarray)):
ret = [self.data[i] for i in idx]
Expand Down
Loading