File size: 3,320 Bytes
0161e74 | 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | import numpy as np
import pytest
from cell_eval.data import build_random_anndata
from cell_eval.utils import guess_is_lognorm
def test_is_lognorm_true():
data = build_random_anndata(normlog=True)
assert guess_is_lognorm(data)
def test_is_lognorm_view():
data = build_random_anndata(normlog=True)
sub = data[:100]
assert guess_is_lognorm(sub)
def test_is_lognorm_false():
data = build_random_anndata(normlog=False)
assert not guess_is_lognorm(data)
def test_guess_is_lognorm_valid_lognorm():
"""Test that valid log1p normalized data returns True."""
data = build_random_anndata(normlog=True, random_state=42)
# Should return True without raising exception
assert guess_is_lognorm(
data,
)
def test_guess_is_lognorm_valid_lognorm_sparse():
"""Test that valid log1p normalized sparse data returns True."""
data = build_random_anndata(normlog=True, as_sparse=True, random_state=42)
# Should return True without raising exception
assert guess_is_lognorm(
data,
)
def test_guess_is_lognorm_integer_data():
"""Test that integer data (raw counts) returns False."""
data = build_random_anndata(normlog=False, random_state=42)
# Should return False - integer data indicates raw counts
assert not guess_is_lognorm(
data,
)
def test_guess_is_lognorm_edge_case_near_threshold():
"""Test that values near but below threshold return True."""
data = build_random_anndata(normlog=True, random_state=42)
# Modify data to have values near threshold (10.9)
data.X = np.random.uniform(
0,
14.9,
size=data.X.shape, # type: ignore
)
# Should return True without raising exception
assert guess_is_lognorm(
data,
)
def test_guess_is_lognorm_exceeds_threshold():
"""Test that data with max value > 11.0 raises ValueError when ."""
data = build_random_anndata(normlog=True, random_state=42)
# Modify data to exceed threshold (mix of valid and invalid)
data.X = np.random.uniform(
0,
15.1,
size=data.X.shape, # type: ignore
)
with pytest.raises(ValueError, match="Invalid scale.*exceeds log1p threshold"):
guess_is_lognorm(
data,
)
def test_guess_is_lognorm_negative_values():
"""Test that data with negative values raises ValueError when ."""
data = build_random_anndata(normlog=True, random_state=42)
# Modify data to include negative values
data.X = np.random.uniform(
-1,
9,
size=data.X.shape, # type: ignore
)
with pytest.raises(ValueError, match="Invalid scale.*is negative"):
guess_is_lognorm(
data,
)
def test_guess_is_lognorm_mixed_scales():
"""Test mixed scenario: some cells with raw counts, some with log1p."""
data = build_random_anndata(normlog=True, random_state=42)
n_cells = data.X.shape[0] # type: ignore
half = n_cells // 2
data.X[:half] = np.random.uniform(0, 9, size=(half, data.X.shape[1])) # type: ignore
data.X[half:] = np.random.uniform(100, 5000, size=(n_cells - half, data.X.shape[1])) # type: ignore
with pytest.raises(ValueError, match="Invalid scale.*exceeds log1p threshold"):
guess_is_lognorm(
data,
)
|