From d8717048851f7753f53c39b8e8f9d805865cc13a Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 28 Mar 2017 18:09:43 -0400 Subject: [PATCH] stubs out mvp --- src/py/crankshaft/crankshaft/__init__.py | 1 + .../crankshaft/analysis_data_provider.py | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/py/crankshaft/crankshaft/__init__.py b/src/py/crankshaft/crankshaft/__init__.py index a8060f8..1ad4317 100644 --- a/src/py/crankshaft/crankshaft/__init__.py +++ b/src/py/crankshaft/crankshaft/__init__.py @@ -4,3 +4,4 @@ import crankshaft.clustering import crankshaft.space_time_dynamics import crankshaft.segmentation import analysis_data_provider +import crankshaft.optimization diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index cbc27bc..22ac1c6 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -1,9 +1,9 @@ """class for fetching data""" import plpy import pysal_utils as pu +import numpy as np - -class AnalysisDataProvider: +class AnalysisDataProvider(object): def get_getis(self, w_type, params): """fetch data for getis ord's g""" try: @@ -65,3 +65,31 @@ class AnalysisDataProvider: return data except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) + + def get_column(self, table, column): + """ + """ + query = ''' + SELECT array_agg("{column}" ORDER BY "cartodb_id" ASC) as col + FROM "{table}" + '''.format(table=table, column=column) + resp = plpy.execute(query) + return np.array(resp[0]['col'], dtype=float) + + def get_pairwise_distances(self, drain, source): + """retuns the pairwise distances between row i and j for all i in table1 and j in table1""" + query = ''' + SELECT array_agg(ST_Distance(d."the_geom"::geography, + s."the_geom"::geography) / 1000.0 + ORDER BY d."cartodb_id" ASC) as dist + FROM "{drain}" as d, "{source}" as s + GROUP BY s."cartodb_id" + ORDER BY s."cartodb_id" ASC + '''.format(drain=drain, source=source) + + resp = plpy.execute(query) + + # len(s) x len(d) matrix + return np.array([np.array(row['dist'], dtype=float) + for row in resp], dtype=float) +