|
| 1 | +import pytest |
| 2 | +import networkx as nx |
| 3 | +import pandas as pd |
| 4 | +import numpy as np |
| 5 | +import snp2cell |
| 6 | + |
| 7 | +snp2cell.util.set_num_cpu(1) |
| 8 | + |
| 9 | + |
| 10 | +def test_initialization_with_path(snp2cell_instance, tmp_path): |
| 11 | + # Create a temporary file to simulate the path |
| 12 | + path = tmp_path / "test_data.pkl" |
| 13 | + snp2cell_instance.save_data(path=str(path)) |
| 14 | + |
| 15 | + s2c = snp2cell.SNP2CELL(path=str(path), seed=42) |
| 16 | + assert s2c is not None, "snp2cell object was not created" |
| 17 | + assert s2c.grn is None, "GRN should be None" |
| 18 | + assert s2c.adata is None, "AnnData should be None" |
| 19 | + assert s2c.scores is None, "Scores should be None" |
| 20 | + |
| 21 | + |
| 22 | +def test_init_scores(snp2cell_instance): |
| 23 | + G = nx.Graph() |
| 24 | + G.add_edges_from([(1, 2), (2, 3)]) |
| 25 | + snp2cell_instance._set_grn(G) |
| 26 | + snp2cell_instance._init_scores() |
| 27 | + assert snp2cell_instance.scores is not None, "Scores should be initialized" |
| 28 | + assert ( |
| 29 | + snp2cell_instance.scores_prop is not None |
| 30 | + ), "Propagated scores should be initialized" |
| 31 | + assert snp2cell_instance.scores_rand == {}, "Random scores should be initialized" |
| 32 | + assert snp2cell_instance.de_groups == {}, "DE groups should be initialized" |
| 33 | + |
| 34 | + |
| 35 | +def test_set_grn(snp2cell_instance): |
| 36 | + G = nx.Graph() |
| 37 | + G.add_edges_from([(1, 2), (2, 3)]) |
| 38 | + snp2cell_instance._set_grn(G) |
| 39 | + assert snp2cell_instance.grn is not None, "GRN should be set" |
| 40 | + assert list(snp2cell_instance.grn.edges) == [ |
| 41 | + (1, 2), |
| 42 | + (2, 3), |
| 43 | + ], "GRN edges should match" |
| 44 | + |
| 45 | + |
| 46 | +def test_add_de_groups(snp2cell_instance): |
| 47 | + snp2cell_instance._add_de_groups("group1", ["A", "B"]) |
| 48 | + assert "group1" in snp2cell_instance.de_groups, "Group1 should be added" |
| 49 | + assert snp2cell_instance.de_groups["group1"] == [ |
| 50 | + "A", |
| 51 | + "B", |
| 52 | + ], "Group1 values should match" |
| 53 | + |
| 54 | + with pytest.raises(ValueError): |
| 55 | + snp2cell_instance._add_de_groups("group1", ["C"]) |
| 56 | + |
| 57 | + snp2cell_instance._add_de_groups("group2", ["C"]) |
| 58 | + with pytest.raises(ValueError): |
| 59 | + snp2cell_instance._add_de_groups("group2", ["A"]) |
| 60 | + |
| 61 | + |
| 62 | +def test_get_perturbed_stats(snp2cell_instance): |
| 63 | + snp2cell_instance.scores_rand["test_key"] = pd.DataFrame(np.random.randn(10, 3)) |
| 64 | + |
| 65 | + for suffix in snp2cell.SUFFIX: |
| 66 | + result = snp2cell_instance._get_perturbed_stats("test_key", suffix.value) |
| 67 | + assert isinstance(result, pd.DataFrame), "Result should be a DataFrame" |
| 68 | + |
| 69 | + |
| 70 | +def test_robust_z_score(): |
| 71 | + series = pd.Series([1, 2, 3, 4, 5]) |
| 72 | + result = snp2cell.SNP2CELL._robust_z_score(series) |
| 73 | + assert isinstance(result, pd.Series), "Result should be a Series" |
| 74 | + assert len(result) == 5, "Result length should match input length" |
| 75 | + |
| 76 | + |
| 77 | +def test_get_scores(snp2cell_instance): |
| 78 | + # Add some scores to the instance |
| 79 | + snp2cell_instance.add_grn_from_networkx(nx.from_edgelist([(1, 2), (2, 3)])) |
| 80 | + snp2cell_instance.scores = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) |
| 81 | + snp2cell_instance.scores_prop = pd.DataFrame({"A": [7, 8, 9], "B": [10, 11, 12]}) |
| 82 | + snp2cell_instance.scores_rand = {"test_key": pd.DataFrame(np.random.randn(10, 3))} |
| 83 | + |
| 84 | + # Test retrieving original and propagated scores |
| 85 | + for which in ["original", "propagated"]: |
| 86 | + scores = snp2cell_instance.get_scores(which=which) |
| 87 | + assert scores is not None, "Scores should be retrieved" |
| 88 | + assert isinstance(scores, pd.DataFrame), "Scores should be a DataFrame" |
| 89 | + assert "A" in scores.columns, "Scores should have column 'A'" |
| 90 | + assert "B" in scores.columns, "Scores should have column 'B'" |
| 91 | + |
| 92 | + # Test retrieving perturbed scores |
| 93 | + scores = snp2cell_instance.get_scores(which="perturbed") |
| 94 | + assert scores is not None, "Scores should be retrieved" |
| 95 | + assert isinstance(scores, dict), "Scores should be a dictionary" |
| 96 | + assert "test_key" in scores, "Scores should have key 'test_key'" |
| 97 | + assert isinstance(scores["test_key"], pd.DataFrame), "Scores should be a DataFrame" |
| 98 | + |
| 99 | + # Test retrieving with query |
| 100 | + scores = snp2cell_instance.get_scores(which="propagated", query="A > 7") |
| 101 | + assert len(scores) == 2, "Query should filter the DataFrame" |
| 102 | + |
| 103 | + # Test retrieving with sort_key |
| 104 | + scores = snp2cell_instance.get_scores(which="propagated", sort_key="A") |
| 105 | + assert scores.iloc[0]["A"] == 9, "Scores should be sorted in descending order" |
| 106 | + |
| 107 | + |
| 108 | +def test_remove_scores(snp2cell_instance): |
| 109 | + snp2cell_instance.add_grn_from_networkx(nx.from_edgelist([(1, 2), (2, 3)])) |
| 110 | + |
| 111 | + # Add some scores to the instance |
| 112 | + snp2cell_instance.scores = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) |
| 113 | + snp2cell_instance.scores_prop = pd.DataFrame( |
| 114 | + {"A": [7, 8, 9], "A__pval": [7, 8, 9], "B": [10, 11, 12]} |
| 115 | + ) |
| 116 | + snp2cell_instance.scores_rand = {"A": pd.DataFrame(np.random.randn(10, 3))} |
| 117 | + |
| 118 | + # Test removing non-existing scores (should not raise an error) |
| 119 | + snp2cell_instance.remove_scores(which="original", items=["C"]) |
| 120 | + assert snp2cell_instance.scores is not None, "Original scores should not be removed" |
| 121 | + assert ( |
| 122 | + snp2cell_instance.scores.shape[1] == 2 |
| 123 | + ), "Original scores should not be removed" |
| 124 | + assert ( |
| 125 | + snp2cell_instance.scores_prop is not None |
| 126 | + ), "Propagated scores should not be removed" |
| 127 | + assert ( |
| 128 | + snp2cell_instance.scores_prop.shape[1] == 3 |
| 129 | + ), "Propagated scores should not be removed" |
| 130 | + assert "A" in snp2cell_instance.scores_rand, "Random scores should not be removed" |
| 131 | + |
| 132 | + # Test removing original scores |
| 133 | + snp2cell_instance.remove_scores(which="original", items=["A"]) |
| 134 | + assert ( |
| 135 | + "A" not in snp2cell_instance.scores.columns |
| 136 | + ), "Original scores should be removed" |
| 137 | + assert ( |
| 138 | + "A" not in snp2cell_instance.scores_prop.columns |
| 139 | + ), "Propagated scores should also be removed" |
| 140 | + assert ( |
| 141 | + "A__pval" not in snp2cell_instance.scores_prop.columns |
| 142 | + ), "Corresponding statistics should also be removed" |
| 143 | + assert ( |
| 144 | + "A" not in snp2cell_instance.scores_rand |
| 145 | + ), "Random scores should also be removed" |
| 146 | + |
| 147 | + # Add scores to the instance |
| 148 | + snp2cell_instance.scores = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) |
| 149 | + snp2cell_instance.scores_prop = pd.DataFrame( |
| 150 | + {"A": [7, 8, 9], "A__pval": [7, 8, 9], "B": [10, 11, 12]} |
| 151 | + ) |
| 152 | + snp2cell_instance.scores_rand = {"A": pd.DataFrame(np.random.randn(10, 3))} |
| 153 | + |
| 154 | + # Test removing propagated scores |
| 155 | + snp2cell_instance.remove_scores(which="propagated", items=["A"]) |
| 156 | + assert ( |
| 157 | + "A" in snp2cell_instance.scores.columns |
| 158 | + ), "Original scores should not be removed" |
| 159 | + assert ( |
| 160 | + "A" not in snp2cell_instance.scores_prop.columns |
| 161 | + ), "Propagated scores should be removed" |
| 162 | + assert ( |
| 163 | + "A" not in snp2cell_instance.scores_rand |
| 164 | + ), "Random scores should also be removed" |
| 165 | + |
| 166 | + # Add scores to the instance |
| 167 | + snp2cell_instance.scores = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) |
| 168 | + snp2cell_instance.scores_prop = pd.DataFrame( |
| 169 | + {"A": [7, 8, 9], "A__pval": [7, 8, 9], "B": [10, 11, 12]} |
| 170 | + ) |
| 171 | + snp2cell_instance.scores_rand = {"A": pd.DataFrame(np.random.randn(10, 3))} |
| 172 | + |
| 173 | + # Test removing random scores |
| 174 | + snp2cell_instance.remove_scores(which="perturbed", items=["A"]) |
| 175 | + assert ( |
| 176 | + "A" in snp2cell_instance.scores.columns |
| 177 | + ), "Original scores should not be removed" |
| 178 | + assert ( |
| 179 | + "A" in snp2cell_instance.scores_prop.columns |
| 180 | + ), "Propagated scores should not be removed" |
| 181 | + assert "A" not in snp2cell_instance.scores_rand, "Random scores should be removed" |
| 182 | + |
| 183 | + # Test removing all propagated scores |
| 184 | + snp2cell_instance.remove_scores(which="propagated") |
| 185 | + assert ( |
| 186 | + snp2cell_instance.scores_prop.shape[1] == 0 |
| 187 | + ), "All propagated scores should be removed" |
| 188 | + assert ( |
| 189 | + len(snp2cell_instance.scores_rand) == 0 |
| 190 | + ), "All random scores should also be removed" |
| 191 | + |
| 192 | + # Test removing all original scores |
| 193 | + snp2cell_instance.remove_scores(which="original") |
| 194 | + assert ( |
| 195 | + snp2cell_instance.scores.shape[1] == 0 |
| 196 | + ), "All original scores should be removed" |
0 commit comments