File size: 3,691 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Tests for the benchmark module."""

import numpy as np
import pandas as pd
import pytest


def test_correlate_with_halflives(analyzed_adata):
    """correlate_with_halflives returns correlation dict."""
    from scptr.benchmark import correlate_with_halflives

    # Create synthetic half-life data matching some gene names
    gene_names = analyzed_adata.var_names.tolist()
    hl_df = pd.DataFrame({
        "gene_symbol": gene_names[:50],
        "half_life_hours": np.random.exponential(5, size=50),
    })

    result = correlate_with_halflives(analyzed_adata, hl_df)

    assert "spearman_r" in result
    assert "pearson_r" in result
    assert "n_genes" in result
    assert result["n_genes"] == 50


def test_correlate_no_overlap(analyzed_adata):
    """correlate_with_halflives handles no gene overlap gracefully."""
    from scptr.benchmark import correlate_with_halflives

    hl_df = pd.DataFrame({
        "gene_symbol": ["FAKE_GENE_1", "FAKE_GENE_2"],
        "half_life_hours": [5.0, 10.0],
    })

    result = correlate_with_halflives(analyzed_adata, hl_df)
    assert result["n_genes"] == 0
    assert np.isnan(result["spearman_r"])


def test_are_enrichment(analyzed_adata):
    """are_enrichment runs without error."""
    from scptr.benchmark import are_enrichment

    result = are_enrichment(analyzed_adata)
    assert "label" in result
    assert result["label"] == "ARE"
    assert "p_value" in result


def test_nmd_enrichment(analyzed_adata):
    """nmd_enrichment runs without error."""
    from scptr.benchmark import nmd_enrichment

    result = nmd_enrichment(analyzed_adata)
    assert "label" in result
    assert result["label"] == "NMD"
    assert "p_value" in result


def test_subsampling_robustness(analyzed_adata):
    """subsampling_robustness returns DataFrame with expected columns."""
    from scptr.benchmark import subsampling_robustness

    result = subsampling_robustness(
        analyzed_adata, fractions=[0.5, 0.8], n_repeats=2
    )

    assert isinstance(result, pd.DataFrame)
    assert "fraction" in result.columns
    assert "spearman_r" in result.columns
    assert "pearson_r" in result.columns
    assert len(result) == 4  # 2 fractions * 2 repeats


def test_cross_dataset_consistency(analyzed_adata):
    """cross_dataset_consistency works with multiple copies."""
    from scptr.benchmark import cross_dataset_consistency

    result = cross_dataset_consistency({
        "dataset_A": analyzed_adata,
        "dataset_B": analyzed_adata,
    })

    assert isinstance(result, pd.DataFrame)
    assert len(result) == 1  # 1 pair
    assert "spearman_r" in result.columns
    # Same data should have perfect correlation
    assert result["spearman_r"].iloc[0] > 0.99


def test_enrichment_barplot(analyzed_adata):
    """enrichment_barplot produces a figure."""
    import matplotlib
    matplotlib.use("Agg")
    from scptr.plotting import enrichment_barplot

    results = [
        {"label": "ARE", "median_gamma_in_set": 0.5,
         "median_gamma_background": 0.3, "p_value": 0.01},
        {"label": "NMD", "median_gamma_in_set": 0.6,
         "median_gamma_background": 0.3, "p_value": 0.005},
    ]

    fig = enrichment_barplot(results)
    assert fig is not None


def test_halflife_scatter(analyzed_adata):
    """halflife_scatter produces a figure."""
    import matplotlib
    matplotlib.use("Agg")
    from scptr.plotting import halflife_scatter

    gene_names = analyzed_adata.var_names.tolist()
    hl_df = pd.DataFrame({
        "gene_symbol": gene_names[:20],
        "half_life_hours": np.random.exponential(5, size=20),
    })

    fig = halflife_scatter(analyzed_adata, hl_df)
    assert fig is not None