Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example in README loses votes and contains non-explicit assumptions #34

Closed
InnovativeInventor opened this issue Jun 22, 2021 · 2 comments · Fixed by #73
Closed

Example in README loses votes and contains non-explicit assumptions #34

InnovativeInventor opened this issue Jun 22, 2021 · 2 comments · Fixed by #73
Labels
bug Something isn't working

Comments

@InnovativeInventor
Copy link
Member

InnovativeInventor commented Jun 22, 2021

import geopandas as gpd
import geopandas
import maup

blocks = geopandas.read_file("zip://./examples/blocks.zip")
precincts = geopandas.read_file("zip://./examples/precincts.zip")
districts = geopandas.read_file("zip://./examples/districts.zip")

election_columns = ["PRES16D", "PRES16R"]

assignment = maup.assign(blocks, precincts)
weights = blocks.TOTPOP / assignment.map(precincts.TOTPOP)
prorated = maup.prorate(assignment, precincts[election_columns], weights)
blocks[election_columns] = prorated

print(blocks[election_columns].sum())
print(precincts[election_columns].sum())

forcing precinct TOTPOP to equal block TOTPOP doesn't resovle the issue:

import geopandas as gpd
import geopandas
import maup

blocks = geopandas.read_file("zip://./examples/blocks.zip")
precincts = geopandas.read_file("zip://./examples/precincts.zip")
districts = geopandas.read_file("zip://./examples/districts.zip")
precincts["TOTPOP"] *= blocks.TOTPOP.sum()/precincts.TOTPOP.sum()

assert precincts["TOTPOP"].sum() == blocks["TOTPOP"].sum()
election_columns = ["PRES16D", "PRES16R"]

assignment = maup.assign(blocks, precincts)
weights = blocks.TOTPOP / assignment.map(precincts.TOTPOP)
prorated = maup.prorate(assignment, precincts[election_columns], weights)
blocks[election_columns] = prorated

print(blocks[election_columns].sum())
print(precincts[election_columns].sum())
@InnovativeInventor InnovativeInventor added the bug Something isn't working label Jun 22, 2021
@InnovativeInventor
Copy link
Member Author

InnovativeInventor commented Jun 23, 2021

Note: forcing the source and target geometries to be cropped to each other like so does not resolve the issue.

precincts["geometry"] = maup.crop_to(precincts, blocks)
blocks["geometry"] = maup.crop_to(blocks, precincts)

Additionally, cropping then dropping the cropped out blocks also does not resolve the issue:

blocks = blocks.drop(blocks[blocks.area==0].index)

@amybecker
Copy link

amybecker commented Jun 23, 2021

This works. You need to BOTH drop the empty geometries and weight with respect to assignment.

import geopandas as gpd
import geopandas
import maup
import math

blocks = geopandas.read_file("zip://./examples/blocks.zip")
precincts = geopandas.read_file("zip://./examples/precincts.zip")
districts = geopandas.read_file("zip://./examples/districts.zip")

precincts["geometry"] = maup.crop_to(precincts, blocks)
blocks["geometry"] = maup.crop_to(blocks, precincts)
blocks = blocks[~blocks["geometry"].is_empty]
election_columns = ["PRES16D", "PRES16R"]

assignment = maup.assign(blocks, precincts)
blocks['assignment'] = assignment

weights = blocks.TOTPOP / assignment.map(blocks.TOTPOP.groupby(assignment).sum())
assert math.isclose(sum(weights),len(precincts))
prorated = maup.prorate(assignment, precincts[election_columns], weights)
blocks[election_columns] = prorated

print(blocks[election_columns].sum())
print(precincts[election_columns].sum())

@InnovativeInventor InnovativeInventor unpinned this issue Jun 23, 2021
InnovativeInventor added a commit that referenced this issue Nov 17, 2022
pjrule added a commit that referenced this issue Feb 8, 2023
* Make the weights in the README have explicit assumptions

Fixes #34, finally . . .

---------

Co-authored-by: Parker J. Rule <parker.rule@tufts.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants