diff --git a/src/py/crankshaft/test/fixtures/getis.json b/src/py/crankshaft/test/fixtures/getis.json new file mode 100644 index 0000000..f2f7d88 --- /dev/null +++ b/src/py/crankshaft/test/fixtures/getis.json @@ -0,0 +1 @@ +[[0.43099999999999999, -0.4491160909028587, 0.32667395669425392], [0.021999999999999999, -0.93788225530474956, 0.17415246162388542], [0.44600000000000001, -0.4491160909028587, 0.32667395669425392], [0.216, -0.64751176059715954, 0.25865039275726565], [0.11700000000000001, -0.96347562762330119, 0.1676544447116961], [0.049000000000000002, -0.20152426147617952, 0.42014433032225806], [0.072999999999999995, -0.46619525535633877, 0.32053787260380839], [0.16300000000000001, -0.74976349152754251, 0.22669858024734424], [0.052999999999999999, -0.64751176059715954, 0.25865039275726565], [0.156, -0.56083197633324544, 0.28745604294545346], [0.29599999999999999, -0.24280805846929457, 0.40407704630238783], [0.29099999999999998, -0.26928692847374969, 0.39385444517601476], [0.44, -0.67722330498207772, 0.24913214171492104], [0.16500000000000001, -0.011684382726338413, 0.49533871177388633], [0.378, -0.56473075182761978, 0.28612845711175672], [0.042999999999999997, -0.99072103490325258, 0.1609109084310375], [0.45600000000000002, -0.28743876271061125, 0.38688819224593796], [0.156, 0.13295980219309725, 0.4471125856868039], [0.19500000000000001, 0.0063207047268819955, 0.49747842043259183], [0.0070000000000000001, -1.0415760797098768, 0.1488041317793396], [0.111, -0.36942477246818262, 0.35590556804466089], [0.22500000000000001, 0.17506551624725114, 0.43051407691606425], [0.26900000000000002, -0.44363455341924385, 0.32865340985373048], [0.33800000000000002, -0.43284736233536597, 0.33256283131987785], [0.189, -0.60291193010388022, 0.27328363953070622], [0.20799999999999999, 0.035856268747985663, 0.48569848295465989], [0.123, 0.089678570182411793, 0.4642713229152271], [0.0030000000000000001, -1.1057224660076166, 0.13442333147768415], [0.16800000000000001, 0.11677761660057258, 0.45351814086906583], [0.29999999999999999, -0.013808110669475293, 0.49449153588626749], [0.378, -0.23290337417697893, 0.40791821913691739], [0.41699999999999998, 0.22945579816510672, 0.40925733604961323], [0.002, 6.5207630666532674, 3.4975355944766306e-11], [0.46200000000000002, -0.14672827921301743, 0.44167324829783083], [0.126, 0.22908126100922532, 0.40940287888500482], [0.047, -1.0840432095769956, 0.13917282042962942], [0.017000000000000001, 4.0493273511601489, 2.5682527508119612e-05], [0.067000000000000004, -0.93058949009641589, 0.17603297655968531], [0.435, -0.39105578866749485, 0.34787799959221344], [0.0030000000000000001, 6.4632623511010854, 5.1234683162704187e-11], [0.098000000000000004, 0.29440403358766087, 0.38422459253128149], [0.38400000000000001, -0.42044605311189681, 0.33707981556243372], [0.38700000000000001, 0.056785871984068648, 0.47735788415038005], [0.36899999999999999, -0.15875022725747437, 0.43693283214176271], [0.20399999999999999, 0.14086541129997771, 0.44398813370614665], [0.014, 6.0345506436163996, 7.9702822119998018e-10], [0.13400000000000001, -0.85883340027064359, 0.19521621870003536], [0.14499999999999999, -0.93901580426004294, 0.17386131505073421], [0.029000000000000001, 2.052209368389633, 0.020074659704574338], [0.45300000000000001, -0.2873860843780221, 0.38690835753455055], [0.27200000000000002, 0.11238736962962483, 0.45525813472736054], [0.47599999999999998, -0.22667990193583551, 0.41033632813474463], [0.060999999999999999, -0.66318887096788159, 0.25360479911357758], [0.47599999999999998, -0.41451366242647014, 0.33924898169485895], [0.14499999999999999, -0.65907426867863195, 0.25492403906163263], [0.127, -0.56550838435631712, 0.28586401153505392], [0.311, -0.019571757025783815, 0.49219249707263191], [0.27700000000000002, -0.049735022936677371, 0.48016677335804769], [0.20899999999999999, 0.34564692985400752, 0.36480403618322277], [0.26200000000000001, -0.055301366746933138, 0.47794918666392716], [0.315, -0.11227385176628404, 0.45530313698210811], [0.39900000000000002, -0.23091105262392728, 0.4086919520030482], [0.38, -0.05314541432928175, 0.47880802357740249], [0.29499999999999998, -0.048726126739086051, 0.48056877723722968], [0.29999999999999999, -0.65725648028245498, 0.25550800720533962], [0.35499999999999998, -0.29942829331962884, 0.38230663846055501], [0.39000000000000001, -0.38409317960223854, 0.35045469320011735], [0.249, -0.52740757430541685, 0.29895529238584306], [0.246, 0.34740441796104815, 0.36414375737078841], [0.089999999999999997, 0.47786308046182885, 0.31637383012047438], [0.23400000000000001, 0.0047163311140149293, 0.49811846308566454], [0.307, -0.098930796747205507, 0.46059660828759452], [0.40500000000000003, -0.37158786444006753, 0.35509986295795459], [0.45800000000000002, 0.60261000309518942, 0.27338408212548115], [0.44600000000000001, 0.046168582994860159, 0.48158794144995187], [0.28499999999999998, -0.049232649167827866, 0.48036694626611731], [0.050000000000000003, 1.0035582996961303, 0.15779578142846606], [0.218, 0.52664582141189142, 0.29921978282283035]] diff --git a/src/py/crankshaft/test/fixtures/neighbors_getis.json b/src/py/crankshaft/test/fixtures/neighbors_getis.json new file mode 100644 index 0000000..61e0c0e --- /dev/null +++ b/src/py/crankshaft/test/fixtures/neighbors_getis.json @@ -0,0 +1 @@ +[{"neighbors": [2, 6, 5, 10, 3], "hr8893": 1.624458, "id": 1}, {"neighbors": [4, 7, 9, 14, 16], "hr8893": 2.2554919999999998, "id": 2}, {"neighbors": [6, 0, 3, 8, 10], "hr8893": 1.4678899999999999, "id": 3}, {"neighbors": [8, 12, 2, 4, 6], "hr8893": 2.4842559999999998, "id": 4}, {"neighbors": [9, 12, 1, 3, 8], "hr8893": 0.0, "id": 5}, {"neighbors": [11, 10, 0, 15, 6], "hr8893": 9.0486730000000009, "id": 6}, {"neighbors": [2, 10, 0, 8, 17], "hr8893": 6.0294889999999999, "id": 7}, {"neighbors": [14, 1, 22, 16, 9], "hr8893": 1.8003849999999999, "id": 8}, {"neighbors": [3, 12, 19, 2, 18], "hr8893": 4.581251, "id": 9}, {"neighbors": [4, 16, 12, 1, 20], "hr8893": 3.7906070000000001, "id": 10}, {"neighbors": [17, 6, 5, 15, 0], "hr8893": 1.4474359999999999, "id": 11}, {"neighbors": [15, 5, 13, 21, 27], "hr8893": 1.1919660000000001, "id": 12}, {"neighbors": [8, 19, 3, 9, 4], "hr8893": 0.0, "id": 13}, {"neighbors": [21, 11, 28, 27, 15], "hr8893": 1.608017, "id": 14}, {"neighbors": [7, 16, 22, 1, 29], "hr8893": 1.9498120000000001, "id": 15}, {"neighbors": [11, 27, 26, 5, 10], "hr8893": 0.74509000000000003, "id": 16}, {"neighbors": [25, 9, 14, 29, 20], "hr8893": 4.1733180000000001, "id": 17}, {"neighbors": [31, 10, 18, 26, 6], "hr8893": 3.7832520000000001, "id": 18}, {"neighbors": [32, 17, 23, 19, 8], "hr8893": 2.0851359999999999, "id": 19}, {"neighbors": [23, 12, 20, 8, 18], "hr8893": 2.1763020000000002, "id": 20}, {"neighbors": [25, 23, 19, 34, 9], "hr8893": 6.3093469999999998, "id": 21}, {"neighbors": [13, 28, 27, 11, 35], "hr8893": 10.855743, "id": 22}, {"neighbors": [30, 14, 29, 24, 7], "hr8893": 4.211354, "id": 23}, {"neighbors": [19, 20, 34, 39, 36], "hr8893": 0.80481000000000003, "id": 24}, {"neighbors": [30, 41, 22, 43, 52], "hr8893": 3.2153309999999999, "id": 25}, {"neighbors": [20, 33, 16, 34, 29], "hr8893": 2.8336640000000002, "id": 26}, {"neighbors": [38, 31, 27, 15, 17], "hr8893": 1.5920399999999999, "id": 27}, {"neighbors": [35, 15, 21, 28, 26], "hr8893": 1.5711580000000001, "id": 28}, {"neighbors": [21, 37, 35, 27, 13], "hr8893": 3.1275900000000001, "id": 29}, {"neighbors": [33, 22, 30, 42, 16], "hr8893": 4.4168960000000004, "id": 30}, {"neighbors": [43, 22, 24, 29, 41], "hr8893": 3.0174859999999999, "id": 31}, {"neighbors": [40, 17, 26, 32, 49], "hr8893": 9.9242450000000009, "id": 32}, {"neighbors": [45, 39, 18, 31, 23], "hr8893": 7.9739570000000004, "id": 33}, {"neighbors": [25, 29, 44, 42, 34], "hr8893": 5.0054639999999999, "id": 34}, {"neighbors": [36, 20, 25, 23, 39], "hr8893": 2.4638909999999998, "id": 35}, {"neighbors": [27, 46, 37, 28, 38], "hr8893": 0.0, "id": 36}, {"neighbors": [39, 34, 50, 48, 23], "hr8893": 7.377974, "id": 37}, {"neighbors": [47, 28, 35, 46, 21], "hr8893": 1.0038750000000001, "id": 38}, {"neighbors": [51, 26, 35, 40, 27], "hr8893": 3.1900469999999999, "id": 39}, {"neighbors": [36, 45, 48, 32, 23], "hr8893": 45.905405999999999, "id": 40}, {"neighbors": [49, 31, 38, 45, 57], "hr8893": 2.447597, "id": 41}, {"neighbors": [52, 43, 30, 24, 53], "hr8893": 1.2949580000000001, "id": 42}, {"neighbors": [43, 44, 33, 53, 29], "hr8893": 5.9330980000000002, "id": 43}, {"neighbors": [53, 42, 30, 41, 60], "hr8893": 4.1339969999999999, "id": 44}, {"neighbors": [33, 42, 59, 58, 34], "hr8893": 4.298311, "id": 45}, {"neighbors": [48, 39, 32, 56, 40], "hr8893": 27.483827000000002, "id": 46}, {"neighbors": [35, 55, 47, 54, 37], "hr8893": 0.96979099999999996, "id": 47}, {"neighbors": [37, 54, 46, 35, 55], "hr8893": 0.0, "id": 48}, {"neighbors": [45, 50, 39, 62, 56], "hr8893": 2.934466, "id": 49}, {"neighbors": [40, 57, 51, 56, 45], "hr8893": 4.4564269999999997, "id": 50}, {"neighbors": [48, 36, 63, 59, 39], "hr8893": 4.629264, "id": 51}, {"neighbors": [61, 38, 55, 49, 57], "hr8893": 4.9415329999999997, "id": 52}, {"neighbors": [41, 64, 53, 43, 60], "hr8893": 3.9900410000000002, "id": 53}, {"neighbors": [43, 60, 64, 41, 42], "hr8893": 2.064324, "id": 54}, {"neighbors": [47, 55, 46, 37, 35], "hr8893": 3.0402529999999999, "id": 55}, {"neighbors": [54, 46, 61, 51, 67], "hr8893": 3.905411, "id": 56}, {"neighbors": [66, 62, 48, 45, 57], "hr8893": 4.3328389999999999, "id": 57}, {"neighbors": [49, 65, 61, 56, 51], "hr8893": 3.8941110000000001, "id": 58}, {"neighbors": [68, 59, 60, 44, 42], "hr8893": 6.8287940000000003, "id": 59}, {"neighbors": [69, 58, 63, 50, 44], "hr8893": 3.2639469999999999, "id": 60}, {"neighbors": [53, 68, 64, 58, 43], "hr8893": 3.2821630000000002, "id": 61}, {"neighbors": [67, 51, 55, 57, 65], "hr8893": 3.2957619999999999, "id": 62}, {"neighbors": [63, 48, 56, 66, 70], "hr8893": 7.2496790000000004, "id": 63}, {"neighbors": [62, 70, 69, 59, 50], "hr8893": 3.041846, "id": 64}, {"neighbors": [60, 53, 52, 71, 41], "hr8893": 1.618018, "id": 65}, {"neighbors": [57, 72, 66, 67, 75], "hr8893": 4.9108010000000002, "id": 66}, {"neighbors": [56, 75, 62, 74, 65], "hr8893": 1.991457, "id": 67}, {"neighbors": [61, 72, 65, 55, 57], "hr8893": 3.1461920000000001, "id": 68}, {"neighbors": [60, 58, 76, 71, 73], "hr8893": 7.2666500000000003, "id": 69}, {"neighbors": [73, 63, 59, 70, 77], "hr8893": 3.1109040000000001, "id": 70}, {"neighbors": [74, 63, 77, 69, 62], "hr8893": 2.9802710000000001, "id": 71}, {"neighbors": [68, 64, 76, 60, 53], "hr8893": 3.8667669999999998, "id": 72}, {"neighbors": [65, 67, 75, 61, 57], "hr8893": 1.8684080000000001, "id": 73}, {"neighbors": [69, 76, 77, 68, 59], "hr8893": 12.577033999999999, "id": 74}, {"neighbors": [75, 70, 66, 77, 62], "hr8893": 7.8035990000000002, "id": 75}, {"neighbors": [74, 66, 72, 65, 70], "hr8893": 3.4714900000000002, "id": 76}, {"neighbors": [68, 73, 71, 69, 60], "hr8893": 4.334822, "id": 77}, {"neighbors": [70, 74, 69, 73, 63], "hr8893": 8.4515370000000001, "id": 78}] diff --git a/src/py/crankshaft/test/test_clustering_getis.py b/src/py/crankshaft/test/test_clustering_getis.py new file mode 100644 index 0000000..fc26fc7 --- /dev/null +++ b/src/py/crankshaft/test/test_clustering_getis.py @@ -0,0 +1,38 @@ +import unittest +import numpy as np + + +# from mock_plpy import MockPlPy +# plpy = MockPlPy() +# +# import sys +# sys.modules['plpy'] = plpy +from helper import plpy, fixture_file + +import crankshaft.clustering as cc +import crankshaft.pysal_utils as pu +from crankshaft import random_seeds +import json + +class GetisTest(unittest.TestCase): + """Testing class for Getis-Ord's G funtion""" + + def setUp(self): + plpy._reset() + print(help(cc)) + self.neighbors_data = json.loads(open(fixture_file('neighbors_getis.json')).read()) + self.getis_data = json.loads(open(fixture_file('getis.json')).read()) + + def test_getis_ord(self): + """Test Getis-Ord's G*""" + data = [ { 'id': d['id'], + 'attr1': d['hr8893'], + 'neighbors': d['neighbors'] } for d in self.neighbors_data] + plpy._define_result('select', data) + random_seeds.set_random_seeds(1234) + result = cc.getis_ord('subquery', 'value', 'knn', 5, 99, 'the_geom', 'cartodb_id') + result = [(row[0], row[1]) for row in result] + expected = self.getis_data + for ([res_z, res_p], [exp_z, exp_p]) in zip(result, expected): + self.assertAlmostEqual(res_val, exp_val) + self.assertEqual(res_quad, exp_quad)