Merge branch 'adds-getis-analysis' of https://github.com/cartodb/crankshaft into adds-getis-analysis
This commit is contained in:
commit
5127845100
@ -11,12 +11,13 @@ import crankshaft.pysal_utils as pu
|
||||
|
||||
# High level interface ---------------------------------------
|
||||
|
||||
|
||||
def getis_ord(subquery, attr,
|
||||
w_type, num_ngbrs, permutations, geom_col, id_col):
|
||||
"""
|
||||
Getis-Ord's G*
|
||||
Implementation building neighbors with a PostGIS database and PySAL's Getis-Ord's G*
|
||||
hotspot/coldspot module.
|
||||
Implementation building neighbors with a PostGIS database and PySAL's
|
||||
Getis-Ord's G* hotspot/coldspot module.
|
||||
Andy Eschbacher
|
||||
"""
|
||||
|
||||
@ -41,7 +42,7 @@ def getis_ord(subquery, attr,
|
||||
|
||||
attr_vals = pu.get_attributes(result)
|
||||
|
||||
## build PySAL weight object
|
||||
# build PySAL weight object
|
||||
weight = pu.get_weight(result, w_type, num_ngbrs)
|
||||
|
||||
# calculate Getis-Ord's G* z- and p-values
|
||||
|
@ -14,6 +14,7 @@ import crankshaft.pysal_utils as pu
|
||||
from crankshaft import random_seeds
|
||||
import json
|
||||
|
||||
|
||||
class GetisTest(unittest.TestCase):
|
||||
"""Testing class for Getis-Ord's G funtion
|
||||
This test replicates the work done in PySAL documentation:
|
||||
@ -22,21 +23,22 @@ class GetisTest(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
plpy._reset()
|
||||
self.neighbors_data = json.loads(open(fixture_file('neighbors_getis.json')).read())
|
||||
self.neighbors_data = json.loads(
|
||||
open(fixture_file('neighbors_getis.json')).read())
|
||||
self.getis_data = json.loads(open(fixture_file('getis.json')).read())
|
||||
|
||||
def test_getis_ord(self):
|
||||
"""Test Getis-Ord's G*"""
|
||||
data = [ { 'id': d['id'],
|
||||
data = [{'id': d['id'],
|
||||
'attr1': d['value'],
|
||||
'neighbors': d['neighbors'] } for d in self.neighbors_data]
|
||||
'neighbors': d['neighbors']} for d in self.neighbors_data]
|
||||
plpy._define_result('select', data)
|
||||
random_seeds.set_random_seeds(1234)
|
||||
result = cc.getis_ord('subquery', 'value', 'knn', 5, 999, 'the_geom', 'cartodb_id')
|
||||
result = cc.getis_ord('subquery', 'value',
|
||||
'knn', 5, 999, 'the_geom', 'cartodb_id')
|
||||
result = [(row[0], row[1]) for row in result]
|
||||
expected = np.array(self.getis_data)[:, 0:2]
|
||||
for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected):
|
||||
self.assertAlmostEqual(res_z, exp_z, delta=1e-2)
|
||||
if exp_p <= 0.05:
|
||||
self.assertTrue(res_p < 0.05)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user