|
| 1 | +import unittest |
| 2 | +import libpysal |
| 3 | +import geopandas |
| 4 | +from libpysal.common import pandas, RTOL, ATOL |
| 5 | +from .. import lee |
| 6 | +import numpy |
| 7 | + |
| 8 | + |
| 9 | +PANDAS_EXTINCT = pandas is None |
| 10 | + |
| 11 | +class Lee_Tester(unittest.TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.data = geopandas.read_file(libpysal.examples.get_path("columbus.shp")) |
| 14 | + self.w = libpysal.weights.Queen.from_dataframe(self.data) |
| 15 | + self.w.transform = 'r' |
| 16 | + self.x = self.data[['HOVAL']].values |
| 17 | + self.y = self.data[['CRIME']].values |
| 18 | + |
| 19 | + def test_global(self): |
| 20 | + numpy.random.seed(2478879) |
| 21 | + result = lee.Spatial_Pearson(connectivity=self.w.sparse).fit(self.x, self.y) |
| 22 | + known = numpy.array([[ 0.30136527, -0.23625603], |
| 23 | + [-0.23625603, 0.53512008]]) |
| 24 | + numpy.testing.assert_allclose(known,result.association_, rtol=RTOL, atol=ATOL) |
| 25 | + numpy.testing.assert_array_equal(result.reference_distribution_.shape, (999,2,2)) |
| 26 | + first_rep = numpy.array([[ 0.22803705, -0.08053692], |
| 27 | + [-0.08053692, 0.18897318]]) |
| 28 | + |
| 29 | + second_rep = numpy.array([[ 0.14179274, -0.06962692], |
| 30 | + [-0.06962692, 0.13688337]]) |
| 31 | + numpy.testing.assert_allclose(first_rep, result.reference_distribution_[0], |
| 32 | + rtol=RTOL, atol=ATOL) |
| 33 | + numpy.testing.assert_allclose(second_rep, result.reference_distribution_[1], |
| 34 | + rtol=RTOL, atol=ATOL) |
| 35 | + |
| 36 | + known_significance = numpy.array([[0.125, 0.026], |
| 37 | + [0.026, 0.001]]) |
| 38 | + numpy.testing.assert_allclose(known_significance, result.significance_, |
| 39 | + rtol=RTOL, atol=ATOL) |
| 40 | + |
| 41 | + def test_local(self): |
| 42 | + numpy.random.seed(2478879) |
| 43 | + result = lee.Spatial_Pearson_Local(connectivity=self.w.sparse).fit(self.x, self.y) |
| 44 | + known_locals = numpy.array([ 0.10246023, -0.24169198, -0.1308714 , |
| 45 | + 0.00895543, -0.16080899, -0.00950808, |
| 46 | + -0.14615398, -0.0627634 , 0.00661232, |
| 47 | + -0.42354628, -0.73121006, 0.02060548, |
| 48 | + 0.05187356, 0.06515283, -0.64400723, |
| 49 | + -0.37489818, -2.06573667, -0.10931854, |
| 50 | + 0.50823848, -0.06338637, -0.10559429, |
| 51 | + 0.03282849, -0.86618915, -0.62333825, |
| 52 | + -0.40910044,-0.41866868, -0.00702983, |
| 53 | + -0.4246288 , -0.52142507, -0.22481772, |
| 54 | + 0.1931263 , -1.39355214, 0.02036755, |
| 55 | + 0.22896308, -0.00240854, -0.30405211, |
| 56 | + -0.66950406, -0.21481868, -0.60320158, |
| 57 | + -0.38117303, -0.45584563, 0.32019362, |
| 58 | + -0.02818729, -0.02214172, 0.05587915, |
| 59 | + 0.0295999 , -0.78818135, 0.16854472, |
| 60 | + 0.2378127 ]) |
| 61 | + numpy.testing.assert_allclose(known_locals, result.associations_, |
| 62 | + rtol=RTOL, atol=ATOL) |
| 63 | + significances = numpy.array([0.154, 0.291, 0.358, 0.231, 0.146, |
| 64 | + 0.335, 0.325, 0.388, 0.244, 0.111, |
| 65 | + 0.019, 0.165, 0.136, 0.073, 0.014, |
| 66 | + 0.029, 0.002, 0.376, 0.003, 0.265, |
| 67 | + 0.449, 0.121, 0.072, 0.006, 0.036, |
| 68 | + 0.06 , 0.355, 0.01 , 0.017, 0.168, |
| 69 | + 0.022, 0.003, 0.217, 0.016, 0.337, |
| 70 | + 0.137, 0.015, 0.128, 0.11 , 0.09 , |
| 71 | + 0.168, 0.031, 0.457, 0.44 , 0.141, |
| 72 | + 0.249, 0.158, 0.018, 0.031]) |
| 73 | + numpy.testing.assert_allclose(significances, result.significance_, |
| 74 | + rtol=RTOL, atol=ATOL) |
| 75 | + |
| 76 | +suite = unittest.TestSuite() |
| 77 | +test_classes = [Lee_Tester] |
| 78 | +for i in test_classes: |
| 79 | + a = unittest.TestLoader().loadTestsFromTestCase(i) |
| 80 | + suite.addTest(a) |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + runner = unittest.TextTestRunner() |
| 84 | + runner.run(suite) |
0 commit comments