File size: 1,992 Bytes
9d901ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Tests for readwrite validation."""

import warnings

import numpy as np
import pytest
from anndata import AnnData
from scipy.sparse import csr_matrix


@pytest.fixture
def tmp_h5ad_with_layers(tmp_path):
    """Create a temporary h5ad file with unspliced/spliced layers."""
    adata = AnnData(
        X=csr_matrix(np.ones((10, 5), dtype=np.float32)),
        layers={
            "spliced": csr_matrix(np.ones((10, 5), dtype=np.float32)),
            "unspliced": csr_matrix(np.ones((10, 5), dtype=np.float32)),
        },
    )
    path = tmp_path / "with_layers.h5ad"
    adata.write_h5ad(path)
    return path


@pytest.fixture
def tmp_h5ad_without_layers(tmp_path):
    """Create a temporary h5ad file without unspliced/spliced layers."""
    adata = AnnData(X=csr_matrix(np.ones((10, 5), dtype=np.float32)))
    path = tmp_path / "without_layers.h5ad"
    adata.write_h5ad(path)
    return path


def test_read_h5ad_no_warning_with_layers(tmp_h5ad_with_layers):
    """read_h5ad does not warn when layers are present."""
    from scptr.readwrite import read_h5ad

    with warnings.catch_warnings():
        warnings.simplefilter("error")
        adata = read_h5ad(str(tmp_h5ad_with_layers))

    assert "unspliced" in adata.layers
    assert "spliced" in adata.layers


def test_read_h5ad_warns_missing_layers(tmp_h5ad_without_layers):
    """read_h5ad warns when unspliced/spliced layers are missing."""
    from scptr.readwrite import read_h5ad

    with pytest.warns(UserWarning, match="missing expected layers"):
        read_h5ad(str(tmp_h5ad_without_layers))


def test_validate_layers_warns_partial(tmp_path):
    """_validate_layers warns when only one layer is missing."""
    from scptr.readwrite import _validate_layers

    adata = AnnData(
        X=csr_matrix(np.ones((10, 5), dtype=np.float32)),
        layers={"spliced": csr_matrix(np.ones((10, 5), dtype=np.float32))},
    )

    with pytest.warns(UserWarning, match="unspliced"):
        _validate_layers(adata)