|
| 1 | +import unittest |
| 2 | +import logging |
| 3 | +import cmapPy.pandasGEXpress.setup_GCToo_logger as setup_logger |
| 4 | +import cmapPy.math.fast_corr as fast_corr |
| 5 | +import numpy |
| 6 | +import pandas |
| 7 | + |
| 8 | + |
| 9 | +logger = logging.getLogger(setup_logger.LOGGER_NAME) |
| 10 | + |
| 11 | +num_iterations_functional_tests = 20 |
| 12 | +max_dimension_functional_tests = 10 |
| 13 | +multiplier_max_functional_tests = 100 |
| 14 | + |
| 15 | + |
| 16 | +class TestFastCorr(unittest.TestCase): |
| 17 | + @staticmethod |
| 18 | + def build_standard_x_y(): |
| 19 | + x = numpy.array([[1,7,2], [5,3,11]]) |
| 20 | + logger.debug("x: {}".format(x)) |
| 21 | + logger.debug("x.shape: {}".format(x.shape)) |
| 22 | + |
| 23 | + y = numpy.array([[13, 17, 19], [23, 31, 29]]) |
| 24 | + logger.debug("y: {}".format(y)) |
| 25 | + logger.debug("y.shape: {}".format(y.shape)) |
| 26 | + |
| 27 | + return x, y |
| 28 | + |
| 29 | + def test_fast_corr_just_x(self): |
| 30 | + logger.debug("*************happy path just x") |
| 31 | + x, _ = TestFastCorr.build_standard_x_y() |
| 32 | + |
| 33 | + ex = numpy.corrcoef(x, rowvar=False) |
| 34 | + logger.debug("expected ex: {}".format(ex)) |
| 35 | + |
| 36 | + r = fast_corr.fast_corr(x) |
| 37 | + logger.debug("r: {}".format(r)) |
| 38 | + |
| 39 | + self.assertTrue(numpy.allclose(ex, r)) |
| 40 | + |
| 41 | + #happy path just x, other direction |
| 42 | + ex = numpy.corrcoef(x, rowvar=True) |
| 43 | + logger.debug("happy path just x, other direction, expected ex: {}".format(ex)) |
| 44 | + r = fast_corr.fast_corr(x.T) |
| 45 | + logger.debug("r: {}".format(r)) |
| 46 | + self.assertTrue(numpy.allclose(ex, r)) |
| 47 | + |
| 48 | + def test_fast_corr_x_and_y(self): |
| 49 | + logger.debug("*************happy path x and y") |
| 50 | + x, y = TestFastCorr.build_standard_x_y() |
| 51 | + |
| 52 | + combined = numpy.hstack([x, y]) |
| 53 | + logger.debug("combined: {}".format(combined)) |
| 54 | + logger.debug("combined.shape: {}".format(combined.shape)) |
| 55 | + |
| 56 | + off_diag_ind = combined.shape[1] / 2 |
| 57 | + |
| 58 | + raw_ex = numpy.corrcoef(combined, rowvar=False) |
| 59 | + logger.debug("raw expected produced from numpy.cov on full combined - raw_ex: {}".format(raw_ex)) |
| 60 | + ex = raw_ex[:off_diag_ind, off_diag_ind:] |
| 61 | + logger.debug("expected ex: {}".format(ex)) |
| 62 | + |
| 63 | + r = fast_corr.fast_corr(x, y) |
| 64 | + logger.debug("r: {}".format(r)) |
| 65 | + self.assertTrue(numpy.allclose(ex, r)) |
| 66 | + |
| 67 | + #happy path x and y, other direction |
| 68 | + combined = numpy.hstack([x.T, y.T]) |
| 69 | + logger.debug("*************happy path x and y, other direction - combined: {}".format(combined)) |
| 70 | + logger.debug("combined.shape: {}".format(combined.shape)) |
| 71 | + |
| 72 | + off_diag_ind = combined.shape[1] / 2 |
| 73 | + |
| 74 | + raw_ex = numpy.corrcoef(combined, rowvar=False) |
| 75 | + logger.debug("raw expected produced from numpy.cov on full combined - raw_ex: {}".format(raw_ex)) |
| 76 | + ex = raw_ex[:off_diag_ind, off_diag_ind:] |
| 77 | + logger.debug("expected ex: {}".format(ex)) |
| 78 | + |
| 79 | + r = fast_corr.fast_corr(x.T, y.T) |
| 80 | + logger.debug("r: {}".format(r)) |
| 81 | + self.assertTrue(numpy.allclose(ex, r)) |
| 82 | + |
| 83 | + def test_fast_corr_x_and_y_different_shapes(self): |
| 84 | + logger.debug("*************happy path x and y different shapes") |
| 85 | + x, _ = TestFastCorr.build_standard_x_y() |
| 86 | + y = numpy.array([[13, 17, 19, 41, 23], [23, 29, 31, 37, 43]]) |
| 87 | + logger.debug("y.shape: {}".format(y.shape)) |
| 88 | + logger.debug("y:\n{}".format(y)) |
| 89 | + |
| 90 | + combined = numpy.hstack([x, y]) |
| 91 | + logger.debug("combined: {}".format(combined)) |
| 92 | + logger.debug("combined.shape: {}".format(combined.shape)) |
| 93 | + |
| 94 | + raw_ex = numpy.corrcoef(combined, rowvar=False) |
| 95 | + logger.debug("raw expected produced from numpy.cov on full combined - raw_ex: {}".format(raw_ex)) |
| 96 | + logger.debug("raw_ex.shape: {}".format(raw_ex.shape)) |
| 97 | + |
| 98 | + ex = raw_ex[:x.shape[1], -y.shape[1]:] |
| 99 | + logger.debug("expected ex: {}".format(ex)) |
| 100 | + logger.debug("ex.shape: {}".format(ex.shape)) |
| 101 | + |
| 102 | + r = fast_corr.fast_corr(x, y) |
| 103 | + logger.debug("r: {}".format(r)) |
| 104 | + self.assertTrue(numpy.allclose(ex, r)) |
| 105 | + |
| 106 | + def test_fast_corr_functional(self): |
| 107 | + logger.debug("*************happy path functional test using randomly generated matrices") |
| 108 | + |
| 109 | + for i in xrange(num_iterations_functional_tests): |
| 110 | + #the dimension containing the observations must have at least size 2 |
| 111 | + x_shape = [numpy.random.randint(2, max_dimension_functional_tests), |
| 112 | + numpy.random.randint(1, max_dimension_functional_tests)] |
| 113 | + logger.debug("x_shape: {}".format(x_shape)) |
| 114 | + |
| 115 | + x = numpy.random.rand(x_shape[0], x_shape[1]) * numpy.random.randint(1, multiplier_max_functional_tests, size=1) |
| 116 | + logger.debug("x:\n{}".format(x)) |
| 117 | + |
| 118 | + y_other_shape = numpy.random.randint(1, max_dimension_functional_tests, size=1)[0] |
| 119 | + y_shape = (x_shape[0], y_other_shape) |
| 120 | + logger.debug("y_shape: {}".format(y_shape)) |
| 121 | + y = numpy.random.rand(y_shape[0], y_shape[1]) * numpy.random.randint(1, multiplier_max_functional_tests, size=1) |
| 122 | + logger.debug("y:\n{}".format(y)) |
| 123 | + |
| 124 | + combined = numpy.hstack([x, y]) |
| 125 | + |
| 126 | + raw_ex = numpy.corrcoef(combined, rowvar=False) |
| 127 | + logger.debug("raw_ex.shape: {}".format(raw_ex.shape)) |
| 128 | + |
| 129 | + ex = raw_ex[:x.shape[1], -y.shape[1]:] |
| 130 | + logger.debug("ex:\n{}".format(ex)) |
| 131 | + logger.debug("ex.shape: {}".format(ex.shape)) |
| 132 | + |
| 133 | + r = fast_corr.fast_corr(x, y) |
| 134 | + logger.debug("r:\n{}".format(r)) |
| 135 | + logger.debug("r.shape: {}".format(r.shape)) |
| 136 | + |
| 137 | + self.assertTrue(numpy.allclose(ex, r)) |
| 138 | + |
| 139 | + def test_fast_spearman(self): |
| 140 | + x, y = TestFastCorr.build_standard_x_y() |
| 141 | + |
| 142 | + ex = numpy.array([[1.0, 1.0, 1.0], [-1.0, -1.0, -1.0], [1.0, 1.0, 1.0]]) |
| 143 | + |
| 144 | + r = fast_corr.fast_spearman(x, y) |
| 145 | + logger.debug("r: {}".format(r)) |
| 146 | + |
| 147 | + self.assertTrue(numpy.allclose(ex, r)) |
| 148 | + |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + setup_logger.setup(verbose=True) |
| 152 | + |
| 153 | + unittest.main() |
0 commit comments