|
| 1 | +import logging |
| 2 | +import unittest |
| 3 | +import numpy as np |
| 4 | +import pandas as pd |
| 5 | +import scipy.stats as stats |
| 6 | + |
| 7 | +import diffxpy.api as de |
| 8 | + |
| 9 | + |
| 10 | +class TestTwosample(unittest.TestCase): |
| 11 | + |
| 12 | + def test_null_distribution_wald(self, n_cells: int = 2000, n_genes: int = 100, n_groups: int = 2): |
| 13 | + """ |
| 14 | + Test if de.test_wald_loc() generates a uniform p-value distribution |
| 15 | + if it is given data simulated based on the null model. Returns the p-value |
| 16 | + of the two-side Kolmgorov-Smirnov test for equality of the observed |
| 17 | + p-value distriubution and a uniform distribution. |
| 18 | +
|
| 19 | + :param n_cells: Number of cells to simulate (number of observations per test). |
| 20 | + :param n_genes: Number of genes to simulate (number of tests). |
| 21 | + """ |
| 22 | + logging.getLogger("tensorflow").setLevel(logging.ERROR) |
| 23 | + logging.getLogger("batchglm").setLevel(logging.WARNING) |
| 24 | + logging.getLogger("diffxpy").setLevel(logging.WARNING) |
| 25 | + from batchglm.api.models.numpy.glm_nb import Simulator |
| 26 | + |
| 27 | + sim = Simulator(num_observations=n_cells, num_features=n_genes) |
| 28 | + sim.generate_sample_description(num_batches=0, num_conditions=0) |
| 29 | + sim.generate() |
| 30 | + |
| 31 | + random_sample_description = pd.DataFrame({ |
| 32 | + "condition": np.random.randint(n_groups, size=sim.nobs) |
| 33 | + }) |
| 34 | + |
| 35 | + test = de.test.two_sample( |
| 36 | + data=sim.input_data, |
| 37 | + grouping=random_sample_description["condition"].values, |
| 38 | + test="wald", |
| 39 | + noise_model="nb", |
| 40 | + ) |
| 41 | + summary = test.summary() |
| 42 | + |
| 43 | + # Compare p-value distribution under null model against uniform distribution. |
| 44 | + pval_h0 = stats.kstest(test.pval.flatten(), 'uniform').pvalue |
| 45 | + |
| 46 | + logging.getLogger("diffxpy").info('KS-test pvalue for null model match of test_wald_loc(): %f' % pval_h0) |
| 47 | + assert pval_h0 > 0.05, "KS-Test failed: pval_h0=%f is <= 0.05!" % np.round(pval_h0, 5) |
| 48 | + |
| 49 | + return True |
| 50 | + |
| 51 | + def test_null_distribution_lrt(self, n_cells: int = 2000, n_genes: int = 100, n_groups: int = 2): |
| 52 | + """ |
| 53 | + Test if de.test_wald_loc() generates a uniform p-value distribution |
| 54 | + if it is given data simulated based on the null model. Returns the p-value |
| 55 | + of the two-side Kolmgorov-Smirnov test for equality of the observed |
| 56 | + p-value distriubution and a uniform distribution. |
| 57 | +
|
| 58 | + :param n_cells: Number of cells to simulate (number of observations per test). |
| 59 | + :param n_genes: Number of genes to simulate (number of tests). |
| 60 | + """ |
| 61 | + logging.getLogger("tensorflow").setLevel(logging.ERROR) |
| 62 | + logging.getLogger("batchglm").setLevel(logging.WARNING) |
| 63 | + logging.getLogger("diffxpy").setLevel(logging.WARNING) |
| 64 | + from batchglm.api.models.numpy.glm_nb import Simulator |
| 65 | + |
| 66 | + sim = Simulator(num_observations=n_cells, num_features=n_genes) |
| 67 | + sim.generate_sample_description(num_batches=0, num_conditions=0) |
| 68 | + sim.generate() |
| 69 | + |
| 70 | + random_sample_description = pd.DataFrame({ |
| 71 | + "condition": np.random.randint(n_groups, size=sim.nobs) |
| 72 | + }) |
| 73 | + |
| 74 | + test = de.test.two_sample( |
| 75 | + data=sim.input_data, |
| 76 | + grouping=random_sample_description["condition"], |
| 77 | + test="wald", |
| 78 | + noise_model="nb", |
| 79 | + ) |
| 80 | + summary = test.summary() |
| 81 | + |
| 82 | + # Compare p-value distribution under null model against uniform distribution. |
| 83 | + pval_h0 = stats.kstest(test.pval.flatten(), 'uniform').pvalue |
| 84 | + |
| 85 | + logging.getLogger("diffxpy").info('KS-test pvalue for null model match of test_wald_loc(): %f' % pval_h0) |
| 86 | + assert pval_h0 > 0.05, "KS-Test failed: pval_h0=%f is <= 0.05!" % np.round(pval_h0, 5) |
| 87 | + |
| 88 | + return True |
| 89 | + |
| 90 | + def test_null_distribution_rank(self, n_cells: int = 2000, n_genes: int = 100, n_groups: int = 2): |
| 91 | + """ |
| 92 | + Test if de.test_wald_loc() generates a uniform p-value distribution |
| 93 | + if it is given data simulated based on the null model. Returns the p-value |
| 94 | + of the two-side Kolmgorov-Smirnov test for equality of the observed |
| 95 | + p-value distriubution and a uniform distribution. |
| 96 | +
|
| 97 | + :param n_cells: Number of cells to simulate (number of observations per test). |
| 98 | + :param n_genes: Number of genes to simulate (number of tests). |
| 99 | + """ |
| 100 | + logging.getLogger("tensorflow").setLevel(logging.ERROR) |
| 101 | + logging.getLogger("batchglm").setLevel(logging.WARNING) |
| 102 | + logging.getLogger("diffxpy").setLevel(logging.WARNING) |
| 103 | + from batchglm.api.models.numpy.glm_nb import Simulator |
| 104 | + |
| 105 | + sim = Simulator(num_observations=n_cells, num_features=n_genes) |
| 106 | + sim.generate_sample_description(num_batches=0, num_conditions=0) |
| 107 | + sim.generate() |
| 108 | + |
| 109 | + random_sample_description = pd.DataFrame({ |
| 110 | + "condition": np.random.randint(n_groups, size=sim.nobs) |
| 111 | + }) |
| 112 | + |
| 113 | + test = de.test.two_sample( |
| 114 | + data=sim.input_data, |
| 115 | + grouping=random_sample_description["condition"], |
| 116 | + test="rank" |
| 117 | + ) |
| 118 | + summary = test.summary() |
| 119 | + |
| 120 | + # Compare p-value distribution under null model against uniform distribution. |
| 121 | + pval_h0 = stats.kstest(test.pval.flatten(), 'uniform').pvalue |
| 122 | + |
| 123 | + logging.getLogger("diffxpy").info('KS-test pvalue for null model match of test_wald_loc(): %f' % pval_h0) |
| 124 | + assert pval_h0 > 0.05, "KS-Test failed: pval_h0=%f is <= 0.05!" % np.round(pval_h0, 5) |
| 125 | + |
| 126 | + return True |
| 127 | + |
| 128 | + def test_null_distribution_ttest(self, n_cells: int = 2000, n_genes: int = 100, n_groups: int = 2): |
| 129 | + """ |
| 130 | + Test if de.test_wald_loc() generates a uniform p-value distribution |
| 131 | + if it is given data simulated based on the null model. Returns the p-value |
| 132 | + of the two-side Kolmgorov-Smirnov test for equality of the observed |
| 133 | + p-value distriubution and a uniform distribution. |
| 134 | +
|
| 135 | + :param n_cells: Number of cells to simulate (number of observations per test). |
| 136 | + :param n_genes: Number of genes to simulate (number of tests). |
| 137 | + """ |
| 138 | + logging.getLogger("tensorflow").setLevel(logging.ERROR) |
| 139 | + logging.getLogger("batchglm").setLevel(logging.WARNING) |
| 140 | + logging.getLogger("diffxpy").setLevel(logging.WARNING) |
| 141 | + from batchglm.api.models.numpy.glm_nb import Simulator |
| 142 | + |
| 143 | + sim = Simulator(num_observations=n_cells, num_features=n_genes) |
| 144 | + sim.generate_sample_description(num_batches=0, num_conditions=0) |
| 145 | + sim.generate() |
| 146 | + |
| 147 | + random_sample_description = pd.DataFrame({ |
| 148 | + "condition": np.random.randint(n_groups, size=sim.nobs) |
| 149 | + }) |
| 150 | + |
| 151 | + test = de.test.two_sample( |
| 152 | + data=sim.input_data, |
| 153 | + grouping=random_sample_description["condition"], |
| 154 | + test="t_test" |
| 155 | + ) |
| 156 | + summary = test.summary() |
| 157 | + |
| 158 | + # Compare p-value distribution under null model against uniform distribution. |
| 159 | + pval_h0 = stats.kstest(test.pval.flatten(), 'uniform').pvalue |
| 160 | + |
| 161 | + logging.getLogger("diffxpy").info('KS-test pvalue for null model match of test_wald_loc(): %f' % pval_h0) |
| 162 | + assert pval_h0 > 0.05, "KS-Test failed: pval_h0=%f is <= 0.05!" % np.round(pval_h0, 5) |
| 163 | + |
| 164 | + return True |
| 165 | + |
| 166 | + |
| 167 | +if __name__ == '__main__': |
| 168 | + unittest.main() |
0 commit comments