def test_spatial_markov(self): """Test Spatial Markov.""" data = [ { 'id': d['id'], 'attr1': d['y1995'], 'attr2': d['y1996'], 'attr3': d['y1997'], 'attr4': d['y1998'], 'attr5': d['y1999'], 'attr6': d['y2000'], 'attr7': d['y2001'], 'attr8': d['y2002'], 'attr9': d['y2003'], 'attr10': d['y2004'], 'attr11': d['y2005'], 'attr12': d['y2006'], 'attr13': d['y2007'], 'attr14': d['y2008'], 'attr15': d['y2009'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] print(str(data[0])) plpy._define_result('select', data) random_seeds.set_random_seeds(1234) result = std.spatial_markov_trend('subquery', ['y1995', 'y1996', 'y1997', 'y1998', 'y1999', 'y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006', 'y2007', 'y2008', 'y2009'], 5, 'knn', 5, 0, 'the_geom', 'cartodb_id') self.assertTrue(result != None) result = [(row[0], row[1], row[2], row[3], row[4]) for row in result] print result[0] expected = self.markov_data for ([res_trend, res_up, res_down, res_vol, res_id], [exp_trend, exp_up, exp_down, exp_vol, exp_id] ) in zip(result, expected): self.assertAlmostEqual(res_trend, exp_trend)
def test_kmeans_nonspatial(self): """ test for k-means non-spatial """ # data from: # http://scikit-learn.org/stable/modules/generated/sklearn.cluster.KMeans.html#sklearn-cluster-kmeans data_raw = [ OrderedDict([("arr_col1", [1, 1, 1, 4, 4, 4]), ("arr_col2", [2, 4, 0, 2, 4, 0]), ("rowid", [1, 2, 3, 4, 5, 6])]) ] random_seeds.set_random_seeds(1234) kmeans = Kmeans(FakeDataProvider(data_raw)) clusters = kmeans.nonspatial('subquery', ['col1', 'col2'], 2) cl1 = clusters[0][0] cl2 = clusters[3][0] for idx, val in enumerate(clusters): if idx < 3: self.assertEqual(val[0], cl1) else: self.assertEqual(val[0], cl2) # raises exception for no data with self.assertRaises(Exception): kmeans = Kmeans(FakeDataProvider([])) kmeans.nonspatial('subquery', ['col1', 'col2'], 2)
def test_moran(self): """Test Moran's I global""" data = [{"id": d["id"], "attr1": d["value"], "neighbors": d["neighbors"]} for d in self.neighbors_data] plpy._define_result("select", data) random_seeds.set_random_seeds(1235) result = cc.moran("table", "value", "knn", 5, 99, "the_geom", "cartodb_id") print "result == None?", result == None result_moran = result[0][0] expected_moran = np.array([row[0] for row in self.moran_data]).mean() self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
def test_moran(self): """Test Moran's I global""" data = [{ 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] plpy._define_result('select', data) random_seeds.set_random_seeds(1235) result = cc.moran('table', 'value', 'knn', 5, 99, 'the_geom', 'cartodb_id') print 'result == None?', result == None result_moran = result[0][0] expected_moran = np.array([row[0] for row in self.moran_data]).mean() self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
def test_moran_local_rate(self): """Test Moran's I rate""" data = [ { 'id': d['id'], 'attr1': d['value'], 'attr2': 1, 'neighbors': d['neighbors'] } for d in self.neighbors_data] plpy._define_result('select', data) random_seeds.set_random_seeds(1234) result = cc.moran_local_rate('table', 'numerator', 'denominator', 0.05, 5, 99, 'the_geom', 'cartodb_id', 'knn') result = [(row[0], row[1]) for row in result] expected = self.moran_data for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected): self.assertAlmostEqual(res_val, exp_val)
def test_moran(self): """Test Moran's I global""" data = [{"id": d["id"], "attr1": d["value"], "neighbors": d["neighbors"]} for d in self.neighbors_data] random_seeds.set_random_seeds(1235) moran = Moran(FakeDataProvider(data)) result = moran.global_stat("table", "value", "knn", 5, 99, "the_geom", "cartodb_id") result_moran = result[0][0] expected_moran = np.array([row[0] for row in self.moran_data]).mean() self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
def test_moran_local(self): """Test Moran's I local""" data = [{"id": d["id"], "attr1": d["value"], "neighbors": d["neighbors"]} for d in self.neighbors_data] plpy._define_result("select", data) random_seeds.set_random_seeds(1234) result = cc.moran_local("subquery", "value", "knn", 5, 99, "the_geom", "cartodb_id") result = [(row[0], row[1]) for row in result] expected = self.moran_data for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected): self.assertAlmostEqual(res_val, exp_val) self.assertEqual(res_quad, exp_quad)
def test_moran_local(self): """Test Moran's I local""" data = [ { 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] plpy._define_result('select', data) random_seeds.set_random_seeds(1234) result = cc.moran_local('subquery', 'value', 'knn', 5, 99, 'the_geom', 'cartodb_id') result = [(row[0], row[1]) for row in result] expected = self.moran_data for ([res_val, res_quad], [exp_val, exp_quad]) in zip(result, expected): self.assertAlmostEqual(res_val, exp_val) self.assertEqual(res_quad, exp_quad)
def test_moran_local_rate(self): """Test Moran's I rate""" data = [ {"id": d["id"], "attr1": d["value"], "attr2": 1, "neighbors": d["neighbors"]} for d in self.neighbors_data ] random_seeds.set_random_seeds(1234) moran = Moran(FakeDataProvider(data)) result = moran.local_rate_stat("subquery", "numerator", "denominator", "knn", 5, 99, "the_geom", "cartodb_id") result = [(row[0], row[1]) for row in result] zipped_values = zip(result, self.moran_data) for ([res_val, res_quad], [exp_val, exp_quad]) in zipped_values: self.assertAlmostEqual(res_val, exp_val)
def test_moran(self): """Test Moran's I global""" data = [{ 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] random_seeds.set_random_seeds(1235) moran = Moran(FakeDataProvider(data)) result = moran.global_stat('table', 'value', 'knn', 5, 99, 'the_geom', 'cartodb_id') result_moran = result[0][0] expected_moran = np.array([row[0] for row in self.moran_data]).mean() self.assertAlmostEqual(expected_moran, result_moran, delta=10e-2)
def test_getis_ord(self): """Test Getis-Ord's G*""" data = [{'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors']} for d in self.neighbors_data] random_seeds.set_random_seeds(1234) getis = Getis(FakeDataProvider(data)) result = getis.getis_ord('subquery', 'value', 'queen', None, 999, 'the_geom', 'cartodb_id') result = [(row[0], row[1]) for row in result] expected = np.array(self.getis_data)[:, 0:2] for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected): self.assertAlmostEqual(res_z, exp_z, delta=1e-2)
def test_local_stat(self): """Test Moran's I local""" data = [ OrderedDict([("id", d["id"]), ("attr1", d["value"]), ("neighbors", d["neighbors"])]) for d in self.neighbors_data ] moran = Moran(FakeDataProvider(data)) random_seeds.set_random_seeds(1234) result = moran.local_stat("subquery", "value", "knn", 5, 99, "the_geom", "cartodb_id") result = [(row[0], row[1]) for row in result] zipped_values = zip(result, self.moran_data) for ([res_val, res_quad], [exp_val, exp_quad]) in zipped_values: self.assertAlmostEqual(res_val, exp_val) self.assertEqual(res_quad, exp_quad)
def test_getis_ord(self): """Test Getis-Ord's G*""" data = [{ 'id': d['id'], 'attr1': d['value'], 'neighbors': d['neighbors'] } for d in self.neighbors_data] random_seeds.set_random_seeds(1234) getis = Getis(FakeDataProvider(data)) result = getis.getis_ord('subquery', 'value', 'queen', None, 999, 'the_geom', 'cartodb_id') result = [(row[0], row[1]) for row in result] expected = np.array(self.getis_data)[:, 0:2] for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected): self.assertAlmostEqual(res_z, exp_z, delta=1e-2)
def test_kmeans(self): """ """ data = [{'xs': d['xs'], 'ys': d['ys'], 'ids': d['ids']} for d in self.cluster_data] random_seeds.set_random_seeds(1234) kmeans = Kmeans(FakeDataProvider(data)) clusters = kmeans.spatial('subquery', 2) labels = [a[1] for a in clusters] c1 = [a for a in clusters if a[1] == 0] c2 = [a for a in clusters if a[1] == 1] self.assertEqual(len(np.unique(labels)), 2) self.assertEqual(len(c1), 20) self.assertEqual(len(c2), 20)
def test_local_stat(self): """Test Moran's I local""" data = [ OrderedDict([('id', d['id']), ('attr1', d['value']), ('neighbors', d['neighbors'])]) for d in self.neighbors_data ] moran = Moran(FakeDataProvider(data)) random_seeds.set_random_seeds(1234) result = moran.local_stat('subquery', 'value', 'knn', 5, 99, 'the_geom', 'cartodb_id') result = [(row[0], row[6]) for row in result] zipped_values = zip(result, self.moran_data) for ([res_quad, res_val], [exp_val, exp_quad]) in zipped_values: self.assertAlmostEqual(res_val, exp_val) self.assertEqual(res_quad, exp_quad)
def test_kmeans(self): """ """ data = [{ 'xs': d['xs'], 'ys': d['ys'], 'ids': d['ids'] } for d in self.cluster_data] random_seeds.set_random_seeds(1234) kmeans = Kmeans(FakeDataProvider(data)) clusters = kmeans.spatial('subquery', 2) labels = [a[1] for a in clusters] c1 = [a for a in clusters if a[1] == 0] c2 = [a for a in clusters if a[1] == 1] self.assertEqual(len(np.unique(labels)), 2) self.assertEqual(len(c1), 20) self.assertEqual(len(c2), 20)
def test_moran_local_rate(self): """Test Moran's I rate""" data = [{ 'id': d['id'], 'attr1': d['value'], 'attr2': 1, 'neighbors': d['neighbors'] } for d in self.neighbors_data] random_seeds.set_random_seeds(1234) moran = Moran(FakeDataProvider(data)) result = moran.local_rate_stat('subquery', 'numerator', 'denominator', 'knn', 5, 99, 'the_geom', 'cartodb_id') result = [(row[0], row[6]) for row in result] zipped_values = zip(result, self.moran_data) for ([res_quad, res_val], [exp_val, exp_quad]) in zipped_values: self.assertAlmostEqual(res_val, exp_val)
def test_spatial_markov(self): """Test Spatial Markov.""" data = [{'id': d['id'], 'attr1': d['y1995'], 'attr2': d['y1996'], 'attr3': d['y1997'], 'attr4': d['y1998'], 'attr5': d['y1999'], 'attr6': d['y2000'], 'attr7': d['y2001'], 'attr8': d['y2002'], 'attr9': d['y2003'], 'attr10': d['y2004'], 'attr11': d['y2005'], 'attr12': d['y2006'], 'attr13': d['y2007'], 'attr14': d['y2008'], 'attr15': d['y2009'], 'neighbors': d['neighbors']} for d in self.neighbors_data] # print(str(data[0])) markov = Markov(FakeDataProvider(data)) random_seeds.set_random_seeds(1234) result = markov.spatial_trend('subquery', ['y1995', 'y1996', 'y1997', 'y1998', 'y1999', 'y2000', 'y2001', 'y2002', 'y2003', 'y2004', 'y2005', 'y2006', 'y2007', 'y2008', 'y2009'], 5, 'knn', 5, 0, 'the_geom', 'cartodb_id') self.assertTrue(result is not None) result = [(row[0], row[1], row[2], row[3], row[4]) for row in result] print(result[0]) expected = self.markov_data for ([res_trend, res_up, res_down, res_vol, res_id], [exp_trend, exp_up, exp_down, exp_vol, exp_id] ) in zip(result, expected): self.assertAlmostEqual(res_trend, exp_trend)