Skip to main content

maup

Build Status Code Coverage PyPI Package

maup is the geospatial toolkit for redistricting data. The package streamlines the basic workflows that arise when working with blocks, precincts, and districts, such as

The project's priorities are to be efficient by using spatial indices whenever possible and to integrate well with the existing ecosystem around pandas, geopandas and shapely. The package is distributed under the MIT License.

Installation

To install from PyPI, run pip install maup from your terminal.

If you are using Anaconda, we recommend installing geopandas first by running conda install -c conda-forge geopandas and then running pip install maup.

Examples

Here are some basic situations where you might find maup helpful. For these examples, let's assume that you have some shapefiles with data at varying scales, and that you've used geopandas.read_file to read those shapefiles into three GeoDataFrames:

  • blocks: Census blocks with demographic data.
  • precincts: Precinct geometries with election data but no demographic data.
  • districts: Legislative district geometries with no data attached.

Assigning precincts to districts

The assign function in maup takes two sets of geometries called sources and targets and returns a pandas Series. The Series maps each geometry in sources to the geometry in targets that covers it. (Here, geometry A covers geometry B if every point of A and its boundary lies in B or its boundary.) If a source geometry is not covered by one single target geometry, it is assigned to the target geometry that covers the largest portion of its area.

from maup import assign

assignment = assign(precincts, districts)

# Add the assigned districts as a column of the `precincts` GeoDataFrame:
precincts["DISTRICT"] = assignment

As an aside, you can use that assignment object to create a gerrychain Partition representing the division of the precincts into legislative districts:

from gerrychain import Graph, Partition

graph = Graph.from_geodataframe(precincts)
legislative_districts = Partition(graph, assignment)

Aggregating block data to precincts

If you want to aggregate columns called "TOTPOP", "NH_BLACK", and "NH_WHITE" from blocks up to precincts, you can run:

from maup import assign

variables = ["TOTPOP", "NH_BLACK", "NH_WHITE"]

assignment = assign(blocks, precincts)
precincts[variables] = blocks[variables].groupby(assignment).sum()

If you want to move data from one set of geometries to another but your source and target geometries do not nest neatly (i.e. have overlaps), see Prorating data when units do not nest neatly.

Disaggregating data from precincts down to blocks

It's common to have data at a coarser scale and want to try and disaggregate or prorate it down to finer-scaled geometries. For example, let's say we want to prorate some election data in columns "PRESD16", "PRESR16" from our precincts GeoDataFrame down to our blocks GeoDataFrame.

The first crucial step is to decide how we want to distribute a precinct's data to the blocks within it. Since we're prorating election data, it makes sense to use a block's total population or voting-age population. Here's how we might prorate by population ("TOTPOP"):

from maup import assign

election_columns = ["PRESD16", "PRESR16"]
assignment = assign(blocks, precincts)

# We prorate the vote totals according to each block's share of the overall
# precinct population:
weights = blocks.TOTPOP / assignment.map(precincts.TOTPOP)
prorated = assignment.map(precincts[election_columns]) * weights

# Add the prorated vote totals as columns on the `blocks` GeoDataFrame:
blocks[election_columns] = prorated

Warning about areal interpolation

We strongly urge you not to prorate by area! The area of a census block is not a good predictor of its population. In fact, the correlation goes in the other direction: larger census blocks are less populous than smaller ones.

Prorating data when units do not nest neatly

Suppose you have a shapefile of precincts with some election results data and you want to join that data onto a different, more recent precincts shapefile. The two sets of precincts will have overlaps, and will not nest neatly like the blocks and precincts did in the above examples. (Not that blocks and precincts always nest neatly...)

We can use intersections to break the two sets of precincts into pieces that nest neatly into both sets. Then we can disaggregate from the old precincts onto these pieces, and aggregate up from the pieces to the new precincts. This move is a bit complicated, so maup has a function called prorate that does just that.

We'll use our same blocks GeoDataFrame to estimate the populations of the pieces for the purposes of proration.

from maup import intersections, prorate

columns = ["SEND12", "SENR12"]

pieces = intersections(old_precincts, new_precincts)

# Weight by prorated population from blocks
weights = blocks["TOTPOP"].groupby(assign(blocks, pieces)).sum()

# Use blocks to estimate population of each piece
new_precincts[columns] = prorate(
    pieces,
    old_precincts[columns],
    weight_by=weights
)

Modifiable areal unit problem

The name of this package comes from the modifiable areal unit problem (MAUP): the same spatial data will look different depending on how you divide up the space. Since maup is all about changing the way your data is aggregated and partitioned, we have named it after the MAUP to encourage that the toolkit be used thoughtfully and responsibly.

Metadata

Release files for maup 0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for maup 0.3
File Size Uploaded Provenance Actions
maup-0.3.tar.gz 6.2 kB Without provenance Details for maup-0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for maup 0.3
File Interpreter ABI Platform Actions
maup-0.3-py3-none-any.whl Python 3 none any Details for maup-0.3-py3-none-any.whl

Total release size: 14.0 kB

Release files / maup-0.3.tar.gz

Download URL maup-0.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
694158463cd0b4dc2fe30508e1fcb6a1bbfb2940704bca7a8774cf11715a47a5
BLAKE2b-256 checksum
How to use checksums
e757255aad70aa1acfb628a8639f8c0a74a415764d24715bc47c54725a87b845
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release files / maup-0.3-py3-none-any.whl

Download URL maup-0.3-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
563368a1b737225bbc1a7bd71cfd14f57fde765044c72df1bfd0381df574a5af
BLAKE2b-256 checksum
How to use checksums
f6149f6ecf8c4abbd3d82dd76488736807b7544cb25f6259b291c7f90d599c96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Security for maup 0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page