Skip to content

Commit

Permalink
Just deprecate dtype argument (#1153)
Browse files Browse the repository at this point in the history
* Revert "Remove deprecated type argument to anndata constructor (#1126)"

This reverts commit f3e9c32.

* Update warning
  • Loading branch information
ivirshup authored Oct 4, 2023
1 parent f40d6f1 commit 85162c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
21 changes: 19 additions & 2 deletions anndata/_core/anndata.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,12 @@ def __init__(
varm: np.ndarray | Mapping[str, Sequence[Any]] | None = None,
layers: Mapping[str, np.ndarray | sparse.spmatrix] | None = None,
raw: Mapping[str, Any] | None = None,
*,
dtype: np.dtype | type | str | None = None,
shape: tuple[int, int] | None = None,
filename: PathLike | None = None,
filemode: Literal["r", "r+"] | None = None,
asview: bool = False,
*,
obsp: np.ndarray | Mapping[str, Sequence[Any]] | None = None,
varp: np.ndarray | Mapping[str, Sequence[Any]] | None = None,
oidx: Index1D = None,
Expand All @@ -367,6 +368,7 @@ def __init__(
varm=varm,
raw=raw,
layers=layers,
dtype=dtype,
shape=shape,
obsp=obsp,
varp=varp,
Expand Down Expand Up @@ -442,7 +444,7 @@ def _init_as_actual(
obsp=None,
raw=None,
layers=None,
*,
dtype=None,
shape=None,
filename=None,
filemode=None,
Expand Down Expand Up @@ -515,6 +517,21 @@ def _init_as_actual(
if shape is not None:
raise ValueError("`shape` needs to be `None` if `X` is not `None`.")
_check_2d_shape(X)
# if type doesn’t match, a copy is made, otherwise, use a view
if dtype is not None:
warnings.warn(
"The dtype argument is deprecated and will be removed in late 2024.",
FutureWarning,
)
if issparse(X) or isinstance(X, ma.MaskedArray):
# TODO: maybe use view on data attribute of sparse matrix
# as in readwrite.read_10x_h5
if X.dtype != np.dtype(dtype):
X = X.astype(dtype)
elif isinstance(X, (ZarrArray, DaskArray)):
X = X.astype(dtype)
else: # is np.ndarray or a subclass, convert to true np.ndarray
X = np.array(X, dtype, copy=False)
# data matrix and shape
self._X = X
n_obs, n_vars = X.shape
Expand Down
24 changes: 24 additions & 0 deletions anndata/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ def test_obsvar_vector_Xlayer(adata):
adata.obs_vector("a", layer="X")


# This should break in 0.9
def test_dtype_warning():
# Tests a warning is thrown
with pytest.warns(FutureWarning):
a = AnnData(np.ones((3, 3)), dtype=np.float32)
assert a.X.dtype == np.float32

# This shouldn't warn, shouldn't copy
with warnings.catch_warnings(record=True) as record:
b_X = np.ones((3, 3), dtype=np.float64)
b = AnnData(b_X)
assert not record
assert b_X is b.X
assert b.X.dtype == np.float64

# Should warn, should copy
with pytest.warns(FutureWarning):
c_X = np.ones((3, 3), dtype=np.float32)
c = AnnData(c_X, dtype=np.float64)
assert not record
assert c_X is not c.X
assert c.X.dtype == np.float64


def test_deprecated_write_attribute(tmp_path):
pth = tmp_path / "file.h5"
A = np.random.randn(20, 10)
Expand Down
2 changes: 1 addition & 1 deletion docs/release-notes/0.10.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ We expect to make a full release by October.
```

* {meth}`anndata.AnnData.transpose` no longer copies unnecessarily. If you rely on the copying behavior, call `.copy` on the resulting object. {pr}`1114` {user}`ivirshup`
* Removal of deprecated `dtype` argument to `AnnData` constructor {pr}`1126` {user}`ivirshup`

```{rubric} Other updates
```
Expand All @@ -51,6 +50,7 @@ We expect to make a full release by October.
```

* Deprecate `anndata.read`, which was just an alias for {func}`anndata.read_h5ad` {pr}`1108` {user}`ivirshup`.
* `dtype` argument to `AnnData` constructor is now deprecated {pr}`1153` {user}`ivirshup`

```{rubric} Bug fixes
```
Expand Down

0 comments on commit 85162c7

Please sign in to comment.