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

Replace spmatrix with _CSMatrix as appropriate #3431

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

flying-sheep
Copy link
Member

@flying-sheep flying-sheep commented Jan 10, 2025

  • Closes #
  • Tests included or not required because:
  • Release notes not necessary because: The only user-facing change is some subtly typing fixes

Preparation for sparray support: make sure spmatrix is only used where it makes sense.

I checked every single instance of spmatrix and either tested that coo_matrix also works or replaced it with _CSMatrix = csr_matrix | csc_matrix.

TODO after this PR:

  1. replace spmatrix with _SpBase = sparray | spmatrix and _CSMatrix with _CSBase = cs{cr}_matrix | cs{cr}_array and test
  2. figure out bsr_array

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

Attention: Patch coverage is 87.37864% with 13 lines in your changes missing coverage. Please review.

Project coverage is 76.70%. Comparing base (72b0b81) to head (120e4f8).

Files with missing lines Patch % Lines
src/scanpy/tools/_utils.py 84.21% 3 Missing ⚠️
src/scanpy/_utils/__init__.py 87.50% 2 Missing ⚠️
src/scanpy/metrics/_common.py 60.00% 2 Missing ⚠️
src/scanpy/preprocessing/_utils.py 71.42% 2 Missing ⚠️
src/scanpy/tools/_louvain.py 0.00% 2 Missing ⚠️
src/scanpy/tools/_utils_clustering.py 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3431      +/-   ##
==========================================
+ Coverage   76.67%   76.70%   +0.03%     
==========================================
  Files         112      112              
  Lines       12945    12957      +12     
==========================================
+ Hits         9925     9939      +14     
+ Misses       3020     3018       -2     
Files with missing lines Coverage Δ
src/scanpy/_utils/compute/is_constant.py 79.48% <100.00%> (-0.52%) ⬇️
src/scanpy/external/tl/_harmony_timeseries.py 35.71% <ø> (ø)
src/scanpy/external/tl/_palantir.py 33.33% <ø> (ø)
src/scanpy/get/_aggregated.py 95.23% <100.00%> (+0.03%) ⬆️
src/scanpy/get/get.py 92.15% <100.00%> (ø)
src/scanpy/metrics/_gearys_c.py 60.00% <100.00%> (+2.50%) ⬆️
src/scanpy/metrics/_morans_i.py 67.16% <100.00%> (+2.98%) ⬆️
src/scanpy/neighbors/_types.py 100.00% <100.00%> (ø)
...preprocessing/_deprecated/highly_variable_genes.py 95.55% <ø> (ø)
src/scanpy/preprocessing/_deprecated/sampling.py 100.00% <ø> (ø)
... and 18 more

... and 1 file with indirect coverage changes

@flying-sheep flying-sheep marked this pull request as ready for review January 10, 2025 14:51
Copy link
Contributor

@ilan-gold ilan-gold left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, about the plan:

replace spmatrix with _SpBase = sparray | spmatrix and _CSMatrix with _CSBase = cs{cr}_matrix | cs{cr}_array and test

I am not sure this will go so smoothly because of differing APIs and version compatibility. It was something of a nightmare in AnnData...

Comment on lines +18 to +22
def _resolve_vals(
val: NDArray | sparse.spmatrix | DaskArray,
) -> NDArray | sparse.csr_matrix | DaskArray:
msg = f"Unsupported type {type(val)}"
raise TypeError(msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this type hint make sense? Why not Any?

@@ -298,9 +298,9 @@ def calculate_qc_metrics(
)
# Pass X so I only have to do it once
X = _choose_mtx_rep(adata, use_raw=use_raw, layer=layer)
if isspmatrix_coo(X):
if isinstance(X, spmatrix) and not isinstance(X, _CSMatrix):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems different than the one before, and I'm not sure we want to be more permissive

@@ -105,28 +105,33 @@ def preprocess_with_pca(adata, n_pcs: int | None = None, random_state=0):


def get_init_pos_from_paga(
adata, adjacency=None, random_state=0, neighbors_key=None, obsp=None
adata: AnnData,
adjacency: spmatrix | None = None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the spmatrix here intentional? I could see an argument for it because this function doesn't use it (so this type is permissive) but also, it fetches it out of obsp if it's not present suggesting that it has an "expected" type

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

Successfully merging this pull request may close these issues.

2 participants