small refactoring

This commit is contained in:
Andy Eschbacher
2018-03-05 11:36:10 -05:00
parent 5b0e75f1d3
commit 3174b8797c
4 changed files with 18 additions and 17 deletions

View File

@@ -3,14 +3,14 @@ CREATE OR REPLACE FUNCTION
CDB_SpatialLag(
subquery TEXT,
column_name TEXT,
w_type TEXT,
num_ngbrs INT,
w_type TEXT DEFAULT 'knn',
num_ngbrs INT DEFAULT 5,
geom_col TEXT DEFAULT 'the_geom',
id_col TEXT DEFAULT 'cartodb_id')
RETURNS TABLE (spatial_lag NUMERIC, rowid INT)
AS $$
from crankshaft.spatial_lag import Spatial
spatial = Spatial()
return spatial.spatial_lag(subquery, column_name, w_type,
num_ngbrs, geom_col, id_col)
from crankshaft.spatial_lag import SpatialLag
s_lag = SpatialLag()
return s_lag.spatial_lag(subquery, column_name, w_type,
num_ngbrs, geom_col, id_col)
$$ LANGUAGE plpythonu;

View File

@@ -1,2 +1,2 @@
"""Import all functions from for spatial lag"""
from spatial_lag import *
from spatial_lag import SpatialLag

View File

@@ -2,17 +2,17 @@
Spatial Lag (using local kNN neighbors identifying spatial lag for a feature)
"""
import pysal as ps
from collections import OrderedDict
from crankshaft.analysis_data_provider import AnalysisDataProvider
import pysal as ps
# crankshaft module
from crankshaft.analysis_data_provider import AnalysisDataProvider
import crankshaft.pysal_utils as pu
# High level interface ---------------------------------------
class Spatial:
class SpatialLag(object):
def __init__(self, data_provider=None):
if data_provider is None:
self.data_provider = AnalysisDataProvider()

View File

@@ -2,7 +2,7 @@ import unittest
import numpy as np
from helper import fixture_file
from crankshaft.spatial_lag import Spatial
from crankshaft.spatial_lag import SpatialLag
from crankshaft.analysis_data_provider import AnalysisDataProvider
import crankshaft.pysal_utils as pu
from crankshaft import random_seeds
@@ -11,10 +11,12 @@ from collections import OrderedDict
class FakeDataProvider(AnalysisDataProvider):
"""Data provider for existing parsed data"""
def __init__(self, mock_data):
self.mock_result = mock_data
def get_neighbor(self, w_type, params):
def get_neighbor(self, w_type, params): # pylint: disable=unused-argument
"""mock get_neighbor"""
return self.mock_result
@@ -28,9 +30,9 @@ class SpatialLagTest(unittest.TestCase):
"geom_col": "the_geom",
"num_ngbrs": 10}
self.neighbors_data = json.loads(
open(fixture_file('lag_data.json')).read())
open(fixture_file('lag_data.json')).read())
self.lag_result = json.loads(
open(fixture_file('lag_result.json')).read())
open(fixture_file('lag_result.json')).read())
def test_local_stat(self):
"""Test Spatial Lag function"""
@@ -39,12 +41,11 @@ class SpatialLagTest(unittest.TestCase):
('neighbors', d['neighbors'])])
for d in self.neighbors_data]
spatial = Spatial(FakeDataProvider(data))
# random_seeds.set_random_seeds(1234)
spatial = SpatialLag(FakeDataProvider(data))
result = spatial.spatial_lag('subquery', 'value',
'knn', 5, 'the_geom', 'cartodb_id')
result = [(row[0], row[1]) for row in result]
zipped_values = zip(result, self.lag_result)
for ([res_lag, res_id], [exp_id, exp_lag]) in zipped_values:
for ([res_lag, _], [_, exp_lag]) in zipped_values:
self.assertEqual(res_lag, exp_lag)