refactors internals of analysis data provider

This commit is contained in:
Andy Eschbacher
2018-03-05 14:51:12 -05:00
parent 3174b8797c
commit e3aa99dae3
7 changed files with 47 additions and 35 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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