Skip to content

Commit

Permalink
Slight refactor to Vector Index get
Browse files Browse the repository at this point in the history
  • Loading branch information
mavaylon1 committed Feb 16, 2025
1 parent b115ca3 commit 55338fc
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 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, **kwargs):
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 Down Expand Up @@ -199,8 +204,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 @@ -1440,7 +1457,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

0 comments on commit 55338fc

Please sign in to comment.