From cc0a683a268a62f197b9fee4ae11c05fb44cea03 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 6 Dec 2016 12:26:25 -0500 Subject: [PATCH] fix query templating / response access --- src/py/crankshaft/crankshaft/analysis_data_provider.py | 6 ++++-- src/py/crankshaft/crankshaft/clustering/kmeans.py | 9 +++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index 973bd1b..eb730ff 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -46,13 +46,15 @@ class AnalysisDataProvider: def get_nonspatial_kmeans(self, params): """fetch data for non-spatial kmeans""" + agg_cols = ', '.join(['array_agg({0}) As arr_col{1}'.format(idx+1, val) + for idx, val in enumerate(params['colnames'])]) + query = ''' SELECT {cols}, array_agg({id_col}) As rowid FROM ({subquery}) As a '''.format(subquery=params['subquery'], id_col=params['id_col'], - cols=', '.join(['array_agg({0}) As arr_{0}'.format(c) - for c in params['colnames']])) + cols=agg_cols) try: data = plpy.execute(query) return data diff --git a/src/py/crankshaft/crankshaft/clustering/kmeans.py b/src/py/crankshaft/crankshaft/clustering/kmeans.py index 2f53d15..bb3343b 100644 --- a/src/py/crankshaft/crankshaft/clustering/kmeans.py +++ b/src/py/crankshaft/crankshaft/clustering/kmeans.py @@ -59,9 +59,9 @@ class Kmeans: # fill array with values for k-means clustering if standarize: cluster_columns = _scale_data( - _extract_columns(data, colnames)) + _extract_columns(data, len(colnames))) else: - cluster_columns = _extract_columns(data, colnames) + cluster_columns = _extract_columns(data, len(colnames)) print str(cluster_columns) # TODO: decide on optimal parameters for most cases @@ -84,14 +84,15 @@ class Kmeans: # -- Preprocessing steps -def _extract_columns(data, colnames): +def _extract_columns(data, n_cols): """ Extract the features from the query and pack them into a NumPy array data (list of dicts): result of the kmeans request id_col_name (string): name of column which has the row id (not a feature of the analysis) """ - return np.array([data[0]['arr_{}'.format(c)] for c in colnames], + return np.array([data[0]['arr_col{0}'.format(i+1)] + for i in xrange(n_cols)], dtype=float).T