|
2 | 2 | import numpy as np |
3 | 3 | from scipy import sparse |
4 | 4 |
|
| 5 | +from anndata import AnnData |
5 | 6 | from anndata.utils import asarray |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
9 | | -from anndata.tests.helpers import gen_adata |
| 10 | +from anndata.tests.helpers import gen_adata, assert_equal |
10 | 11 |
|
11 | 12 | UNLABELLED_ARRAY_TYPES = [ |
12 | 13 | pytest.param(sparse.csr_matrix, id="csr"), |
|
22 | 23 | @pytest.mark.parametrize("orig_array_type", UNLABELLED_ARRAY_TYPES) |
23 | 24 | @pytest.mark.parametrize("new_array_type", UNLABELLED_ARRAY_TYPES) |
24 | 25 | def test_setter_singular_dim(shape, orig_array_type, new_array_type): |
| 26 | + # https://github.com/theislab/anndata/issues/500 |
25 | 27 | adata = gen_adata(shape, X_type=orig_array_type) |
26 | 28 | adata.X = new_array_type(np.ones(shape)) |
27 | 29 | np.testing.assert_equal(asarray(adata.X), 1) |
| 30 | + |
| 31 | + |
| 32 | +############################### |
| 33 | +# Tests for `adata.X is None` # |
| 34 | +############################### |
| 35 | + |
| 36 | + |
| 37 | +def test_set_x_is_none(): |
| 38 | + # test setter and getter |
| 39 | + adata = AnnData(np.array([[1, 2, 3], [4, 5, 6]]), dict(o1=[1, 2], o2=[3, 4])) |
| 40 | + adata.X = None |
| 41 | + assert adata.X is None |
| 42 | + |
| 43 | + |
| 44 | +def test_del_set_equiv_X(): |
| 45 | + """Tests that `del adata.X` is equivalent to `adata.X = None`""" |
| 46 | + # test setter and deleter |
| 47 | + orig = gen_adata((10, 10)) |
| 48 | + copy = orig.copy() |
| 49 | + |
| 50 | + del orig.X |
| 51 | + copy.X = None |
| 52 | + |
| 53 | + assert orig.X is None |
| 54 | + assert_equal(orig, copy) |
| 55 | + |
| 56 | + # Check that deleting again is still fine |
| 57 | + del orig.X |
| 58 | + assert orig.X is None |
| 59 | + |
| 60 | + |
| 61 | +def test_init_X_as_none(): |
| 62 | + # test initialiser |
| 63 | + shape = (3, 5) |
| 64 | + adata = AnnData(None, uns=dict(test=np.array((3, 3))), shape=shape) |
| 65 | + assert adata.X is None |
| 66 | + assert adata.shape == shape |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize("shape", SINGULAR_SHAPES + [pytest.param((5, 3), id="(5, 3)")]) |
| 70 | +def test_transpose_with_X_as_none(shape): |
| 71 | + adata = gen_adata(shape, X_type=lambda x: None) |
| 72 | + adataT = adata.transpose() |
| 73 | + assert_equal(adataT.shape, shape[::-1]) |
| 74 | + assert_equal(adataT.obsp.keys(), adata.varp.keys()) |
| 75 | + assert_equal(adataT.T, adata) |
0 commit comments