diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index 610dddb..b4a4a3a 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -26,39 +26,49 @@ def verify_data(func): class AnalysisDataProvider(object): @verify_data - def get_getis(self, w_type, params): - """fetch data for getis ord's g""" - query = pu.construct_neighbor_query(w_type, params) - return plpy.execute(query) + def get_weight_and_attrs(self, w_type, params): + """fetch data for moran's i, getis, and spark markov analyses + This method returns a feature id, a list of its neighbors ids, and the + attribute(s) of the feature. - @verify_data - def get_markov(self, w_type, params): - """fetch data for spatial markov""" - query = pu.construct_neighbor_query(w_type, params) - return plpy.execute(query) - - @verify_data - def get_neighbor(self, w_type, params): - """fetch data for moran's i analyses""" + Args: + w_type (str): Type of weight. One of ``knn`` (default) or + ``queen``. + params (:obj:`dict`): Parameters for data retrieval. The keys are + defined below, with the descriptions of their values. + - `id_col` (str): Name of database index. Defaults to + `cartodb_id` + - `geom_col` (str): Geometry column. Defaults to `the_geom`. + - `subquery` (str): Query to get access to data + - `num_ngbrs` (int, optional): Number of neighbors if using kNN + - `time_cols` (list of str, optional): If using with spatial + markov, this is a list of columns for the analysis. They should + be ordered in time. + - `numerator` (str, optional): The numerator in Moran's I local + rate + - `denominator` (str, optional): Used in conjunction with + `numerator`. + """ query = pu.construct_neighbor_query(w_type, params) return plpy.execute(query) @verify_data def get_nonspatial_kmeans(self, params): """ - Fetch data for non-spatial k-means. + Fetch data for non-spatial k-means. - Inputs - a dict (params) with the following keys: - colnames: a (text) list of column names (e.g., - `['andy', 'cookie']`) - id_col: the name of the id column (e.g., `'cartodb_id'`) - subquery: the subquery for exposing the data (e.g., - SELECT * FROM favorite_things) - Output: - A SQL query for packaging the data for consumption within - `KMeans().nonspatial`. Format will be a list of length one, - with the first element a dict with keys ('rowid', 'attr1', - 'attr2', ...) + Args: + params (:obj:`dict`) - A :obj:`dict` with the following keys: + - colnames: a (text) list of column names (e.g., + `['andy', 'cookie']`) + - id_col: the name of the id column (e.g., `'cartodb_id'`) + - subquery: the subquery for exposing the data (e.g., + SELECT * FROM favorite_things) + Returns: + `plpy.respone`: A response from the database. The data has been + packaged consumption within `KMeans().nonspatial`. Format will be a + list of length one, with the first element a dict with keys + ('rowid', 'attr1', 'attr2', ...) """ agg_cols = ', '.join([ 'array_agg({0}) As arr_col{1}'.format(val, idx+1) diff --git a/src/py/crankshaft/crankshaft/clustering/getis.py b/src/py/crankshaft/crankshaft/clustering/getis.py index 2bee3a2..254d49b 100644 --- a/src/py/crankshaft/crankshaft/clustering/getis.py +++ b/src/py/crankshaft/crankshaft/clustering/getis.py @@ -37,7 +37,7 @@ class Getis(object): ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) - result = self.data_provider.get_getis(w_type, params) + result = self.data_provider.get_weight_and_attrs(w_type, params) attr_vals = pu.get_attributes(result) # build PySAL weight object diff --git a/src/py/crankshaft/crankshaft/clustering/moran.py b/src/py/crankshaft/crankshaft/clustering/moran.py index e10af73..640f814 100644 --- a/src/py/crankshaft/crankshaft/clustering/moran.py +++ b/src/py/crankshaft/crankshaft/clustering/moran.py @@ -66,7 +66,7 @@ class Moran(object): ("subquery", subquery), ("num_ngbrs", num_ngbrs)]) - result = self.data_provider.get_neighbor(w_type, params) + result = self.data_provider.get_weight_and_attrs(w_type, params) attr_vals = pu.get_attributes(result) weight = pu.get_weight(result, w_type, num_ngbrs) diff --git a/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py b/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py index 6b02f6d..09201d9 100644 --- a/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py +++ b/src/py/crankshaft/crankshaft/pysal_utils/pysal_utils.py @@ -9,14 +9,16 @@ import pysal as ps def construct_neighbor_query(w_type, query_vals): """Return query (a string) used for finding neighbors - @param w_type text: type of neighbors to calculate ('knn' or 'queen') - @param query_vals dict: values used to construct the query + + Args: + w_type (:obj:`str`): type of neighbors to calculate. One of 'knn' + or 'queen') + query_vals (:obj:`dict`): values used to construct the query """ - if w_type.lower() == 'knn': - return knn(query_vals) - else: + if w_type.lower() == 'queen': return queen(query_vals) + return knn(query_vals) # Build weight object diff --git a/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py b/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py index 20daaf1..66913c1 100644 --- a/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py +++ b/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py @@ -61,7 +61,7 @@ class Markov(object): "subquery": subquery, "num_ngbrs": num_ngbrs} - result = self.data_provider.get_markov(w_type, params) + result = self.data_provider.get_weight_and_attrs(w_type, params) # build weight weights = pu.get_weight(result, w_type) diff --git a/src/py/crankshaft/test/test_clustering_getis.py b/src/py/crankshaft/test/test_clustering_getis.py index 61add11..71c8282 100644 --- a/src/py/crankshaft/test/test_clustering_getis.py +++ b/src/py/crankshaft/test/test_clustering_getis.py @@ -41,7 +41,7 @@ class FakeDataProvider(AnalysisDataProvider): def __init__(self, mock_data): self.mock_result = mock_data - def get_getis(self, w_type, param): + def get_weight_and_attrs(self, w_type, param): return self.mock_result diff --git a/src/py/crankshaft/test/test_clustering_moran.py b/src/py/crankshaft/test/test_clustering_moran.py index 0d02fa0..2d71f15 100644 --- a/src/py/crankshaft/test/test_clustering_moran.py +++ b/src/py/crankshaft/test/test_clustering_moran.py @@ -14,7 +14,7 @@ class FakeDataProvider(AnalysisDataProvider): def __init__(self, mock_data): self.mock_result = mock_data - def get_neighbor(self, w_type, params): + def get_weight_and_attrs(self, w_type, params): return self.mock_result