Modify sql code to user the python virtualenv

This commit is contained in:
Javier Goizueta 2016-03-09 15:00:50 +01:00
parent cdd2d9e722
commit 8e972128eb
5 changed files with 21 additions and 0 deletions

18
src/pg/sql/01_py.sql Normal file
View File

@ -0,0 +1,18 @@
-- Use the crankshaft python module
CREATE OR REPLACE FUNCTION _cdb_crankshaft_activate_py()
RETURNS VOID
AS $$
# activate virtualenv
# TODO: parameterize with environment variables or something
venv_path = '/home/ubuntu/crankshaft/src/py/dev'
activate_path = venv_path + '/bin/activate_this.py'
exec(open(activate_path).read(),
dict(__file__=activate_path))
# import something from virtualenv
# from crankshaft import random_seeds
# do some stuff
# random_seeds.set_random_seeds(123)
# plpy.notice('here we are')
$$ LANGUAGE plpythonu;

View File

@ -4,6 +4,7 @@
CREATE OR REPLACE FUNCTION
_cdb_random_seeds (seed_value INTEGER) RETURNS VOID
AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft import random_seeds
random_seeds.set_random_seeds(seed_value)
$$ LANGUAGE plpythonu;

View File

@ -11,6 +11,7 @@ CREATE OR REPLACE FUNCTION
w_type TEXT DEFAULT 'knn')
RETURNS TABLE (moran FLOAT, quads TEXT, significance FLOAT, ids INT)
AS $$
plpy.execute('SELECT cdb_crankshaft._cdb_crankshaft_activate_py()')
from crankshaft.clustering import moran_local
# TODO: use named parameters or a dictionary
return moran_local(t, attr, significance, num_ngbrs, permutations, geom_column, id_col, w_type)
@ -29,6 +30,7 @@ CREATE OR REPLACE FUNCTION
w_type TEXT DEFAULT 'knn')
RETURNS TABLE(moran FLOAT, quads TEXT, significance FLOAT, 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(t, numerator, denominator, significance, num_ngbrs, permutations, geom_column, id_col, w_type)