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
Changes from 5 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
27 changes: 21 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,20 @@ 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: # This is for test_to_hierarchical_dataframe_empty_tables
try:
data = self.target.get(slice(None), **kwargs)
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]
except IndexError:
"""
Note: TODO: test_to_hierarchical_dataframe_indexed_dtr_on_last_level
"""
for i in indices:
ret.append(self.__getitem_helper(i, **kwargs))
return ret


Expand Down Expand Up @@ -1453,7 +1469,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