Skip to content

Commit 46969b4

Browse files
authored
Remove use of AnnData constructor dtype kwarg (#2658) (#2659)
* Remove use of AnnData constructor dtype kwarg (#2658) * Remove use of AnnData constructor dtype kwarg * release note * Fix release note (cherry picked from commit 0b49eeb) * Fix test * Set release date * Add release notes to index
1 parent efce8f8 commit 46969b4

File tree

9 files changed

+32
-26
lines changed

9 files changed

+32
-26
lines changed

docs/release-notes/1.9.5.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### 1.9.5 {small}`2023-09-08`
2+
3+
```{rubric} Bug fixes
4+
```
5+
6+
- Remove use of deprecated `dtype` argument to AnnData constructor {pr}`2658` {smaller}`Isaac Virshup`

docs/release-notes/release-latest.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Version 1.9
22

3+
```{include} /release-notes/1.9.5.md
4+
```
5+
36
```{include} /release-notes/1.9.4.md
47
```
58

scanpy/datasets/_datasets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def blobs(
4949
cluster_std=cluster_std,
5050
random_state=0,
5151
)
52-
return ad.AnnData(X, obs=dict(blobs=y.astype(str)), dtype=X.dtype)
52+
return ad.AnnData(X, obs=dict(blobs=y.astype(str)))
5353

5454

5555
@check_datasetdir_exists
@@ -172,13 +172,13 @@ def paul15() -> ad.AnnData:
172172
backup_url = 'http://falexwolf.de/data/paul15.h5'
173173
_utils.check_presence_download(filename, backup_url)
174174
with h5py.File(filename, 'r') as f:
175-
X = f['data.debatched'][()]
175+
X = f['data.debatched'][()].astype(np.float32)
176176
gene_names = f['data.debatched_rownames'][()].astype(str)
177177
cell_names = f['data.debatched_colnames'][()].astype(str)
178178
clusters = f['cluster.id'][()].flatten().astype(int)
179179
infogenes_names = f['info.genes_strings'][()].astype(str)
180180
# each row has to correspond to a observation, therefore transpose
181-
adata = ad.AnnData(X.transpose(), dtype=np.float32)
181+
adata = ad.AnnData(X.transpose())
182182
adata.var_names = gene_names
183183
adata.row_names = cell_names
184184
# names reflecting the cell type identifications from the paper

scanpy/testing/_helpers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
def check_rep_mutation(func, X, *, fields=("layer", "obsm"), **kwargs):
2323
"""Check that only the array meant to be modified is modified."""
24-
adata = sc.AnnData(X=X.copy(), dtype=X.dtype)
24+
adata = sc.AnnData(X=X.copy())
2525
for field in fields:
2626
sc.get._set_obs_rep(adata, X, **{field: field})
2727
X_array = asarray(X)

scanpy/tests/test_get.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ def adata():
3737
adata.layers['double'] is sparse np.ones((2,2)) * 2 to also test sparse matrices
3838
"""
3939
return AnnData(
40-
X=np.ones((2, 2)),
40+
X=np.ones((2, 2), dtype=int),
4141
obs=pd.DataFrame(
4242
{"obs1": [0, 1], "obs2": ["a", "b"]}, index=["cell1", "cell2"]
4343
),
4444
var=pd.DataFrame(
4545
{"gene_symbols": ["genesymbol1", "genesymbol2"]}, index=["gene1", "gene2"]
4646
),
4747
layers={"double": sparse.csr_matrix(np.ones((2, 2)), dtype=int) * 2},
48-
dtype=int,
4948
)
5049

5150

@@ -60,12 +59,11 @@ def test_obs_df(adata):
6059

6160
# make raw with different genes than adata
6261
adata.raw = AnnData(
63-
X=np.array([[1, 2, 3], [2, 4, 6]]),
62+
X=np.array([[1, 2, 3], [2, 4, 6]], dtype=np.float64),
6463
var=pd.DataFrame(
6564
{"gene_symbols": ["raw1", "raw2", 'raw3']},
6665
index=["gene2", "gene3", "gene4"],
6766
),
68-
dtype='float64',
6967
)
7068
pd.testing.assert_frame_equal(
7169
sc.get.obs_df(
@@ -157,9 +155,8 @@ def test_repeated_gene_symbols():
157155
gene_symbols = [f"symbol_{i}" for i in ["a", "b", "b", "c"]]
158156
var_names = pd.Index([f"id_{i}" for i in ["a", "b.1", "b.2", "c"]])
159157
adata = sc.AnnData(
160-
np.arange(3 * 4).reshape((3, 4)),
158+
np.arange(3 * 4, dtype=np.float32).reshape((3, 4)),
161159
var=pd.DataFrame({"gene_symbols": gene_symbols}, index=var_names),
162-
dtype=np.float32,
163160
)
164161

165162
with pytest.raises(KeyError, match="symbol_b"):

scanpy/tests/test_highly_variable_genes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def test_seurat_v3_mean_var_output_with_batchkey():
483483

484484
def test_cellranger_n_top_genes_warning():
485485
X = np.random.poisson(2, (100, 30))
486-
adata = sc.AnnData(X, dtype=X.dtype)
486+
adata = sc.AnnData(X)
487487
sc.pp.normalize_total(adata)
488488
sc.pp.log1p(adata)
489489

scanpy/tests/test_normalization.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ def typ(request):
3737

3838

3939
@pytest.mark.parametrize('dtype', ['float32', 'int64'])
40-
def test_normalize_total(typ, dtype):
41-
adata = AnnData(typ(X_total), dtype=dtype)
40+
def test_normalize_total(array_type, dtype):
41+
adata = AnnData(array_type(X_total).astype(dtype))
4242
sc.pp.normalize_total(adata, key_added='n_counts')
4343
assert np.allclose(np.ravel(adata.X.sum(axis=1)), [3.0, 3.0, 3.0])
4444
sc.pp.normalize_total(adata, target_sum=1, key_added='n_counts2')
4545
assert np.allclose(np.ravel(adata.X.sum(axis=1)), [1.0, 1.0, 1.0])
4646

47-
adata = AnnData(typ(X_frac), dtype=dtype)
47+
adata = AnnData(array_type(X_frac).astype(dtype))
4848
sc.pp.normalize_total(adata, exclude_highly_expressed=True, max_fraction=0.7)
4949
assert np.allclose(np.ravel(adata.X[:, 1:3].sum(axis=1)), [1.0, 1.0, 1.0])
5050

@@ -59,17 +59,17 @@ def test_normalize_total_rep(typ, dtype):
5959

6060

6161
@pytest.mark.parametrize('dtype', ['float32', 'int64'])
62-
def test_normalize_total_layers(typ, dtype):
63-
adata = AnnData(typ(X_total), dtype=dtype)
62+
def test_normalize_total_layers(array_type, dtype):
63+
adata = AnnData(array_type(X_total).astype(dtype))
6464
adata.layers["layer"] = adata.X.copy()
6565
with pytest.warns(FutureWarning, match=r".*layers.*deprecated"):
6666
sc.pp.normalize_total(adata, layers=["layer"])
6767
assert np.allclose(adata.layers["layer"].sum(axis=1), [3.0, 3.0, 3.0])
6868

6969

7070
@pytest.mark.parametrize('dtype', ['float32', 'int64'])
71-
def test_normalize_total_view(typ, dtype):
72-
adata = AnnData(typ(X_total), dtype=dtype)
71+
def test_normalize_total_view(array_type, dtype):
72+
adata = AnnData(array_type(X_total).astype(dtype))
7373
v = adata[:, :]
7474

7575
sc.pp.normalize_total(v)
@@ -127,7 +127,7 @@ def test_normalize_pearson_residuals_values(sparsity_func, dtype, theta, clip):
127127
residuals_reference = (X - mu) / np.sqrt(mu + mu**2 / theta)
128128

129129
# compute output to test
130-
adata = AnnData(sparsity_func(X), dtype=dtype)
130+
adata = AnnData(sparsity_func(X).astype(dtype))
131131
output = sc.experimental.pp.normalize_pearson_residuals(
132132
adata, theta=theta, clip=clip, inplace=False
133133
)

scanpy/tests/test_preprocessing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def test_scale_array(count_matrix_format, zero_center):
147147
Test that running sc.pp.scale on an anndata object and an array returns the same results.
148148
"""
149149
X = count_matrix_format(sp.random(100, 200, density=0.3).toarray())
150-
adata = sc.AnnData(X=X.copy(), dtype=np.float64)
150+
adata = sc.AnnData(X=X.copy().astype(np.float64))
151151

152152
sc.pp.scale(adata, zero_center=zero_center)
153153
scaled_X = sc.pp.scale(X, zero_center=zero_center, copy=True)
@@ -254,7 +254,7 @@ def test_downsample_counts_per_cell(count_matrix_format, replace, dtype):
254254
TARGET = 1000
255255
X = np.random.randint(0, 100, (1000, 100)) * np.random.binomial(1, 0.3, (1000, 100))
256256
X = X.astype(dtype)
257-
adata = AnnData(X=count_matrix_format(X), dtype=dtype)
257+
adata = AnnData(X=count_matrix_format(X).astype(dtype))
258258
with pytest.raises(ValueError):
259259
sc.pp.downsample_counts(
260260
adata, counts_per_cell=TARGET, total_counts=TARGET, replace=replace
@@ -286,7 +286,7 @@ def test_downsample_counts_per_cell_multiple_targets(
286286
TARGETS = np.random.randint(500, 1500, 1000)
287287
X = np.random.randint(0, 100, (1000, 100)) * np.random.binomial(1, 0.3, (1000, 100))
288288
X = X.astype(dtype)
289-
adata = AnnData(X=count_matrix_format(X), dtype=dtype)
289+
adata = AnnData(X=count_matrix_format(X).astype(dtype))
290290
initial_totals = np.ravel(adata.X.sum(axis=1))
291291
with pytest.raises(ValueError):
292292
sc.pp.downsample_counts(adata, counts_per_cell=[40, 10], replace=replace)
@@ -312,7 +312,7 @@ def test_downsample_counts_per_cell_multiple_targets(
312312
def test_downsample_total_counts(count_matrix_format, replace, dtype):
313313
X = np.random.randint(0, 100, (1000, 100)) * np.random.binomial(1, 0.3, (1000, 100))
314314
X = X.astype(dtype)
315-
adata_orig = AnnData(X=count_matrix_format(X), dtype=dtype)
315+
adata_orig = AnnData(X=count_matrix_format(X))
316316
total = X.sum()
317317
target = np.floor_divide(total, 10)
318318
initial_totals = np.ravel(adata_orig.X.sum(axis=1))

scanpy/tests/test_scaling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
def test_scale(typ, dtype):
2929
# test AnnData arguments
3030
# test scaling with default zero_center == True
31-
adata0 = AnnData(typ(X), dtype=dtype)
31+
adata0 = AnnData(typ(X).astype(dtype))
3232
sc.pp.scale(adata0)
3333
assert np.allclose(csr_matrix(adata0.X).toarray(), X_centered)
3434
# test scaling with explicit zero_center == True
35-
adata1 = AnnData(typ(X), dtype=dtype)
35+
adata1 = AnnData(typ(X).astype(dtype))
3636
sc.pp.scale(adata1, zero_center=True)
3737
assert np.allclose(csr_matrix(adata1.X).toarray(), X_centered)
3838
# test scaling with explicit zero_center == False
39-
adata2 = AnnData(typ(X), dtype=dtype)
39+
adata2 = AnnData(typ(X).astype(dtype))
4040
sc.pp.scale(adata2, zero_center=False)
4141
assert np.allclose(csr_matrix(adata2.X).toarray(), X_scaled)
4242
# test bare count arguments, for simplicity only with explicit copy=True

0 commit comments

Comments
 (0)