adding spatial lag function, tests and changing data provider name in moran test
This commit is contained in:
1
src/py/crankshaft/test/fixtures/lag_data.json
vendored
Normal file
1
src/py/crankshaft/test/fixtures/lag_data.json
vendored
Normal file
File diff suppressed because one or more lines are too long
1
src/py/crankshaft/test/fixtures/lag_result.json
vendored
Normal file
1
src/py/crankshaft/test/fixtures/lag_result.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -14,7 +14,7 @@ class FakeDataProvider(AnalysisDataProvider):
|
||||
def __init__(self, mock_data):
|
||||
self.mock_result = mock_data
|
||||
|
||||
def get_moran(self, w_type, params):
|
||||
def get_neighbor(self, w_type, params):
|
||||
return self.mock_result
|
||||
|
||||
|
||||
|
||||
50
src/py/crankshaft/test/test_spatial_lag.py
Normal file
50
src/py/crankshaft/test/test_spatial_lag.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
from helper import fixture_file
|
||||
from crankshaft.spatial_lag import Spatial
|
||||
from crankshaft.analysis_data_provider import AnalysisDataProvider
|
||||
import crankshaft.pysal_utils as pu
|
||||
from crankshaft import random_seeds
|
||||
import json
|
||||
from collections import OrderedDict
|
||||
|
||||
|
||||
class FakeDataProvider(AnalysisDataProvider):
|
||||
def __init__(self, mock_data):
|
||||
self.mock_result = mock_data
|
||||
|
||||
def get_neighbor(self, w_type, params):
|
||||
return self.mock_result
|
||||
|
||||
|
||||
class SpatialLagTest(unittest.TestCase):
|
||||
"""Testing class for Spatial Lag function"""
|
||||
|
||||
def setUp(self):
|
||||
self.params = {"id_col": "cartodb_id",
|
||||
"attr1": "mehak",
|
||||
"subquery": "SELECT * FROM m_list",
|
||||
"geom_col": "the_geom",
|
||||
"num_ngbrs": 10}
|
||||
self.neighbors_data = json.loads(
|
||||
open(fixture_file('lag_data.json')).read())
|
||||
self.lag_result = json.loads(
|
||||
open(fixture_file('lag_result.json')).read())
|
||||
|
||||
def test_local_stat(self):
|
||||
"""Test Spatial Lag function"""
|
||||
data = [OrderedDict([('id', d['id']),
|
||||
('attr1', d['value']),
|
||||
('neighbors', d['neighbors'])])
|
||||
for d in self.neighbors_data]
|
||||
|
||||
spatial = Spatial(FakeDataProvider(data))
|
||||
# random_seeds.set_random_seeds(1234)
|
||||
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:
|
||||
self.assertEqual(res_lag, exp_lag)
|
||||
Reference in New Issue
Block a user