maup
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
- Assigning smaller geometries like blocks and precincts to districts
- Aggregating data from smaller geometries to larger ones, and
- Disaggregating data from larger geometries to smaller ones.
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.
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-level data to the precinct level
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()
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.
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.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| maup-0.2.tar.gz | 4.7 kB | Without provenance | Details for maup-0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| maup-0.2-py3-none-any.whl | Python 3 | none | any | Details for maup-0.2-py3-none-any.whl |
Total release size: 10.9 kB
Release files / maup-0.2.tar.gz
| Download URL | maup-0.2.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ebb3394d998b80679e064f4111f39658272152986bd2c60e83a2842bd7ecd39c
|
|
BLAKE2b-256 checksum How to use checksums |
9be37e3c25d4933e7e858d54886565f392c1e5c30325ed97de368a1a75514b83
|
| 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.2-py3-none-any.whl
| Download URL | maup-0.2-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
9770d832e09f300f098751d8d9d3fde8c0b6bc26453e7426ce9911d2c433ac18
|
|
BLAKE2b-256 checksum How to use checksums |
59e9d78268ffd9d20e83e908d0368d24acc6ef5ba8b5f04e9aefa6b28e63358a
|
| 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.2
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.