add test for global moran

This commit is contained in:
Andy Eschbacher 2016-03-30 12:09:49 -04:00
parent 4c243bf1d3
commit 02b74813ac

View File

@ -52,7 +52,7 @@ class MoranTest(unittest.TestCase):
data = [ { 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] data = [ { 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data]
plpy._define_result('select', data) plpy._define_result('select', data)
random_seeds.set_random_seeds(1234) random_seeds.set_random_seeds(1234)
result = cc.moran_local('table', 'value', 99, 'the_geom', 'cartodb_id', 'knn', 5) result = cc.moran_local('subquery', 'value', 99, 'the_geom', 'cartodb_id', 'knn', 5)
result = [(row[0], row[1]) for row in result] result = [(row[0], row[1]) for row in result]
expected = self.moran_data expected = self.moran_data
for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected): for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected):
@ -70,3 +70,14 @@ class MoranTest(unittest.TestCase):
expected = self.moran_data expected = self.moran_data
for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected): for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected):
self.assertAlmostEqual(res_val, exp_val) self.assertAlmostEqual(res_val, exp_val)
def test_moran(self):
"""Test Moran's I global"""
data = [{ 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data]
plpy._define_result('select', data)
random_seeds.set_random_seeds(1235)
result = cc.moran('table', 'value', 99, 'the_geom', 'cartodb_id', 'knn', 5)
print 'result == None?', result == None
result_moran = result[0][0]
expected_moran = np.array([row[0] for row in self.moran_data]).mean()
self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)