adding permutations to interface

This commit is contained in:
Andy Eschbacher 2016-09-13 09:05:24 -04:00
parent ccccf68066
commit ce4cc637ae
2 changed files with 15 additions and 14 deletions

View File

@ -6,10 +6,11 @@ CREATE OR REPLACE FUNCTION
column_name TEXT, column_name TEXT,
w_type TEXT DEFAULT 'knn', w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5, num_ngbrs INT DEFAULT 5,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom', geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id') id_col TEXT DEFAULT 'cartodb_id')
RETURNS TABLE (z_val NUMERIC, p_val NUMERIC, rowid BIGINT) RETURNS TABLE (z_val NUMERIC, p_val NUMERIC, p_z_sim NUMERIC, rowid BIGINT)
AS $$ AS $$
from crankshaft.clustering import getis_ord from crankshaft.clustering import getis_ord
return getis_ord(subquery, column_name, w_type, num_ngbrs, geom_col, id_col) return getis_ord(subquery, column_name, w_type, num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;

View File

@ -1,10 +1,7 @@
""" """
Moran's I geostatistics (global clustering & outliers presence) Getis-Ord's G geostatistics (hotspot/coldspot analysis)
""" """
# TODO: Fill in local neighbors which have null/NoneType values with the
# average of the their neighborhood
import pysal as ps import pysal as ps
import plpy import plpy
from collections import OrderedDict from collections import OrderedDict
@ -15,11 +12,11 @@ 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, 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 Getis-Ord's G Implementation building neighbors with a PostGIS database and PySAL's Getis-Ord's G*
hotspot/coldspot analysis with PySAL. hotspot/coldspot module.
Andy Eschbacher Andy Eschbacher
""" """
@ -38,14 +35,17 @@ def getis_ord(subquery, attr,
result = plpy.execute(query) result = plpy.execute(query)
# if there are no neighbors, exit # if there are no neighbors, exit
if len(result) == 0: if len(result) == 0:
return pu.empty_zipped_array(3) return pu.empty_zipped_array(4)
except plpy.SPIError, err: except plpy.SPIError, err:
plpy.error('Query failed: %s' % err) plpy.error('Query failed: %s' % err)
attr_vals = pu.get_attributes(result) attr_vals = pu.get_attributes(result)
## build PySAL weight object
weight = pu.get_weight(result, w_type, num_ngbrs) weight = pu.get_weight(result, w_type, num_ngbrs)
# calculate LISA values # calculate Getis-Ord's G* z- and p-values
getis = ps.esda.getisord.G_Local(attr_vals, weight, star=True) getis = ps.esda.getisord.G_Local(attr_vals, weight,
star=True, permutations=permutations)
return zip(getis.z_sim, getis.p_sim, weight.id_order) return zip(getis.z_sim, getis.p_sim, getis.p_z_sim, weight.id_order)