def test_EG_bad_district_plan(self): district1 = District(id=1, party_votes={'partyA': 70, 'partyB': 30}) district2 = District(id=2, party_votes={'partyA': 70, 'partyB': 30}) district_plan = {district1, "not a district", district2} with self.assertRaises(TypeError): efficiency_gap(district_plan, self.key_a, self.key_b)
def test_mmd_bad_district_plan(self): district1 = District(id=1, party_votes={'partyA': 70, 'partyB': 30}) district2 = District(id=2, party_votes={'partyA': 70, 'partyB': 30}) district_plan = {district1, "not a district", district2} with self.assertRaises(TypeError): mean_median_diff(district_plan, self.key_a, self.key_b)
def setUp(self): # District example from Public Mapping Project District Builder district1 = District(id=1, party_votes={'partyA': 6, 'partyB': 150}) district2 = District(id=2, party_votes={'partyA': 42, 'partyB': 114}) self.district_plan = {district1, district2} self.key_a = 'partyA' self.key_b = 'partyB'
def setUp(self): district1 = District(population=100) district2 = District(population=130) district3 = District(population=125) district4 = District(population=110) district5 = District(population=90) self.district_plan = { district1, district2, district3, district4, district5 }
def test_competitiveness_bad_district_plan(self): range = 0.05 district1 = District(id=1, party_votes={'partyA': 6, 'partyB': 150}) district2 = District(id=2, party_votes={'partyA': 42, 'partyB': 114}) district_plan = {district1, "not a district", district2} with self.assertRaises(TypeError): competitiveness(district_plan, self.key_a, self.key_b, range)
def test_convex_hull_triangle_3857(self): # An equilateral triangle around Lake Mendota geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9942183.378309947,5335705.868703798],[-9966678.038775941,5335248.39511508],[-9954034.524793552,5314264.133688814],[-9942183.378309947,5335705.868703798]]]}') district = District(geometry=geom) self.assertAlmostEqual(1.0, convex_hull_ratio(district), places=5)
def test_polsby_popper_square_3857(self): # A square around Lake Winnebago geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9839815.088179024,5505529.83629639],[-9881396.831566159,5468840.062719505],[-9844707.057989275,5427258.31933237],[-9803125.31460214,5463948.092909254],[-9839815.088179024,5505529.83629639]]]}') district = District(geometry=geom) self.assertAlmostEqual(math.pi/4, polsby_popper(district), places=5)
def test_polsby_popper_line_4326(self): # A thin line through Lake Merritt: From PlanScore geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-122.2631266, 37.804111], [-122.2631266, 37.804112], [-122.2484841, 37.804112], [-122.2484841, 37.804111], [-122.2631266, 37.804111]]]}') district = District(geometry = geom) self.assertAlmostEqual(0., polsby_popper(district), places=3)
def test_polsby_popper_square_4326(self): # A square around Lake Merritt: From PlanScore geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-122.2631266, 37.7987797], [-122.2631266, 37.8103489], [-122.2484841, 37.8103489], [-122.2484841, 37.7987797], [-122.2631266, 37.7987797]]]}') district = District(geometry=geom) self.assertAlmostEqual(math.pi/4, polsby_popper(district), places=5)
def test_polsby_popper_not_geometry(self): not_geometry = "Not a geometry" district = District(id=id, geometry=not_geometry) with self.assertRaises(AttributeError): polsby_popper(district)
def test_polsby_popper_triangle_3857(self): # An equilateral triangle around Lake Mendota geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9942183.378309947,5335705.868703798],[-9966678.038775941,5335248.39511508],[-9954034.524793552,5314264.133688814],[-9942183.378309947,5335705.868703798]]]}') district = District(geometry=geom) triangle_score = (4.0*math.pi) * ((math.sqrt(3.0)/4.0)/9.0) self.assertAlmostEqual(triangle_score, polsby_popper(district), places=5)
def setUp(self): # District example from https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2457468 district1 = District(id=1, party_votes={'partyA': 70, 'partyB': 30}) district2 = District(id=2, party_votes={'partyA': 70, 'partyB': 30}) district3 = District(id=3, party_votes={'partyA': 70, 'partyB': 30}) district4 = District(id=4, party_votes={'partyA': 54, 'partyB': 46}) district5 = District(id=5, party_votes={'partyA': 54, 'partyB': 46}) district6 = District(id=6, party_votes={'partyA': 54, 'partyB': 46}) district7 = District(id=7, party_votes={'partyA': 54, 'partyB': 46}) district8 = District(id=8, party_votes={'partyA': 54, 'partyB': 46}) district9 = District(id=9, party_votes={'partyA': 35, 'partyB': 65}) district10 = District(id=10, party_votes={'partyA': 35, 'partyB': 65}) self.district_plan = { district1, district2, district3, district4, district5, district6, district7, district8, district9, district10 } self.key_a = 'partyA' self.key_b = 'partyB'
def test_polsby_popper_comparison_esri_shapefile(self): shape_file = r"tests/tl_2014_55_sldl/tl_2014_55_sldl.shp" driver = ogr.GetDriverByName('ESRI Shapefile') data_source = driver.Open(shape_file, 0) if data_source is None: print ("Open failed.\n") sys.exit(1) layer = data_source.GetLayer() layer.ResetReading() compact = None dispersed = None for feature in layer: # feature field one is the district ID id = feature.GetField(1) # compact if id == "027": # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() compact = District(id=27, geometry=geometry) # dispersed if id == "077": # the feature's geometry geom = feature.GetGeometryRef() geometry = geom.Clone() dispersed = District(id=77, geometry=geometry) compact_score = polsby_popper(compact) dispersed_score = polsby_popper(dispersed) self.assertTrue(compact_score > dispersed_score)
def test_convex_hull_star_3857(self): # A star in the continental 48 star = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-12146761.038853927,5875255.742111786],[-11809365.518752303,5017520.836898427],[-12525054.307214756,4436715.824286485],[-11613536.329536539,4300041.463210875],[-11468388.828200482,3389834.284892711],[-10894266.370623887,4110894.8290304607],[-10033430.080825375,3781492.6633242397],[-10370825.600927,4639227.568537598],[-9655136.812464546,5220032.581149541],[-10566654.790142763,5356706.942225151],[-10711802.291478822,6266914.120543315],[-11285924.749055414,5545853.576405566],[-12146761.038853927,5875255.742111786]]]}') area = star.GetArea() hexagon = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-12146761.038853927,5875255.742111786],[-12525054.307214756,4436715.824286485],[-11468388.828200482,3389834.284892711],[-10033430.080825375,3781492.6633242397],[-9655136.812464546,5220032.581149541],[-10711802.291478822,6266914.120543315],[-12146761.038853927,5875255.742111786]]]}') convex_hull = hexagon.GetArea() district = District(geometry=star) self.assertAlmostEqual((area/convex_hull), convex_hull_ratio(district), places=5)
def test_schwartzberg_square_3857(self): # A square around Lake Winnebago geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9839815.088179024,5505529.83629639],[-9881396.831566159,5468840.062719505],[-9844707.057989275,5427258.31933237],[-9803125.31460214,5463948.092909254],[-9839815.088179024,5505529.83629639]]]}') district = District(geometry=geom) radius = math.sqrt(1/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(4/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
def test_schwartzberg_square_4326(self): # A square around Lake Merritt: From PlanScore geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-122.2631266, 37.7987797], [-122.2631266, 37.8103489], [-122.2484841, 37.8103489], [-122.2484841, 37.7987797], [-122.2631266, 37.7987797]]]}') district = District(geometry=geom) radius = math.sqrt(1/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(4/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
def test_schwartzberg_triangle_3857(self): # An equilateral triangle around Lake Mendota geom = ogr.CreateGeometryFromJson('{"type": "Polygon", "coordinates": [[[-9942183.378309947,5335705.868703798],[-9966678.038775941,5335248.39511508],[-9954034.524793552,5314264.133688814],[-9942183.378309947,5335705.868703798]]]}') district = District(geometry=geom) area_of_triangle = math.sqrt(3)/4 radius = math.sqrt(area_of_triangle/math.pi) circumference = 2*math.pi*radius schwartzberg_score = 1/(3/circumference) self.assertAlmostEqual(schwartzberg_score, schwartzberg(district), places=5)
district_plan = [] # iterate through all of the features # to get individual district numbers and geometries # Congress has 8 unique districts, which are numbered 1-8 for feature in layer: # the number of the current district district_number = feature["District_N"] # use district_geometry for the geometry geom = feature.GetGeometryRef() district_geometry = geom.Clone() district_plan.append( District(id=district_number, geometry=district_geometry)) # 2010 population data from: http://www.publicmapping.org/resources/state-resources/wisconsin/wisconsin-2010-census-statistics # 2016 dem/rep votes for president from: https://data-ltsb.opendata.arcgis.com/datasets/01869b4b585e44bbbac5d550b7bb6fb8_0 ideal_population_size = 710873 for district in district_plan: party_votes = [] if district.id == "1": district.population = 728042 district.party_votes = {'dem': 150448, 'rep': 187380} elif district.id == "2": district.population = 751169 district.party_votes = {'dem': 271505, 'rep': 119606} elif district.id == "3": district.population = 729957 district.party_votes = {'dem': 161002, 'rep': 177179}
def test_set_id(self): district = District(id=10) self.assertEqual(10, district.id)
def test_dict_party(self): party_votes = {"republican": 100, "democrat": 200, "other": 700} district = District(party_votes=party_votes) self.assertEqual(party_votes, district.party_votes)
def test_no_id(self): district = District() self.assertTrue(district.id is None)
def test_no_geometry(self): district = District() with self.assertRaises(TypeError): has_geometry(district)
def test_polsby_popper_no_geometry(self): district = District() with self.assertRaises(TypeError): polsby_popper(district)