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

Check indexing arg types #1293

Closed
ilan-gold opened this issue Jan 10, 2024 · 5 comments
Closed

Check indexing arg types #1293

ilan-gold opened this issue Jan 10, 2024 · 5 comments
Assignees
Milestone

Comments

@ilan-gold
Copy link
Contributor

Please describe your wishes and possible alternatives to achieve the desired result.

Working on #1224, we discovered that np.where is called internally by scipy on boolean masks, but there are places in our code where we also call it, likely unnecessarily. It would be good to review these places to see if they are still necessary and then see if there is an effect on performance.

@ivirshup
Copy link
Member

My main concern here is whether doing adata[adata.obs[...] == ...].X will hit the optimized path for indexing if X is backed (since we only optimized for mask indexing, and not positional)

@ivirshup ivirshup added this to the 0.10.5 milestone Jan 18, 2024
@ivirshup
Copy link
Member

ivirshup commented Jan 18, 2024

It does not, which kind of defeats the optimization. This now needs doing.

Confirmed by adding a print(row, col) to BaseCompressedSparseDataset.__getitem__ and running:

import anndata as ad, numpy as np, zarr
from scipy import sparse

rng = np.random.default_rng()

g = zarr.open()
X = sparse.random(10_000, 1_000, density=0.01, format="csr", random_state=rng)
ad.experimental.write_elem(g, "X", X, dataset_kwargs={"chunks": 1_000})
X_backed = ad.experimental.sparse_dataset(g["X"])

bool_idx = np.zeros(X.shape[0], dtype=bool)
bool_idx[5000:] = True
bool_idx[:1000] = True

adata = ad.AnnData(X=X_backed)
adata[bool_idx].X
[   0    1    2 ... 9997 9998 9999] slice(None, None, None)

<6000x1000 sparse matrix of type '<class 'numpy.float64'>'
	with 59999 stored elements in Compressed Sparse Row format>

@flying-sheep
Copy link
Member

flying-sheep commented Jan 18, 2024

slight aside: np.where is designed for np.where(cond, vals_if_true, vals_if_false), using np.where(cond)[0] as replacement for np.flatnonzero(cond) is awkward.

@ivirshup ivirshup modified the milestones: 0.10.5, 0.10.6 Jan 25, 2024
@ivirshup ivirshup modified the milestones: 0.10.6, 0.10.7 Mar 11, 2024
@flying-sheep flying-sheep modified the milestones: 0.10.7, 0.10.8 Apr 9, 2024
@github-actions github-actions bot added the stale label Jun 10, 2024
@flying-sheep
Copy link
Member

@ilan-gold what’s missing here?

@ilan-gold
Copy link
Contributor Author

Closing this as it is no longer needed. I've gone through the remaining instances and haven't found any that affect the performance optimizations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants