adding hot/cold/outlier getters

This commit is contained in:
Andy Eschbacher 2016-05-20 14:05:59 -04:00
parent cc4a35ebd9
commit f571e59a95

View File

@ -10,6 +10,7 @@ CREATE OR REPLACE FUNCTION
num_ngbrs INT DEFAULT 5) num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran NUMERIC, significance NUMERIC) RETURNS TABLE (moran NUMERIC, significance NUMERIC)
AS $$ AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import moran_local from crankshaft.clustering import moran_local
# TODO: use named parameters or a dictionary # TODO: use named parameters or a dictionary
return moran(subquery, attr, num_ngbrs, permutations, geom_col, id_col, w_type) return moran(subquery, attr, num_ngbrs, permutations, geom_col, id_col, w_type)
@ -17,21 +18,93 @@ $$ LANGUAGE plpythonu;
-- Moran's I Local -- Moran's I Local
CREATE OR REPLACE FUNCTION CREATE OR REPLACE FUNCTION
CDB_AreasOfInterest_Local( _CDB_AreasOfInterest_Local(
subquery TEXT, subquery TEXT,
attr TEXT, attr TEXT,
permutations INT DEFAULT 99, permutations INT,
geom_col TEXT DEFAULT 'the_geom', geom_col TEXT,
id_col TEXT DEFAULT 'cartodb_id', id_col TEXT,
w_type TEXT DEFAULT 'knn', w_type TEXT,
num_ngbrs INT DEFAULT 5) num_ngbrs INT)
RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC) RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$ AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import moran_local from crankshaft.clustering import moran_local
# TODO: use named parameters or a dictionary # TODO: use named parameters or a dictionary
return moran_local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs) return moran_local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs)
$$ LANGUAGE plpythonu; $$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION
CDB_AreasOfInterest_Local(
subquery TEXT,
attr TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs);
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialHotspots(
subquery TEXT,
attr TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('HH', 'HL');
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialColdspots(
subquery TEXT,
attr TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('LL', 'LH');
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialOutliers(
subquery TEXT,
attr TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local(subquery, attr, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('HL', 'LH');
$$ LANGUAGE SQL;
-- Moran's I Rate (global) -- Moran's I Rate (global)
CREATE OR REPLACE FUNCTION CREATE OR REPLACE FUNCTION
CDB_AreasOfInterest_Global_Rate( CDB_AreasOfInterest_Global_Rate(
@ -45,6 +118,7 @@ CREATE OR REPLACE FUNCTION
num_ngbrs INT DEFAULT 5) num_ngbrs INT DEFAULT 5)
RETURNS TABLE (moran FLOAT, significance FLOAT) RETURNS TABLE (moran FLOAT, significance FLOAT)
AS $$ AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import moran_local from crankshaft.clustering import moran_local
# TODO: use named parameters or a dictionary # TODO: use named parameters or a dictionary
return moran_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs) return moran_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs)
@ -52,6 +126,25 @@ $$ LANGUAGE plpythonu;
-- Moran's I Local Rate -- Moran's I Local Rate
CREATE OR REPLACE FUNCTION
_CDB_AreasOfInterest_Local_Rate(
subquery TEXT,
numerator TEXT,
denominator TEXT,
permutations INT,
geom_col TEXT,
id_col TEXT,
w_type TEXT,
num_ngbrs INT)
RETURNS
TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import moran_local_rate
# TODO: use named parameters or a dictionary
return moran_local_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs)
$$ LANGUAGE plpythonu;
CREATE OR REPLACE FUNCTION CREATE OR REPLACE FUNCTION
CDB_AreasOfInterest_Local_Rate( CDB_AreasOfInterest_Local_Rate(
subquery TEXT, subquery TEXT,
@ -65,10 +158,71 @@ CREATE OR REPLACE FUNCTION
RETURNS RETURNS
TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC) TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$ AS $$
from crankshaft.clustering import moran_local_rate
# TODO: use named parameters or a dictionary SELECT moran, quads, significance, ids, y
return moran_local_rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs) FROM cdb_crankshaft._CDB_AreasOfInterest_Local_Rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs);
$$ LANGUAGE plpythonu;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialHotspots_Rate(
subquery TEXT,
numerator TEXT,
denominator TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS
TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local_Rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('HH', 'HL');
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialColdspots_Rate(
subquery TEXT,
numerator TEXT,
denominator TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS
TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local_Rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('LL', 'LH');
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION
CDB_GetSpatialOutliers_Rate(
subquery TEXT,
numerator TEXT,
denominator TEXT,
permutations INT DEFAULT 99,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id',
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5)
RETURNS
TABLE(moran NUMERIC, quads TEXT, significance NUMERIC, ids INT, y NUMERIC)
AS $$
SELECT moran, quads, significance, ids, y
FROM cdb_crankshaft._CDB_AreasOfInterest_Local_Rate(subquery, numerator, denominator, permutations, geom_col, id_col, w_type, num_ngbrs)
WHERE quads IN ('HL', 'LH');
$$ LANGUAGE SQL;
-- -- Moran's I Local Bivariate -- -- Moran's I Local Bivariate
-- CREATE OR REPLACE FUNCTION -- CREATE OR REPLACE FUNCTION