forked from gerrymandr/python-mander
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
82 lines (54 loc) · 2.46 KB
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from os import path, getcwd
import json
import copy
from unittest import TestCase
from mander.districts import District
from mander.metrics import calculatePolsbyPopper, calculateConvexHull, calculateReock, calculateSchwartzberg
from mander.metrics import scoresToGeojson
base_dir = path.dirname(path.realpath(__file__))
# Test individual metrics
metricFunctions = {
'polsbypopper': calculatePolsbyPopper,
'convexhull': calculateConvexHull,
'reock': calculateReock,
'schwartzberg': calculateSchwartzberg
}
test_districts = ['CD_CA_9', 'CD_IL_4', 'CD_PA_7', 'CD_CA_24', 'CD_MA_9']
metrics = {}
for m in metricFunctions:
metrics[m] = []
test_expected = copy.deepcopy(metrics)
test_returned = copy.deepcopy(metrics)
def collectTestValues(method, district):
test_expected[method].append('%.4f' % district_scores[method])
test_returned[method].append('%.4f' % metricFunctions[method](district))
for d in test_districts:
district_boundaries_file = path.join(base_dir, 'data', d + '.geojson')
district_scores_file = path.join(base_dir, 'data', d + '_scores.json')
with open(district_boundaries_file) as district_boundary_data:
with open(district_scores_file) as district_scores_data:
district_boundary = json.load(district_boundary_data)
district_scores = json.load(district_scores_data)
test_district = District(path=district_boundaries_file) # TODO use python object instead of file path parameter
# Collect expected and returned values
for m in metricFunctions:
collectTestValues(m, test_district)
class TestMetrics(TestCase):
def test_polsbypopper(self):
self.assertEqual(test_expected['polsbypopper'], test_returned['polsbypopper'])
def test_convexhull(self):
self.assertEqual(test_expected['convexhull'], test_returned['convexhull'])
def test_reock(self):
self.assertEqual(test_expected['reock'], test_returned['reock'])
def test_schwartzberg(self):
self.assertEqual(test_expected['schwartzberg'], test_returned['schwartzberg'])
# Test scoresToGeojson
scores_data = scoresToGeojson(District(path=path.join(base_dir, 'data', 'MN_Senate_2017.geojson')), 'polsbypopper')
scores = json.loads(scores_data)
contains_score = 0
for feature in scores['features']:
if isinstance(feature['properties']['polsbypopper'], float):
contains_score = contains_score + 1
class TestScoreGeojson(TestCase):
def test_alldistrictsscored(self):
self.assertEqual(contains_score, 67)