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 ---------------------------------------
|
# High level interface ---------------------------------------
|
||||||
|
|
||||||
|
|
||||||
def getis_ord(subquery, attr,
|
def getis_ord(subquery, attr,
|
||||||
w_type, num_ngbrs, permutations, geom_col, id_col):
|
w_type, num_ngbrs, permutations, geom_col, id_col):
|
||||||
"""
|
"""
|
||||||
Getis-Ord's G*
|
Getis-Ord's G*
|
||||||
Implementation building neighbors with a PostGIS database and PySAL's Getis-Ord's G*
|
Implementation building neighbors with a PostGIS database and PySAL's
|
||||||
hotspot/coldspot module.
|
Getis-Ord's G* hotspot/coldspot module.
|
||||||
Andy Eschbacher
|
Andy Eschbacher
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@ -41,11 +42,11 @@ def getis_ord(subquery, attr,
|
|||||||
|
|
||||||
attr_vals = pu.get_attributes(result)
|
attr_vals = pu.get_attributes(result)
|
||||||
|
|
||||||
## build PySAL weight object
|
# build PySAL weight object
|
||||||
weight = pu.get_weight(result, w_type, num_ngbrs)
|
weight = pu.get_weight(result, w_type, num_ngbrs)
|
||||||
|
|
||||||
# calculate Getis-Ord's G* z- and p-values
|
# calculate Getis-Ord's G* z- and p-values
|
||||||
getis = ps.esda.getisord.G_Local(attr_vals, weight,
|
getis = ps.esda.getisord.G_Local(attr_vals, weight,
|
||||||
star=True, permutations=permutations)
|
star=True, permutations=permutations)
|
||||||
|
|
||||||
return zip(getis.z_sim, getis.p_sim, getis.p_z_sim, weight.id_order)
|
return zip(getis.z_sim, getis.p_sim, getis.p_z_sim, weight.id_order)
|
||||||
|
@ -14,29 +14,31 @@ import crankshaft.pysal_utils as pu
|
|||||||
from crankshaft import random_seeds
|
from crankshaft import random_seeds
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
|
||||||
class GetisTest(unittest.TestCase):
|
class GetisTest(unittest.TestCase):
|
||||||
"""Testing class for Getis-Ord's G funtion
|
"""Testing class for Getis-Ord's G funtion
|
||||||
This test replicates the work done in PySAL documentation:
|
This test replicates the work done in PySAL documentation:
|
||||||
https://pysal.readthedocs.io/en/v1.11.0/users/tutorials/autocorrelation.html#local-g-and-g
|
https://pysal.readthedocs.io/en/v1.11.0/users/tutorials/autocorrelation.html#local-g-and-g
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
plpy._reset()
|
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())
|
self.getis_data = json.loads(open(fixture_file('getis.json')).read())
|
||||||
|
|
||||||
def test_getis_ord(self):
|
def test_getis_ord(self):
|
||||||
"""Test Getis-Ord's G*"""
|
"""Test Getis-Ord's G*"""
|
||||||
data = [ { 'id': d['id'],
|
data = [{'id': d['id'],
|
||||||
'attr1': d['value'],
|
'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)
|
plpy._define_result('select', data)
|
||||||
random_seeds.set_random_seeds(1234)
|
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]
|
result = [(row[0], row[1]) for row in result]
|
||||||
expected = np.array(self.getis_data)[:, 0:2]
|
expected = np.array(self.getis_data)[:, 0:2]
|
||||||
for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected):
|
for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected):
|
||||||
self.assertAlmostEqual(res_z, exp_z, delta=1e-2)
|
self.assertAlmostEqual(res_z, exp_z, delta=1e-2)
|
||||||
if exp_p <= 0.05:
|
if exp_p <= 0.05:
|
||||||
self.assertTrue(res_p < 0.05)
|
self.assertTrue(res_p < 0.05)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user