diff --git a/src/py/crankshaft/crankshaft/analysis_data_provider.py b/src/py/crankshaft/crankshaft/analysis_data_provider.py index ad572ec..6af37f5 100644 --- a/src/py/crankshaft/crankshaft/analysis_data_provider.py +++ b/src/py/crankshaft/crankshaft/analysis_data_provider.py @@ -1,10 +1,12 @@ """class for fetching data""" import plpy +import pysal_utils as pu class AnalysisDataProvider: - def get_markov(self, query): + def get_markov(self, w_type, params): try: + query = pu.construct_neighbor_query(w_type, params) data = plpy.execute(query) if len(data) == 0: @@ -14,10 +16,12 @@ class AnalysisDataProvider: except plpy.SPIError, err: plpy.error('Analysis failed: %s' % err) - def get_moran(self, query): + def get_moran(self, w_type, params): """fetch data for moran's i analyses""" try: + query = pu.construct_neighbor_query(w_type, params) data = plpy.execute(query) + # if there are no neighbors, exit if len(data) == 0: return pu.empty_zipped_array(2) diff --git a/src/py/crankshaft/crankshaft/clustering/moran.py b/src/py/crankshaft/crankshaft/clustering/moran.py index 7cc9ba5..70a8501 100644 --- a/src/py/crankshaft/crankshaft/clustering/moran.py +++ b/src/py/crankshaft/crankshaft/clustering/moran.py @@ -31,15 +31,13 @@ class Moran: core clusters with PySAL. Andy Eschbacher """ - qvals = OrderedDict([("id_col", id_col), - ("attr1", attr_name), - ("geom_col", geom_col), - ("subquery", subquery), - ("num_ngbrs", num_ngbrs)]) + params = OrderedDict([("id_col", id_col), + ("attr1", attr_name), + ("geom_col", geom_col), + ("subquery", subquery), + ("num_ngbrs", num_ngbrs)]) - query = pu.construct_neighbor_query(w_type, qvals) - - result = self.data_provider.get_moran(query) + result = self.data_provider.get_moran(w_type, params) # collect attributes attr_vals = pu.get_attributes(result) @@ -63,15 +61,13 @@ class Moran: # geometries with attributes that are null are ignored # resulting in a collection of not as near neighbors - qvals = OrderedDict([("id_col", id_col), - ("attr1", attr), - ("geom_col", geom_col), - ("subquery", subquery), - ("num_ngbrs", num_ngbrs)]) + params = OrderedDict([("id_col", id_col), + ("attr1", attr), + ("geom_col", geom_col), + ("subquery", subquery), + ("num_ngbrs", num_ngbrs)]) - query = pu.construct_neighbor_query(w_type, qvals) - - result = self.data_provider.get_moran(query) + result = self.data_provider.get_moran(w_type, params) attr_vals = pu.get_attributes(result) weight = pu.get_weight(result, w_type, num_ngbrs) @@ -91,16 +87,14 @@ class Moran: Moran's I Rate (global) Andy Eschbacher """ - qvals = OrderedDict([("id_col", id_col), - ("attr1", numerator), - ("attr2", denominator) - ("geom_col", geom_col), - ("subquery", subquery), - ("num_ngbrs", num_ngbrs)]) + params = OrderedDict([("id_col", id_col), + ("attr1", numerator), + ("attr2", denominator) + ("geom_col", geom_col), + ("subquery", subquery), + ("num_ngbrs", num_ngbrs)]) - query = pu.construct_neighbor_query(w_type, qvals) - - result = self.data_provider.get_moran(query) + result = self.data_provider.get_moran(w_type, params) # collect attributes numer = pu.get_attributes(result, 1) @@ -123,16 +117,14 @@ class Moran: # geometries with values that are null are ignored # resulting in a collection of not as near neighbors - qvals = OrderedDict([("id_col", id_col), - ("numerator", numerator), - ("denominator", denominator), - ("geom_col", geom_col), - ("subquery", subquery), - ("num_ngbrs", num_ngbrs)]) + params = OrderedDict([("id_col", id_col), + ("numerator", numerator), + ("denominator", denominator), + ("geom_col", geom_col), + ("subquery", subquery), + ("num_ngbrs", num_ngbrs)]) - query = pu.construct_neighbor_query(w_type, qvals) - - result = self.data_provider.get_moran(query) + result = self.data_provider.get_moran(w_type, params) # collect attributes numer = pu.get_attributes(result, 1) @@ -156,16 +148,14 @@ class Moran: Moran's I (local) Bivariate (untested) """ - qvals = OrderedDict([("id_col", id_col), - ("attr1", attr1), - ("attr2", attr2), - ("geom_col", geom_col), - ("subquery", subquery), - ("num_ngbrs", num_ngbrs)]) + params = OrderedDict([("id_col", id_col), + ("attr1", attr1), + ("attr2", attr2), + ("geom_col", geom_col), + ("subquery", subquery), + ("num_ngbrs", num_ngbrs)]) - query = pu.construct_neighbor_query(w_type, qvals) - - result = self.data_provider.get_moran(query) + result = self.data_provider.get_moran(w_type, params) # collect attributes attr1_vals = pu.get_attributes(result, 1) diff --git a/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py b/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py index 51db0ef..a1f0edb 100644 --- a/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py +++ b/src/py/crankshaft/crankshaft/space_time_dynamics/markov.py @@ -54,15 +54,13 @@ class Markov: if len(time_cols) < 2: plpy.error('More than one time column needs to be passed') - qvals = {"id_col": id_col, - "time_cols": time_cols, - "geom_col": geom_col, - "subquery": subquery, - "num_ngbrs": num_ngbrs} + params = {"id_col": id_col, + "time_cols": time_cols, + "geom_col": geom_col, + "subquery": subquery, + "num_ngbrs": num_ngbrs} - query = pu.construct_neighbor_query(w_type, qvals) - - query_result = self.data_provider.get_markov(query) + query_result = self.data_provider.get_markov(w_type, params) # build weight weights = pu.get_weight(query_result, w_type) diff --git a/src/py/crankshaft/test/test_clustering_kmeans.py b/src/py/crankshaft/test/test_clustering_kmeans.py index 04f99f6..93633b0 100644 --- a/src/py/crankshaft/test/test_clustering_kmeans.py +++ b/src/py/crankshaft/test/test_clustering_kmeans.py @@ -21,7 +21,7 @@ class FakeDataProvider(AnalysisDataProvider): def __init__(self, mocked_result): self.mocked_result = mocked_result - def get_spatial_kmeans(self, w_type, params): + def get_spatial_kmeans(self, query): return self.mocked_result def get_nonspatial_kmeans(self, query, standarize): diff --git a/src/py/crankshaft/test/test_space_time_dynamics.py b/src/py/crankshaft/test/test_space_time_dynamics.py index 20e659c..d14563e 100644 --- a/src/py/crankshaft/test/test_space_time_dynamics.py +++ b/src/py/crankshaft/test/test_space_time_dynamics.py @@ -17,7 +17,7 @@ class FakeDataProvider(AnalysisDataProvider): def __init__(self, data): self.mock_result = data - def get_markov(self, query): + def get_markov(self, w_type, params): return self.mock_result