Skip to content

Commit 006732a

Browse files
authored
Workaround for large memory usage from using np.ix_ with scipy.sparse (#381)
1 parent 405b43e commit 006732a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

anndata/_core/index.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,22 @@ def unpack_index(index: Index) -> Tuple[Index1D, Index1D]:
118118

119119

120120
@singledispatch
121-
def _subset(a: Union[np.ndarray, spmatrix, pd.DataFrame], subset_idx: Index):
121+
def _subset(a: Union[np.ndarray, pd.DataFrame], subset_idx: Index):
122122
# Select as combination of indexes, not coordinates
123123
# Correcting for indexing behaviour of np.ndarray
124124
if all(isinstance(x, cabc.Iterable) for x in subset_idx):
125125
subset_idx = np.ix_(*subset_idx)
126126
return a[subset_idx]
127127

128128

129+
@_subset.register(spmatrix)
130+
def _subset_spmatrix(a: spmatrix, subset_idx: Index):
131+
# Correcting for indexing behaviour of sparse.spmatrix
132+
if len(subset_idx) > 1 and all(isinstance(x, cabc.Iterable) for x in subset_idx):
133+
subset_idx = (subset_idx[0].reshape(-1, 1), *subset_idx[1:])
134+
return a[subset_idx]
135+
136+
129137
@_subset.register(pd.DataFrame)
130138
def _subset_df(df: pd.DataFrame, subset_idx: Index):
131139
return df.iloc[subset_idx]

0 commit comments

Comments
 (0)