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

(chore): disallow AnnData(...) with non-csr{r,c} matrices/arrays #1829

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/release-notes/1829.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Disallow declaration of {class}`~anndata.AnnData` with non-`cs{r,c}` sparse data-structures {user}`ilan-gold`
20 changes: 12 additions & 8 deletions src/anndata/_core/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import pandas as pd
from scipy import sparse

from anndata.compat import SpArray

from .._warnings import ImplicitModificationWarning
from ..utils import (
ensure_df_homogeneous,
Expand Down Expand Up @@ -39,14 +41,16 @@ def coerce_array(
warnings.warn(msg, ImplicitModificationWarning)
value = value.A
return value
elif isinstance(value, sparse.spmatrix):
msg = (
f"AnnData previously had undefined behavior around matrices of type {type(value)}."
"In 0.12, passing in this type will throw an error. Please convert to a supported type."
"Continue using for this minor version at your own risk."
)
warnings.warn(msg, FutureWarning)
return value
is_non_csc_r_array_or_matrix = (
(isinstance(value, base) and not isinstance(value, csr_c_format))
for base, csr_c_format in [
(sparse.spmatrix, sparse.csr_matrix | sparse.csc_matrix),
(SpArray, sparse.csr_array | sparse.csc_array),
]
)
if any(is_non_csc_r_array_or_matrix):
msg = f"Only CSR and CSC {'matrices' if isinstance(value, sparse.spmatrix) else 'arrays'} are supported."
raise ValueError(msg)
if isinstance(value, pd.DataFrame):
if allow_df:
raise_value_error_if_multiindex_columns(value, name)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ def test_set_dense_x_view_from_sparse():
assert_equal(orig.X[30:], x[30:]) # change propagates through


def test_warn_on_non_csr_csc_matrix():
X = sparse.eye(100)
flying-sheep marked this conversation as resolved.
Show resolved Hide resolved
with pytest.warns(
FutureWarning,
match=rf"AnnData previously had undefined behavior around matrices of type {type(X)}.*",
def test_fail_on_non_csr_csc_matrix():
X = sparse.eye(100, format="coo")
with pytest.raises(
ValueError,
match=r"Only CSR and CSC.*",
):
ad.AnnData(X=X)
Loading